Skip to content

Commit

Permalink
replace iota values with string constants
Browse files Browse the repository at this point in the history
  • Loading branch information
MlkMahmud committed Dec 22, 2023
1 parent 6b1fa6b commit 4038c20
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 277 deletions.
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
lexer := lexer.NewLexer()
parser := parser.NewParser()

filePaths := []string{}
jackFiles := []string{}

if info.IsDir() {
entries, err := os.ReadDir(source)
Expand All @@ -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))
Expand Down
10 changes: 5 additions & 5 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
49 changes: 30 additions & 19 deletions testdata/expected/Array.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -28,7 +30,10 @@
}
}
},
{ "Target": { "Name": "i" }, "Value": { "Type": 1, "Value": "0" } },
{
"Target": { "Name": "i" },
"Value": { "Type": "int", "Value": "0" }
},
{
"Body": {
"Statements": [
Expand All @@ -39,7 +44,7 @@
},
"Value": {
"Arguments": [
{ "Type": 3, "Value": "ENTER THE NEXT NUMBER: " }
{ "Type": "string", "Value": "ENTER THE NEXT NUMBER: " }
],
"Callee": {
"Object": { "Name": "Keyboard" },
Expand All @@ -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" },
Expand All @@ -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" }
Expand All @@ -108,7 +119,7 @@
"Expression": {
"Arguments": [
{
"Operator": 3,
"Operator": "/",
"Left": { "Name": "sum" },
"Right": { "Name": "length" }
}
Expand All @@ -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" }
]
}
}
Expand Down
Loading

0 comments on commit 4038c20

Please sign in to comment.