mirror of
https://github.com/Kugelschieber/asl.git
synced 2026-01-18 12:00:25 +00:00
Issue #18, but syntax is: foreach (variable) => (expression) {...}.
This commit is contained in:
@@ -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
|
foreach unit => allUnits { // foreach, iterates over all units in this case
|
||||||
// element is available as _x here
|
// element is available as "unit" here
|
||||||
|
// _x is still available due to how SQF works!
|
||||||
}
|
}
|
||||||
|
|
||||||
switch x { // there is no "break" in SQF
|
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 |
|
| exitwith |
|
||||||
| waituntil |
|
| waituntil |
|
||||||
| code |
|
| code |
|
||||||
|
| in |
|
||||||
|
|
||||||
## What's missing?
|
## What's missing?
|
||||||
|
|
||||||
|
|||||||
@@ -199,9 +199,14 @@ func (c *Compiler) parseFor() {
|
|||||||
|
|
||||||
func (c *Compiler) parseForeach() {
|
func (c *Compiler) parseForeach() {
|
||||||
c.expect("foreach")
|
c.expect("foreach")
|
||||||
|
element := c.get().Token
|
||||||
|
c.next()
|
||||||
|
c.expect("=")
|
||||||
|
c.expect(">")
|
||||||
expr := c.parseExpression(false)
|
expr := c.parseExpression(false)
|
||||||
c.expect("{")
|
c.expect("{")
|
||||||
c.appendOut("{", true)
|
c.appendOut("{", true)
|
||||||
|
c.appendOut(element+" = _x;", true)
|
||||||
c.parseBlock()
|
c.parseBlock()
|
||||||
c.expect("}")
|
c.expect("}")
|
||||||
c.appendOut("} forEach ("+expr+");", true)
|
c.appendOut("} forEach ("+expr+");", true)
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ func TestParserFor(t *testing.T) {
|
|||||||
|
|
||||||
func TestParserForeach(t *testing.T) {
|
func TestParserForeach(t *testing.T) {
|
||||||
got := getCompiled(t, "test/tokenizer_foreach.asl")
|
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)
|
equal(t, got, want)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,7 +47,8 @@ var keywords = []string{
|
|||||||
"catch",
|
"catch",
|
||||||
"exitwith",
|
"exitwith",
|
||||||
"waituntil",
|
"waituntil",
|
||||||
"code"}
|
"code",
|
||||||
|
"in"}
|
||||||
|
|
||||||
var whitespace = []byte{' ', '\n', '\t', '\r'}
|
var whitespace = []byte{' ', '\n', '\t', '\r'}
|
||||||
var identifier = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
|
var identifier = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func TestTokenizerFor(t *testing.T) {
|
|||||||
|
|
||||||
func TestTokenizerForach(t *testing.T) {
|
func TestTokenizerForach(t *testing.T) {
|
||||||
got := getTokens(t, "test/tokenizer_foreach.asl")
|
got := getTokens(t, "test/tokenizer_foreach.asl")
|
||||||
want := []string{"foreach", "allUnits", "{", "}"}
|
want := []string{"foreach", "unit", "=", ">", "allUnits", "{", "}"}
|
||||||
|
|
||||||
compareLength(t, &got, &want)
|
compareLength(t, &got, &want)
|
||||||
compareTokens(t, &got, &want)
|
compareTokens(t, &got, &want)
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
foreach allUnits {
|
foreach unit => allUnits {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user