-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.go
58 lines (50 loc) · 1.46 KB
/
shell.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
package main
import (
"bufio"
"fmt"
"os"
"github.com/jomy10/nootlang/interpreter"
"github.com/jomy10/nootlang/parser"
"github.com/jomy10/nootlang/runtime"
"github.com/jomy10/nootlang/stdlib"
)
// TODO: accept expressions
func main() {
fmt.Println(` $$\ $$\ $$\
$$ | \__| $$ |
$$$$$$$\ $$$$$$\ $$$$$$\ $$$$$$\ $$\ $$$$$$$\ $$$$$$$\
$$ __$$\ $$ __$$\ $$ __$$\\_$$ _| $$ |$$ _____|$$ __$$\
$$ | $$ |$$ / $$ |$$ / $$ | $$ | $$ |\$$$$$$\ $$ | $$ |
$$ | $$ |$$ | $$ |$$ | $$ | $$ |$$\ $$ | \____$$\ $$ | $$ |
$$ | $$ |\$$$$$$ |\$$$$$$ | \$$$$ |$$ |$$$$$$$ |$$ | $$ |
\__| \__| \______/ \______/ \____/ \__|\_______/ \__| \__|
`)
// Start runtime
runtime := runtime.NewRuntime(os.Stdout, os.Stderr, os.Stdin)
stdlib.Register(&runtime)
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("$ The nootlang interactive shell v0.0.1")
for {
fmt.Print("$ ")
scanner.Scan()
source := scanner.Text()
tokens, err := parser.Tokenize(source)
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
continue
}
nodes, err := parser.Parse(tokens)
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
continue
}
for _, node := range nodes {
val, err := interpreter.ExecNode(&runtime, node)
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
continue
}
fmt.Printf("> %v\n", val)
}
}
}