-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
156 lines (128 loc) · 3.36 KB
/
Copy pathmain.go
File metadata and controls
156 lines (128 loc) · 3.36 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
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
package main
import (
"fmt"
"os"
"rune/pkg/rune"
"strings"
)
const version = "0.1"
const (
exitCodeOk = 0
exitCodeError = 1
exitCodeParseError = 65
exitCodeEvalError = 70
runeExtension = ".rn"
)
func printUsage() {
asciiArt := `
____ _ _ _ _ _____
| _ \| | | | \ | | ____|
| |_) | | | | \| | _|
| _ <| |_| | |\ | |___
|_| \_\\___/|_| \_|_____|
`
fmt.Fprintf(os.Stderr, "%s\n", asciiArt)
fmt.Fprintf(os.Stderr, "Rune Interpreter v%s\n", version)
fmt.Fprintf(os.Stderr, "Copyright: Alexander Satretdinov (c), 2025\n")
fmt.Fprintf(os.Stderr, "Based on the Lox programming language and Robert Nystrom's book.\n")
fmt.Fprintf(os.Stderr, "A simple interpreter for processing and evaluating scripts.\n\n")
fmt.Fprintf(os.Stderr, "Usage: rune <command> <filename>\n")
fmt.Fprintf(os.Stderr, "Commands:\n")
fmt.Fprintf(os.Stderr, " tokenize - Tokenizes the input file\n")
fmt.Fprintf(os.Stderr, " evaluate - Evaluates a single expression from the input file\n")
fmt.Fprintf(os.Stderr, " run - Runs the program from the input file\n")
fmt.Fprintf(os.Stderr, " version - Prints the version of the interpreter\n")
os.Exit(1)
}
func tokenize(fileContents []byte) int {
tokens, errors := rune.Scan(fileContents)
for _, token := range tokens {
fmt.Println(token)
}
if len(errors) == 0 {
return exitCodeOk
}
for _, err := range errors {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
return exitCodeParseError
}
func evaluate(fileContents []byte) int {
tokens, errors := rune.Scan(fileContents)
if len(errors) > 0 {
return exitCodeParseError
}
expr, err := rune.ParseExpr(tokens)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return exitCodeParseError
}
result, err := rune.EvaluateExpr(expr)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return exitCodeEvalError
}
fmt.Println(result)
return exitCodeOk
}
func run(fileContents []byte) int {
tokens, errors := rune.Scan(fileContents)
if len(errors) > 0 {
for _, err := range errors {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
return exitCodeParseError
}
stmts, err := rune.ParseStmts(tokens)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return exitCodeParseError
}
interpreter := rune.NewInterpreter()
resolver := rune.NewResolver(interpreter)
if err := resolver.ResolveStmts(stmts); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return exitCodeParseError
}
if err := interpreter.EvaluateStmts(stmts); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return exitCodeEvalError
}
return exitCodeOk
}
func main() {
if len(os.Args) < 2 {
printUsage()
return
}
command := os.Args[1]
if command == "version" {
fmt.Println(fmt.Sprintf("Rune Interpreter v%s", version))
return
}
if len(os.Args) < 3 {
printUsage()
return
}
fileName := os.Args[2]
if !strings.HasSuffix(fileName, runeExtension) {
fmt.Fprintf(os.Stderr, "Error: Only .rn files are supported. Provided file: %s\n", fileName)
os.Exit(exitCodeError)
}
fileContents, err := os.ReadFile(fileName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading file: %v\n", err)
os.Exit(exitCodeError)
}
switch command {
case "tokenize":
os.Exit(tokenize(fileContents))
case "evaluate":
os.Exit(evaluate(fileContents))
case "run":
os.Exit(run(fileContents))
default:
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", command)
printUsage()
}
}