diff --git a/main.go b/main.go index 5928c89..ad7f532 100644 --- a/main.go +++ b/main.go @@ -31,7 +31,7 @@ func main() { lexer := lexer.NewLexer() parser := parser.NewParser() - filePaths := []string{} + jackFiles := []string{} if info.IsDir() { entries, err := os.ReadDir(source) @@ -42,17 +42,17 @@ func main() { for _, entry := range entries { if fileName := entry.Name(); strings.HasSuffix(fileName, ".jack") { - filePaths = append(filePaths, filepath.Join(source, fileName)) + jackFiles = append(jackFiles, filepath.Join(source, fileName)) } } } else { if !strings.HasSuffix(source, ".jack") { printHelpMessage() } - filePaths = append(filePaths, source) + jackFiles = append(jackFiles, source) } - for _, src := range filePaths { + for _, src := range jackFiles { tokens := lexer.Tokenize(src) class := parser.Parse(tokens) fmt.Printf("ClassName: %s\nVar Count: %d\nSubroutine Count: %d\n", class.Name, len(class.Vars), len(class.Subroutines)) diff --git a/parser/parser.go b/parser/parser.go index 685526d..ca03246 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -335,7 +335,7 @@ func (parser *Parser) parseUnaryExpression() types.UnaryExpr { // GRAMMAR: ('-' | '~') term opToken := parser.getNextToken() parser.assertToken(opToken, []string{"-", "~"}) - operator, _ := types.GetUnaryOperator(opToken.Lexeme) + operator := types.UnaryOperator(opToken.Lexeme) return types.UnaryExpr{ Operator: operator, @@ -350,7 +350,7 @@ func (parser *Parser) parseExpression() types.Expr { if helpers.IsBinaryOperator(nexToken) { parser.assertToken(parser.getNextToken(), []string{"+", "-", "*", "/", "<", ">", "="}) - operator, _ := types.GetBinaryOperator(nexToken.Lexeme) + operator := types.BinaryOperator(nexToken.Lexeme) return types.BinaryExpr{ Left: term, @@ -361,7 +361,7 @@ func (parser *Parser) parseExpression() types.Expr { if helpers.IsLogicalOperator(nexToken) { parser.assertToken(parser.getNextToken(), []string{"&", "|"}) - operator, _ := types.GetLogicalOperator(nexToken.Lexeme) + operator := types.LogicalOperator(nexToken.Lexeme) return types.LogicalExpr{ Left: term, Operator: operator, @@ -516,7 +516,7 @@ func (parser *Parser) parseSubroutineDec() (subroutine types.SubroutineDecl) { parser.assertToken(subroutineTypeToken, []string{"boolean", "char", "className", "int", "void"}) parser.assertToken(subroutineNameToken, []string{"subroutineName"}) - subroutineKind, _ := types.GetSymbolKindFromString(subroutineKindToken.Lexeme) + subroutineKind := types.SymbolKind(subroutineKindToken.Lexeme) subroutine.Name = types.Ident{Name: subroutineNameToken.Lexeme} subroutine.Kind = subroutineKind @@ -540,7 +540,7 @@ func (parser *Parser) parseClassVarDec() (vars []types.VarDecl) { parser.assertToken(varTypeToken, []string{"boolean", "char", "className", "int"}) parser.assertToken(varNameToken, []string{"varName"}) - varKind, _ := types.GetSymbolKindFromString(varKindToken.Lexeme) + varKind := types.SymbolKind(varKindToken.Lexeme) vars = append(vars, types.VarDecl{Name: varNameToken.Lexeme, Type: varTypeToken.Lexeme, Kind: varKind}) // Check if it's a multi var declaration. diff --git a/testdata/expected/Array.json b/testdata/expected/Array.json index f367459..b0707a6 100644 --- a/testdata/expected/Array.json +++ b/testdata/expected/Array.json @@ -4,14 +4,16 @@ { "Name": { "Name": "main" }, "Params": null, - "Kind": 3, + "Kind": "function", "Type": "void", "Body": { "Statements": [ { "Target": { "Name": "length" }, "Value": { - "Arguments": [{ "Type": 3, "Value": "HOW MANY NUMBERS? " }], + "Arguments": [ + { "Type": "string", "Value": "HOW MANY NUMBERS? " } + ], "Callee": { "Object": { "Name": "Keyboard" }, "Property": { "Name": "readInt" } @@ -28,7 +30,10 @@ } } }, - { "Target": { "Name": "i" }, "Value": { "Type": 1, "Value": "0" } }, + { + "Target": { "Name": "i" }, + "Value": { "Type": "int", "Value": "0" } + }, { "Body": { "Statements": [ @@ -39,7 +44,7 @@ }, "Value": { "Arguments": [ - { "Type": 3, "Value": "ENTER THE NEXT NUMBER: " } + { "Type": "string", "Value": "ENTER THE NEXT NUMBER: " } ], "Callee": { "Object": { "Name": "Keyboard" }, @@ -50,28 +55,34 @@ { "Target": { "Name": "i" }, "Value": { - "Operator": 0, + "Operator": "+", "Left": { "Name": "i" }, - "Right": { "Type": 1, "Value": "1" } + "Right": { "Type": "int", "Value": "1" } } } ] }, "Condition": { - "Operator": 4, + "Operator": "\u003c", "Left": { "Name": "i" }, "Right": { "Name": "length" } } }, - { "Target": { "Name": "i" }, "Value": { "Type": 1, "Value": "0" } }, - { "Target": { "Name": "sum" }, "Value": { "Type": 1, "Value": "0" } }, + { + "Target": { "Name": "i" }, + "Value": { "Type": "int", "Value": "0" } + }, + { + "Target": { "Name": "sum" }, + "Value": { "Type": "int", "Value": "0" } + }, { "Body": { "Statements": [ { "Target": { "Name": "sum" }, "Value": { - "Operator": 0, + "Operator": "+", "Left": { "Name": "sum" }, "Right": { "Indexer": { "Name": "i" }, @@ -82,22 +93,22 @@ { "Target": { "Name": "i" }, "Value": { - "Operator": 0, + "Operator": "+", "Left": { "Name": "i" }, - "Right": { "Type": 1, "Value": "1" } + "Right": { "Type": "int", "Value": "1" } } } ] }, "Condition": { - "Operator": 4, + "Operator": "\u003c", "Left": { "Name": "i" }, "Right": { "Name": "length" } } }, { "Expression": { - "Arguments": [{ "Type": 3, "Value": "THE AVERAGE IS: " }], + "Arguments": [{ "Type": "string", "Value": "THE AVERAGE IS: " }], "Callee": { "Object": { "Name": "Output" }, "Property": { "Name": "printString" } @@ -108,7 +119,7 @@ "Expression": { "Arguments": [ { - "Operator": 3, + "Operator": "/", "Left": { "Name": "sum" }, "Right": { "Name": "length" } } @@ -131,10 +142,10 @@ { "Expression": null } ], "Vars": [ - { "Name": "a", "Kind": 6, "Type": "Array" }, - { "Name": "length", "Kind": 6, "Type": "int" }, - { "Name": "i", "Kind": 6, "Type": "int" }, - { "Name": "sum", "Kind": 6, "Type": "int" } + { "Name": "a", "Kind": "var", "Type": "Array" }, + { "Name": "length", "Kind": "var", "Type": "int" }, + { "Name": "i", "Kind": "var", "Type": "int" }, + { "Name": "sum", "Kind": "var", "Type": "int" } ] } } diff --git a/testdata/expected/Square.json b/testdata/expected/Square.json index 175720f..3c340a7 100644 --- a/testdata/expected/Square.json +++ b/testdata/expected/Square.json @@ -8,7 +8,7 @@ { "Name": "Ay", "Type": "int" }, { "Name": "Asize", "Type": "int" } ], - "Kind": 1, + "Kind": "constructor", "Type": "Square", "Body": { "Statements": [ @@ -16,7 +16,7 @@ { "Target": { "Name": "y" }, "Value": { "Name": "Ay" } }, { "Target": { "Name": "size" }, "Value": { "Name": "Asize" } }, { "Expression": { "Arguments": null, "Callee": { "Name": "draw" } } }, - { "Expression": { "Type": 4, "Value": "this" } } + { "Expression": { "Type": "this", "Value": "this" } } ], "Vars": null } @@ -24,13 +24,13 @@ { "Name": { "Name": "dispose" }, "Params": null, - "Kind": 4, + "Kind": "method", "Type": "void", "Body": { "Statements": [ { "Expression": { - "Arguments": [{ "Type": 4, "Value": "this" }], + "Arguments": [{ "Type": "this", "Value": "this" }], "Callee": { "Object": { "Name": "Memory" }, "Property": { "Name": "deAlloc" } @@ -45,13 +45,13 @@ { "Name": { "Name": "draw" }, "Params": null, - "Kind": 4, + "Kind": "method", "Type": "void", "Body": { "Statements": [ { "Expression": { - "Arguments": [{ "Type": 0, "Value": "true" }], + "Arguments": [{ "Type": "boolean", "Value": "true" }], "Callee": { "Object": { "Name": "Screen" }, "Property": { "Name": "setColor" } @@ -64,12 +64,12 @@ { "Name": "x" }, { "Name": "y" }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "x" }, "Right": { "Name": "size" } }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "y" }, "Right": { "Name": "size" } } @@ -88,13 +88,13 @@ { "Name": { "Name": "erase" }, "Params": null, - "Kind": 4, + "Kind": "method", "Type": "void", "Body": { "Statements": [ { "Expression": { - "Arguments": [{ "Type": 0, "Value": "false" }], + "Arguments": [{ "Type": "boolean", "Value": "false" }], "Callee": { "Object": { "Name": "Screen" }, "Property": { "Name": "setColor" } @@ -107,12 +107,12 @@ { "Name": "x" }, { "Name": "y" }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "x" }, "Right": { "Name": "size" } }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "y" }, "Right": { "Name": "size" } } @@ -131,37 +131,37 @@ { "Name": { "Name": "incSize" }, "Params": null, - "Kind": 4, + "Kind": "method", "Type": "void", "Body": { "Statements": [ { "Condition": { - "Operator": 0, + "Operator": "\u0026", "Left": { "Expression": { - "Operator": 4, + "Operator": "\u003c", "Left": { "Expression": { - "Operator": 0, + "Operator": "+", "Left": { "Name": "y" }, "Right": { "Name": "size" } } }, - "Right": { "Type": 1, "Value": "254" } + "Right": { "Type": "int", "Value": "254" } } }, "Right": { "Expression": { - "Operator": 4, + "Operator": "\u003c", "Left": { "Expression": { - "Operator": 0, + "Operator": "+", "Left": { "Name": "x" }, "Right": { "Name": "size" } } }, - "Right": { "Type": 1, "Value": "510" } + "Right": { "Type": "int", "Value": "510" } } } }, @@ -176,9 +176,9 @@ { "Target": { "Name": "size" }, "Value": { - "Operator": 0, + "Operator": "+", "Left": { "Name": "size" }, - "Right": { "Type": 1, "Value": "2" } + "Right": { "Type": "int", "Value": "2" } } }, { @@ -199,15 +199,15 @@ { "Name": { "Name": "decSize" }, "Params": null, - "Kind": 4, + "Kind": "method", "Type": "void", "Body": { "Statements": [ { "Condition": { - "Operator": 5, + "Operator": "\u003e", "Left": { "Name": "size" }, - "Right": { "Type": 1, "Value": "2" } + "Right": { "Type": "int", "Value": "2" } }, "ThenStmt": { "Statements": [ @@ -220,9 +220,9 @@ { "Target": { "Name": "size" }, "Value": { - "Operator": 1, + "Operator": "-", "Left": { "Name": "size" }, - "Right": { "Type": 1, "Value": "2" } + "Right": { "Type": "int", "Value": "2" } } }, { @@ -243,21 +243,21 @@ { "Name": { "Name": "moveUp" }, "Params": null, - "Kind": 4, + "Kind": "method", "Type": "void", "Body": { "Statements": [ { "Condition": { - "Operator": 5, + "Operator": "\u003e", "Left": { "Name": "y" }, - "Right": { "Type": 1, "Value": "1" } + "Right": { "Type": "int", "Value": "1" } }, "ThenStmt": { "Statements": [ { "Expression": { - "Arguments": [{ "Type": 0, "Value": "false" }], + "Arguments": [{ "Type": "boolean", "Value": "false" }], "Callee": { "Object": { "Name": "Screen" }, "Property": { "Name": "setColor" } @@ -269,23 +269,23 @@ "Arguments": [ { "Name": "x" }, { - "Operator": 1, + "Operator": "-", "Left": { "Expression": { - "Operator": 0, + "Operator": "+", "Left": { "Name": "y" }, "Right": { "Name": "size" } } }, - "Right": { "Type": 1, "Value": "1" } + "Right": { "Type": "int", "Value": "1" } }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "x" }, "Right": { "Name": "size" } }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "y" }, "Right": { "Name": "size" } } @@ -299,14 +299,14 @@ { "Target": { "Name": "y" }, "Value": { - "Operator": 1, + "Operator": "-", "Left": { "Name": "y" }, - "Right": { "Type": 1, "Value": "2" } + "Right": { "Type": "int", "Value": "2" } } }, { "Expression": { - "Arguments": [{ "Type": 0, "Value": "true" }], + "Arguments": [{ "Type": "boolean", "Value": "true" }], "Callee": { "Object": { "Name": "Screen" }, "Property": { "Name": "setColor" } @@ -319,14 +319,14 @@ { "Name": "x" }, { "Name": "y" }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "x" }, "Right": { "Name": "size" } }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "y" }, - "Right": { "Type": 1, "Value": "1" } + "Right": { "Type": "int", "Value": "1" } } ], "Callee": { @@ -347,27 +347,27 @@ { "Name": { "Name": "moveDown" }, "Params": null, - "Kind": 4, + "Kind": "method", "Type": "void", "Body": { "Statements": [ { "Condition": { - "Operator": 4, + "Operator": "\u003c", "Left": { "Expression": { - "Operator": 0, + "Operator": "+", "Left": { "Name": "y" }, "Right": { "Name": "size" } } }, - "Right": { "Type": 1, "Value": "254" } + "Right": { "Type": "int", "Value": "254" } }, "ThenStmt": { "Statements": [ { "Expression": { - "Arguments": [{ "Type": 0, "Value": "false" }], + "Arguments": [{ "Type": "boolean", "Value": "false" }], "Callee": { "Object": { "Name": "Screen" }, "Property": { "Name": "setColor" } @@ -380,14 +380,14 @@ { "Name": "x" }, { "Name": "y" }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "x" }, "Right": { "Name": "size" } }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "y" }, - "Right": { "Type": 1, "Value": "1" } + "Right": { "Type": "int", "Value": "1" } } ], "Callee": { @@ -399,14 +399,14 @@ { "Target": { "Name": "y" }, "Value": { - "Operator": 0, + "Operator": "+", "Left": { "Name": "y" }, - "Right": { "Type": 1, "Value": "2" } + "Right": { "Type": "int", "Value": "2" } } }, { "Expression": { - "Arguments": [{ "Type": 0, "Value": "true" }], + "Arguments": [{ "Type": "boolean", "Value": "true" }], "Callee": { "Object": { "Name": "Screen" }, "Property": { "Name": "setColor" } @@ -418,23 +418,23 @@ "Arguments": [ { "Name": "x" }, { - "Operator": 1, + "Operator": "-", "Left": { "Expression": { - "Operator": 0, + "Operator": "+", "Left": { "Name": "y" }, "Right": { "Name": "size" } } }, - "Right": { "Type": 1, "Value": "1" } + "Right": { "Type": "int", "Value": "1" } }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "x" }, "Right": { "Name": "size" } }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "y" }, "Right": { "Name": "size" } } @@ -457,21 +457,21 @@ { "Name": { "Name": "moveLeft" }, "Params": null, - "Kind": 4, + "Kind": "method", "Type": "void", "Body": { "Statements": [ { "Condition": { - "Operator": 5, + "Operator": "\u003e", "Left": { "Name": "x" }, - "Right": { "Type": 1, "Value": "1" } + "Right": { "Type": "int", "Value": "1" } }, "ThenStmt": { "Statements": [ { "Expression": { - "Arguments": [{ "Type": 0, "Value": "false" }], + "Arguments": [{ "Type": "boolean", "Value": "false" }], "Callee": { "Object": { "Name": "Screen" }, "Property": { "Name": "setColor" } @@ -482,24 +482,24 @@ "Expression": { "Arguments": [ { - "Operator": 1, + "Operator": "-", "Left": { "Expression": { - "Operator": 0, + "Operator": "+", "Left": { "Name": "x" }, "Right": { "Name": "size" } } }, - "Right": { "Type": 1, "Value": "1" } + "Right": { "Type": "int", "Value": "1" } }, { "Name": "y" }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "x" }, "Right": { "Name": "size" } }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "y" }, "Right": { "Name": "size" } } @@ -513,14 +513,14 @@ { "Target": { "Name": "x" }, "Value": { - "Operator": 1, + "Operator": "-", "Left": { "Name": "x" }, - "Right": { "Type": 1, "Value": "2" } + "Right": { "Type": "int", "Value": "2" } } }, { "Expression": { - "Arguments": [{ "Type": 0, "Value": "true" }], + "Arguments": [{ "Type": "boolean", "Value": "true" }], "Callee": { "Object": { "Name": "Screen" }, "Property": { "Name": "setColor" } @@ -533,12 +533,12 @@ { "Name": "x" }, { "Name": "y" }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "x" }, - "Right": { "Type": 1, "Value": "1" } + "Right": { "Type": "int", "Value": "1" } }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "y" }, "Right": { "Name": "size" } } @@ -561,27 +561,27 @@ { "Name": { "Name": "moveRight" }, "Params": null, - "Kind": 4, + "Kind": "method", "Type": "void", "Body": { "Statements": [ { "Condition": { - "Operator": 4, + "Operator": "\u003c", "Left": { "Expression": { - "Operator": 0, + "Operator": "+", "Left": { "Name": "x" }, "Right": { "Name": "size" } } }, - "Right": { "Type": 1, "Value": "510" } + "Right": { "Type": "int", "Value": "510" } }, "ThenStmt": { "Statements": [ { "Expression": { - "Arguments": [{ "Type": 0, "Value": "false" }], + "Arguments": [{ "Type": "boolean", "Value": "false" }], "Callee": { "Object": { "Name": "Screen" }, "Property": { "Name": "setColor" } @@ -594,12 +594,12 @@ { "Name": "x" }, { "Name": "y" }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "x" }, - "Right": { "Type": 1, "Value": "1" } + "Right": { "Type": "int", "Value": "1" } }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "y" }, "Right": { "Name": "size" } } @@ -613,14 +613,14 @@ { "Target": { "Name": "x" }, "Value": { - "Operator": 0, + "Operator": "+", "Left": { "Name": "x" }, - "Right": { "Type": 1, "Value": "2" } + "Right": { "Type": "int", "Value": "2" } } }, { "Expression": { - "Arguments": [{ "Type": 0, "Value": "true" }], + "Arguments": [{ "Type": "boolean", "Value": "true" }], "Callee": { "Object": { "Name": "Screen" }, "Property": { "Name": "setColor" } @@ -631,24 +631,24 @@ "Expression": { "Arguments": [ { - "Operator": 1, + "Operator": "-", "Left": { "Expression": { - "Operator": 0, + "Operator": "+", "Left": { "Name": "x" }, "Right": { "Name": "size" } } }, - "Right": { "Type": 1, "Value": "1" } + "Right": { "Type": "int", "Value": "1" } }, { "Name": "y" }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "x" }, "Right": { "Name": "size" } }, { - "Operator": 0, + "Operator": "+", "Left": { "Name": "y" }, "Right": { "Name": "size" } } @@ -670,8 +670,8 @@ } ], "Vars": [ - { "Name": "x", "Kind": 2, "Type": "int" }, - { "Name": "y", "Kind": 2, "Type": "int" }, - { "Name": "size", "Kind": 2, "Type": "int" } + { "Name": "x", "Kind": "field", "Type": "int" }, + { "Name": "y", "Kind": "field", "Type": "int" }, + { "Name": "size", "Kind": "field", "Type": "int" } ] } diff --git a/testdata/expected/SquareGame.json b/testdata/expected/SquareGame.json index 5db2ec2..837a65b 100644 --- a/testdata/expected/SquareGame.json +++ b/testdata/expected/SquareGame.json @@ -4,7 +4,7 @@ { "Name": { "Name": "new" }, "Params": null, - "Kind": 1, + "Kind": "constructor", "Type": "SquareGame", "Body": { "Statements": [ @@ -12,9 +12,9 @@ "Target": { "Name": "square" }, "Value": { "Arguments": [ - { "Type": 1, "Value": "0" }, - { "Type": 1, "Value": "0" }, - { "Type": 1, "Value": "30" } + { "Type": "int", "Value": "0" }, + { "Type": "int", "Value": "0" }, + { "Type": "int", "Value": "30" } ], "Callee": { "Object": { "Name": "Square" }, @@ -24,9 +24,9 @@ }, { "Target": { "Name": "direction" }, - "Value": { "Type": 1, "Value": "0" } + "Value": { "Type": "int", "Value": "0" } }, - { "Expression": { "Type": 4, "Value": "this" } } + { "Expression": { "Type": "this", "Value": "this" } } ], "Vars": null } @@ -34,7 +34,7 @@ { "Name": { "Name": "dispose" }, "Params": null, - "Kind": 4, + "Kind": "method", "Type": "void", "Body": { "Statements": [ @@ -49,7 +49,7 @@ }, { "Expression": { - "Arguments": [{ "Type": 4, "Value": "this" }], + "Arguments": [{ "Type": "this", "Value": "this" }], "Callee": { "Object": { "Name": "Memory" }, "Property": { "Name": "deAlloc" } @@ -64,15 +64,15 @@ { "Name": { "Name": "moveSquare" }, "Params": null, - "Kind": 4, + "Kind": "method", "Type": "void", "Body": { "Statements": [ { "Condition": { - "Operator": 6, + "Operator": "=", "Left": { "Name": "direction" }, - "Right": { "Type": 1, "Value": "1" } + "Right": { "Type": "int", "Value": "1" } }, "ThenStmt": { "Statements": [ @@ -91,9 +91,9 @@ }, { "Condition": { - "Operator": 6, + "Operator": "=", "Left": { "Name": "direction" }, - "Right": { "Type": 1, "Value": "2" } + "Right": { "Type": "int", "Value": "2" } }, "ThenStmt": { "Statements": [ @@ -112,9 +112,9 @@ }, { "Condition": { - "Operator": 6, + "Operator": "=", "Left": { "Name": "direction" }, - "Right": { "Type": 1, "Value": "3" } + "Right": { "Type": "int", "Value": "3" } }, "ThenStmt": { "Statements": [ @@ -133,9 +133,9 @@ }, { "Condition": { - "Operator": 6, + "Operator": "=", "Left": { "Name": "direction" }, - "Right": { "Type": 1, "Value": "4" } + "Right": { "Type": "int", "Value": "4" } }, "ThenStmt": { "Statements": [ @@ -154,7 +154,7 @@ }, { "Expression": { - "Arguments": [{ "Type": 1, "Value": "5" }], + "Arguments": [{ "Type": "int", "Value": "5" }], "Callee": { "Object": { "Name": "Sys" }, "Property": { "Name": "wait" } @@ -169,13 +169,13 @@ { "Name": { "Name": "run" }, "Params": null, - "Kind": 4, + "Kind": "method", "Type": "void", "Body": { "Statements": [ { "Target": { "Name": "exit" }, - "Value": { "Type": 0, "Value": "false" } + "Value": { "Type": "boolean", "Value": "false" } }, { "Body": { @@ -202,22 +202,22 @@ ] }, "Condition": { - "Operator": 6, + "Operator": "=", "Left": { "Name": "key" }, - "Right": { "Type": 1, "Value": "0" } + "Right": { "Type": "int", "Value": "0" } } }, { "Condition": { - "Operator": 6, + "Operator": "=", "Left": { "Name": "key" }, - "Right": { "Type": 1, "Value": "81" } + "Right": { "Type": "int", "Value": "81" } }, "ThenStmt": { "Statements": [ { "Target": { "Name": "exit" }, - "Value": { "Type": 0, "Value": "true" } + "Value": { "Type": "boolean", "Value": "true" } } ] }, @@ -225,9 +225,9 @@ }, { "Condition": { - "Operator": 6, + "Operator": "=", "Left": { "Name": "key" }, - "Right": { "Type": 1, "Value": "90" } + "Right": { "Type": "int", "Value": "90" } }, "ThenStmt": { "Statements": [ @@ -246,9 +246,9 @@ }, { "Condition": { - "Operator": 6, + "Operator": "=", "Left": { "Name": "key" }, - "Right": { "Type": 1, "Value": "88" } + "Right": { "Type": "int", "Value": "88" } }, "ThenStmt": { "Statements": [ @@ -267,15 +267,15 @@ }, { "Condition": { - "Operator": 6, + "Operator": "=", "Left": { "Name": "key" }, - "Right": { "Type": 1, "Value": "131" } + "Right": { "Type": "int", "Value": "131" } }, "ThenStmt": { "Statements": [ { "Target": { "Name": "direction" }, - "Value": { "Type": 1, "Value": "1" } + "Value": { "Type": "int", "Value": "1" } } ] }, @@ -283,15 +283,15 @@ }, { "Condition": { - "Operator": 6, + "Operator": "=", "Left": { "Name": "key" }, - "Right": { "Type": 1, "Value": "133" } + "Right": { "Type": "int", "Value": "133" } }, "ThenStmt": { "Statements": [ { "Target": { "Name": "direction" }, - "Value": { "Type": 1, "Value": "2" } + "Value": { "Type": "int", "Value": "2" } } ] }, @@ -299,15 +299,15 @@ }, { "Condition": { - "Operator": 6, + "Operator": "=", "Left": { "Name": "key" }, - "Right": { "Type": 1, "Value": "130" } + "Right": { "Type": "int", "Value": "130" } }, "ThenStmt": { "Statements": [ { "Target": { "Name": "direction" }, - "Value": { "Type": 1, "Value": "3" } + "Value": { "Type": "int", "Value": "3" } } ] }, @@ -315,15 +315,15 @@ }, { "Condition": { - "Operator": 6, + "Operator": "=", "Left": { "Name": "key" }, - "Right": { "Type": 1, "Value": "132" } + "Right": { "Type": "int", "Value": "132" } }, "ThenStmt": { "Statements": [ { "Target": { "Name": "direction" }, - "Value": { "Type": 1, "Value": "4" } + "Value": { "Type": "int", "Value": "4" } } ] }, @@ -351,31 +351,31 @@ ] }, "Condition": { - "Operator": 1, + "Operator": "~", "Operand": { "Expression": { - "Operator": 6, + "Operator": "=", "Left": { "Name": "key" }, - "Right": { "Type": 1, "Value": "0" } + "Right": { "Type": "int", "Value": "0" } } } } } ] }, - "Condition": { "Operator": 1, "Operand": { "Name": "exit" } } + "Condition": { "Operator": "~", "Operand": { "Name": "exit" } } }, { "Expression": null } ], "Vars": [ - { "Name": "key", "Kind": 6, "Type": "char" }, - { "Name": "exit", "Kind": 6, "Type": "boolean" } + { "Name": "key", "Kind": "var", "Type": "char" }, + { "Name": "exit", "Kind": "var", "Type": "boolean" } ] } } ], "Vars": [ - { "Name": "square", "Kind": 2, "Type": "Square" }, - { "Name": "direction", "Kind": 2, "Type": "int" } + { "Name": "square", "Kind": "field", "Type": "Square" }, + { "Name": "direction", "Kind": "field", "Type": "int" } ] } diff --git a/types/constants.go b/types/constants.go index 1634306..75336cf 100644 --- a/types/constants.go +++ b/types/constants.go @@ -1,133 +1,53 @@ package types - -type SymbolKind int +type SymbolKind string const ( - Argument SymbolKind = iota - Constructor - Field - Function - Method - Static - Var + Argument SymbolKind = "argument" + Constructor SymbolKind = "constructor" + Field SymbolKind = "field" + Function SymbolKind = "function" + Method SymbolKind = "method" + Static SymbolKind = "static" + Var SymbolKind = "var" ) -func (s SymbolKind) String() string { - return []string{"argument", "constructor", "field", "function", "method", "static", "var"}[s] -} - -var symbolKindMap = map[string]SymbolKind{ - "argument": Argument, - "constructor": Constructor, - "field": Field, - "function": Function, - "method": Method, - "static": Static, - "var": Var, -} - -func GetSymbolKindFromString(str string) (SymbolKind, bool) { - symbolKind, found := symbolKindMap[str] - return symbolKind, found -} - -type BinaryOperator int +type BinaryOperator string const ( - Addition BinaryOperator = iota - Subraction - Multiplication - Division - LessThan - GreaterThan - Equals + Addition BinaryOperator = "+" + Subraction BinaryOperator = "-" + Multiplication BinaryOperator = "*" + Division BinaryOperator = "/" + LessThan BinaryOperator = "<" + GreaterThan BinaryOperator = ">" + Equals BinaryOperator = "=" ) -func (op BinaryOperator) String() string { - return []string{"+", "-", "*", "/", "<", ">", "="}[op] -} - -var binaryOperatorMap = map[string]BinaryOperator{ - "+": Addition, - "-": Subraction, - "*": Multiplication, - "/": Division, - "<": LessThan, - ">": GreaterThan, - "=": Equals, -} - -func GetBinaryOperator(str string) (BinaryOperator, bool) { - op, found := binaryOperatorMap[str] - return op, found -} - -type LogicalOperator int +type LogicalOperator string const ( - And LogicalOperator = iota - Or + And LogicalOperator = "&" + Or LogicalOperator = "|" ) -func (op LogicalOperator) String() string { - return []string{"&", "|"}[op] -} - -var logicalOperatorMap = map[string]LogicalOperator{ - "&": And, - "|": Or, -} - -func GetLogicalOperator(str string) (LogicalOperator, bool) { - op, found := logicalOperatorMap[str] - return op, found -} - -type UnaryOperator int +type UnaryOperator string const ( - ArithmeticNegation UnaryOperator = iota - BooleanNegation + ArithmeticNegation UnaryOperator = "-" + BooleanNegation UnaryOperator = "~" ) -func (op UnaryOperator) String() string { - return []string{"-", "~"}[op] -} - -var unaryOperatorMap = map[string]UnaryOperator{ - "-": ArithmeticNegation, - "~": BooleanNegation, -} - -func GetUnaryOperator(str string) (UnaryOperator, bool) { - op, found := unaryOperatorMap[str] - return op, found -} - -type LiteralType int +type LiteralType string const ( - BooleanLiteral LiteralType = iota - IntegerLiteral - NullLiteral - StringLiteral - ThisLiteral + BooleanLiteral LiteralType = "boolean" + IntegerLiteral LiteralType = "int" + NullLiteral LiteralType = "null" + StringLiteral LiteralType = "string" + ThisLiteral LiteralType = "this" ) -var literalTypesMap = map[string]LiteralType{ - "true": BooleanLiteral, - "false": BooleanLiteral, - "null": NullLiteral, - "string": StringLiteral, - "this": ThisLiteral, -} - -func GetLiteralType(str string) (LiteralType, bool) { - literal, ok := literalTypesMap[str] - return literal, ok -} - var KEYWORDS = map[string]bool{ "class": true, "constructor": true, "method": true, "function": true, "int": true, "boolean": true,