diff --git a/pj1-go/lexer.go b/pj1-go/lexer/lexer.go similarity index 94% rename from pj1-go/lexer.go rename to pj1-go/lexer/lexer.go index 97cd3df..26ffabe 100644 --- a/pj1-go/lexer.go +++ b/pj1-go/lexer/lexer.go @@ -1,4 +1,4 @@ -package main +package lexer import ( "fmt" @@ -6,6 +6,8 @@ import ( "strconv" "strings" "unicode/utf8" + + "git.bonsai.cool/kayprish/pj1/pj1-go/util" ) type TokenType int @@ -102,7 +104,7 @@ func (t Token) String() string { type Lexer struct { source string - tokens []Token + Tokens []Token startByte int currentByte int @@ -123,7 +125,7 @@ func (l *Lexer) ScanTokens() { l.scanToken() } - l.tokens = append(l.tokens, NewToken(EOF, "", nil, l.line)) + l.Tokens = append(l.Tokens, NewToken(EOF, "", nil, l.line)) } func (l Lexer) atEnd() bool { @@ -226,7 +228,7 @@ func (l *Lexer) scanToken() { } else { // TODO: if there are multiple bad characters // coalesce similar errors into one - error(l.line, fmt.Sprintf("Unexpected character, %v.", c)) + util.Error(l.line, fmt.Sprintf("Unexpected character, %v.", c)) } } } @@ -351,7 +353,7 @@ func (l *Lexer) str() { case 'e': c = '\x1b' default: - error(l.line, fmt.Sprintf("Invalid escape sequence \\%v.", l.peek())) + util.Error(l.line, fmt.Sprintf("Invalid escape sequence \\%v.", l.peek())) return } } @@ -360,7 +362,7 @@ func (l *Lexer) str() { } if l.atEnd() { - error(l.line, "Unterminated string.") + util.Error(l.line, "Unterminated string.") return } @@ -378,5 +380,5 @@ func (l *Lexer) addSimpleToken(ttype TokenType) { func (l *Lexer) addToken(ttype TokenType, literal interface{}) { text := l.source[l.startByte:l.currentByte] - l.tokens = append(l.tokens, NewToken(ttype, text, literal, l.line)) + l.Tokens = append(l.Tokens, NewToken(ttype, text, literal, l.line)) } diff --git a/pj1-go/tokentype_string.go b/pj1-go/lexer/tokentype_string.go similarity index 99% rename from pj1-go/tokentype_string.go rename to pj1-go/lexer/tokentype_string.go index 13258ef..e804fd2 100644 --- a/pj1-go/tokentype_string.go +++ b/pj1-go/lexer/tokentype_string.go @@ -1,6 +1,6 @@ // Code generated by "stringer -type=TokenType"; DO NOT EDIT. -package main +package lexer import "strconv" diff --git a/pj1-go/pj1.go b/pj1-go/pj1.go index 694f167..45e320a 100644 --- a/pj1-go/pj1.go +++ b/pj1-go/pj1.go @@ -5,10 +5,9 @@ import ( "fmt" "io/ioutil" "os" -) -var ( - hadError bool = false + "git.bonsai.cool/kayprish/pj1/pj1-go/lexer" + "git.bonsai.cool/kayprish/pj1/pj1-go/util" ) func main() { @@ -30,7 +29,7 @@ func runFile(path string) { } run(string(bytes[:])) - if hadError { + if util.HadError { os.Exit(65) } } @@ -46,7 +45,7 @@ func runPrompt() { fmt.Println(line) run(line) - hadError = false + util.HadError = false } if err := scanner.Err(); err != nil { fmt.Fprintln(os.Stderr, "reading standard input:", err) @@ -54,21 +53,11 @@ func runPrompt() { } func run(source string) { - lexer := NewLexer(source) - lexer.ScanTokens() - var tokens []Token = lexer.tokens + l := lexer.NewLexer(source) + l.ScanTokens() + var tokens []lexer.Token = l.Tokens for _, token := range tokens { fmt.Println(token) } } - -// TODO: might have to rename -func error(line int, msg string) { - report(line, "", msg) - hadError = true -} - -func report(line int, where string, msg string) { - fmt.Fprintln(os.Stderr, "[line "+fmt.Sprint(line)+"] Error"+where+": "+msg) -} diff --git a/pj1-go/util/error.go b/pj1-go/util/error.go new file mode 100644 index 0000000..1cf71e7 --- /dev/null +++ b/pj1-go/util/error.go @@ -0,0 +1,19 @@ +package util + +import ( + "fmt" + "os" +) + +var ( + HadError bool = false +) + +func Error(line int, msg string) { + report(line, "", msg) + HadError = true +} + +func report(line int, where string, msg string) { + fmt.Fprintln(os.Stderr, "[line "+fmt.Sprint(line)+"] Error"+where+": "+msg) +}