-
Notifications
You must be signed in to change notification settings - Fork 2
/
compterpreter.go
113 lines (93 loc) · 2.03 KB
/
compterpreter.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package dockerlang
import (
"bufio"
"os"
"text/scanner"
)
type Config struct {
ShowUsage bool
SrcFileName string
BinFileName string
}
// is this a compiler or an interpreter? who knows????
type Compterpreter struct {
Config *Config
Scanner scanner.Scanner
CurrentChar rune
CurrentToken Token
Symbols *Symbols
Tokens []Token
StackTree *Expr
}
func NewCompterpreter(c *Config) *Compterpreter {
// whenever we create a new compterpreter, we will also
// create a new execution engine which is set in the global
// scope.
err := NewExecutionEngine()
if err != nil {
panic(err)
}
return &Compterpreter{
Config: c,
Symbols: PopulateSymbols(),
}
}
func (c *Compterpreter) Compterpret() error {
var (
err error
)
// always shutdown the docker execution engine
// TODO uncomment the code below once we figure out a way to figure the below comment out.
// defer ShutdownExecutionEngine() // TODO kill this network only once all the containers have completed and been killed
// initialize a scanner to read through source code character
// by character
err = c.LoadSourceCode()
if err != nil {
return err
}
// start interpreting
err = c.Interpret()
if err != nil {
return err
}
return nil
}
func (c *Compterpreter) LoadSourceCode() error {
// check to see if provided file exists
info, err := os.Stat(c.Config.SrcFileName)
if err != nil {
return err
}
// TODO check filesize and permissions of file
_ = info
// open file
fd, err := os.Open(c.Config.SrcFileName)
if err != nil {
return err
}
// set source code scanner
reader := bufio.NewReader(fd)
c.Scanner.Init(reader)
return nil
}
func (c *Compterpreter) Interpret() error {
var (
err error
)
// Identifies tokens in the provided .doc code
err = c.Lex()
if err != nil {
return err
}
// Creates c.StackTree representing the provided .doc code
err = c.Parse()
if err != nil {
return err
}
// Actually dockerize and evaluate the StackTree
err = c.Evaluate()
if err != nil {
return err
}
return nil
}