-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (44 loc) · 1020 Bytes
/
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
package main
import (
"fmt"
"strings"
"os"
"time"
"pongo/lexer"
"pongo/parser"
"pongo/evaluator"
"pongo/object"
)
func main() {
// verify correct num of command args
argLength := len(os.Args)
if argLength != 2 {
fmt.Println("Error: Incorrect number of command arguments.")
fmt.Println("Usage: pongo [filename]")
os.Exit(1)
}
// verify file extension is .pgo
var filename string
filename = os.Args[1]
if !strings.HasSuffix(filename, ".pgo") {
fmt.Println("Error: Unrecognized file type.")
fmt.Println("Pongo only recognizes '.pgo' files.")
os.Exit(1)
}
// now read the file
b, err := os.ReadFile(filename)
if err != nil {
fmt.Println("Error: file not found.") // TODO better message
os.Exit(1)
}
code := string(b)
Interpret(code)
}
func Interpret(source string) {
start := time.Now()
l := lexer.New(source)
p := parser.New(l)
env := object.NewEnvironment()
program := p.ParseProgram()
evaluator.Eval(program, env, start)
}