mirror of
https://github.com/Kugelschieber/asl.git
synced 2026-01-18 12:00:25 +00:00
More testing, fixed for, added array parsing, switch is buggy.
This commit is contained in:
@@ -2,6 +2,7 @@ package asl
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"fmt" // TODO: remove
|
||||
)
|
||||
|
||||
const TAB = " "
|
||||
@@ -9,6 +10,8 @@ const TAB = " "
|
||||
// Parses tokens, validates code to a specific degree
|
||||
// and writes SQF code into desired location.
|
||||
func Parse(token []Token, prettyPrinting bool) string {
|
||||
fmt.Print("")
|
||||
|
||||
initParser(token, prettyPrinting)
|
||||
|
||||
for tokenIndex < len(token) {
|
||||
@@ -54,20 +57,43 @@ func parseVar() {
|
||||
if accept("=") {
|
||||
next()
|
||||
appendOut(" = ", false)
|
||||
parseExpression(true)
|
||||
|
||||
if accept("[") {
|
||||
parseArray()
|
||||
} else {
|
||||
parseExpression(true)
|
||||
}
|
||||
}
|
||||
|
||||
expect(";")
|
||||
appendOut(";", true)
|
||||
}
|
||||
|
||||
func parseArray() {
|
||||
expect("[")
|
||||
appendOut("[", false)
|
||||
|
||||
if !accept("]") {
|
||||
parseExpression(true)
|
||||
|
||||
for accept(",") {
|
||||
next()
|
||||
appendOut(",", false)
|
||||
parseExpression(true)
|
||||
}
|
||||
}
|
||||
|
||||
expect("]")
|
||||
appendOut("]", false)
|
||||
}
|
||||
|
||||
func parseIf() {
|
||||
expect("if")
|
||||
appendOut("if (", false)
|
||||
parseExpression(true)
|
||||
appendOut(") then {", true)
|
||||
expect("{")
|
||||
parseExpression(true)
|
||||
parseBlock()
|
||||
expect("}")
|
||||
|
||||
if accept("else") {
|
||||
@@ -103,13 +129,14 @@ func parseSwitch() {
|
||||
appendOut("};", true)
|
||||
}
|
||||
|
||||
// FIXME
|
||||
func parseSwitchBlock() {
|
||||
if accept("}") {
|
||||
return
|
||||
}
|
||||
|
||||
if accept("case") {
|
||||
expect("case")
|
||||
next()
|
||||
appendOut("case ", false)
|
||||
parseExpression(true)
|
||||
expect(":")
|
||||
@@ -121,7 +148,7 @@ func parseSwitchBlock() {
|
||||
appendOut("};", true)
|
||||
}
|
||||
} else if accept("default") {
|
||||
expect("default")
|
||||
next()
|
||||
expect(":")
|
||||
appendOut("default:", true)
|
||||
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package asl
|
||||
package asl_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"io/ioutil"
|
||||
"asl"
|
||||
)
|
||||
|
||||
func TestParserDeclaration(t *testing.T) {
|
||||
got := getCompiled(t, "test/tokenizer_var.asl")
|
||||
want := "x = 1;\n"
|
||||
want := "x = 1;\narray = [1,2,3];\n"
|
||||
|
||||
equal(t, got, want)
|
||||
}
|
||||
@@ -47,6 +48,10 @@ func TestParserEach(t *testing.T) {
|
||||
equal(t, got, want)
|
||||
}
|
||||
|
||||
func TestParserSwitch(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestParserFunction(t *testing.T) {
|
||||
got := getCompiled(t, "test/tokenizer_func.asl")
|
||||
want := "TestFunction = {\nparam0 = _this select 0;\nparam1 = _this select 1;\nreturn true;\n};\n"
|
||||
@@ -55,12 +60,12 @@ func TestParserFunction(t *testing.T) {
|
||||
}
|
||||
|
||||
// TODO
|
||||
func TestParserAssignResult(t *testing.T) {
|
||||
/*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 TestExpression(t *testing.T) {
|
||||
got := getCompiled(t, "test/parser_expression.asl")
|
||||
@@ -84,9 +89,9 @@ func getCompiled(t *testing.T, file string) string {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
tokens := Tokenize(code)
|
||||
tokens := asl.Tokenize(code)
|
||||
|
||||
return Parse(tokens, true)
|
||||
return asl.Parse(tokens, true)
|
||||
}
|
||||
|
||||
func equal(t *testing.T, got, want string) {
|
||||
|
||||
@@ -15,6 +15,8 @@ var delimiter = []byte{
|
||||
'}',
|
||||
'(',
|
||||
')',
|
||||
'[',
|
||||
']',
|
||||
'<',
|
||||
'>',
|
||||
'!',
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
func TestTokenizerVar(t *testing.T) {
|
||||
got := getTokens(t, "test/tokenizer_var.asl")
|
||||
want := []string{"var", "x", "=", "1", ";"}
|
||||
want := []string{"var", "x", "=", "1", ";", "var", "array", "=", "[", "1", ",", "2", ",", "3", "]", ";"}
|
||||
|
||||
compareLength(t, &got, &want)
|
||||
compareTokens(t, &got, &want)
|
||||
@@ -45,6 +45,14 @@ func TestTokenizerEach(t *testing.T) {
|
||||
compareTokens(t, &got, &want)
|
||||
}
|
||||
|
||||
func TestTokenizerSwitch(t *testing.T) {
|
||||
got := getTokens(t, "test/tokenizer_switch.asl")
|
||||
want := []string{"switch", "x", "{", "case", "1", ":", "x", "=", "1", ";", "break", ";", "case", "2", ":", "x", "=", "2", ";", "break", ";", "default", ":", "x", "=", "3", ";", "}"}
|
||||
|
||||
compareLength(t, &got, &want)
|
||||
compareTokens(t, &got, &want)
|
||||
}
|
||||
|
||||
func TestTokenizerFunction(t *testing.T) {
|
||||
got := getTokens(t, "test/tokenizer_func.asl")
|
||||
want := []string{"func", "TestFunction", "(", "param0", ",", "param1", ")", "{", "return", "true", ";", "}"}
|
||||
|
||||
@@ -23,5 +23,5 @@ func main() {
|
||||
token := asl.Tokenize(code)
|
||||
out := asl.Parse(token, true)
|
||||
|
||||
fmt.Println("OUTPUT:\n-------\n"+out)
|
||||
fmt.Print("OUTPUT:\n-------\n"+out)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user