Fixed switch, something is wrong with function call parameters.

This commit is contained in:
Marvin Blum
2015-10-21 22:48:03 +02:00
parent 7aa5691863
commit dfe02ecef1
6 changed files with 43 additions and 27 deletions

View File

@@ -1,10 +1,10 @@
# ToDo # ToDo
* assign to returned values * ~~assign to returned values~~
* special cases (like if ... exitWith) * special cases (like if ... exitWith)
* sqf: ... sqf whitespace * sqf: ... sqf whitespace
* solution for build in commands which do not require left values * solution for build in commands which do not require left values
* ~~pretty/minified printing~~ * ~~pretty/minified printing~~
* usage * ~~usage~~
* recursive compiling * recursive compiling
* concurrent compiling * concurrent compiling

View File

@@ -1,8 +1,7 @@
/*switch somevar { func myFunc(a, b) {
case 1: return a > b;
x = 1; }
case 2:
x = 2;
}*/
var array = [1, 2, 3]; myFunc(1+3/4, 2-(66*22)/3-((123)));
foo();

View File

@@ -2,7 +2,6 @@ package asl
import ( import (
"strconv" "strconv"
"fmt" // TODO: remove
) )
const TAB = " " const TAB = " "
@@ -10,8 +9,6 @@ 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) {
@@ -40,6 +37,8 @@ func parseBlock() {
parseReturn() parseReturn()
} else if accept("sqf") { } else if accept("sqf") {
parseSqf() parseSqf()
} else if accept("case") || accept("default") {
return
} else { } else {
parseStatement() parseStatement()
} }
@@ -129,7 +128,6 @@ func parseSwitch() {
appendOut("};", true) appendOut("};", true)
} }
// FIXME
func parseSwitchBlock() { func parseSwitchBlock() {
if accept("}") { if accept("}") {
return return
@@ -142,7 +140,7 @@ func parseSwitchBlock() {
expect(":") expect(":")
appendOut(":", true) appendOut(":", true)
if !accept("case") && !accept("}") { if !accept("case") && !accept("}") && !accept("default") {
appendOut("{", true) appendOut("{", true)
parseBlock() parseBlock()
appendOut("};", true) appendOut("};", true)
@@ -289,13 +287,13 @@ func parseFunctionCall(out bool) string {
output := "[" output := "["
expect("(") expect("(")
//output += parseParameter() output += parseParameter(false)
expect(")") expect(")")
//expect(";") expect(";")
output += "] call " output += "] call "
if out { if out {
appendOut(output, true) appendOut(output, false)
} }
return output return output
@@ -305,26 +303,34 @@ func parseBuildinFunctionCall(name string) {
// FIXME: does not work for all kind of commands // FIXME: does not work for all kind of commands
expect("(") expect("(")
appendOut("[", false) appendOut("[", false)
parseParameter() parseParameter(true)
expect(")") expect(")")
appendOut("] ", false) appendOut("] ", false)
expect("(") expect("(")
appendOut(name + " [", false) appendOut(name + " [", false)
parseParameter() parseParameter(true)
expect(")") expect(")")
expect(";") expect(";")
appendOut("];", true) appendOut("];", true)
} }
func parseParameter() { func parseParameter(out bool) string {
output := ""
for !accept(")") { for !accept(")") {
parseExpression(true) output += parseExpression(out)
if !accept(")") { if !accept(")") {
expect(",") expect(",")
appendOut(", ", false) output += ", "
} }
} }
if out {
appendOut(output, false)
}
return output
} }
func parseExpression(out bool) string { func parseExpression(out bool) string {

View File

@@ -59,13 +59,12 @@ func TestParserFunction(t *testing.T) {
equal(t, got, want) equal(t, got, want)
} }
// 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")
@@ -81,6 +80,13 @@ func TestExpression2(t *testing.T) {
equal(t, got, want) equal(t, got, want)
} }
func TestFunctionCall(t *testing.T) {
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"
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

@@ -9,10 +9,10 @@ import (
const version = "0.1" const version = "0.1"
func usage() { func usage() {
fmt.Println("Usage: asl [-v|-r|-pretty] <input file/folder> [<output file/folder>]") fmt.Println("Usage: asl [-v|-r|-pretty] <input file/folder> [<output file/folder>]\n")
fmt.Println("-v (optional) shows asl version") fmt.Println("-v (optional) shows asl version")
fmt.Println("-r (optional) recursivly compile all asl files in folder") fmt.Println("-r (optional) recursivly compile all asl files in folder")
fmt.Println("-pretty (optional) activates pretty printing") fmt.Println("-pretty (optional) activates pretty printing\n")
fmt.Println("<input file/folder> file or directory to compile") 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") fmt.Println("<output file/folder> (optional) output file/folder, if not set, files will be created alongside their asl files")
} }

View File

@@ -0,0 +1,5 @@
func myFunc(a, b) {
return a > b;
}
myFunc(1+3/4, 2-(66*22)/3-((123)));