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
* assign to returned values
* ~~assign to returned values~~
* special cases (like if ... exitWith)
* sqf: ... sqf whitespace
* solution for build in commands which do not require left values
* ~~pretty/minified printing~~
* usage
* ~~usage~~
* recursive compiling
* concurrent compiling

View File

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

View File

@@ -2,7 +2,6 @@ package asl
import (
"strconv"
"fmt" // TODO: remove
)
const TAB = " "
@@ -10,8 +9,6 @@ const TAB = " "
// Parses tokens, validates code to a specific degree
// and writes SQF code into desired location.
func Parse(token []Token, prettyPrinting bool) string {
fmt.Print("")
initParser(token, prettyPrinting)
for tokenIndex < len(token) {
@@ -40,6 +37,8 @@ func parseBlock() {
parseReturn()
} else if accept("sqf") {
parseSqf()
} else if accept("case") || accept("default") {
return
} else {
parseStatement()
}
@@ -129,7 +128,6 @@ func parseSwitch() {
appendOut("};", true)
}
// FIXME
func parseSwitchBlock() {
if accept("}") {
return
@@ -142,7 +140,7 @@ func parseSwitchBlock() {
expect(":")
appendOut(":", true)
if !accept("case") && !accept("}") {
if !accept("case") && !accept("}") && !accept("default") {
appendOut("{", true)
parseBlock()
appendOut("};", true)
@@ -289,13 +287,13 @@ func parseFunctionCall(out bool) string {
output := "["
expect("(")
//output += parseParameter()
output += parseParameter(false)
expect(")")
//expect(";")
expect(";")
output += "] call "
if out {
appendOut(output, true)
appendOut(output, false)
}
return output
@@ -305,26 +303,34 @@ func parseBuildinFunctionCall(name string) {
// FIXME: does not work for all kind of commands
expect("(")
appendOut("[", false)
parseParameter()
parseParameter(true)
expect(")")
appendOut("] ", false)
expect("(")
appendOut(name + " [", false)
parseParameter()
parseParameter(true)
expect(")")
expect(";")
appendOut("];", true)
}
func parseParameter() {
func parseParameter(out bool) string {
output := ""
for !accept(")") {
parseExpression(true)
output += parseExpression(out)
if !accept(")") {
expect(",")
appendOut(", ", false)
output += ", "
}
}
if out {
appendOut(output, false)
}
return output
}
func parseExpression(out bool) string {

View File

@@ -59,13 +59,12 @@ func TestParserFunction(t *testing.T) {
equal(t, got, want)
}
// TODO
/*func TestParserAssignResult(t *testing.T) {
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 TestExpression(t *testing.T) {
got := getCompiled(t, "test/parser_expression.asl")
@@ -81,6 +80,13 @@ func TestExpression2(t *testing.T) {
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 {
code, err := ioutil.ReadFile(file)

View File

@@ -9,10 +9,10 @@ import (
const version = "0.1"
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("-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("<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)));