Fixed NaN type, updated changelog for future release, added support for null and unary build in functions. Need to add binary build in functions.

This commit is contained in:
Marvin Blum
2016-01-06 22:15:09 +01:00
parent 9d909a0b8a
commit fbfbb8a55c
3 changed files with 63 additions and 15 deletions

View File

@@ -1,5 +1,13 @@
# Changelog # Changelog
**1.2.0**
* better error output
* concurrent compiling
* errors are handled per file and won't stop the whole compilation
* function name check for build in functions
* function parameter count check for build in functions
**1.1.1** **1.1.1**
* arrays can now be declared within expressions * arrays can now be declared within expressions

View File

@@ -1,7 +1,11 @@
package parser package parser
import ( import (
"errors"
"fmt"
"strconv"
"tokenizer" "tokenizer"
"types"
) )
const new_line = "\r\n" const new_line = "\r\n"
@@ -215,6 +219,12 @@ func (c *Compiler) parseForeach() {
func (c *Compiler) parseFunction() { func (c *Compiler) parseFunction() {
c.expect("func") c.expect("func")
// check for build in function
if buildin, _ := types.GetFunction(c.get().Token); buildin == true {
panic(errors.New(c.get().Token + " is a build in function, choose a different name"))
}
c.appendOut(c.get().Token+" = {", true) c.appendOut(c.get().Token+" = {", true)
c.next() c.next()
c.expect("(") c.expect("(")
@@ -359,30 +369,42 @@ func (c *Compiler) parseFunctionCall(out bool, name string) string {
output := "" output := ""
c.expect("(") c.expect("(")
leftParams, leftParamCount := c.parseParameter(false) params, paramCount := c.parseParameter(false)
c.expect(")") c.expect(")")
if c.accept("(") { // buildin function
// buildin function exists, buildin := types.GetFunction(name)
c.next()
rightParams, rightParamCount := c.parseParameter(false)
c.expect(")")
if leftParamCount > 1 { if exists {
leftParams = "[" + leftParams + "]" // check parameter count
if exists && paramCount < buildin.ArgsCount {
panic(errors.New("Function expected " + strconv.Itoa(buildin.ArgsCount) + " parameter but found " + strconv.Itoa(paramCount)))
} }
if rightParamCount > 1 { if buildin.Type == types.NULL {
rightParams = "[" + rightParams + "]" output = name
} else if buildin.Type == types.UNARY {
if paramCount == 1 {
output = name + " " + params
} else {
output = "[" + params + "] call " + name
}
} else {
// TODO
}
fmt.Println(name)
/*if leftParamCount > 1 {
leftParams = "[" + leftParams + "]"
} }
if leftParamCount > 0 { if leftParamCount > 0 {
output = leftParams + " " + name + " " + rightParams output = leftParams + " " + name + " " + rightParams
} else { } else {
output = name + " " + rightParams output = name + " " + rightParams
} }*/
} else { } else {
output = "[" + leftParams + "] call " + name output = "[" + params + "] call " + name
} }
if out { if out {

View File

@@ -8,6 +8,7 @@ import (
const ( const (
// type for object types // type for object types
TYPE = 1 TYPE = 1
NAN = "NaN"
// types for functions // types for functions
NULL = 2 NULL = 2
@@ -22,6 +23,7 @@ type FunctionType struct {
Name string Name string
Type int // one of the constants NULL, UNARY, BINARY Type int // one of the constants NULL, UNARY, BINARY
ArgsCount int ArgsCount int
ArgsLeft int // number of args on left side for binary functions
} }
var functions []FunctionType var functions []FunctionType
@@ -29,6 +31,8 @@ var functions []FunctionType
// Returns function type information by name. // Returns function type information by name.
// If not found, the first parameter will be false. // If not found, the first parameter will be false.
func GetFunction(name string) (bool, FunctionType) { func GetFunction(name string) (bool, FunctionType) {
name = strings.ToLower(name)
for _, function := range functions { for _, function := range functions {
if function.Name == name { if function.Name == name {
return true, function return true, function
@@ -74,7 +78,7 @@ func parseTypes(content string) {
func parseNullFunction(line string) { func parseNullFunction(line string) {
parts := getParts(line) parts := getParts(line)
functions = append(functions, FunctionType{parts[0], NULL, 0}) functions = append(functions, FunctionType{parts[0], NULL, 0, 0})
} }
func parseUnaryFunction(line string) { func parseUnaryFunction(line string) {
@@ -85,7 +89,7 @@ func parseUnaryFunction(line string) {
} }
args := getArgs(parts[1]) args := getArgs(parts[1])
functions = append(functions, FunctionType{parts[0], UNARY, len(args)}) functions = append(functions, FunctionType{parts[0], UNARY, len(args) - getNaNArgs(args), 0})
} }
func parseBinaryFunction(line string) { func parseBinaryFunction(line string) {
@@ -96,8 +100,10 @@ func parseBinaryFunction(line string) {
} }
argsLeft := getArgs(parts[0]) argsLeft := getArgs(parts[0])
argsLeftCount := len(argsLeft) - getNaNArgs(argsLeft)
argsRight := getArgs(parts[2]) argsRight := getArgs(parts[2])
functions = append(functions, FunctionType{parts[1], BINARY, len(argsLeft) + len(argsRight)}) argsRightCount := len(argsRight) - getNaNArgs(argsRight)
functions = append(functions, FunctionType{parts[1], BINARY, argsLeftCount + argsRightCount, argsLeftCount})
} }
func getParts(line string) []string { func getParts(line string) []string {
@@ -108,3 +114,15 @@ func getParts(line string) []string {
func getArgs(part string) []string { func getArgs(part string) []string {
return strings.Split(part, ",") return strings.Split(part, ",")
} }
func getNaNArgs(args []string) int {
nan := 0
for _, arg := range args {
if arg == NAN {
nan++
}
}
return nan
}