From 1208bda68cdf0ed4d0afb844f338b721568d6be3 Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Sat, 14 Nov 2015 16:22:39 +0100 Subject: [PATCH] Issue #21. --- README.md | 12 ++++-------- src/parser/parser.go | 27 +++++++++++++++------------ src/parser/parser_test.go | 7 +++++++ test/parser_expression_array.asl | 1 + 4 files changed, 27 insertions(+), 20 deletions(-) create mode 100644 test/parser_expression_array.asl diff --git a/README.md b/README.md index 6248c8b..d2ee5b5 100644 --- a/README.md +++ b/README.md @@ -252,18 +252,14 @@ The following features are not implemented yet, but will be in 1.1.0 or a future * scopes * else if -* arrays within expressions (like someArray-[someEntity]) +* selector in expression scopes won't be supported, since they are a stupid concept and can be replaced by functions. -There is a simple workaround for arrays within expressions: + +Selectors in expressions do not work (yet): ``` -// want: ... forEach allCurators-[myCurator]; - -var myCuratorArray = [myCurator]; // or a more complex expression within array -foreach allCurators-myCuratorArray { - // ... -} +var x = ([1, 2, 3]-[1, 2])[0]; // should result in 3 ``` ## Contribute diff --git a/src/parser/parser.go b/src/parser/parser.go index 3e58b2a..52a85e8 100644 --- a/src/parser/parser.go +++ b/src/parser/parser.go @@ -70,34 +70,35 @@ func (c *Compiler) parseVar() { if c.accept("=") { c.next() c.appendOut(" = ", false) - - if c.accept("[") { - c.parseArray() - } else { - c.parseExpression(true) - } + c.parseExpression(true) } c.expect(";") c.appendOut(";", true) } -func (c *Compiler) parseArray() { +func (c *Compiler) parseArray(out bool) string { + output := "" c.expect("[") - c.appendOut("[", false) + output += "[" if !c.accept("]") { - c.parseExpression(true) + output += c.parseExpression(false) for c.accept(",") { c.next() - c.appendOut(",", false) - c.parseExpression(true) + output += ","+c.parseExpression(false) } } c.expect("]") - c.appendOut("]", false) + output += "]" + + if out { + c.appendOut(output, false) + } + + return output } func (c *Compiler) parseIf() { @@ -463,6 +464,8 @@ func (c *Compiler) parseIdentifier() string { name := c.get().Token c.next() output = "(" + c.parseFunctionCall(false, name) + ")" + } else if c.accept("[") { + output += c.parseArray(false) } else if c.seek("[") { output += "("+c.get().Token c.next() diff --git a/src/parser/parser_test.go b/src/parser/parser_test.go index 443fa44..20e2d3a 100644 --- a/src/parser/parser_test.go +++ b/src/parser/parser_test.go @@ -161,6 +161,13 @@ func TestParserPreprocessor(t *testing.T) { equal(t, got, want) } +func TestParserExpressionArray(t *testing.T) { + got := getCompiled(t, "test/parser_expression_array.asl") + want := "x = [1,2,3]-[2,3];\r\n" + + equal(t, got, want) +} + func getCompiled(t *testing.T, file string) string { code, err := ioutil.ReadFile(file) diff --git a/test/parser_expression_array.asl b/test/parser_expression_array.asl new file mode 100644 index 0000000..84e35fb --- /dev/null +++ b/test/parser_expression_array.asl @@ -0,0 +1 @@ +var x = [1, 2, 3]-[2, 3];