config/lang: math operations

This commit is contained in:
Mitchell Hashimoto 2015-02-26 14:26:14 -08:00
parent 684228e371
commit 07b78fdf84
7 changed files with 259 additions and 208 deletions

View File

@ -15,11 +15,107 @@ func registerBuiltins(scope *ast.BasicScope) *ast.BasicScope {
if scope.FuncMap == nil {
scope.FuncMap = make(map[string]ast.Function)
}
// Implicit conversions
scope.FuncMap["__builtin_FloatToInt"] = builtinFloatToInt()
scope.FuncMap["__builtin_FloatToString"] = builtinFloatToString()
scope.FuncMap["__builtin_IntToFloat"] = builtinIntToFloat()
scope.FuncMap["__builtin_IntToString"] = builtinIntToString()
scope.FuncMap["__builtin_StringToInt"] = builtinStringToInt()
// Math operations
scope.FuncMap["__builtin_IntMath"] = builtinIntMath()
scope.FuncMap["__builtin_FloatMath"] = builtinFloatMath()
return scope
}
func builtinFloatMath() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeInt},
Variadic: true,
VariadicType: ast.TypeFloat,
ReturnType: ast.TypeFloat,
Callback: func(args []interface{}) (interface{}, error) {
op := args[0].(ast.ArithmeticOp)
result := args[1].(float64)
for _, raw := range args[2:] {
arg := raw.(float64)
switch op {
case ast.ArithmeticOpAdd:
result += arg
case ast.ArithmeticOpSub:
result -= arg
case ast.ArithmeticOpMul:
result *= arg
case ast.ArithmeticOpDiv:
result /= arg
}
}
return result, nil
},
}
}
func builtinIntMath() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeInt},
Variadic: true,
VariadicType: ast.TypeInt,
ReturnType: ast.TypeInt,
Callback: func(args []interface{}) (interface{}, error) {
op := args[0].(ast.ArithmeticOp)
result := args[1].(int)
for _, raw := range args[2:] {
arg := raw.(int)
switch op {
case ast.ArithmeticOpAdd:
result += arg
case ast.ArithmeticOpSub:
result -= arg
case ast.ArithmeticOpMul:
result *= arg
case ast.ArithmeticOpDiv:
result /= arg
}
}
return result, nil
},
}
}
func builtinFloatToInt() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeFloat},
ReturnType: ast.TypeInt,
Callback: func(args []interface{}) (interface{}, error) {
return int(args[0].(float64)), nil
},
}
}
func builtinFloatToString() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeFloat},
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
return strconv.FormatFloat(
args[0].(float64), 'g', -1, 64), nil
},
}
}
func builtinIntToFloat() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeInt},
ReturnType: ast.TypeFloat,
Callback: func(args []interface{}) (interface{}, error) {
return float64(args[0].(int)), nil
},
}
}
func builtinIntToString() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeInt},

View File

