-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
73 lines (58 loc) · 1.44 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"os"
flag "github.com/spf13/pflag"
"github.com/flipez/rocket-lang/evaluator"
"github.com/flipez/rocket-lang/lexer"
"github.com/flipez/rocket-lang/object"
"github.com/flipez/rocket-lang/parser"
"github.com/flipez/rocket-lang/repl"
)
func main() {
version := flag.BoolP("version", "v", false, "Prints the version and build date.")
exec := flag.StringP("exec", "e", "", "Runs the given code.")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: rocket-lang [flags] [program file] [arguments]\n\nAvailable flags:\n")
flag.PrintDefaults()
}
flag.Parse()
if *version {
print(repl.SplashVersion())
return
}
if len(*exec) > 0 {
runProgram(*exec, "")
return
}
if len(os.Args) == 1 {
repl.Start(os.Stdin, os.Stdout)
} else {
file, err := os.ReadFile(os.Args[1])
if err == nil {
runProgram(string(file), os.Args[1])
}
}
}
func runProgram(input string, file string) {
env := object.NewEnvironment()
l := lexer.New(input, file)
p := parser.New(l, make(map[string]struct{}))
object.AddEvaluator(evaluator.Eval)
program, _ := p.ParseProgram()
if len(p.Errors()) > 0 {
printParserErrors(p.Errors())
return
}
evaluated := evaluator.Eval(program, env)
if evaluated != nil {
fmt.Println(evaluated.Inspect())
}
}
func printParserErrors(errors []string) {
fmt.Println("🔥 Great, you broke it!")
fmt.Println(" parser errors:")
for _, msg := range errors {
fmt.Printf("\t %s\n", msg)
}
}