aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/shell/shell.go
blob: 19a42239cefce0c90433861dd8b168ba06d0bcca (plain) (blame)
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
package shell

import (
	"fmt"
	"os"

	"golang.org/x/term"
)

var commands = []string{
	"help",
	"clear",
	"let",
	"del",
	"slice",
	"rematch",
	"repattern",
	"tokenize",
	"opt_simplify", "opt_tighten", "opt_flatten", "opt_sort", "opt_tidy", "opt_contradictions", "opt_compact", "opt_strictEq",
	"parse",
	"compile",
}

func (inter *Interpreter) Run() error {
	oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
	if err != nil {
		panic(err)
	}
	defer term.Restore(int(os.Stdin.Fd()), oldState)
	inter.term = term.NewTerminal(os.Stdin, "atlasi> ")
	inter.term.SetPrompt(
		string(inter.term.Escape.Yellow) + "atlasi> " +
			string(inter.term.Escape.Reset),
	)

	for {
		line, err := inter.term.ReadLine()
		if err != nil {
			return err
		}
		tokens := inter.Tokenize(line)
		fatal, err := inter.Eval(inter.term, tokens)
		if fatal {
			return err
		} else if err != nil {
			fmt.Fprintln(inter.term, string(inter.term.Escape.Red), "Error:",
				string(inter.term.Escape.Reset), err)
		}
	}
}