mirror of
https://github.com/Kugelschieber/asl.git
synced 2026-01-18 12:00:25 +00:00
Added conditional expressions.
This commit is contained in:
@@ -1 +1 @@
|
|||||||
var x = (1+(2+3))/(6*(someVariable+99-100))-(20)+anotherVariable+foo();
|
var x = (a < b <= c) >= 10 && true;
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ func parseIf() {
|
|||||||
parseExpression(true)
|
parseExpression(true)
|
||||||
appendOut(") then {", true)
|
appendOut(") then {", true)
|
||||||
expect("{")
|
expect("{")
|
||||||
parseBlock()
|
parseExpression(true)
|
||||||
expect("}")
|
expect("}")
|
||||||
|
|
||||||
if accept("else") {
|
if accept("else") {
|
||||||
@@ -301,41 +301,34 @@ func parseParameter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseExpression(out bool) string {
|
func parseExpression(out bool) string {
|
||||||
/*openingBrackets := 0
|
output := parseArith()
|
||||||
output := ""
|
|
||||||
|
|
||||||
for !accept(",") && !accept(":") && !accept(";") && !accept("{") && !accept("}") && (openingBrackets != 0 || !accept(")")) {
|
for accept("<") || accept(">") || accept("&") || accept("|") || accept("=") {
|
||||||
current := get().token
|
if accept("<") {
|
||||||
|
output += "<"
|
||||||
if out {
|
next()
|
||||||
appendOut(current, false)
|
} else if accept(">") {
|
||||||
|
output += ">"
|
||||||
|
next()
|
||||||
|
} else if accept("&") {
|
||||||
|
next()
|
||||||
|
expect("&")
|
||||||
|
output += "&&"
|
||||||
|
} else if accept("|") {
|
||||||
|
next()
|
||||||
|
expect("|")
|
||||||
|
output += "||"
|
||||||
} else {
|
} else {
|
||||||
output += current
|
output += "="
|
||||||
}
|
|
||||||
|
|
||||||
if accept("(") {
|
|
||||||
openingBrackets++
|
|
||||||
} else if accept(")") {
|
|
||||||
openingBrackets--
|
|
||||||
}
|
|
||||||
|
|
||||||
next()
|
next()
|
||||||
}
|
}
|
||||||
|
|
||||||
return output*/
|
if accept("=") {
|
||||||
|
output += "="
|
||||||
output := parseFactor()
|
|
||||||
|
|
||||||
for accept("+") || accept("-") {
|
|
||||||
if accept("+") {
|
|
||||||
output += "+"
|
|
||||||
next()
|
next()
|
||||||
|
}
|
||||||
|
|
||||||
output += parseExpression(false)
|
output += parseExpression(false)
|
||||||
} else {
|
|
||||||
output += "-"
|
|
||||||
next()
|
|
||||||
output += parseExpression(false)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if out {
|
if out {
|
||||||
@@ -378,14 +371,30 @@ func parseFactor() string {
|
|||||||
for accept("*") || accept("/") { // TODO: modulo?
|
for accept("*") || accept("/") { // TODO: modulo?
|
||||||
if accept("*") {
|
if accept("*") {
|
||||||
output += "*"
|
output += "*"
|
||||||
next()
|
|
||||||
output += parseExpression(false)
|
|
||||||
} else {
|
} else {
|
||||||
output += "/"
|
output += "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
next()
|
||||||
|
output += parseExpression(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseArith() string {
|
||||||
|
output := parseFactor()
|
||||||
|
|
||||||
|
for accept("+") || accept("-") {
|
||||||
|
if accept("+") {
|
||||||
|
output += "+"
|
||||||
|
} else {
|
||||||
|
output += "-"
|
||||||
|
}
|
||||||
|
|
||||||
next()
|
next()
|
||||||
output += parseExpression(false)
|
output += parseExpression(false)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,12 +55,26 @@ func TestParserFunction(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
/*func TestParserAssignResult(t *testing.T) {
|
func TestParserAssignResult(t *testing.T) {
|
||||||
got := getCompiled(t, "test/parser_assign_result.asl")
|
got := getCompiled(t, "test/parser_assign_result.asl")
|
||||||
want := "x = [1, 2, 3] call foo;\ny = [1, 2, 3] call bar;"
|
want := "x = [1, 2, 3] call foo;\ny = [1, 2, 3] call bar;"
|
||||||
|
|
||||||
equal(t, got, want)
|
equal(t, got, want)
|
||||||
}*/
|
}
|
||||||
|
|
||||||
|
func TestExpression(t *testing.T) {
|
||||||
|
got := getCompiled(t, "test/parser_expression.asl")
|
||||||
|
want := "x = (1+(2+3))/(6*(someVariable+99-100))-(20)+anotherVariable+([] call foo);\n"
|
||||||
|
|
||||||
|
equal(t, got, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpression2(t *testing.T) {
|
||||||
|
got := getCompiled(t, "test/parser_expression2.asl")
|
||||||
|
want := "x = true||(3>=4&&5<8);\n"
|
||||||
|
|
||||||
|
equal(t, got, want)
|
||||||
|
}
|
||||||
|
|
||||||
func getCompiled(t *testing.T, file string) string {
|
func getCompiled(t *testing.T, file string) string {
|
||||||
code, err := ioutil.ReadFile(file)
|
code, err := ioutil.ReadFile(file)
|
||||||
|
|||||||
1
test/parser_expression.asl
Normal file
1
test/parser_expression.asl
Normal file
@@ -0,0 +1 @@
|
|||||||
|
var x = (1+(2+3))/(6*(someVariable+99-100))-(20)+anotherVariable+foo();
|
||||||
1
test/parser_expression2.asl
Normal file
1
test/parser_expression2.asl
Normal file
@@ -0,0 +1 @@
|
|||||||
|
var x = true || (3 >= 4 && 5 < 8);
|
||||||
Reference in New Issue
Block a user