More testing, fixed for, added array parsing, switch is buggy.

This commit is contained in:
Marvin Blum
2015-10-15 18:54:38 +02:00
parent a8c6da2958
commit 7aa5691863
8 changed files with 73 additions and 13 deletions

View File

@@ -1 +1,8 @@
var x = (a < b <= c) >= 10 && true; /*switch somevar {
case 1:
x = 1;
case 2:
x = 2;
}*/
var array = [1, 2, 3];

View File

@@ -2,6 +2,7 @@ package asl
import ( import (
"strconv" "strconv"
"fmt" // TODO: remove
) )
const TAB = " " const TAB = " "
@@ -9,6 +10,8 @@ const TAB = " "
// Parses tokens, validates code to a specific degree // Parses tokens, validates code to a specific degree
// and writes SQF code into desired location. // and writes SQF code into desired location.
func Parse(token []Token, prettyPrinting bool) string { func Parse(token []Token, prettyPrinting bool) string {
fmt.Print("")
initParser(token, prettyPrinting) initParser(token, prettyPrinting)
for tokenIndex < len(token) { for tokenIndex < len(token) {
@@ -54,20 +57,43 @@ func parseVar() {
if accept("=") { if accept("=") {
next() next()
appendOut(" = ", false) appendOut(" = ", false)
if accept("[") {
parseArray()
} else {
parseExpression(true) parseExpression(true)
} }
}
expect(";") expect(";")
appendOut(";", true) appendOut(";", true)
} }
func parseArray() {
expect("[")
appendOut("[", false)
if !accept("]") {
parseExpression(true)
for accept(",") {
next()
appendOut(",", false)
parseExpression(true)
}
}
expect("]")
appendOut("]", false)
}
func parseIf() { func parseIf() {
expect("if") expect("if")
appendOut("if (", false) appendOut("if (", false)
parseExpression(true) parseExpression(true)
appendOut(") then {", true) appendOut(") then {", true)
expect("{") expect("{")
parseExpression(true) parseBlock()
expect("}") expect("}")
if accept("else") { if accept("else") {
@@ -103,13 +129,14 @@ func parseSwitch() {
appendOut("};", true) appendOut("};", true)
} }
// FIXME
func parseSwitchBlock() { func parseSwitchBlock() {
if accept("}") { if accept("}") {
return return
} }
if accept("case") { if accept("case") {
expect("case") next()
appendOut("case ", false) appendOut("case ", false)
parseExpression(true) parseExpression(true)
expect(":") expect(":")
@@ -121,7 +148,7 @@ func parseSwitchBlock() {
appendOut("};", true) appendOut("};", true)
} }
} else if accept("default") { } else if accept("default") {
expect("default") next()
expect(":") expect(":")
appendOut("default:", true) appendOut("default:", true)

View File

@@ -1,13 +1,14 @@
package asl package asl_test
import ( import (
"testing" "testing"
"io/ioutil" "io/ioutil"
"asl"
) )
func TestParserDeclaration(t *testing.T) { func TestParserDeclaration(t *testing.T) {
got := getCompiled(t, "test/tokenizer_var.asl") got := getCompiled(t, "test/tokenizer_var.asl")
want := "x = 1;\n" want := "x = 1;\narray = [1,2,3];\n"
equal(t, got, want) equal(t, got, want)
} }
@@ -47,6 +48,10 @@ func TestParserEach(t *testing.T) {
equal(t, got, want) equal(t, got, want)
} }
func TestParserSwitch(t *testing.T) {
}
func TestParserFunction(t *testing.T) { func TestParserFunction(t *testing.T) {
got := getCompiled(t, "test/tokenizer_func.asl") got := getCompiled(t, "test/tokenizer_func.asl")
want := "TestFunction = {\nparam0 = _this select 0;\nparam1 = _this select 1;\nreturn true;\n};\n" want := "TestFunction = {\nparam0 = _this select 0;\nparam1 = _this select 1;\nreturn true;\n};\n"
@@ -55,12 +60,12 @@ 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) { func TestExpression(t *testing.T) {
got := getCompiled(t, "test/parser_expression.asl") got := getCompiled(t, "test/parser_expression.asl")
@@ -84,9 +89,9 @@ func getCompiled(t *testing.T, file string) string {
t.FailNow() 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) { func equal(t *testing.T, got, want string) {

View File

@@ -15,6 +15,8 @@ var delimiter = []byte{
'}', '}',
'(', '(',
')', ')',
'[',
']',
'<', '<',
'>', '>',
'!', '!',

View File

@@ -7,7 +7,7 @@ import (
func TestTokenizerVar(t *testing.T) { func TestTokenizerVar(t *testing.T) {
got := getTokens(t, "test/tokenizer_var.asl") 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) compareLength(t, &got, &want)
compareTokens(t, &got, &want) compareTokens(t, &got, &want)
@@ -45,6 +45,14 @@ func TestTokenizerEach(t *testing.T) {
compareTokens(t, &got, &want) 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) { func TestTokenizerFunction(t *testing.T) {
got := getTokens(t, "test/tokenizer_func.asl") got := getTokens(t, "test/tokenizer_func.asl")
want := []string{"func", "TestFunction", "(", "param0", ",", "param1", ")", "{", "return", "true", ";", "}"} want := []string{"func", "TestFunction", "(", "param0", ",", "param1", ")", "{", "return", "true", ";", "}"}

View File

@@ -23,5 +23,5 @@ func main() {
token := asl.Tokenize(code) token := asl.Tokenize(code)
out := asl.Parse(token, true) out := asl.Parse(token, true)
fmt.Println("OUTPUT:\n-------\n"+out) fmt.Print("OUTPUT:\n-------\n"+out)
} }

10
test/tokenizer_switch.asl Normal file
View File

@@ -0,0 +1,10 @@
switch x {
case 1:
x = 1;
break;
case 2:
x = 2;
break;
default:
x = 3;
}

View File

@@ -7,3 +7,4 @@ comment
*/ */
var x = 1; var x = 1;
var array = [1, 2, 3];