From 0b26c5bdf8b09cf45fab3a8ece14787865ef2d6d Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Wed, 28 Oct 2015 18:08:08 +0100 Subject: [PATCH 1/2] Issue #11. --- in/simple.asl | 5 ++--- src/asl/parser.go | 12 +++++++----- src/asl/parser_test.go | 4 ++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/in/simple.asl b/in/simple.asl index 268016e..aff052d 100644 --- a/in/simple.asl +++ b/in/simple.asl @@ -1,4 +1,3 @@ -switch x { - case 1: - x = 1; +func myFunc(a, b) { + // ... } diff --git a/src/asl/parser.go b/src/asl/parser.go index e17bbf6..ae03fd9 100644 --- a/src/asl/parser.go +++ b/src/asl/parser.go @@ -1,7 +1,7 @@ package asl import ( - "strconv" + ) const TAB = " " @@ -217,19 +217,21 @@ func parseFunctionParameter() { if accept("{") { return } - - i := int64(0) + + appendOut("params [", false) for !accept(")") { name := get().token next() - appendOut(name+" = _this select "+strconv.FormatInt(i, 10)+";", true) - i++ + appendOut("\""+name+"\"", false) if !accept(")") { expect(",") + appendOut(",", false) } } + + appendOut("];", true) } func parseReturn() { diff --git a/src/asl/parser_test.go b/src/asl/parser_test.go index f84a9dc..84913f3 100644 --- a/src/asl/parser_test.go +++ b/src/asl/parser_test.go @@ -57,7 +57,7 @@ func TestParserSwitch(t *testing.T) { func TestParserFunction(t *testing.T) { got := getCompiled(t, "test/tokenizer_func.asl") - want := "TestFunction = {\r\nparam0 = _this select 0;\r\nparam1 = _this select 1;\r\nreturn true;\r\n};\r\n" + want := "TestFunction = {\r\nparams [\"param0\",\"param1\"];\r\nreturn true;\r\n};\r\n" equal(t, got, want) } @@ -85,7 +85,7 @@ func TestParserExpression2(t *testing.T) { func TestParserFunctionCall(t *testing.T) { got := getCompiled(t, "test/parser_func_call.asl") - want := "myFunc = {\r\na = _this select 0;\r\nb = _this select 1;\r\nreturn a>b;\r\n};\r\n[1+3/4, 2-(66*22)/3-((123))] call myFunc;\r\n" + want := "myFunc = {\r\nparams [\"a\",\"b\"];\r\nreturn a>b;\r\n};\r\n[1+3/4, 2-(66*22)/3-((123))] call myFunc;\r\n" equal(t, got, want) } From 352bfd6a9b3d4d22ecc74feac170475fa3eee0ec Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Wed, 28 Oct 2015 18:12:20 +0100 Subject: [PATCH 2/2] Issue #9. --- in/simple.asl | 2 +- src/asl/parser.go | 10 +++++++++- src/asl/parser_test.go | 7 +++++++ test/parser_func_params.asl | 3 +++ 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 test/parser_func_params.asl diff --git a/in/simple.asl b/in/simple.asl index aff052d..a2589de 100644 --- a/in/simple.asl +++ b/in/simple.asl @@ -1,3 +1,3 @@ -func myFunc(a, b) { +func myFunc(a = 1, b = "string") { // ... } diff --git a/src/asl/parser.go b/src/asl/parser.go index ae03fd9..6aab297 100644 --- a/src/asl/parser.go +++ b/src/asl/parser.go @@ -223,7 +223,15 @@ func parseFunctionParameter() { for !accept(")") { name := get().token next() - appendOut("\""+name+"\"", false) + + if accept("=") { + next() + value := get().token + next() + appendOut("[\""+name+"\","+value+"]", false) + } else { + appendOut("\""+name+"\"", false) + } if !accept(")") { expect(",") diff --git a/src/asl/parser_test.go b/src/asl/parser_test.go index 84913f3..5657977 100644 --- a/src/asl/parser_test.go +++ b/src/asl/parser_test.go @@ -139,6 +139,13 @@ func TestParserArray(t *testing.T) { equal(t, got, want) } +func TestParserFunctionParams(t *testing.T) { + got := getCompiled(t, "test/parser_func_params.asl") + want := "myFunc = {\r\nparams [[\"a\",1],[\"b\",2]];\r\nreturn a+b;\r\n};\r\n" + + equal(t, got, want) +} + func getCompiled(t *testing.T, file string) string { code, err := ioutil.ReadFile(file) diff --git a/test/parser_func_params.asl b/test/parser_func_params.asl new file mode 100644 index 0000000..352b44a --- /dev/null +++ b/test/parser_func_params.asl @@ -0,0 +1,3 @@ +func myFunc(a = 1, b = 2) { + return a+b; +}