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; { each array {
printSomething("nice"); foo(_x);
printSomething("nice");
printSomething("nice");
printSomething("nice");
printSomething("nice");
} }

Binary file not shown.

View File

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

View File

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