Issue #18, but syntax is: foreach (variable) => (expression) {...}.

This commit is contained in:
Marvin Blum
2015-10-31 16:40:58 +01:00
parent 3ee9a310e8
commit 6c55846e9f
6 changed files with 14 additions and 6 deletions

View File

@@ -199,9 +199,14 @@ func (c *Compiler) parseFor() {
func (c *Compiler) parseForeach() {
c.expect("foreach")
element := c.get().Token
c.next()
c.expect("=")
c.expect(">")
expr := c.parseExpression(false)
c.expect("{")
c.appendOut("{", true)
c.appendOut(element+" = _x;", true)
c.parseBlock()
c.expect("}")
c.appendOut("} forEach ("+expr+");", true)

View File

@@ -44,7 +44,7 @@ func TestParserFor(t *testing.T) {
func TestParserForeach(t *testing.T) {
got := getCompiled(t, "test/tokenizer_foreach.asl")
want := "{\r\n} forEach (allUnits);\r\n"
want := "{\r\nunit = _x;\r\n} forEach (allUnits);\r\n"
equal(t, got, want)
}

View File

@@ -47,7 +47,8 @@ var keywords = []string{
"catch",
"exitwith",
"waituntil",
"code"}
"code",
"in"}
var whitespace = []byte{' ', '\n', '\t', '\r'}
var identifier = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"

View File

@@ -40,7 +40,7 @@ func TestTokenizerFor(t *testing.T) {
func TestTokenizerForach(t *testing.T) {
got := getTokens(t, "test/tokenizer_foreach.asl")
want := []string{"foreach", "allUnits", "{", "}"}
want := []string{"foreach", "unit", "=", ">", "allUnits", "{", "}"}
compareLength(t, &got, &want)
compareTokens(t, &got, &want)