-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
85 lines (73 loc) · 1.6 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
package main
import (
"fmt"
"gojo/config"
"gojo/interpreter"
"gojo/lexer"
"gojo/parser"
"gojo/repl"
"os"
)
func main() {
env := config.LoadConfig()
// Repl mode
if env.ReplMode {
i := interpreter.New()
repl.StartREPL(i)
return
}
// Input through input_program.js
inputFile := env.InputFile
if inputFile == "" {
inputFile = "input_program.js"
}
var input string
var err error
input, err = readFile(inputFile)
if err != nil {
fmt.Printf("Error reading input file: %v\n", err)
return
}
printInput(input)
// Initialize lexer, parser, and interpreter
// ✋ Note: keep all instances of these as l, p and i
l := lexer.New(input)
p := parser.New(l)
program := p.ParseProgram()
if config.LoadConfig().Verbose {
printProgramDetails(program)
}
// Check for parser errors
if len(p.Errors()) != 0 {
printParserErrors(p)
return
}
i := interpreter.New()
i.Interpret(program)
}
func printInput(input string) {
fmt.Println("╔═══ Input:")
fmt.Println(input)
}
func printProgramDetails(program *parser.Program) {
fmt.Println("╔═══ Program:")
fmt.Printf(" Statements: (%d elements)\n", len(program.Statements))
for _, stmt := range program.Statements {
fmt.Printf(" - %s\n", stmt)
}
fmt.Printf(" Start: %d\n", program.Start)
fmt.Printf(" End: %d\n", program.End)
}
func printParserErrors(p *parser.Parser) {
fmt.Println("⚠️ Parser Errors:")
for _, err := range p.Errors() {
fmt.Println(err)
}
}
func readFile(filename string) (string, error) {
content, err := os.ReadFile(filename)
if err != nil {
return "", err
}
return string(content), nil
}