Fixed function call and buildin function call, all tests are green.

Still need to solve special cases and sqf: ... sqf blocks.
This commit is contained in:
Marvin Blum
2015-10-23 20:09:26 +02:00
parent dfe02ecef1
commit 04d0f4b61b
6 changed files with 52 additions and 43 deletions

View File

@@ -122,7 +122,7 @@ someUnit addItem "NVGoogles";
is equivalent to: is equivalent to:
``` ```
$addItem(someUnit)("NVGoogles"); addItem(someUnit)("NVGoogles");
``` ```
## Contribute ## Contribute

View File

@@ -1,7 +1,4 @@
func myFunc(a, b) { // TODO:
return a > b; //var _x = setHitIndex(vehicle()(player))(1, 1);
} var _x = setHit(player)("head");
myFunc(1+3/4, 2-(66*22)/3-((123)));
foo(); foo();

View File

@@ -261,13 +261,10 @@ func parseStatement() {
if accept("=") { if accept("=") {
appendOut(name, false) appendOut(name, false)
parseAssignment() parseAssignment()
} else if name == "$" {
name = get().token
next()
parseBuildinFunctionCall(name)
} else { } else {
parseFunctionCall(true) parseFunctionCall(true, name)
appendOut(name + ";", true) expect(";")
appendOut(";", true)
} }
if !end() { if !end() {
@@ -277,20 +274,41 @@ func parseStatement() {
func parseAssignment() { func parseAssignment() {
expect("=") expect("=")
appendOut(" = " + get().token, false) appendOut(" = ", false)
next() parseExpression(true)
expect(";") expect(";")
appendOut(";", true) appendOut(";", true)
} }
func parseFunctionCall(out bool) string { func parseFunctionCall(out bool, name string) string {
output := "[" output := ""
expect("(") expect("(")
output += parseParameter(false) leftParams, leftParamCount := parseParameter(false)
expect(")") expect(")")
expect(";")
output += "] call " if accept("(") {
// buildin function
next()
rightParams, rightParamCount := parseParameter(false)
expect(")")
if leftParamCount > 1 {
leftParams = "["+leftParams+"]"
}
if rightParamCount > 1 {
rightParams = "["+rightParams+"]"
}
if leftParamCount > 1 {
output = leftParams+" "+name+" "+rightParams
} else {
output = name+" "+rightParams
}
} else {
output = "["+leftParams+"] call "+name
}
if out { if out {
appendOut(output, false) appendOut(output, false)
@@ -299,26 +317,13 @@ func parseFunctionCall(out bool) string {
return output return output
} }
func parseBuildinFunctionCall(name string) { func parseParameter(out bool) (string, int) {
// FIXME: does not work for all kind of commands
expect("(")
appendOut("[", false)
parseParameter(true)
expect(")")
appendOut("] ", false)
expect("(")
appendOut(name + " [", false)
parseParameter(true)
expect(")")
expect(";")
appendOut("];", true)
}
func parseParameter(out bool) string {
output := "" output := ""
count := 0
for !accept(")") { for !accept(")") {
output += parseExpression(out) output += parseExpression(out)
count++
if !accept(")") { if !accept(")") {
expect(",") expect(",")
@@ -330,7 +335,7 @@ func parseParameter(out bool) string {
appendOut(output, false) appendOut(output, false)
} }
return output return output, count
} }
func parseExpression(out bool) string { func parseExpression(out bool) string {
@@ -377,7 +382,7 @@ func parseIdentifier() string {
if seek("(") { if seek("(") {
name := get().token name := get().token
next() next()
output = "("+parseFunctionCall(false)+name+")" output = "("+parseFunctionCall(false, name)+")"
} else { } else {
output = get().token output = get().token
next() next()

View File

@@ -61,32 +61,39 @@ func TestParserFunction(t *testing.T) {
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);\n"
equal(t, got, want) equal(t, got, want)
} }
func TestExpression(t *testing.T) { func TestParserExpression(t *testing.T) {
got := getCompiled(t, "test/parser_expression.asl") got := getCompiled(t, "test/parser_expression.asl")
want := "x = (1+(2+3))/(6*(someVariable+99-100))-(20)+anotherVariable+([] call foo);\n" want := "x = (1+(2+3))/(6*(someVariable+99-100))-(20)+anotherVariable+([] call foo);\n"
equal(t, got, want) equal(t, got, want)
} }
func TestExpression2(t *testing.T) { func TestParserExpression2(t *testing.T) {
got := getCompiled(t, "test/parser_expression2.asl") got := getCompiled(t, "test/parser_expression2.asl")
want := "x = true||(3>=4&&5<8);\n" want := "x = true||(3>=4&&5<8);\n"
equal(t, got, want) equal(t, got, want)
} }
func TestFunctionCall(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 = {\na = _this select 0;\nb = _this select 1;\nreturn a>b;\n};\n[1+3/4, 2-(66*22)/3-((123))] call myFunc;\n" want := "myFunc = {\na = _this select 0;\nb = _this select 1;\nreturn a>b;\n};\n[1+3/4, 2-(66*22)/3-((123))] call myFunc;\n"
equal(t, got, want) equal(t, got, want)
} }
func TestParserBuildinFunctionCall(t *testing.T) {
got := getCompiled(t, "test/parser_buildin_func.asl")
want := "_x = (setHit \"head\");\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

@@ -24,7 +24,6 @@ var delimiter = []byte{
':', ':',
'&', '&',
'|', '|',
'$',
'+', '+',
'-', '-',
'*', '*',

View File

@@ -0,0 +1 @@
var _x = setHit(player)("head");