Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Commit

Permalink
Fixed binary buildin functions and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kugelschieber committed Jan 14, 2016
1 parent e3a7c5d commit 737b96d
Show file tree
Hide file tree
Showing 9 changed files with 2,269 additions and 57 deletions.
65 changes: 16 additions & 49 deletions src/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package parser

import (
"errors"
"fmt"
"strconv"
"tokenizer"
"types"
)
Expand Down Expand Up @@ -369,24 +367,19 @@ func (c *Compiler) parseFunctionCall(out bool, name string) string {
output := ""

c.expect("(")
paramsStr, params, paramCount := c.parseParameter(false)
paramsStr, paramCount := c.parseParameter(false)
c.expect(")")

// buildin function
buildin := types.GetFunction(name)

if buildin != nil {
// check parameter count
if paramCount < buildin.ArgsLeft+buildin.ArgsRight {
panic(errors.New("Function expected " + strconv.Itoa(buildin.ArgsLeft+buildin.ArgsRight) + " parameter but found " + strconv.Itoa(paramCount)))
}

if buildin.Type == types.NULL {
output = name
} else if buildin.Type == types.UNARY {
output = c.parseUnaryFunction(name, paramsStr, paramCount)
} else {
output = c.parseBinaryFunction(name, params, buildin)
output = c.parseBinaryFunction(name, paramsStr, buildin, paramCount)
}
} else {
output = "[" + paramsStr + "] call " + name
Expand All @@ -411,63 +404,37 @@ func (c *Compiler) parseUnaryFunction(name, paramsStr string, paramCount int) st
return output
}

func (c *Compiler) parseBinaryFunction(name string, params []string, buildin *types.FunctionType) string {
// FIXME number of elements can't be determined if array is parameter
func (c *Compiler) parseBinaryFunction(name string, leftParamsStr string, buildin *types.FunctionType, paramCount int) string {
output := ""

fmt.Println(len(params))
fmt.Println(buildin.ArgsLeft)
fmt.Println(buildin.ArgsRight)
fmt.Println(params[0])
fmt.Println(params[1])
fmt.Println(params[2])

if buildin.ArgsLeft == 1 {
output = params[0] + " "
} else {
output = "["

for i := 0; i < buildin.ArgsLeft; i++ {
output += params[i]

if i != buildin.ArgsLeft-1 {
output += ","
}
}
c.next()
rightParamsStr, rightParamCount := c.parseParameter(false)
c.expect(")")

output += "] "
if paramCount > 1 {
leftParamsStr = "[" + leftParamsStr + "]"
}

output += name
if rightParamCount > 1 {
rightParamsStr = "[" + rightParamsStr + "]"
}

if buildin.ArgsRight-buildin.ArgsLeft == 1 {
output += " " + params[1]
if paramCount > 0 {
output = leftParamsStr + " " + name + " " + rightParamsStr
} else {
output += " ["

for i := buildin.ArgsLeft; i < buildin.ArgsRight; i++ {
output += params[i]

if i != buildin.ArgsRight-buildin.ArgsLeft-1 {
output += ","
}
}

output += "]"
output = name + " " + rightParamsStr
}

return output
}

func (c *Compiler) parseParameter(out bool) (string, []string, int) {
func (c *Compiler) parseParameter(out bool) (string, int) {
output := ""
params := make([]string, 0)
count := 0

for !c.accept(")") {
expr := c.parseExpression(out)
output += expr
params = append(params, expr)
count++

if !c.accept(")") {
Expand All @@ -480,7 +447,7 @@ func (c *Compiler) parseParameter(out bool) (string, []string, int) {
c.appendOut(output, false)
}

return output, params, count
return output, count
}

func (c *Compiler) parseExpression(out bool) string {
Expand Down
39 changes: 33 additions & 6 deletions src/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"parser"
"testing"
"tokenizer"
"types"
)

const (
types_file = "../../test/types"
)

func TestParserDeclaration(t *testing.T) {
Expand Down Expand Up @@ -91,12 +96,32 @@ func TestParserFunctionCall(t *testing.T) {
equal(t, got, want)
}

/*func TestParserBuildinFunctionCall(t *testing.T) {
got := getCompiled(t, "../../test/parser_buildin_func.asl")
want := "_x = (([player, foo] getVar bar) setHit [\"head\", \"tail\"]);\r\n"
func TestParserNullBuildinFunctionCall(t *testing.T) {
types.LoadTypes(types_file)

got := getCompiled(t, "../../test/parser_null_buildin_func.asl")
want := "_volume = (radioVolume);\r\n"

equal(t, got, want)
}

func TestParserUnaryBuildinFunctionCall(t *testing.T) {
types.LoadTypes(types_file)

got := getCompiled(t, "../../test/parser_unary_buildin_func.asl")
want := "_isReady = (unitReady soldier);\r\n"

equal(t, got, want)
}

func TestParserBinaryBuildinFunctionCall(t *testing.T) {
types.LoadTypes(types_file)

got := getCompiled(t, "../../test/parser_binary_buildin_func.asl")
want := "someCar setHit [\"motor\", 1];\r\n"

equal(t, got, want)
}*/
}

func TestParserOperator(t *testing.T) {
got := getCompiled(t, "../../test/parser_operator.asl")
Expand Down Expand Up @@ -154,12 +179,14 @@ func TestParserInlineCode(t *testing.T) {
equal(t, got, want)
}

/*func TestParserPreprocessor(t *testing.T) {
func TestParserPreprocessor(t *testing.T) {
types.LoadTypes(types_file)

got := getCompiled(t, "../../test/tokenizer_preprocessor.asl")
want := "\r\n#define HELLO_WORLD \"Hello World!\"\r\nhint HELLO_WORLD;\r\n"

equal(t, got, want)
}*/
}

func TestParserExpressionArray(t *testing.T) {
got := getCompiled(t, "../../test/parser_expression_array.asl")
Expand Down
2 changes: 1 addition & 1 deletion src/tokenizer/tokenizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestTokenizerInlineCode(t *testing.T) {

func TestTokenizerPreprocessor(t *testing.T) {
got := getTokens(t, "../../test/tokenizer_preprocessor.asl")
want := []string{"#define HELLO_WORLD \"Hello World!\"", "hint", "(", ")", "(", "HELLO_WORLD", ")", ";"}
want := []string{"#define HELLO_WORLD \"Hello World!\"", "hint", "(", "HELLO_WORLD", ")", ";"}

compareLength(t, &got, &want)
compareTokens(t, &got, &want)
Expand Down
18 changes: 18 additions & 0 deletions src/types/loader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package types_test

import (
"testing"
"types"
)

func TestTypesGetFunction(t *testing.T) {
if err := types.LoadTypes("../../test/types"); err != nil {
t.Error(err)
}

function := types.GetFunction("hint")

if function == nil {
t.Error("Function 'hint' not found in type list")
}
}
1 change: 1 addition & 0 deletions test/parser_binary_buildin_func.asl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setHit(someCar)("motor", 1);
1 change: 0 additions & 1 deletion test/parser_buildin_func.asl

This file was deleted.

1 change: 1 addition & 0 deletions test/parser_null_buildin_func.asl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var _volume = radioVolume();
1 change: 1 addition & 0 deletions test/parser_unary_buildin_func.asl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var _isReady = unitReady(soldier);
Loading

0 comments on commit 737b96d

Please sign in to comment.