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 _a = 1;
|
||||||
var _b = 2;
|
var _b = 2;
|
||||||
|
|
||||||
func somefunc(_x, _y, _z) {
|
func myFunc(_x, _y) {
|
||||||
if _x < _y {
|
if _x < _y {
|
||||||
hint(_z);
|
myFunc(_y, _x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _a < _b {
|
if _a < _b {
|
||||||
somefunc(1, "two", 3);
|
if _b < _a {
|
||||||
}
|
myFunc(_a, _b+9-(2/2));
|
||||||
else{
|
}
|
||||||
_a = 3;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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("=") {
|
if accept("=") {
|
||||||
next()
|
next()
|
||||||
appendOut(" = "+get().token)
|
appendOut(" = ")
|
||||||
next()
|
parseExpression()
|
||||||
}
|
}
|
||||||
|
|
||||||
appendOut(";\n")
|
appendOut(";\n")
|
||||||
@@ -46,7 +46,7 @@ func parseVar() {
|
|||||||
func parseIf() {
|
func parseIf() {
|
||||||
expect("if")
|
expect("if")
|
||||||
appendOut("if (")
|
appendOut("if (")
|
||||||
parseCondition()
|
parseExpression()
|
||||||
appendOut(") then {\n")
|
appendOut(") then {\n")
|
||||||
expect("{")
|
expect("{")
|
||||||
parseBlock()
|
parseBlock()
|
||||||
@@ -63,17 +63,6 @@ func parseIf() {
|
|||||||
appendOut("};\n")
|
appendOut("};\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseCondition() {
|
|
||||||
for !accept("{") {
|
|
||||||
appendOut(get().token)
|
|
||||||
next()
|
|
||||||
|
|
||||||
if !accept("{") {
|
|
||||||
appendOut(" ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseFunction() {
|
func parseFunction() {
|
||||||
expect("func")
|
expect("func")
|
||||||
appendOut(get().token+" = {\n")
|
appendOut(get().token+" = {\n")
|
||||||
@@ -89,7 +78,7 @@ func parseFunction() {
|
|||||||
|
|
||||||
func parseFunctionParameter() {
|
func parseFunctionParameter() {
|
||||||
// empty parameter list
|
// empty parameter list
|
||||||
if accept(")") {
|
if accept("{") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +100,7 @@ func parseFunctionParameter() {
|
|||||||
func parseStatement() {
|
func parseStatement() {
|
||||||
// empty block
|
// empty block
|
||||||
if accept("}") {
|
if accept("}") {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// variable or function name
|
// variable or function name
|
||||||
@@ -125,6 +114,10 @@ func parseStatement() {
|
|||||||
parseFunctionCall()
|
parseFunctionCall()
|
||||||
appendOut(name+";\n")
|
appendOut(name+";\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !end() {
|
||||||
|
parseStatement()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAssignment() {
|
func parseAssignment() {
|
||||||
@@ -137,24 +130,41 @@ func parseAssignment() {
|
|||||||
|
|
||||||
func parseFunctionCall() {
|
func parseFunctionCall() {
|
||||||
expect("(")
|
expect("(")
|
||||||
params := parseParameter()
|
appendOut("[")
|
||||||
|
parseParameter()
|
||||||
expect(")")
|
expect(")")
|
||||||
expect(";")
|
expect(";")
|
||||||
appendOut("["+params+"] call ")
|
appendOut("] call ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseParameter() string {
|
func parseParameter() {
|
||||||
params := ""
|
|
||||||
|
|
||||||
for !accept(")") {
|
for !accept(")") {
|
||||||
params += get().token
|
parseExpression()
|
||||||
next()
|
|
||||||
|
|
||||||
if !accept(")") {
|
if !accept(")") {
|
||||||
expect(",")
|
expect(",")
|
||||||
params += ", "
|
appendOut(", ")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return params
|
|
||||||
|
func parseExpression() {
|
||||||
|
openingBrackets := 0
|
||||||
|
|
||||||
|
for !accept(",") && !accept(";") && !accept("{") && !accept("}") && (openingBrackets != 0 || !accept(")")) {
|
||||||
|
current := get().token
|
||||||
|
appendOut(current)
|
||||||
|
|
||||||
|
if accept("(") {
|
||||||
|
openingBrackets++
|
||||||
|
} else if accept(")") {
|
||||||
|
openingBrackets--
|
||||||
|
}
|
||||||
|
|
||||||
|
next()
|
||||||
|
}
|
||||||
|
|
||||||
|
if openingBrackets != 0 {
|
||||||
|
//panic("Opening bracket not closed")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ func initParser(token []Token) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func accept(token string) bool {
|
func accept(token string) bool {
|
||||||
return tokenEqual(token, get())
|
return tokenIndex < len(tokens) && tokenEqual(token, get())
|
||||||
}
|
}
|
||||||
|
|
||||||
func expect(token string) {
|
func expect(token string) {
|
||||||
@@ -40,6 +40,10 @@ func get() Token {
|
|||||||
return tokens[tokenIndex]
|
return tokens[tokenIndex]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func end() bool {
|
||||||
|
return tokenIndex == len(tokens)
|
||||||
|
}
|
||||||
|
|
||||||
func tokenEqual(a string, b Token) bool {
|
func tokenEqual(a string, b Token) bool {
|
||||||
return a == b.token
|
return a == b.token
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,11 +17,15 @@ var delimiter = []byte{'=',
|
|||||||
'<',
|
'<',
|
||||||
'>',
|
'>',
|
||||||
'!',
|
'!',
|
||||||
','}
|
',',
|
||||||
|
'&',
|
||||||
|
'|'}
|
||||||
|
|
||||||
var keywords = []string{"var",
|
var keywords = []string{"var",
|
||||||
"if",
|
"if",
|
||||||
"func"}
|
"func",
|
||||||
|
"true",
|
||||||
|
"false"}
|
||||||
|
|
||||||
var whitespace = []byte{' ', '\n', '\t'}
|
var whitespace = []byte{' ', '\n', '\t'}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user