From 7aa569186373bd6d2784263ba9247e93efb1a4c7 Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Thu, 15 Oct 2015 18:54:38 +0200 Subject: [PATCH] More testing, fixed for, added array parsing, switch is buggy. --- in/simple.asl | 9 ++++++++- src/asl/parser.go | 35 +++++++++++++++++++++++++++++++---- src/asl/parser_test.go | 17 +++++++++++------ src/asl/tokenizer.go | 2 ++ src/asl/tokenizer_test.go | 10 +++++++++- src/main/asl.go | 2 +- test/tokenizer_switch.asl | 10 ++++++++++ test/tokenizer_var.asl | 1 + 8 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 test/tokenizer_switch.asl diff --git a/in/simple.asl b/in/simple.asl index aded02e..27c2872 100644 --- a/in/simple.asl +++ b/in/simple.asl @@ -1 +1,8 @@ -var x = (a < b <= c) >= 10 && true; +/*switch somevar { + case 1: + x = 1; + case 2: + x = 2; +}*/ + +var array = [1, 2, 3]; diff --git a/src/asl/parser.go b/src/asl/parser.go index 3517437..f540efa 100644 --- a/src/asl/parser.go +++ b/src/asl/parser.go @@ -2,6 +2,7 @@ package asl import ( "strconv" + "fmt" // TODO: remove ) const TAB = " " @@ -9,6 +10,8 @@ const TAB = " " // Parses tokens, validates code to a specific degree // and writes SQF code into desired location. func Parse(token []Token, prettyPrinting bool) string { + fmt.Print("") + initParser(token, prettyPrinting) for tokenIndex < len(token) { @@ -54,20 +57,43 @@ func parseVar() { if accept("=") { next() appendOut(" = ", false) - parseExpression(true) + + if accept("[") { + parseArray() + } else { + parseExpression(true) + } } expect(";") appendOut(";", true) } +func parseArray() { + expect("[") + appendOut("[", false) + + if !accept("]") { + parseExpression(true) + + for accept(",") { + next() + appendOut(",", false) + parseExpression(true) + } + } + + expect("]") + appendOut("]", false) +} + func parseIf() { expect("if") appendOut("if (", false) parseExpression(true) appendOut(") then {", true) expect("{") - parseExpression(true) + parseBlock() expect("}") if accept("else") { @@ -103,13 +129,14 @@ func parseSwitch() { appendOut("};", true) } +// FIXME func parseSwitchBlock() { if accept("}") { return } if accept("case") { - expect("case") + next() appendOut("case ", false) parseExpression(true) expect(":") @@ -121,7 +148,7 @@ func parseSwitchBlock() { appendOut("};", true) } } else if accept("default") { - expect("default") + next() expect(":") appendOut("default:", true) diff --git a/src/asl/parser_test.go b/src/asl/parser_test.go index 4867524..f0788ab 100644 --- a/src/asl/parser_test.go +++ b/src/asl/parser_test.go @@ -1,13 +1,14 @@ -package asl +package asl_test import ( "testing" "io/ioutil" + "asl" ) func TestParserDeclaration(t *testing.T) { got := getCompiled(t, "test/tokenizer_var.asl") - want := "x = 1;\n" + want := "x = 1;\narray = [1,2,3];\n" equal(t, got, want) } @@ -47,6 +48,10 @@ func TestParserEach(t *testing.T) { equal(t, got, want) } +func TestParserSwitch(t *testing.T) { + +} + func TestParserFunction(t *testing.T) { got := getCompiled(t, "test/tokenizer_func.asl") want := "TestFunction = {\nparam0 = _this select 0;\nparam1 = _this select 1;\nreturn true;\n};\n" @@ -55,12 +60,12 @@ 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") @@ -84,9 +89,9 @@ func getCompiled(t *testing.T, file string) string { t.FailNow() } - tokens := Tokenize(code) + tokens := asl.Tokenize(code) - return Parse(tokens, true) + return asl.Parse(tokens, true) } func equal(t *testing.T, got, want string) { diff --git a/src/asl/tokenizer.go b/src/asl/tokenizer.go index dd2fa22..1f2d383 100644 --- a/src/asl/tokenizer.go +++ b/src/asl/tokenizer.go @@ -15,6 +15,8 @@ var delimiter = []byte{ '}', '(', ')', + '[', + ']', '<', '>', '!', diff --git a/src/asl/tokenizer_test.go b/src/asl/tokenizer_test.go index 4a6c35f..7eb6260 100644 --- a/src/asl/tokenizer_test.go +++ b/src/asl/tokenizer_test.go @@ -7,7 +7,7 @@ import ( func TestTokenizerVar(t *testing.T) { got := getTokens(t, "test/tokenizer_var.asl") - want := []string{"var", "x", "=", "1", ";"} + want := []string{"var", "x", "=", "1", ";", "var", "array", "=", "[", "1", ",", "2", ",", "3", "]", ";"} compareLength(t, &got, &want) compareTokens(t, &got, &want) @@ -45,6 +45,14 @@ func TestTokenizerEach(t *testing.T) { compareTokens(t, &got, &want) } +func TestTokenizerSwitch(t *testing.T) { + got := getTokens(t, "test/tokenizer_switch.asl") + want := []string{"switch", "x", "{", "case", "1", ":", "x", "=", "1", ";", "break", ";", "case", "2", ":", "x", "=", "2", ";", "break", ";", "default", ":", "x", "=", "3", ";", "}"} + + compareLength(t, &got, &want) + compareTokens(t, &got, &want) +} + func TestTokenizerFunction(t *testing.T) { got := getTokens(t, "test/tokenizer_func.asl") want := []string{"func", "TestFunction", "(", "param0", ",", "param1", ")", "{", "return", "true", ";", "}"} diff --git a/src/main/asl.go b/src/main/asl.go index c2038db..236d9ad 100644 --- a/src/main/asl.go +++ b/src/main/asl.go @@ -23,5 +23,5 @@ func main() { token := asl.Tokenize(code) out := asl.Parse(token, true) - fmt.Println("OUTPUT:\n-------\n"+out) + fmt.Print("OUTPUT:\n-------\n"+out) } diff --git a/test/tokenizer_switch.asl b/test/tokenizer_switch.asl new file mode 100644 index 0000000..7643ca3 --- /dev/null +++ b/test/tokenizer_switch.asl @@ -0,0 +1,10 @@ +switch x { + case 1: + x = 1; + break; + case 2: + x = 2; + break; + default: + x = 3; +} diff --git a/test/tokenizer_var.asl b/test/tokenizer_var.asl index a701b14..83528d8 100644 --- a/test/tokenizer_var.asl +++ b/test/tokenizer_var.asl @@ -7,3 +7,4 @@ comment */ var x = 1; +var array = [1, 2, 3];