-
Notifications
You must be signed in to change notification settings - Fork 8
/
aria.go
128 lines (111 loc) · 2.53 KB
/
aria.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"bufio"
"fmt"
"github.com/fadion/aria/interpreter"
"github.com/fadion/aria/lexer"
"github.com/fadion/aria/parser"
"github.com/fadion/aria/reader"
"github.com/fadion/aria/reporter"
"github.com/fatih/color"
"github.com/urfave/cli"
"io/ioutil"
"os"
)
func main() {
app := cli.NewApp()
app.Name = "aria"
app.Usage = "an expressive, noiseless, interpreted toy language"
app.Authors = []cli.Author{{
Name: "Fadion Dashi",
Email: "[email protected]",
}}
app.Version = "0.5.0"
app.Commands = []cli.Command{
{
Name: "run",
Usage: "Run an Aria source file",
Action: func(c *cli.Context) error {
if len(c.Args()) != 1 {
color.Red("Run expects a source file as argument.")
}
file := c.Args()[0]
source, err := ioutil.ReadFile(file)
if err != nil {
color.Red("Couldn't read '%s'", file)
return nil
}
lex := lexer.New(reader.New(source))
if reporter.HasErrors() {
printErrors()
return nil
}
parse := parser.New(lex)
program := parse.Parse()
if reporter.HasErrors() {
printErrors()
return nil
}
runner := interpreter.New()
runner.Interpret(program, interpreter.NewScope())
if reporter.HasErrors() {
printErrors()
return nil
}
return nil
},
},
{
Name: "repl",
Usage: "Start the interactive repl",
Action: func(c *cli.Context) error {
input := bufio.NewReader(os.Stdin)
color.Yellow(` _ ___ ___ _
/_\ | _ \_ _| /_\
/ _ \| /| | / _ \
/_/ \_\_|_\___/_/ \_\
`)
color.White("Close by pressing CTRL+C")
fmt.Println()
scope := interpreter.NewScope()
for {
color.Set(color.FgWhite)
fmt.Print(">> ")
color.Unset()
source, _ := input.ReadBytes('\n')
lex := lexer.New(reader.New(source))
if reporter.HasErrors() {
printErrors()
continue
}
parse := parser.New(lex)
program := parse.Parse()
if reporter.HasErrors() {
printErrors()
continue
}
runner := interpreter.New()
object := runner.Interpret(program, scope)
if reporter.HasErrors() {
printErrors()
continue
}
if object != nil {
fmt.Println(object.Inspect())
}
}
},
},
}
app.CommandNotFound = func(ctx *cli.Context, command string) {
fmt.Fprintf(ctx.App.Writer, "Command %q doesn't exist.\n", command)
}
app.Run(os.Args)
}
func printErrors() {
color.White("Oops, found some errors:")
for _, v := range reporter.GetErrors() {
color.Red(v)
}
reporter.ClearErrors()
}