diff --git a/bin/main b/bin/main index 977d7e3..c2d0d60 100755 Binary files a/bin/main and b/bin/main differ diff --git a/in/statements.asl b/in/statements.asl index f12112b..ab9f9a3 100644 --- a/in/statements.asl +++ b/in/statements.asl @@ -1,7 +1,3 @@ -for var _x = 5; _x < 5; _x = _x+1; { - printSomething("nice"); - printSomething("nice"); - printSomething("nice"); - printSomething("nice"); - printSomething("nice"); +each array { + foo(_x); } diff --git a/pkg/linux_amd64/asl.a b/pkg/linux_amd64/asl.a index b96d4fd..7b2e623 100644 Binary files a/pkg/linux_amd64/asl.a and b/pkg/linux_amd64/asl.a differ diff --git a/src/asl/parser.go b/src/asl/parser.go index 540074d..7f3657e 100644 --- a/src/asl/parser.go +++ b/src/asl/parser.go @@ -27,6 +27,8 @@ func parseBlock() { parseSwitch() } else if accept("for") { parseFor() + } else if accept("each") { + parseForeach() } else if accept("func") { parseFunction() } else { @@ -42,7 +44,7 @@ func parseVar() { if accept("=") { next() appendOut(" = ") - parseExpression() + parseExpression(true) } appendOut(";\n") @@ -52,7 +54,7 @@ func parseVar() { func parseIf() { expect("if") appendOut("if (") - parseExpression() + parseExpression(true) appendOut(") then {\n") expect("{") parseBlock() @@ -72,7 +74,7 @@ func parseIf() { func parseWhile() { expect("while") appendOut("while {") - parseExpression() + parseExpression(true) appendOut("} do {\n") expect("{") parseBlock() @@ -83,7 +85,7 @@ func parseWhile() { func parseSwitch() { expect("switch") appendOut("switch (") - parseExpression() + parseExpression(true) appendOut(") do {\n") expect("{") parseSwitchBlock() @@ -99,7 +101,7 @@ func parseSwitchBlock() { if accept("case") { expect("case") appendOut("case ") - parseExpression() + parseExpression(true) expect(":") appendOut(":\n") @@ -132,13 +134,13 @@ func parseFor() { next() } - parseExpression() + parseExpression(true) expect(";") appendOut("}, {") - parseExpression() + parseExpression(true) expect(";") appendOut("}, {") - parseExpression() + parseExpression(true) expect(";") appendOut("}] do {\n") expect("{") @@ -147,6 +149,16 @@ func parseFor() { appendOut("};\n") } +func parseForeach() { + expect("each") + expr := parseExpression(false) + expect("{") + appendOut("{\n") + parseBlock() + expect("}") + appendOut("} forEach ("+expr+");") +} + func parseFunction() { expect("func") appendOut(get().token+" = {\n") @@ -223,7 +235,7 @@ func parseFunctionCall() { func parseParameter() { for !accept(")") { - parseExpression() + parseExpression(true) if !accept(")") { expect(",") @@ -232,12 +244,18 @@ func parseParameter() { } } -func parseExpression() { +func parseExpression(out bool) string { openingBrackets := 0 + output := "" for !accept(",") && !accept(":") && !accept(";") && !accept("{") && !accept("}") && (openingBrackets != 0 || !accept(")")) { current := get().token - appendOut(current) + + if out { + appendOut(current) + } else { + output += current + } if accept("(") { openingBrackets++ @@ -248,7 +266,5 @@ func parseExpression() { next() } - if openingBrackets != 0 { - //panic("Opening bracket not closed") - } + return output } diff --git a/src/asl/tokenizer.go b/src/asl/tokenizer.go index c66cd42..29520db 100644 --- a/src/asl/tokenizer.go +++ b/src/asl/tokenizer.go @@ -9,7 +9,8 @@ type Token struct{ token string } -var delimiter = []byte{'=', +var delimiter = []byte{ + '=', ';', '{', '}', @@ -23,11 +24,13 @@ var delimiter = []byte{'=', '&', '|'} -var keywords = []string{"var", +var keywords = []string{ + "var", "if", "while", "switch", "for", + "each", "func", "true", "false",