mirror of
https://github.com/Kugelschieber/asl.git
synced 2026-01-18 12:00:25 +00:00
Multiple statements, generalized expressions.
This commit is contained in:
@@ -1,17 +1,24 @@
|
||||
var _a = 1;
|
||||
var _b = 2;
|
||||
|
||||
func somefunc(_x, _y, _z) {
|
||||
func myFunc(_x, _y) {
|
||||
if _x < _y {
|
||||
hint(_z);
|
||||
myFunc(_y, _x);
|
||||
}
|
||||
}
|
||||
|
||||
if _a < _b {
|
||||
somefunc(1, "two", 3);
|
||||
}
|
||||
else{
|
||||
_a = 3;
|
||||
if _b < _a {
|
||||
myFunc(_a, _b+9-(2/2));
|
||||
}
|
||||
}
|
||||
|
||||
hint("this is a hint");
|
||||
if (_a+_b)/2 > 10 {
|
||||
hint("a");
|
||||
} else {
|
||||
myFunc("multiple", "statements");
|
||||
hint("b");
|
||||
}
|
||||
|
||||
myFunc(_a, _b);
|
||||
myFunc(_a, _b);
|
||||
|
||||
6
in/statements.asl
Normal file
6
in/statements.asl
Normal file
@@ -0,0 +1,6 @@
|
||||
if (4 < (5+5)/2 || ((3-3)*10-5)) {
|
||||
hint("hallo welt");
|
||||
}
|
||||
else {
|
||||
hint("no");
|
||||
}
|
||||
Binary file not shown.
@@ -35,8 +35,8 @@ func parseVar() {
|
||||
|
||||
if accept("=") {
|
||||
next()
|
||||
appendOut(" = "+get().token)
|
||||
next()
|
||||
appendOut(" = ")
|
||||
parseExpression()
|
||||
}
|
||||
|
||||
appendOut(";\n")
|
||||
@@ -46,7 +46,7 @@ func parseVar() {
|
||||
func parseIf() {
|
||||
expect("if")
|
||||
appendOut("if (")
|
||||
parseCondition()
|
||||
parseExpression()
|
||||
appendOut(") then {\n")
|
||||
expect("{")
|
||||
parseBlock()
|
||||
@@ -63,17 +63,6 @@ func parseIf() {
|
||||
appendOut("};\n")
|
||||
}
|
||||
|
||||
func parseCondition() {
|
||||
for !accept("{") {
|
||||
appendOut(get().token)
|
||||
next()
|
||||
|
||||
if !accept("{") {
|
||||
appendOut(" ")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func parseFunction() {
|
||||
expect("func")
|
||||
appendOut(get().token+" = {\n")
|
||||
@@ -89,7 +78,7 @@ func parseFunction() {
|
||||
|
||||
func parseFunctionParameter() {
|
||||
// empty parameter list
|
||||
if accept(")") {
|
||||
if accept("{") {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -111,7 +100,7 @@ func parseFunctionParameter() {
|
||||
func parseStatement() {
|
||||
// empty block
|
||||
if accept("}") {
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
// variable or function name
|
||||
@@ -125,6 +114,10 @@ func parseStatement() {
|
||||
parseFunctionCall()
|
||||
appendOut(name+";\n")
|
||||
}
|
||||
|
||||
if !end() {
|
||||
parseStatement()
|
||||
}
|
||||
}
|
||||
|
||||
func parseAssignment() {
|
||||
@@ -137,24 +130,41 @@ func parseAssignment() {
|
||||
|
||||
func parseFunctionCall() {
|
||||
expect("(")
|
||||
params := parseParameter()
|
||||
appendOut("[")
|
||||
parseParameter()
|
||||
expect(")")
|
||||
expect(";")
|
||||
appendOut("["+params+"] call ")
|
||||
appendOut("] call ")
|
||||
}
|
||||
|
||||
func parseParameter() string {
|
||||
params := ""
|
||||
|
||||
func parseParameter() {
|
||||
for !accept(")") {
|
||||
params += get().token
|
||||
next()
|
||||
parseExpression()
|
||||
|
||||
if !accept(")") {
|
||||
expect(",")
|
||||
params += ", "
|
||||
appendOut(", ")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func parseExpression() {
|
||||
openingBrackets := 0
|
||||
|
||||
for !accept(",") && !accept(";") && !accept("{") && !accept("}") && (openingBrackets != 0 || !accept(")")) {
|
||||
current := get().token
|
||||
appendOut(current)
|
||||
|
||||
if accept("(") {
|
||||
openingBrackets++
|
||||
} else if accept(")") {
|
||||
openingBrackets--
|
||||
}
|
||||
|
||||
return params
|
||||
next()
|
||||
}
|
||||
|
||||
if openingBrackets != 0 {
|
||||
//panic("Opening bracket not closed")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ func initParser(token []Token) {
|
||||
}
|
||||
|
||||
func accept(token string) bool {
|
||||
return tokenEqual(token, get())
|
||||
return tokenIndex < len(tokens) && tokenEqual(token, get())
|
||||
}
|
||||
|
||||
func expect(token string) {
|
||||
@@ -40,6 +40,10 @@ func get() Token {
|
||||
return tokens[tokenIndex]
|
||||
}
|
||||
|
||||
func end() bool {
|
||||
return tokenIndex == len(tokens)
|
||||
}
|
||||
|
||||
func tokenEqual(a string, b Token) bool {
|
||||
return a == b.token
|
||||
}
|
||||
|
||||
@@ -17,11 +17,15 @@ var delimiter = []byte{'=',
|
||||
'<',
|
||||
'>',
|
||||
'!',
|
||||
','}
|
||||
',',
|
||||
'&',
|
||||
'|'}
|
||||
|
||||
var keywords = []string{"var",
|
||||
"if",
|
||||
"func"}
|
||||
"func",
|
||||
"true",
|
||||
"false"}
|
||||
|
||||
var whitespace = []byte{' ', '\n', '\t'}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user