// Invalid token found, print error message and exit.
func abort(message string) {
panic(message)
}
// Skip whitespace except newlines, which we will use to indicate the end of a statement.
func (s *Source) skipWhitespace() {
for s.curChar == " " || s.curChar == "\t" || s.curChar == "\r" {
s.nextChar()
}
}
// Skip comments in the code.
func skipComment() {}
// Return the next token.
func (s *Source) getToken() Token {
var token Token
// Check the first character of this token to see if we can decide what it is.
// If it is a multiple character operator (e.g., !=), number, identifier, or keyword then we will process the rest.
// s.skipWhitespace()
println("curChar: " + s.curChar)
s.skipWhitespace()
println("curChar: after 84 " + s.curChar)
if s.curChar == "+" {
token = Token{s.curChar, PLUS}
} else if s.curChar == "-" {
token = Token{s.curChar, MINUS}
} else if s.curChar == "*" {
token = Token{s.curChar, ASTERISK}
} else if s.curChar == "/" {
token = Token{s.curChar, SLASH}
} else if s.curChar == "\n" {
token = Token{s.curChar, NEWLINE}
} else if s.curChar == "\000" {
token = Token{s.curChar, EOF}
} else {
println("Unknown token: " + s.curChar)
}
s.nextChar()
return token
}
// Process the next character.
func (s *Source) nextChar() {
s.curPos++
if s.curPos >= len(s.source) {
s.curChar = "\000" // EOF
} else {
s.curChar = string(s.source[s.curPos])
}
}
func do_lexing(codeString string) { // code_string = `+- */`
// Initialize the source.
source := Source{source: codeString + "\n", curChar: "", curPos: -1}
// Loop through and print all tokens.
token := source.getToken()
println(token.text, token.kind, "token 119")
for token.kind != EOF {
println(token.text, token.kind)
token = source.getToken()
}
}
Output is
curChar:
curChar: after 84
Unknown token:
0 token 199
0
curChar: +
curChar: after 84 +
+ 202
curChar: -
curChar: after 84 -
- 203
curChar:
curChar: after 84 *
* 204
curChar: /
curChar: after 84 /
/ 205
curChar:
curChar: after 84
0
curChar:
curChar: after 84
It should print + and - token first and then give print the unknown token to the terminal but it prints unknown token first and then all other tokens.. I maybe missing something. I am new to golang. println(token.text, token.kind, "token 119") Also this line is not printed first
I tried adding printing statements
You are calling
getTokenfirst before the above println statement andgetTokenhas its own println calls, so it is only logical that the "token 199" isn't printed first.No it shouldn't. You are calling
getTokenwhich callsskipWhitespacewhich callsnextCharONLY IFcurCharis whitespace, butcurCharis""(empty string, which is not whitespace) at the beginning of your program. The output reflects the implementation.