Merge remote-tracking branch 'origin/master' into feature/code-keyword

Conflicts:
	in/simple.asl
This commit is contained in:
Marvin Blum
2015-10-28 18:13:55 +01:00
3 changed files with 27 additions and 7 deletions

View File

@@ -1,7 +1,7 @@
package asl package asl
import ( import (
"strconv"
) )
const TAB = " " const TAB = " "
@@ -217,19 +217,29 @@ func parseFunctionParameter() {
if accept("{") { if accept("{") {
return return
} }
i := int64(0) appendOut("params [", false)
for !accept(")") { for !accept(")") {
name := get().token name := get().token
next() next()
appendOut(name+" = _this select "+strconv.FormatInt(i, 10)+";", true)
i++ if accept("=") {
next()
value := get().token
next()
appendOut("[\""+name+"\","+value+"]", false)
} else {
appendOut("\""+name+"\"", false)
}
if !accept(")") { if !accept(")") {
expect(",") expect(",")
appendOut(",", false)
} }
} }
appendOut("];", true)
} }
func parseReturn() { func parseReturn() {

View File

@@ -57,7 +57,7 @@ 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 = {\r\nparam0 = _this select 0;\r\nparam1 = _this select 1;\r\nreturn true;\r\n};\r\n" want := "TestFunction = {\r\nparams [\"param0\",\"param1\"];\r\nreturn true;\r\n};\r\n"
equal(t, got, want) equal(t, got, want)
} }
@@ -85,7 +85,7 @@ func TestParserExpression2(t *testing.T) {
func TestParserFunctionCall(t *testing.T) { func TestParserFunctionCall(t *testing.T) {
got := getCompiled(t, "test/parser_func_call.asl") got := getCompiled(t, "test/parser_func_call.asl")
want := "myFunc = {\r\na = _this select 0;\r\nb = _this select 1;\r\nreturn a>b;\r\n};\r\n[1+3/4, 2-(66*22)/3-((123))] call myFunc;\r\n" want := "myFunc = {\r\nparams [\"a\",\"b\"];\r\nreturn a>b;\r\n};\r\n[1+3/4, 2-(66*22)/3-((123))] call myFunc;\r\n"
equal(t, got, want) equal(t, got, want)
} }
@@ -139,6 +139,13 @@ func TestParserArray(t *testing.T) {
equal(t, got, want) equal(t, got, want)
} }
func TestParserFunctionParams(t *testing.T) {
got := getCompiled(t, "test/parser_func_params.asl")
want := "myFunc = {\r\nparams [[\"a\",1],[\"b\",2]];\r\nreturn a+b;\r\n};\r\n"
equal(t, got, want)
}
func getCompiled(t *testing.T, file string) string { func getCompiled(t *testing.T, file string) string {
code, err := ioutil.ReadFile(file) code, err := ioutil.ReadFile(file)

View File

@@ -0,0 +1,3 @@
func myFunc(a = 1, b = 2) {
return a+b;
}