diff --git a/src/asl/parser.go b/src/asl/parser.go index 93b7966..21a1b0b 100644 --- a/src/asl/parser.go +++ b/src/asl/parser.go @@ -36,7 +36,7 @@ func parseBlock() { } else if accept("return") { parseReturn() } else if accept("case") || accept("default") { - return + return } else { parseStatement() } @@ -54,34 +54,34 @@ func parseVar() { if accept("=") { next() appendOut(" = ", false) - + if accept("[") { - parseArray() + parseArray() } else { - parseExpression(true) + parseExpression(true) } } - expect(";") + expect(";") appendOut(";", true) } func parseArray() { - expect("[") - appendOut("[", false) - - if !accept("]") { - parseExpression(true) - - for accept(",") { - next() - appendOut(",", false) - parseExpression(true) - } - } - - expect("]") - appendOut("]", false) + expect("[") + appendOut("[", false) + + if !accept("]") { + parseExpression(true) + + for accept(",") { + next() + appendOut(",", false) + parseExpression(true) + } + } + + expect("]") + appendOut("]", false) } func parseIf() { @@ -188,12 +188,12 @@ func parseForeach() { appendOut("{", true) parseBlock() expect("}") - appendOut("} forEach (" + expr + ");", true) + appendOut("} forEach ("+expr+");", true) } func parseFunction() { expect("func") - appendOut(get().token + " = {", true) + appendOut(get().token+" = {", true) next() expect("(") parseFunctionParameter() @@ -215,7 +215,7 @@ func parseFunctionParameter() { for !accept(")") { name := get().token next() - appendOut(name + " = _this select " + strconv.FormatInt(i, 10) + ";", true) + appendOut(name+" = _this select "+strconv.FormatInt(i, 10)+";", true) i++ if !accept(")") { @@ -266,46 +266,46 @@ func parseAssignment() { } func parseFunctionCall(out bool, name string) string { - output := "" - + output := "" + expect("(") leftParams, leftParamCount := parseParameter(false) expect(")") - + if accept("(") { - // buildin function - next() - rightParams, rightParamCount := parseParameter(false) - expect(")") - - if leftParamCount > 1 { - leftParams = "["+leftParams+"]" - } - - if rightParamCount > 1 { - rightParams = "["+rightParams+"]" - } - - if leftParamCount > 0 { - output = leftParams+" "+name+" "+rightParams - } else { - output = name+" "+rightParams - } + // buildin function + next() + rightParams, rightParamCount := parseParameter(false) + expect(")") + + if leftParamCount > 1 { + leftParams = "[" + leftParams + "]" + } + + if rightParamCount > 1 { + rightParams = "[" + rightParams + "]" + } + + if leftParamCount > 0 { + output = leftParams + " " + name + " " + rightParams + } else { + output = name + " " + rightParams + } } else { - output = "["+leftParams+"] call "+name + output = "[" + leftParams + "] call " + name } - + if out { - appendOut(output, false) + appendOut(output, false) } - + return output } func parseParameter(out bool) (string, int) { - output := "" - count := 0 - + output := "" + count := 0 + for !accept(")") { output += parseExpression(out) count++ @@ -315,119 +315,119 @@ func parseParameter(out bool) (string, int) { output += ", " } } - + if out { - appendOut(output, false) + appendOut(output, false) } - + return output, count } func parseExpression(out bool) string { output := parseArith() - + for accept("<") || accept(">") || accept("&") || accept("|") || accept("=") { - if accept("<") { - output += "<" - next() - } else if accept(">") { - output += ">" - next() - } else if accept("&") { - next() - expect("&") - output += "&&" - } else if accept("|") { - next() - expect("|") - output += "||" - } else { - output += "=" - next() - } - - if accept("=") { - output += "=" - next() - } - - output += parseExpression(false) + if accept("<") { + output += "<" + next() + } else if accept(">") { + output += ">" + next() + } else if accept("&") { + next() + expect("&") + output += "&&" + } else if accept("|") { + next() + expect("|") + output += "||" + } else { + output += "=" + next() + } + + if accept("=") { + output += "=" + next() + } + + output += parseExpression(false) } - + if out { - appendOut(output, false) + appendOut(output, false) } - + return output } func parseIdentifier() string { - output := "" - - if seek("(") && !accept("!") && !accept("-") { - name := get().token - next() - output = "("+parseFunctionCall(false, name)+")" - } else if accept("!") || accept("-") { - output = get().token - next() - - if !accept("(") { - output += get().token - next() - } else { - output += parseTerm() - } - } else { - output = get().token - next() - } - - return output + output := "" + + if seek("(") && !accept("!") && !accept("-") { + name := get().token + next() + output = "(" + parseFunctionCall(false, name) + ")" + } else if accept("!") || accept("-") { + output = get().token + next() + + if !accept("(") { + output += get().token + next() + } else { + output += parseTerm() + } + } else { + output = get().token + next() + } + + return output } func parseTerm() string { - if accept("(") { - expect("(") - output := "("+parseExpression(false)+")" - expect(")") - - return output - } - - return parseIdentifier() + if accept("(") { + expect("(") + output := "(" + parseExpression(false) + ")" + expect(")") + + return output + } + + return parseIdentifier() } func parseFactor() string { - output := parseTerm() - - for accept("*") || accept("/") { // TODO: modulo? - if accept("*") { - output += "*" - } else { - output += "/" - } - - next() - output += parseExpression(false) - } - - return output + output := parseTerm() + + for accept("*") || accept("/") { // TODO: modulo? + if accept("*") { + output += "*" + } else { + output += "/" + } + + next() + output += parseExpression(false) + } + + return output } func parseArith() string { - output := parseFactor() - - for accept("+") || accept("-") { - if accept("+") { - output += "+" - } else { - output += "-" - } - - next() - output += parseExpression(false) - } - + output := parseFactor() + + for accept("+") || accept("-") { + if accept("+") { + output += "+" + } else { + output += "-" + } + + next() + output += parseExpression(false) + } + return output } diff --git a/src/asl/parserHelper.go b/src/asl/parserHelper.go index 5db4334..f1b31b2 100644 --- a/src/asl/parserHelper.go +++ b/src/asl/parserHelper.go @@ -38,11 +38,11 @@ func expect(token string) { // Returns true, if the next token matches expected one. // Does not throw parse errors and checks if token is available. func seek(token string) bool { - if tokenIndex+1 >= len(tokens) { - return false - } - - return tokenEqual(token, tokens[tokenIndex+1]) + if tokenIndex+1 >= len(tokens) { + return false + } + + return tokenEqual(token, tokens[tokenIndex+1]) } // Increases token counter, so that the next token is compared. @@ -72,8 +72,8 @@ func tokenEqual(a string, b Token) bool { // Appends the output string to current SQF code output. func appendOut(str string, newLine bool) { out += str - + if newLine && pretty { - out += "\n" + out += "\n" } } diff --git a/src/asl/parser_test.go b/src/asl/parser_test.go index 7317232..af1e3e4 100644 --- a/src/asl/parser_test.go +++ b/src/asl/parser_test.go @@ -1,118 +1,118 @@ package asl_test import ( - "testing" - "io/ioutil" - "asl" + "asl" + "io/ioutil" + "testing" ) func TestParserDeclaration(t *testing.T) { - got := getCompiled(t, "test/tokenizer_var.asl") - want := "x = 1;\narray = [1,2,3];\n" - - equal(t, got, want) + got := getCompiled(t, "test/tokenizer_var.asl") + want := "x = 1;\narray = [1,2,3];\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) + 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 []\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\n") - fmt.Println(" file or directory to compile") - fmt.Println(" (optional) output file/folder, if not set, files will be created alongside their asl files") + fmt.Println("Usage: asl [-v|-r|-pretty] []\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\n") + fmt.Println(" file or directory to compile") + fmt.Println(" (optional) output file/folder, if not set, files will be created alongside their asl files") } func main() { @@ -23,5 +23,5 @@ func main() { token := asl.Tokenize(code) out := asl.Parse(token, true) - fmt.Print("OUTPUT:\n-------\n"+out) + fmt.Print("OUTPUT:\n-------\n" + out) }