-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.go
95 lines (81 loc) · 1.56 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
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
package main
import (
"flag"
"fmt"
"os"
"github.com/skx/gobasic/eval"
"github.com/skx/gobasic/token"
"github.com/skx/gobasic/tokenizer"
)
// This version-string will be updated via travis for generated binaries.
var version = "master/unreleased"
func main() {
//
// Setup some command-line flags
//
lex := flag.Bool("lex", false, "Show the output of the lexer.")
trace := flag.Bool("trace", false, "Trace execution.")
vers := flag.Bool("version", false, "Show our version and exit.")
//
// Parse the flags
//
flag.Parse()
//
// Showing the version?
//
if *vers {
fmt.Printf("gobasic %s\n", version)
os.Exit(1)
}
//
// Test we have a file to interpret
//
if len(flag.Args()) != 1 {
fmt.Printf("Usage: gobasic /path/to/input/script.bas\n")
os.Exit(2)
}
//
// Load the file.
//
data, err := os.ReadFile(flag.Args()[0])
if err != nil {
fmt.Printf("Error reading %s - %s\n", flag.Args()[0], err.Error())
os.Exit(3)
}
//
// Tokenize
//
t := tokenizer.New(string(data))
//
// Are we dumping tokens?
//
if *lex {
for {
tok := t.NextToken()
fmt.Printf("%v\n", tok)
if tok.Type == token.EOF {
break
}
}
os.Exit(0)
}
//
// Create a new evaluator, to run the BASIC program.
//
e, err := eval.New(t)
if err != nil {
fmt.Printf("Error constructing interpreter:\n\t%s\n", err.Error())
os.Exit(0)
}
//
// Enable debugging if we should.
//
e.SetTrace(*trace)
//
// Run the code, and report on any error.
//
err = e.Run()
if err != nil {
fmt.Printf("Error running program:\n\t%s\n", err.Error())
}
}