diff --git a/README.md b/README.md index 60ffec6..eb40547 100644 --- a/README.md +++ b/README.md @@ -86,8 +86,9 @@ for var _i = 0; _i < 100; _i = _i+1 { // var before identifier is optional // ... } -each allUnits { // foreach, iterates over all units in this case - // element is available as _x here +foreach unit => allUnits { // foreach, iterates over all units in this case + // element is available as "unit" here + // _x is still available due to how SQF works! } switch x { // there is no "break" in SQF @@ -242,6 +243,7 @@ Keywords should not be used as identifiers. Here is a full list of all keywords | exitwith | | waituntil | | code | +| in | ## What's missing? diff --git a/src/parser/parser.go b/src/parser/parser.go index 6ccfdb8..3e58b2a 100644 --- a/src/parser/parser.go +++ b/src/parser/parser.go @@ -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) diff --git a/src/parser/parser_test.go b/src/parser/parser_test.go index c6df8b2..443fa44 100644 --- a/src/parser/parser_test.go +++ b/src/parser/parser_test.go @@ -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) } diff --git a/src/tokenizer/tokenizer.go b/src/tokenizer/tokenizer.go index e36f559..8f0f495 100644 --- a/src/tokenizer/tokenizer.go +++ b/src/tokenizer/tokenizer.go @@ -47,7 +47,8 @@ var keywords = []string{ "catch", "exitwith", "waituntil", - "code"} + "code", + "in"} var whitespace = []byte{' ', '\n', '\t', '\r'} var identifier = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" diff --git a/src/tokenizer/tokenizer_test.go b/src/tokenizer/tokenizer_test.go index 8794073..ea50718 100644 --- a/src/tokenizer/tokenizer_test.go +++ b/src/tokenizer/tokenizer_test.go @@ -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) diff --git a/test/tokenizer_foreach.asl b/test/tokenizer_foreach.asl index 21f1506..3e32136 100644 --- a/test/tokenizer_foreach.asl +++ b/test/tokenizer_foreach.asl @@ -1,3 +1,3 @@ -foreach allUnits { +foreach unit => allUnits { // ... }