-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
170 lines (158 loc) · 3.3 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"bufio"
"flag"
"fmt"
"interpreter/src"
"io"
"log"
"log/slog"
"os"
)
const helpStr = `
ayc Interpreter and Bytecode Compiler
Usage:
--repl (-e) : Start REPL
--input (-i) [filepath.ayc] : Input file
--debug (-d) : Enable debug mode
--optimize (-O) : Enable optimizations
--output (-o) : Output bytecode file
--run (-r) [filepath.aycb] : Run bytecode file`
type Ayc struct {
*Args
inputBuffer string
scanner io.ByteScanner
}
func New() *Ayc {
return &Ayc{parseArgs(), "", nil}
}
type Args struct {
repl bool
inputFile *string
debug bool
optimize bool
bytecodeFile *string
outputFile *string
}
func main() {
ayc := New()
if ayc.debug {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
ayc.run()
}
func (a *Ayc) run() {
if a.repl {
a.execRepl()
}
if *a.bytecodeFile != "" {
fmt.Println("Running bytecode file: ", *a.bytecodeFile)
a.runBytecode(*a.bytecodeFile)
return
}
if *a.inputFile != "" {
if *a.outputFile != "" {
a.compileToFile()
} else {
a.execInput(nil)
}
}
}
func (a *Ayc) runBytecode(bytecodeFile string) {
be := src.NewBytecodeEmitter()
be.LoadFromFile(bytecodeFile)
if a.debug {
be.PrintBytecode()
}
vm := src.NewVM(be.Instructions)
vm.Exec()
}
func (a *Ayc) execInput(input *string) {
var lexer *src.Lexer
if input == nil {
lexer = src.NewLexer(*a.inputFile)
} else {
lexer = src.NewInputLexer(*input)
}
parser := lexer.Tokenize()
ast := parser.Parse()
if a.optimize {
analyzer := src.NewAnalyzer(ast)
ast = analyzer.AnalyzeAndEval()
if a.debug {
analyzer.PrintOptimizedTree()
}
}
be := src.NewBytecodeEmitter()
be.Walk(ast)
be.PrintBytecode()
vm := src.NewVM(be.Instructions)
vm.Exec()
}
func (a *Ayc) compileToFile() {
var lexer *src.Lexer
lexer = src.NewLexer(*a.inputFile)
parser := lexer.Tokenize()
ast := parser.Parse()
if a.optimize {
analyzer := src.NewAnalyzer(ast)
ast = analyzer.AnalyzeAndEval()
if a.debug {
analyzer.PrintOptimizedTree()
}
}
be := src.NewBytecodeEmitter()
be.Walk(ast)
if a.debug {
be.PrintBytecode()
}
err := be.OutputToFile(*a.outputFile)
if err != nil {
fmt.Println("Error writing to file: ", err)
}
}
func (a *Ayc) execRepl() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanLines)
fmt.Println("ayc REPL. Type 'exit' to quit, type 'exec!' to run code")
for {
var input string
for {
fmt.Printf(">> ")
scanner.Scan()
input = scanner.Text()
if input == "exit" {
os.Exit(0)
}
if input == "help" {
fmt.Println(helpStr)
}
if input == "exec!" {
a.execInput(&a.inputBuffer)
} else {
a.inputBuffer += input + "\n"
}
}
}
}
func parseArgs() *Args {
inputFile := flag.String("i", "", "Input file")
optimize := flag.Bool("O", false, "Enable optimizations")
debug := flag.Bool("d", false, "Enable debug mode")
bytecodeFile := flag.String("r", "", "Run bytecode file")
outputFile := flag.String("o", "", "Output bytecode file")
repl := flag.Bool("e", false, "Start REPL")
flag.Parse()
if len(os.Args) < 2 {
fmt.Println(helpStr)
log.Fatal(helpStr)
}
return &Args{
inputFile: inputFile,
optimize: *optimize,
debug: *debug,
bytecodeFile: bytecodeFile,
outputFile: outputFile,
repl: *repl,
}
}