-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathra.go
78 lines (62 loc) · 1.41 KB
/
ra.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
package main
import (
"fmt"
"io"
"os"
"os/user"
"github.com/MikhailWahib/Ra/evaluator"
"github.com/MikhailWahib/Ra/lexer"
"github.com/MikhailWahib/Ra/object"
"github.com/MikhailWahib/Ra/parser"
"github.com/MikhailWahib/Ra/repl"
)
func startRepl() {
user, err := user.Current()
if err != nil {
panic(err)
}
fmt.Printf("Hello %s! This is the Ra 𓋹 programming language!\n",
user.Username)
fmt.Printf("Feel free to type in commands\n")
repl.Start(os.Stdin, os.Stdout)
}
func printParserErrors(out io.Writer, errors []string) {
io.WriteString(out, "Woops! We ran into some issues!\n")
io.WriteString(out, " parser errors:\n")
for _, msg := range errors {
io.WriteString(out, "\t"+msg+"\n")
}
}
func executeFile(args []string) {
path := args[1]
// check if the file end with .ra
if path[len(path)-3:] != ".ra" {
panic("File must end with .ra")
}
file, err := os.ReadFile(path)
if err != nil {
panic(err)
}
env := object.NewEnvironment()
l := lexer.New(string(file))
p := parser.New(l)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
printParserErrors(os.Stdout, p.Errors())
}
evaluated := evaluator.Eval(program, env)
if evaluated != nil {
io.WriteString(os.Stdout, evaluated.Inspect())
io.WriteString(os.Stdout, "\n")
}
}
func main() {
args := os.Args
if len(args) < 2 {
startRepl()
}
if len(args) > 2 {
panic("Wrong number of arguments!")
}
executeFile(args)
}