mirror of
https://github.com/Kugelschieber/asl.git
synced 2026-01-18 12:00:25 +00:00
More testing, started parsing expressions, which requires a lot of
rewriting.
This commit is contained in:
@@ -1,5 +1 @@
|
||||
func foo(a, b) {
|
||||
return a > b;
|
||||
}
|
||||
|
||||
var x = foo(1, 2);
|
||||
var x = (1+(2+3))/(6*(someVariable+99-100))-(20)+anotherVariable+foo();
|
||||
|
||||
@@ -57,8 +57,8 @@ func parseVar() {
|
||||
parseExpression(true)
|
||||
}
|
||||
|
||||
expect(";")
|
||||
appendOut(";", true)
|
||||
expect(";")
|
||||
}
|
||||
|
||||
func parseIf() {
|
||||
@@ -241,7 +241,7 @@ func parseStatement() {
|
||||
next()
|
||||
parseBuildinFunctionCall(name)
|
||||
} else {
|
||||
parseFunctionCall()
|
||||
parseFunctionCall(true)
|
||||
appendOut(name + ";", true)
|
||||
}
|
||||
|
||||
@@ -258,13 +258,20 @@ func parseAssignment() {
|
||||
appendOut(";", true)
|
||||
}
|
||||
|
||||
func parseFunctionCall() {
|
||||
func parseFunctionCall(out bool) string {
|
||||
output := "["
|
||||
|
||||
expect("(")
|
||||
appendOut("[", false)
|
||||
parseParameter()
|
||||
//output += parseParameter()
|
||||
expect(")")
|
||||
expect(";")
|
||||
appendOut("] call ", false)
|
||||
//expect(";")
|
||||
output += "] call "
|
||||
|
||||
if out {
|
||||
appendOut(output, true)
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func parseBuildinFunctionCall(name string) {
|
||||
@@ -294,7 +301,7 @@ func parseParameter() {
|
||||
}
|
||||
|
||||
func parseExpression(out bool) string {
|
||||
openingBrackets := 0
|
||||
/*openingBrackets := 0
|
||||
output := ""
|
||||
|
||||
for !accept(",") && !accept(":") && !accept(";") && !accept("{") && !accept("}") && (openingBrackets != 0 || !accept(")")) {
|
||||
@@ -315,5 +322,70 @@ func parseExpression(out bool) string {
|
||||
next()
|
||||
}
|
||||
|
||||
return output*/
|
||||
|
||||
output := parseFactor()
|
||||
|
||||
for accept("+") || accept("-") {
|
||||
if accept("+") {
|
||||
output += "+"
|
||||
next()
|
||||
output += parseExpression(false)
|
||||
} else {
|
||||
output += "-"
|
||||
next()
|
||||
output += parseExpression(false)
|
||||
}
|
||||
}
|
||||
|
||||
if out {
|
||||
appendOut(output, false)
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func parseIdentifier() string {
|
||||
output := ""
|
||||
|
||||
if seek("(") {
|
||||
name := get().token
|
||||
next()
|
||||
output = "("+parseFunctionCall(false)+name+")"
|
||||
} else {
|
||||
output = get().token
|
||||
next()
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func parseTerm() string {
|
||||
if accept("(") {
|
||||
expect("(")
|
||||
output := "("+parseExpression(false)+")"
|
||||
expect(")")
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
return parseIdentifier()
|
||||
}
|
||||
|
||||
func parseFactor() string {
|
||||
output := parseTerm()
|
||||
|
||||
for accept("*") || accept("/") { // TODO: modulo?
|
||||
if accept("*") {
|
||||
output += "*"
|
||||
next()
|
||||
output += parseExpression(false)
|
||||
} else {
|
||||
output += "/"
|
||||
next()
|
||||
output += parseExpression(false)
|
||||
}
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
@@ -37,13 +37,13 @@ func expect(token string) {
|
||||
|
||||
// Returns true, if the next token matches expected one.
|
||||
// Does not throw parse errors and checks if token is available.
|
||||
/*func seek(token string) bool {
|
||||
func seek(token string) bool {
|
||||
if tokenIndex+1 >= len(tokens) {
|
||||
return false
|
||||
}
|
||||
|
||||
return tokenEqual(token, tokens[tokenIndex+1])
|
||||
}*/
|
||||
}
|
||||
|
||||
// Increases token counter, so that the next token is compared.
|
||||
func next() {
|
||||
|
||||
@@ -54,12 +54,13 @@ func TestParserFunction(t *testing.T) {
|
||||
equal(t, got, want)
|
||||
}
|
||||
|
||||
func TestParserAssignResult(t *testing.T) {
|
||||
// TODO
|
||||
/*func TestParserAssignResult(t *testing.T) {
|
||||
got := getCompiled(t, "test/parser_assign_result.asl")
|
||||
want := "x = [1, 2, 3] call foo;\ny = [1, 2, 3] call bar;"
|
||||
|
||||
equal(t, got, want)
|
||||
}
|
||||
}*/
|
||||
|
||||
func getCompiled(t *testing.T, file string) string {
|
||||
code, err := ioutil.ReadFile(file)
|
||||
|
||||
@@ -22,7 +22,11 @@ var delimiter = []byte{
|
||||
':',
|
||||
'&',
|
||||
'|',
|
||||
'$'}
|
||||
'$',
|
||||
'+',
|
||||
'-',
|
||||
'*',
|
||||
'/'} // TODO: modulo?
|
||||
|
||||
var keywords = []string{
|
||||
"var",
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestTokenizerWhile(t *testing.T) {
|
||||
|
||||
func TestTokenizerFor(t *testing.T) {
|
||||
got := getTokens(t, "test/tokenizer_for.asl")
|
||||
want := []string{"for", "var", "i", "=", "0", ";", "i", "<", "100", ";", "i", "=", "i+1", "{", "}"}
|
||||
want := []string{"for", "var", "i", "=", "0", ";", "i", "<", "100", ";", "i", "=", "i", "+", "1", "{", "}"}
|
||||
|
||||
compareLength(t, &got, &want)
|
||||
compareTokens(t, &got, &want)
|
||||
@@ -53,6 +53,14 @@ func TestTokenizerFunction(t *testing.T) {
|
||||
compareTokens(t, &got, &want)
|
||||
}
|
||||
|
||||
func TestTokenizerExpression(t *testing.T) {
|
||||
got := getTokens(t, "test/tokenizer_expr.asl")
|
||||
want := []string{"x", "=", "(", "(", "1", "+", "2", "+", "3", ")", "*", "4", "/", "2", ")", "+", "foo", "(", "1", ",", "2", ",", "3", ")", ";"}
|
||||
|
||||
compareLength(t, &got, &want)
|
||||
compareTokens(t, &got, &want)
|
||||
}
|
||||
|
||||
func compareLength(t *testing.T, got *[]Token, want *[]string) {
|
||||
if len(*got) != len(*want) {
|
||||
t.Error("Length of tokens got and expected tokens not equal, was:")
|
||||
|
||||
@@ -23,5 +23,5 @@ func main() {
|
||||
token := asl.Tokenize(code)
|
||||
out := asl.Parse(token, true)
|
||||
|
||||
fmt.Println("OUTPUT:\n"+out) // TODO: remove
|
||||
fmt.Println("OUTPUT:\n-------\n"+out)
|
||||
}
|
||||
|
||||
1
test/tokenizer_expr.asl
Normal file
1
test/tokenizer_expr.asl
Normal file
@@ -0,0 +1 @@
|
||||
x = ((1+2+3)*4/2)+foo(1, 2, 3);
|
||||
Reference in New Issue
Block a user