mirror of
https://github.com/Kugelschieber/asl.git
synced 2026-01-18 12:00:25 +00:00
Started testing for parser.
This commit is contained in:
2
ToDo.md
2
ToDo.md
@@ -4,7 +4,7 @@
|
||||
* special cases (like if ... exitWith)
|
||||
* sqf: ... sqf whitespace
|
||||
* solution for build in commands which do not require left values
|
||||
* pretty/minified printing
|
||||
* ~~pretty/minified printing~~
|
||||
* usage
|
||||
* recursive compiling
|
||||
* concurrent compiling
|
||||
|
||||
@@ -2,4 +2,4 @@ func foo(a, b) {
|
||||
return a > b;
|
||||
}
|
||||
|
||||
var x = foo();
|
||||
var x = foo(1, 2);
|
||||
|
||||
@@ -151,7 +151,6 @@ func parseFor() {
|
||||
expect(";")
|
||||
appendOut("}, {", false)
|
||||
parseExpression(true)
|
||||
expect(";")
|
||||
appendOut("}] do {", true)
|
||||
expect("{")
|
||||
parseBlock()
|
||||
|
||||
@@ -1,5 +1,85 @@
|
||||
package asl
|
||||
|
||||
import (
|
||||
|
||||
"testing"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
func TestParserDeclaration(t *testing.T) {
|
||||
got := getCompiled(t, "test/tokenizer_var.asl")
|
||||
want := "x = 1;\n"
|
||||
|
||||
equal(t, got, want)
|
||||
}
|
||||
|
||||
func TestParserAssignment(t *testing.T) {
|
||||
got := getCompiled(t, "test/parser_assignment.asl")
|
||||
want := "x = 1;\n"
|
||||
|
||||
equal(t, got, want)
|
||||
}
|
||||
|
||||
func TestParserIf(t *testing.T) {
|
||||
got := getCompiled(t, "test/tokenizer_if.asl")
|
||||
want := "if (a<b) then {\n};\n"
|
||||
|
||||
equal(t, got, want)
|
||||
}
|
||||
|
||||
func TestParserWhile(t *testing.T) {
|
||||
got := getCompiled(t, "test/tokenizer_while.asl")
|
||||
want := "while {true} do {\n};"
|
||||
|
||||
equal(t, got, want)
|
||||
}
|
||||
|
||||
func TestParserFor(t *testing.T) {
|
||||
got := getCompiled(t, "test/tokenizer_for.asl")
|
||||
want := "for [{i=0}, {i<100}, {i=i+1}] do {\n};\n"
|
||||
|
||||
equal(t, got, want)
|
||||
}
|
||||
|
||||
func TestParserEach(t *testing.T) {
|
||||
got := getCompiled(t, "test/tokenizer_each.asl")
|
||||
want := "{\n} forEach (allUnits);\n"
|
||||
|
||||
equal(t, got, want)
|
||||
}
|
||||
|
||||
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"
|
||||
|
||||
equal(t, got, want)
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
if err != nil {
|
||||
t.Error("Could not read test file: "+file)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
tokens := Tokenize(code)
|
||||
|
||||
return Parse(tokens, true)
|
||||
}
|
||||
|
||||
func equal(t *testing.T, got, want string) {
|
||||
if got != want {
|
||||
t.Error("Results do not equal, got:")
|
||||
t.Log(got)
|
||||
t.Log("expected:")
|
||||
t.Log(want)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
func TestVar(t *testing.T) {
|
||||
func TestTokenizerVar(t *testing.T) {
|
||||
got := getTokens(t, "test/tokenizer_var.asl")
|
||||
want := []string{"var", "x", "=", "1", ";"}
|
||||
|
||||
@@ -13,7 +13,7 @@ func TestVar(t *testing.T) {
|
||||
compareTokens(t, &got, &want)
|
||||
}
|
||||
|
||||
func TestIf(t *testing.T) {
|
||||
func TestTokenizerIf(t *testing.T) {
|
||||
got := getTokens(t, "test/tokenizer_if.asl")
|
||||
want := []string{"if", "a", "<", "b", "{", "}"}
|
||||
|
||||
@@ -21,7 +21,7 @@ func TestIf(t *testing.T) {
|
||||
compareTokens(t, &got, &want)
|
||||
}
|
||||
|
||||
func TestWhile(t *testing.T) {
|
||||
func TestTokenizerWhile(t *testing.T) {
|
||||
got := getTokens(t, "test/tokenizer_while.asl")
|
||||
want := []string{"while", "true", "{", "}"}
|
||||
|
||||
@@ -29,9 +29,7 @@ func TestWhile(t *testing.T) {
|
||||
compareTokens(t, &got, &want)
|
||||
}
|
||||
|
||||
//for var i = 0; i < 100; i = i+1 {
|
||||
//}
|
||||
func TestFor(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", "{", "}"}
|
||||
|
||||
@@ -39,7 +37,7 @@ func TestFor(t *testing.T) {
|
||||
compareTokens(t, &got, &want)
|
||||
}
|
||||
|
||||
func TestEach(t *testing.T) {
|
||||
func TestTokenizerEach(t *testing.T) {
|
||||
got := getTokens(t, "test/tokenizer_each.asl")
|
||||
want := []string{"each", "allUnits", "{", "}"}
|
||||
|
||||
@@ -47,7 +45,7 @@ func TestEach(t *testing.T) {
|
||||
compareTokens(t, &got, &want)
|
||||
}
|
||||
|
||||
func TestFunction(t *testing.T) {
|
||||
func TestTokenizerFunction(t *testing.T) {
|
||||
got := getTokens(t, "test/tokenizer_func.asl")
|
||||
want := []string{"func", "TestFunction", "(", "param0", ",", "param1", ")", "{", "return", "true", ";", "}"}
|
||||
|
||||
|
||||
@@ -6,8 +6,15 @@ import (
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
const version = "0.1"
|
||||
|
||||
func usage() {
|
||||
fmt.Println("Usage: asl [-v|-pretty]")
|
||||
fmt.Println("Usage: asl [-v|-r|-pretty] <input file/folder> [<output file/folder>]")
|
||||
fmt.Println("-v (optional) shows asl version")
|
||||
fmt.Println("-r (optional) recursivly compile all asl files in folder")
|
||||
fmt.Println("-pretty (optional) activates pretty printing")
|
||||
fmt.Println("<input file/folder> file or directory to compile")
|
||||
fmt.Println("<output file/folder> (optional) output file/folder, if not set, files will be created alongside their asl files")
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
2
test/parser_assign_result.asl
Normal file
2
test/parser_assign_result.asl
Normal file
@@ -0,0 +1,2 @@
|
||||
var x = foo(1, 2, 3);
|
||||
y = bar(1, 2, 3);
|
||||
1
test/parser_assignment.asl
Normal file
1
test/parser_assignment.asl
Normal file
@@ -0,0 +1 @@
|
||||
x = 1;
|
||||
Reference in New Issue
Block a user