-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfile.demo
52 lines (49 loc) · 1003 Bytes
/
file.demo
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
package main
import (
"fmt"
"io/ioutil"
"magpie/eval"
"magpie/lexer"
"magpie/parser"
"magpie/repl"
"os"
)
func runProgram(filename string) {
wd, err := os.Getwd()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
f, err := ioutil.ReadFile(wd + "/" + filename)
if err != nil {
fmt.Println("magpie: ", err.Error())
os.Exit(1)
}
l := lexer.New(filename, string(f))
p := parser.New(l, wd)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
for _, err := range p.Errors() {
fmt.Println(err)
}
os.Exit(1)
}
scope := eval.NewScope(nil)
eval.REPLColor = false
eval.Eval(program, scope)
// e := eval.Eval(program, scope)
// if e.Inspect() != "nil" {
// fmt.Println(e.Inspect())
// }
}
func main() {
args := os.Args[1:]
//We must reset `os.Args`, or the `flag` module will not functioning correctly
os.Args = os.Args[1:]
if len(args) == 0 {
fmt.Println("Magpie programming language REPL\n")
repl.Start(os.Stdout, true)
} else {
runProgram(args[0])
}
}