@ -95,17 +95,20 @@ type typeCheckArithmetic struct {
func (tc *typeCheckArithmetic) TypeCheck(v *TypeCheck) (ast.Node, error) {
// The arguments are on the stack in reverse order, so pop them off.
args := make([]ast.Type, len(tc.n.Exprs))
exprs := make([]ast.Type, len(tc.n.Exprs))
for i, _ := range tc.n.Exprs {
args[len(tc.n.Exprs)-1-i] = v.StackPop()
exprs[len(tc.n.Exprs)-1-i] = v.StackPop()
}
// Determine the resulting type we want
mathFunc := "__builtin_IntMath"
mathType := ast.TypeInt
switch v := args[0]; v {
switch v := exprs[0]; v {
case ast.TypeInt:
fallthrough
mathFunc = "__builtin_IntMath"
mathType = v
case ast.TypeFloat:
mathFunc = "__builtin_FloatMath"
mathType = v
default:
return nil, fmt.Errorf(
@ -114,9 +117,9 @@ func (tc *typeCheckArithmetic) TypeCheck(v *TypeCheck) (ast.Node, error) {
}
// Verify the args
for i, arg := range args {
for i, arg := range exprs {
if arg != mathType {
cn := v.ImplicitConversion(args[i], mathType, tc.n.Exprs[i])
cn := v.ImplicitConversion(exprs[i], mathType, tc.n.Exprs[i])
if cn != nil {
tc.n.Exprs[i] = cn
continue
@ -131,7 +134,20 @@ func (tc *typeCheckArithmetic) TypeCheck(v *TypeCheck) (ast.Node, error) {
// Return type
v.StackPush(mathType)
return tc.n, nil
// Replace our node with a call to the proper function. This isn't
// type checked but we already verified types.
args := make([]ast.Node, len(tc.n.Exprs)+1)
args[0] = &ast.LiteralNode{
Value: tc.n.Op,
Typex: ast.TypeInt,
Posx: tc.n.Pos(),
}
copy(args[1:], tc.n.Exprs)
return &ast.Call{
Func: mathFunc,
Args: args,
Posx: tc.n.Pos(),
}, nil
}
type typeCheckCall struct {

View File

@ -32,7 +32,12 @@ func Eval(root ast.Node, config *EvalConfig) (interface{}, ast.Type, error) {
}
scope := registerBuiltins(config.GlobalScope)
implicitMap := map[ast.Type]map[ast.Type]string{
ast.TypeFloat: {
ast.TypeInt: "__builtin_FloatToInt",
ast.TypeString: "__builtin_FloatToString",
},
ast.TypeInt: {
ast.TypeFloat: "__builtin_IntToFloat",
ast.TypeString: "__builtin_IntToString",
},
ast.TypeString: {
@ -47,7 +52,8 @@ func Eval(root ast.Node, config *EvalConfig) (interface{}, ast.Type, error) {
// Build up the semantic checks for execution
checks := make(
[]SemanticChecker,
len(config.SemanticChecks), len(config.SemanticChecks)+2)
len(config.SemanticChecks),
len(config.SemanticChecks)+2)
copy(checks, config.SemanticChecks)
checks = append(checks, ic.Visit)
checks = append(checks, tv.Visit)
@ -134,8 +140,6 @@ func (v *evalVisitor) visit(raw ast.Node) ast.Node {
// types as well as any other EvalNode implementations.
func evalNode(raw ast.Node) (EvalNode, error) {
switch n := raw.(type) {
case *ast.Arithmetic:
return &evalArithmetic{n}, nil
case *ast.Call:
return &evalCall{n}, nil
case *ast.Concat:
@ -154,69 +158,6 @@ func evalNode(raw ast.Node) (EvalNode, error) {
}
}
type evalArithmetic struct{ *ast.Arithmetic }
func (v *evalArithmetic) Eval(
s ast.Scope, stack *ast.Stack) (interface{}, ast.Type, error) {
// The arguments are on the stack in reverse order, so pop them off.
var resultType ast.Type
exprs := make([]interface{}, len(v.Exprs))
for i, _ := range v.Exprs {
node := stack.Pop().(*ast.LiteralNode)
exprs[len(v.Exprs)-1-i] = node.Value
resultType = node.Typex
}
// Do the operation
var result interface{}
var err error
switch resultType {
case ast.TypeInt:
result, err = v.evalInt(v.Op, exprs)
default:
return nil, resultType, fmt.Errorf(
"unknown math operand type: %s", resultType)
}
return result, resultType, err
}
func (v *evalArithmetic) evalInt(
op ast.ArithmeticOp, exprs []interface{}) (int, error) {
switch v.Op {
case ast.ArithmeticOpAdd:
result := 0
for _, expr := range exprs {
result += expr.(int)
}
return result, nil
case ast.ArithmeticOpSub:
result := exprs[0].(int)
for _, expr := range exprs[1:] {
result -= expr.(int)
}
return result, nil
case ast.ArithmeticOpMul:
result := exprs[0].(int)
for _, expr := range exprs[1:] {
result *= expr.(int)
}
return result, nil
case ast.ArithmeticOpDiv:
result := exprs[0].(int)
for _, expr := range exprs[1:] {
result /= expr.(int)
}
return result, nil
default:
return 0, fmt.Errorf("unknown math operation: %s", v.Op)
}
}
type evalCall struct{ *ast.Call }
func (v *evalCall) Eval(s ast.Scope, stack *ast.Stack) (interface{}, ast.Type, error) {

View File

@ -71,6 +71,30 @@ func TestEval(t *testing.T) {
ast.TypeString,
},
{
"foo ${42.0+1.0}",
nil,
false,
"foo 43",
ast.TypeString,
},
{
"foo ${42.0+1}",
nil,
false,
"foo 43",
ast.TypeString,
},
{
"foo ${42+1.0}",
nil,
false,
"foo 43",
ast.TypeString,
},
{
"foo ${rand()}",
&ast.BasicScope{

View File

@ -24,7 +24,7 @@ import (
%token <token> ARITH_OP IDENTIFIER INTEGER FLOAT STRING
%type <node> arith expr interpolation literal literalModeTop literalModeValue
%type <node> expr interpolation literal literalModeTop literalModeValue
%type <nodeList> args
%%
@ -116,9 +116,13 @@ expr:
Posx: $1.Pos,
}
}
| arith
| expr ARITH_OP expr
{
$$ = $1
$$ = &ast.Arithmetic{
Op: $2.Value.(ast.ArithmeticOp),
Exprs: []ast.Node{$1, $3},
Posx: $1.Pos(),
}
}
| IDENTIFIER
{
@ -129,27 +133,6 @@ expr:
$$ = &ast.Call{Func: $1.Value.(string), Args: $3, Posx: $1.Pos}
}
arith:
INTEGER ARITH_OP INTEGER
{
$$ = &ast.Arithmetic{
Op: $2.Value.(ast.ArithmeticOp),
Exprs: []ast.Node{
&ast.LiteralNode{
Value: $1.Value.(int),
Typex: ast.TypeInt,
Posx: $1.Pos,
},
&ast.LiteralNode{
Value: $3.Value.(int),
Typex: ast.TypeInt,
Posx: $3.Pos,
},
},
Posx: $1.Pos,
}
}
args:
{
$$ = nil

View File

@ -50,7 +50,7 @@ const parserEofCode = 1
const parserErrCode = 2
const parserMaxDepth = 200
//line lang.y:176
//line lang.y:159
//line yacctab:1
var parserExca = []int{
@ -59,7 +59,7 @@ var parserExca = []int{
-2, 0,
}
const parserNprod = 19
const parserNprod = 18
const parserPrivate = 57344
var parserTokenNames []string
@ -69,41 +69,41 @@ const parserLast = 26
var parserAct = []int{
9, 7, 7, 18, 3, 21, 22, 8, 17, 14,
11, 12, 6, 6, 16, 8, 15, 1, 20, 10,
2, 19, 4, 23, 5, 13,
9, 7, 7, 15, 3, 20, 21, 8, 14, 13,
11, 12, 6, 6, 15, 8, 17, 19, 16, 10,
2, 1, 22, 18, 4, 5,
}
var parserPact = []int{
-2, -1000, -2, -1000, -1000, -1000, -1000, -3, -1000, 11,
-2, 3, -1000, -1000, 0, -1000, -10, -3, -1000, -4,
-1000, -1000, -3, -1000,
-2, -1000, -2, -1000, -1000, -1000, -1000, -3, -1000, 3,
-2, -1000, -1000, 10, -1000, -3, -3, -8, -4, -8,
-1000, -3, -8,
}
var parserPgo = []int{
0, 25, 0, 24, 22, 19, 4, 21, 17,
0, 0, 25, 24, 19, 4, 23, 21,
}
var parserR1 = []int{
0, 8, 8, 5, 5, 6, 6, 3, 2, 2,
2, 2, 2, 2, 1, 7, 7, 7, 4,
0, 7, 7, 4, 4, 5, 5, 2, 1, 1,
1, 1, 1, 1, 6, 6, 6, 3,
}
var parserR2 = []int{
0, 0, 1, 1, 2, 1, 1, 3, 1, 1,
1, 1, 1, 4, 3, 0, 3, 1, 1,
1, 3, 1, 4, 0, 3, 1, 1,
}
var parserChk = []int{
-1000, -8, -5, -6, -4, -3, 15, 4, -6, -2,
-5, 13, 14, -1, 12, 5, 11, 8, 13, -7,
-2, 9, 10, -2,
-1000, -7, -4, -5, -3, -2, 15, 4, -5, -1,
-4, 13, 14, 12, 5, 11, 8, -1, -6, -1,
9, 10, -1,
}
var parserDef = []int{
1, -2, 2, 3, 5, 6, 18, 0, 4, 0,
8, 9, 10, 11, 12, 7, 0, 15, 14, 0,
17, 13, 0, 16,
1, -2, 2, 3, 5, 6, 17, 0, 4, 0,
8, 9, 10, 12, 7, 0, 14, 11, 0, 16,
13, 0, 15,
}
var parserTok1 = []int{
@ -435,55 +435,39 @@ parserdefault:
case 11:
//line lang.y:120
{
parserVAL.node = parserS[parserpt-0].node
parserVAL.node = &ast.Arithmetic{
Op: parserS[parserpt-1].token.Value.(ast.ArithmeticOp),
Exprs: []ast.Node{parserS[parserpt-2].node, parserS[parserpt-0].node},
Posx: parserS[parserpt-2].node.Pos(),
}
}
case 12:
//line lang.y:124
//line lang.y:128
{
parserVAL.node = &ast.VariableAccess{Name: parserS[parserpt-0].token.Value.(string), Posx: parserS[parserpt-0].token.Pos}
}
case 13:
//line lang.y:128
//line lang.y:132
{
parserVAL.node = &ast.Call{Func: parserS[parserpt-3].token.Value.(string), Args: parserS[parserpt-1].nodeList, Posx: parserS[parserpt-3].token.Pos}
}
case 14:
//line lang.y:134
{
parserVAL.node = &ast.Arithmetic{
Op: parserS[parserpt-1].token.Value.(ast.ArithmeticOp),
Exprs: []ast.Node{
&ast.LiteralNode{
Value: parserS[parserpt-2].token.Value.(int),
Typex: ast.TypeInt,
Posx: parserS[parserpt-2].token.Pos,
},
&ast.LiteralNode{
Value: parserS[parserpt-0].token.Value.(int),
Typex: ast.TypeInt,
Posx: parserS[parserpt-0].token.Pos,
},
},
Posx: parserS[parserpt-2].token.Pos,
}
}
case 15:
//line lang.y:154
//line lang.y:137
{
parserVAL.nodeList = nil
}
case 16:
//line lang.y:158
case 15:
//line lang.y:141
{
parserVAL.nodeList = append(parserS[parserpt-2].nodeList, parserS[parserpt-0].node)
}
case 17:
//line lang.y:162
case 16:
//line lang.y:145
{
parserVAL.nodeList = append(parserVAL.nodeList, parserS[parserpt-0].node)
}
case 18:
//line lang.y:168
case 17:
//line lang.y:151
{
parserVAL.node = &ast.LiteralNode{
Value: parserS[parserpt-0].token.Value.(string),

View File

@ -51,22 +51,21 @@ state 5
state 6
literal: STRING. (18)
literal: STRING. (17)
. reduce 18 (src line 166)
. reduce 17 (src line 149)
state 7
interpolation: PROGRAM_BRACKET_LEFT.expr PROGRAM_BRACKET_RIGHT
PROGRAM_BRACKET_LEFT shift 7
IDENTIFIER shift 14
IDENTIFIER shift 13
INTEGER shift 11
FLOAT shift 12
STRING shift 6
. error
arith goto 13
expr goto 9
interpolation goto 5
literal goto 4
@ -81,8 +80,10 @@ state 8
state 9
interpolation: PROGRAM_BRACKET_LEFT expr.PROGRAM_BRACKET_RIGHT
expr: expr.ARITH_OP expr
PROGRAM_BRACKET_RIGHT shift 15
PROGRAM_BRACKET_RIGHT shift 14
ARITH_OP shift 15
. error
@ -100,9 +101,7 @@ state 10
state 11
expr: INTEGER. (9)
arith: INTEGER.ARITH_OP INTEGER
ARITH_OP shift 16
. reduce 9 (src line 103)
@ -113,110 +112,118 @@ state 12
state 13
expr: arith. (11)
. reduce 11 (src line 119)
state 14
expr: IDENTIFIER. (12)
expr: IDENTIFIER.PAREN_LEFT args PAREN_RIGHT
PAREN_LEFT shift 17
. reduce 12 (src line 123)
PAREN_LEFT shift 16
. reduce 12 (src line 127)
state 15
state 14
interpolation: PROGRAM_BRACKET_LEFT expr PROGRAM_BRACKET_RIGHT. (7)
. reduce 7 (src line 92)
state 16
arith: INTEGER ARITH_OP.INTEGER
INTEGER shift 18
. error
state 17
expr: IDENTIFIER PAREN_LEFT.args PAREN_RIGHT
args: . (15)
state 15
expr: expr ARITH_OP.expr
PROGRAM_BRACKET_LEFT shift 7
IDENTIFIER shift 14
IDENTIFIER shift 13
INTEGER shift 11
FLOAT shift 12
STRING shift 6
. reduce 15 (src line 153)
. error
arith goto 13
expr goto 20
expr goto 17
interpolation goto 5
literal goto 4
literalModeTop goto 10
literalModeValue goto 3
args goto 19
state 16
expr: IDENTIFIER PAREN_LEFT.args PAREN_RIGHT
args: . (14)
PROGRAM_BRACKET_LEFT shift 7
IDENTIFIER shift 13
INTEGER shift 11
FLOAT shift 12
STRING shift 6
. reduce 14 (src line 136)
expr goto 19
interpolation goto 5
literal goto 4
literalModeTop goto 10
literalModeValue goto 3
args goto 18
17: shift/reduce conflict (shift 15(0), red'n 11(0)) on ARITH_OP
state 17
expr: expr.ARITH_OP expr
expr: expr ARITH_OP expr. (11)
ARITH_OP shift 15
. reduce 11 (src line 119)
state 18
arith: INTEGER ARITH_OP INTEGER. (14)
. reduce 14 (src line 132)
state 19
expr: IDENTIFIER PAREN_LEFT args.PAREN_RIGHT
args: args.COMMA expr
PAREN_RIGHT shift 21
COMMA shift 22
PAREN_RIGHT shift 20
COMMA shift 21
. error
state 20
args: expr. (17)
state 19
expr: expr.ARITH_OP expr
args: expr. (16)
. reduce 17 (src line 161)
ARITH_OP shift 15
. reduce 16 (src line 144)
state 20
expr: IDENTIFIER PAREN_LEFT args PAREN_RIGHT. (13)
. reduce 13 (src line 131)
state 21
expr: IDENTIFIER PAREN_LEFT args PAREN_RIGHT. (13)
. reduce 13 (src line 127)
state 22
args: args COMMA.expr
PROGRAM_BRACKET_LEFT shift 7
IDENTIFIER shift 14
IDENTIFIER shift 13
INTEGER shift 11
FLOAT shift 12
STRING shift 6
. error
arith goto 13
expr goto 23
expr goto 22
interpolation goto 5
literal goto 4
literalModeTop goto 10
literalModeValue goto 3
state 23
args: args COMMA expr. (16)
state 22
expr: expr.ARITH_OP expr
args: args COMMA expr. (15)
. reduce 16 (src line 157)
ARITH_OP shift 15
. reduce 15 (src line 140)
15 terminals, 9 nonterminals
19 grammar rules, 24/2000 states
0 shift/reduce, 0 reduce/reduce conflicts reported
58 working sets used
memory: parser 28/30000
19 extra closures
27 shift entries, 1 exceptions
15 terminals, 8 nonterminals
18 grammar rules, 23/2000 states
1 shift/reduce, 0 reduce/reduce conflicts reported
57 working sets used
memory: parser 30/30000
18 extra closures
34 shift entries, 1 exceptions
13 goto entries
17 entries saved by goto default
19 entries saved by goto default
Optimizer space used: output 26/30000
26 table entries, 0 zero
maximum spread: 15, maximum offset: 22
maximum spread: 15, maximum offset: 21