Refactoring

This commit is contained in:
jorenchik
2024-08-04 15:30:03 +03:00
parent 7bb9a60547
commit 3723a90363
3 changed files with 63 additions and 55 deletions

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"log"
"os"
"strings"
)
func main() {
@@ -22,6 +21,10 @@ func main() {
fmt.Printf("%s\n", err.Error())
return
}
log.Println("Lexer output:")
for _, el := range tokens {
fmt.Print(el.ToString())
}
automata = parserAutomata()
err = validateGrammar()
@@ -33,29 +36,9 @@ func main() {
if (err != nil) {
log.Fatal(err.Error())
}
log.Println("Parser output:")
for _, element := range questions {
switch element.(type) {
case SingleAnswerQuestion:
fmt.Printf(
"<Single choice> (%s) %s: %s\n",
element.(SingleAnswerQuestion).section,
strings.Trim(element.(SingleAnswerQuestion).question, "\t\n "),
strings.Trim(element.(SingleAnswerQuestion).answer, "\t\n "),
)
case MultipleChoiceQuestion:
fmt.Printf(
"<Multi choice> (%s) %s\n",
element.(MultipleChoiceQuestion).section,
element.(MultipleChoiceQuestion).question,
)
for _, el := range element.(MultipleChoiceQuestion).choices {
opener := '-'
if (el.isCorrect) {
opener = '+'
}
fmt.Printf("\t%c %s\n", opener, strings.Trim(el.answer, "\t\n "))
}
}
fmt.Printf("%s", element.ToString())
}
log.Println("Compilation completed")

View File

@@ -1,16 +1,17 @@
package main
import (
"fmt"
"strings"
)
var tokens []Token
var buffer []rune
var row int32 = 1
var column int32 = 1
var previousRow int32 = -1
var previousColumn int32 = -1
var textStarted bool = false
var row int32 = 1
var column int32 = 1
var previousRow int32 = -1
var previousColumn int32 = -1
var textStarted bool = false
type TokenType int
@@ -36,6 +37,27 @@ type Token struct {
column int32;
}
func (token Token)ToString() string {
content := token.content
if (token.tokenType == TextFragment) {
content = strings.Replace(
strings.Trim(content, " "),
"\n",
"\\n",
-1,
)
}
return fmt.Sprintf(
"%s: \"%s\" %d:%d\n",
ToString(&token.tokenType),
content,
token.row,
token.column,
)
}
func makePostTextToken(ttype TokenType, tokenLen int32, textType TokenType) {
if (len(strings.Trim(string(buffer), " \n\t")) - 1 > 0) {
textFragment := []rune{}

View File

@@ -6,7 +6,9 @@ import (
"log"
)
type Question interface {}
type Question interface {
ToString() string;
}
type SingleAnswerQuestion struct {
id string;
@@ -27,6 +29,32 @@ type MultipleChoiceQuestion struct {
section string;
}
func (question SingleAnswerQuestion)ToString() string {
return fmt.Sprintf(
"<Single choice> (%s) %s: %s\n",
question.section,
strings.Trim(question.question, "\t\n "),
strings.Trim(question.answer, "\t\n "),
)
}
func (question MultipleChoiceQuestion)ToString() string {
acc := ""
acc += fmt.Sprintf(
"<Multi choice> (%s) %s\n",
question.section,
question.question,
)
for _, el := range question.choices {
opener := '-'
if (el.isCorrect) {
opener = '+'
}
acc += fmt.Sprintf("\t%c %s\n", opener, strings.Trim(el.answer, "\t\n "))
}
return acc
}
type QuestionElement struct {
isDash bool;
content string;
@@ -95,31 +123,7 @@ func parserAutomata() map[TokenType][]TokenType {
func validateGrammar() error {
for i := 0; i < len(tokens) - 1; i++ {
token := tokens[i]
content := token.content
if (token.tokenType == TextFragment) {
content = strings.Replace(
strings.Trim(content, " "),
"\n",
"\\n",
-1,
)
}
nextToken := tokens[i + 1]
fmt.Printf(
"%s: \"%s\" %d:%d\n",
ToString(&token.tokenType),
content,
token.row,
token.column,
)
if (false) {
fmt.Print("Possible next tokens:")
for k:=0; k<len(automata[token.tokenType]); k++ {
ttype := automata[token.tokenType][k]
fmt.Printf(" %s,", ToString(&ttype))
}
fmt.Print("\n:")
}
if (!contains(automata[token.tokenType], nextToken.tokenType)) {
return CompilerErr{
message: fmt.Sprintf(
@@ -215,4 +219,3 @@ func ParseQuestions(tokens []Token) ([]Question, error) {
}
return questions, nil
}