This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
66 lines (58 loc) · 1.35 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"github.com/rumlang/rum/interative"
"github.com/rumlang/rum/parser"
"github.com/rumlang/rum/runtime"
)
func main() {
// Check arguments
flag.Parse()
if len(flag.Args()) > 1 {
fmt.Fprintf(os.Stderr, "Only one file argument allowed.\n")
os.Exit(1)
}
// Run REPL if nothing else is specified
if len(flag.Args()) == 0 {
if err := interative.REPL(); err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
os.Exit(0)
}
// By default run the code from -e flag.
var input = ""
if len(flag.Args()) > 0 {
// Get code from a file if specified
filepath := os.Args[1]
file, err := os.Open(filepath)
if err != nil {
fmt.Fprintf(os.Stderr, "not found file %q: %v\n", filepath, err)
os.Exit(1)
}
defer file.Close()
scanner := bufio.NewScanner(file)
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "unable to read %q: %v\n", filepath, err)
os.Exit(1)
}
// Scan the file and add lines to input
for scanner.Scan() {
input += scanner.Text() + "\n"
}
}
// Parse & exec.
root, err := parser.Parse(parser.NewSource(input))
if err != nil {
fmt.Fprintf(os.Stderr, "parsing failed: %v", err)
os.Exit(1)
}
ctx := runtime.NewContext(nil)
if _, err = ctx.TryEval(root); err != nil {
fmt.Fprintf(os.Stderr, "execution failed: %v", err)
os.Exit(1)
}
}