Added for.

This commit is contained in:
Marvin Blum
2015-09-21 17:28:16 +02:00
parent 1f0730b984
commit e3b8ddc0f9
6 changed files with 44 additions and 8 deletions

BIN
bin/main

Binary file not shown.

View File

@@ -22,3 +22,14 @@ if (_a+_b)/2 > 10 {
myFunc(_a, _b); myFunc(_a, _b);
myFunc(_a, _b); myFunc(_a, _b);
var _x = 3;
switch _x {
case 1:
case 2:
somefunc(2);
default:
somefunc(3);
_x = -1;
}

View File

@@ -1,9 +1,7 @@
var _x = 3; for var _x = 5; _x < 5; _x = _x+1; {
printSomething("nice");
switch _x { printSomething("nice");
case 1: printSomething("nice");
case 2: printSomething("nice");
somefunc(2); printSomething("nice");
default:
somefunc(3);
} }

Binary file not shown.

View File

@@ -25,6 +25,8 @@ func parseBlock() {
parseWhile() parseWhile()
} else if accept("switch") { } else if accept("switch") {
parseSwitch() parseSwitch()
} else if accept("for") {
parseFor()
} else if accept("func") { } else if accept("func") {
parseFunction() parseFunction()
} else { } else {
@@ -121,6 +123,30 @@ func parseSwitchBlock() {
parseSwitchBlock() parseSwitchBlock()
} }
func parseFor() {
expect("for")
appendOut("for [{")
// var in first assignment is optional
if accept("var") {
next()
}
parseExpression()
expect(";")
appendOut("}, {")
parseExpression()
expect(";")
appendOut("}, {")
parseExpression()
expect(";")
appendOut("}] do {\n")
expect("{")
parseBlock()
expect("}")
appendOut("};\n")
}
func parseFunction() { func parseFunction() {
expect("func") expect("func")
appendOut(get().token+" = {\n") appendOut(get().token+" = {\n")

View File

@@ -27,6 +27,7 @@ var keywords = []string{"var",
"if", "if",
"while", "while",
"switch", "switch",
"for",
"func", "func",
"true", "true",
"false", "false",