-
Notifications
You must be signed in to change notification settings - Fork 5
/
environment.go
95 lines (80 loc) · 2.3 KB
/
environment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package environment
import (
"fmt"
"github.com/5anthosh/eval/function"
"github.com/shopspring/decimal"
)
//Type of variables
const (
functionType = 1 + iota
variableType
)
//Environment of evaluator
type Environment struct {
symbolTable map[string]uint
variables map[string]interface{}
functions map[string]function.Function
}
//New Environment
func New() *Environment {
return &Environment{
symbolTable: make(map[string]uint),
variables: make(map[string]interface{}),
functions: make(map[string]function.Function),
}
}
//SetFunction to Evaluator
func (e *Environment) SetFunction(function function.Function) error {
if e.CheckSymbolTable(function.Name) {
return fmt.Errorf("%s is already declared", function.Name)
}
e.symbolTableEntry(function.Name, functionType)
e.functions[function.Name] = function
return nil
}
//SetDefaultFunctions to environment
func (e *Environment) SetDefaultFunctions() error {
err := e.SetFunction(function.AbsFunction)
return err
}
//SetNumberVariable in the environment
func (e *Environment) SetNumberVariable(name string, value decimal.Decimal) error {
return e.DeclareVariable(name, value)
}
//DeclareVariable in the environment
func (e *Environment) DeclareVariable(name string, value interface{}) error {
if e.CheckSymbolTable(name) {
return fmt.Errorf("%s is already declared", name)
}
e.symbolTableEntry(name, variableType)
e.variables[name] = value
return nil
}
//GetVariable from the environment
func (e *Environment) GetVariable(name string) (interface{}, bool) {
value, ok := e.variables[name]
return value, ok
}
//GetFunction from the environment
func (e *Environment) GetFunction(name string) (function.Function, bool) {
value, ok := e.functions[name]
return value, ok
}
func (e *Environment) symbolTableEntry(name string, varType uint) {
e.symbolTable[name] = varType
}
//CheckSymbolTable if variable is registed in symbol table
func (e *Environment) CheckSymbolTable(name string) bool {
_, ok := e.symbolTable[name]
return ok
}
//IsVariable check if name is variable
func (e *Environment) IsVariable(name string) bool {
varType, _ := e.symbolTable[name]
return varType == variableType
}
//IsFunction check if name is variable
func (e *Environment) IsFunction(name string) bool {
varType, _ := e.symbolTable[name]
return varType == functionType
}