mirror of
https://github.com/hazemKrimi/jack-compiler.git
synced 2026-05-01 17:48:57 +00:00
chore: handle other token types and special symbols
This commit is contained in:
@@ -1,27 +1,45 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/hazemKrimi/jack-compiler/internal/tokenizer"
|
||||
)
|
||||
|
||||
func ParseTokens(tokens []tokenizer.Token) string {
|
||||
output := "<tokens>\n"
|
||||
var output strings.Builder
|
||||
|
||||
output.WriteString("<tokens>\n")
|
||||
|
||||
for _, token := range tokens {
|
||||
switch token.Type {
|
||||
case tokenizer.SYMBOL:
|
||||
{
|
||||
output += "<symbol>" + token.Value + "</symbol>\n"
|
||||
}
|
||||
case tokenizer.KEYWORD:
|
||||
{
|
||||
output += "<keyword>" + token.Value + "</keyword>\n"
|
||||
var value string
|
||||
|
||||
switch token.Value {
|
||||
case "<":
|
||||
value = "<"
|
||||
case ">":
|
||||
value = ">"
|
||||
case "&":
|
||||
value = "&"
|
||||
default:
|
||||
value = token.Value
|
||||
}
|
||||
|
||||
output.WriteString("<symbol> " + value + " </symbol>\n")
|
||||
case tokenizer.KEYWORD:
|
||||
output.WriteString("<keyword> " + token.Value + " </keyword>\n")
|
||||
case tokenizer.IDENTIFIER:
|
||||
output.WriteString("<identifier> " + token.Value + " </identifier>\n")
|
||||
case tokenizer.INT_CONST:
|
||||
output.WriteString("<integerConstant> " + token.Value + " </integerConstant>\n")
|
||||
case tokenizer.STR_CONST:
|
||||
output.WriteString("<stringConstant> " + token.Value + " </stringConstant>\n")
|
||||
}
|
||||
}
|
||||
|
||||
output += "</tokens>\n"
|
||||
output.WriteString("</tokens>\n")
|
||||
|
||||
return output
|
||||
return output.String()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user