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:
@@ -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(", ")
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
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