Added foreach (called "each").

This commit is contained in:
Marvin Blum
2015-09-21 17:42:42 +02:00
parent e3b8ddc0f9
commit a53f052be8
5 changed files with 37 additions and 22 deletions

BIN
bin/main

Binary file not shown.

View File

@@ -1,7 +1,3 @@
for var _x = 5; _x < 5; _x = _x+1; {
printSomething("nice");
printSomething("nice");
printSomething("nice");
printSomething("nice");
printSomething("nice");
each array {
foo(_x);
}

Binary file not shown.

View File

@@ -27,6 +27,8 @@ func parseBlock() {
parseSwitch()
} else if accept("for") {
parseFor()
} else if accept("each") {
parseForeach()
} else if accept("func") {
parseFunction()
} else {
@@ -42,7 +44,7 @@ func parseVar() {
if accept("=") {
next()
appendOut(" = ")
parseExpression()
parseExpression(true)
}
appendOut(";\n")
@@ -52,7 +54,7 @@ func parseVar() {
func parseIf() {
expect("if")
appendOut("if (")
parseExpression()
parseExpression(true)
appendOut(") then {\n")
expect("{")
parseBlock()
@@ -72,7 +74,7 @@ func parseIf() {
func parseWhile() {
expect("while")
appendOut("while {")
parseExpression()
parseExpression(true)
appendOut("} do {\n")
expect("{")
parseBlock()
@@ -83,7 +85,7 @@ func parseWhile() {
func parseSwitch() {
expect("switch")
appendOut("switch (")
parseExpression()
parseExpression(true)
appendOut(") do {\n")
expect("{")
parseSwitchBlock()
@@ -99,7 +101,7 @@ func parseSwitchBlock() {
if accept("case") {
expect("case")
appendOut("case ")
parseExpression()
parseExpression(true)
expect(":")
appendOut(":\n")
@@ -132,13 +134,13 @@ func parseFor() {
next()
}
parseExpression()
parseExpression(true)
expect(";")
appendOut("}, {")
parseExpression()
parseExpression(true)
expect(";")
appendOut("}, {")
parseExpression()
parseExpression(true)
expect(";")
appendOut("}] do {\n")
expect("{")
@@ -147,6 +149,16 @@ func parseFor() {
appendOut("};\n")
}
func parseForeach() {
expect("each")
expr := parseExpression(false)
expect("{")
appendOut("{\n")
parseBlock()
expect("}")
appendOut("} forEach ("+expr+");")
}
func parseFunction() {
expect("func")
appendOut(get().token+" = {\n")
@@ -223,7 +235,7 @@ func parseFunctionCall() {
func parseParameter() {
for !accept(")") {
parseExpression()
parseExpression(true)
if !accept(")") {
expect(",")
@@ -232,12 +244,18 @@ func parseParameter() {
}
}
func parseExpression() {
func parseExpression(out bool) string {
openingBrackets := 0
output := ""
for !accept(",") && !accept(":") && !accept(";") && !accept("{") && !accept("}") && (openingBrackets != 0 || !accept(")")) {
current := get().token
appendOut(current)
if out {
appendOut(current)
} else {
output += current
}
if accept("(") {
openingBrackets++
@@ -248,7 +266,5 @@ func parseExpression() {
next()
}
if openingBrackets != 0 {
//panic("Opening bracket not closed")
}
return output
}

View File

@@ -9,7 +9,8 @@ type Token struct{
token string
}
var delimiter = []byte{'=',
var delimiter = []byte{
'=',
';',
'{',
'}',
@@ -23,11 +24,13 @@ var delimiter = []byte{'=',
'&',
'|'}
var keywords = []string{"var",
var keywords = []string{
"var",
"if",
"while",
"switch",
"for",
"each",
"func",
"true",
"false",