-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgee.go
45 lines (40 loc) · 884 Bytes
/
gee.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
package gee
import (
"fmt"
"github.com/0x3alex/gee/internal"
"github.com/0x3alex/gee/internal/lexer"
"github.com/0x3alex/gee/internal/parser"
"github.com/0x3alex/gee/internal/tokens"
)
/*
@int - 0, if it is a number, 1 if it is a bool, 2 if it is a string
@any - the value
@error - is set to an error if an error occured
*/
func Eval(s string) (int, any, error) {
l := lexer.New(s)
res, err := l.Lex()
if err != nil {
return 0, nil, err
}
n, err := parser.BuildAST(res)
if err != nil {
return 0, nil, err
}
if n == nil {
return 0, nil, fmt.Errorf("ast returned nil")
}
result, err := internal.EvalAST(n)
if err != nil {
return 0, nil, err
}
var t int
if result.GetType() == tokens.TokTrue || result.GetType() == tokens.TokFalse {
t = 1
} else if result.GetType() == tokens.TokFloat {
t = 0
} else {
t = 2
}
return t, result.GetValue(), nil
}