diff --git a/in/simple.asl b/in/simple.asl index fc5b371..aded02e 100644 --- a/in/simple.asl +++ b/in/simple.asl @@ -1 +1 @@ -var x = (1+(2+3))/(6*(someVariable+99-100))-(20)+anotherVariable+foo(); +var x = (a < b <= c) >= 10 && true; diff --git a/src/asl/parser.go b/src/asl/parser.go index 6605f05..3517437 100644 --- a/src/asl/parser.go +++ b/src/asl/parser.go @@ -67,7 +67,7 @@ func parseIf() { parseExpression(true) appendOut(") then {", true) expect("{") - parseBlock() + parseExpression(true) expect("}") if accept("else") { @@ -301,42 +301,35 @@ func parseParameter() { } func parseExpression(out bool) string { - /*openingBrackets := 0 - output := "" - - for !accept(",") && !accept(":") && !accept(";") && !accept("{") && !accept("}") && (openingBrackets != 0 || !accept(")")) { - current := get().token - - if out { - appendOut(current, false) - } else { - output += current - } - - if accept("(") { - openingBrackets++ - } else if accept(")") { - openingBrackets-- - } - - next() + output := parseArith() + + for accept("<") || accept(">") || accept("&") || accept("|") || accept("=") { + if accept("<") { + output += "<" + next() + } else if accept(">") { + output += ">" + next() + } else if accept("&") { + next() + expect("&") + output += "&&" + } else if accept("|") { + next() + expect("|") + output += "||" + } else { + output += "=" + next() + } + + if accept("=") { + output += "=" + next() + } + + output += parseExpression(false) } - - return output*/ - - output := parseFactor() - - for accept("+") || accept("-") { - if accept("+") { - output += "+" - next() - output += parseExpression(false) - } else { - output += "-" - next() - output += parseExpression(false) - } - } if out { appendOut(output, false) @@ -370,7 +363,7 @@ func parseTerm() string { } return parseIdentifier() -} +} func parseFactor() string { output := parseTerm() @@ -378,14 +371,30 @@ func parseFactor() string { for accept("*") || accept("/") { // TODO: modulo? if accept("*") { output += "*" - next() - output += parseExpression(false) } else { output += "/" - next() - output += parseExpression(false) } + + next() + output += parseExpression(false) } return output } + +func parseArith() string { + output := parseFactor() + + for accept("+") || accept("-") { + if accept("+") { + output += "+" + } else { + output += "-" + } + + next() + output += parseExpression(false) + } + + return output +} diff --git a/src/asl/parser_test.go b/src/asl/parser_test.go index 90a93e2..4867524 100644 --- a/src/asl/parser_test.go +++ b/src/asl/parser_test.go @@ -55,12 +55,26 @@ func TestParserFunction(t *testing.T) { } // TODO -/*func TestParserAssignResult(t *testing.T) { +func TestParserAssignResult(t *testing.T) { got := getCompiled(t, "test/parser_assign_result.asl") want := "x = [1, 2, 3] call foo;\ny = [1, 2, 3] call bar;" equal(t, got, want) -}*/ +} + +func TestExpression(t *testing.T) { + got := getCompiled(t, "test/parser_expression.asl") + want := "x = (1+(2+3))/(6*(someVariable+99-100))-(20)+anotherVariable+([] call foo);\n" + + equal(t, got, want) +} + +func TestExpression2(t *testing.T) { + got := getCompiled(t, "test/parser_expression2.asl") + want := "x = true||(3>=4&&5<8);\n" + + equal(t, got, want) +} func getCompiled(t *testing.T, file string) string { code, err := ioutil.ReadFile(file) diff --git a/test/parser_expression.asl b/test/parser_expression.asl new file mode 100644 index 0000000..fc5b371 --- /dev/null +++ b/test/parser_expression.asl @@ -0,0 +1 @@ +var x = (1+(2+3))/(6*(someVariable+99-100))-(20)+anotherVariable+foo(); diff --git a/test/parser_expression2.asl b/test/parser_expression2.asl new file mode 100644 index 0000000..3eb6bd9 --- /dev/null +++ b/test/parser_expression2.asl @@ -0,0 +1 @@ +var x = true || (3 >= 4 && 5 < 8);