-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (48 loc) · 1.05 KB
/
main.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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/labasubagia/interpreter/evaluator"
"github.com/labasubagia/interpreter/lexer"
"github.com/labasubagia/interpreter/object"
"github.com/labasubagia/interpreter/parser"
"github.com/labasubagia/interpreter/repl"
)
func eval(input string) {
env := object.NewEnvironment()
l := lexer.New(input)
p := parser.New(l)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
for i, e := range p.Errors() {
fmt.Printf("error parse-%d: %s\n", i, e)
}
}
obj := evaluator.Eval(program, env, evaluator.ScopeNone)
if obj != nil && obj.Type() == object.ERROR_OBJ {
fmt.Println(obj.Inspect())
}
}
func main() {
switch {
case len(os.Args) >= 3:
switch os.Args[1] {
case "string":
eval(os.Args[2])
case "file":
file := os.Args[2]
// using file
if ext := filepath.Ext(file); ext != ".newpl" {
panic("file extension must be .newpl")
}
b, err := os.ReadFile(file)
if err != nil {
panic(err)
}
eval(string(b))
}
default:
repl.Start(os.Stdin, os.Stdout)
}
}