-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
67 lines (52 loc) · 1.27 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
67
package main
import (
"flag"
"fmt"
"os"
"github.com/skx/critical/interpreter"
"github.com/skx/critical/stdlib"
)
var version = "unreleased"
func main() {
noStdlib := flag.Bool("no-stdlib", false, "Disable the (embedded) standard library.")
versionFlag := flag.Bool("version", false, "Show our version, and exit.")
flag.Parse()
if *versionFlag {
fmt.Printf("critical %s\n", version)
return
}
// Ensure we have a file to execute.
if len(flag.Args()) < 1 {
fmt.Printf("Usage: critical file.tcl\n")
return
}
// Read our standard library
stdlib := stdlib.Contents()
// Read the file the user wanted
data, err := os.ReadFile(flag.Args()[0])
if err != nil {
fmt.Printf("error reading file %s:%s\n", os.Args[0], err)
return
}
// Join the two inputs, unless we shouldn't.
input := string(data)
if !*noStdlib {
input = string(stdlib) + "\n" + input
}
// Create the interpreter
var out string
var i *interpreter.Interpreter
i, err = interpreter.New(input)
if err != nil {
fmt.Printf("Error creating interpreter %s\n", err)
return
}
// Evaluate the input
out, err = i.Evaluate()
if err != nil && err != interpreter.ErrReturn {
fmt.Printf("Error running program:%s\n", err)
return
}
// Show the result
fmt.Printf("\nResult:%s\n", out)
}