-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple.go
More file actions
60 lines (50 loc) · 1.03 KB
/
simple.go
File metadata and controls
60 lines (50 loc) · 1.03 KB
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
package main
import (
"flag"
"fmt"
"log"
"os"
"simple/simpl"
)
var usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), "%s <input file> \n", os.Args[0])
flag.PrintDefaults()
}
func main() {
flag.Parse()
in := flag.Arg(0)
if in == "" {
usage()
return
}
infile, err := os.Open(in)
if err != nil {
log.Fatalf("could not open file '%v' with err: %v\n", in, err)
}
finfo, err := infile.Stat()
if err != nil {
log.Fatalf("could not stat file '%v' with err: %v\n", in, err)
}
if finfo.IsDir() {
log.Fatalf("'%v' is a directory\n", in)
}
l := simpl.Lexer{In: infile}
tokens, errors := l.Lex()
if len(errors) > 0 {
for _, err := range errors {
fmt.Println("ERROR:", err)
}
verb := "was"
e := "error"
if len(errors) > 1 {
verb = "were"
e += "s"
}
log.Fatalf("there %s %v %s lexing '%v'", verb, len(errors), e, in)
}
p := simpl.Parser{Tokens: tokens}
p.Parse()
i := simpl.NewInterpreter(&p.Lines, os.Stdout)
i.Interpret()
}