-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
280 lines (251 loc) · 6.83 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strconv"
"strings"
)
type Interpreter struct {
variables map[string]interface{}
}
// NewInterpreter initializes an interpreter
func NewInterpreter() *Interpreter {
return &Interpreter{variables: make(map[string]interface{})}
}
// Execute parses and executes a single line of code
func (i *Interpreter) Execute(line string) {
tokens := strings.Fields(line)
if len(tokens) == 0 {
return
}
switch tokens[0] {
case "kemon":
if len(tokens) < 3 || tokens[1] != "achis" {
fmt.Println("bhul hoye gelo vai check kor ekbar")
return
}
arg := strings.Join(tokens[2:], " ")
// Check if the argument is a quoted string
if strings.HasPrefix(arg, "\"") && strings.HasSuffix(arg, "\"") {
fmt.Println(arg[1 : len(arg)-1]) // Remove the quotes for display
return
}
// If not a string, evaluate the expression
value, err := i.evaluateExpression(strings.Fields(arg))
if err == nil {
fmt.Println(value)
} else {
// Check if it's a variable
if value, ok := i.variables[arg]; ok {
fmt.Println(value)
} else {
fmt.Println("bhul hoye gelo vai check kor ekbar")
}
}
case "bol":
if len(tokens) != 3 || tokens[1] != "bhai" {
fmt.Println("bhul hoye gelo vai check kor ekbar")
return
}
varName := tokens[2]
fmt.Printf(`Value bolo %s er =>: `, varName)
var input string
fmt.Scanln(&input)
// Handle string input properly (strip the quotes if present)
if strings.HasPrefix(input, "\"") && strings.HasSuffix(input, "\"") {
i.variables[varName] = input[1 : len(input)-1] // Store the string without quotes
} else if value, err := strconv.Atoi(input); err == nil {
i.variables[varName] = value // Store the integer value
} else {
fmt.Println("bhul hoye gelo vai check kor ekbar: invalid value")
return
}
case "dyakh":
if len(tokens) < 4 || tokens[1] != "jodi" || !strings.Contains(line, "ar nahole:") {
fmt.Println("bhul hoye gelo vai check kor ekbar: Invalid conditional syntax")
return
}
parts := strings.Split(line, "ar nahole:")
if len(parts) != 2 {
fmt.Println("bhul hoye gelo vai check kor ekbar: Missing 'ar nahole'")
return
}
ifPart := strings.TrimSpace(parts[0])
elsePart := strings.TrimSpace(parts[1])
ifTokens := strings.SplitN(ifPart, ":", 2)
if len(ifTokens) != 2 {
fmt.Println("bhul hoye gelo vai check kor ekbar: Missing ':' in 'dyakh jodi'")
return
}
condition := strings.TrimSpace(strings.Join(strings.Fields(ifTokens[0])[2:], " "))
ifCommand := strings.TrimSpace(ifTokens[1])
condValue, err := i.evaluateCondition(condition)
if err != nil {
fmt.Println("bhul hoye gelo vai check kor ekbar: Error evaluating condition")
return
}
if condValue {
i.Execute(ifCommand)
} else {
if strings.HasPrefix(elsePart, "\"") && strings.HasSuffix(elsePart, "\"") {
fmt.Println(elsePart[1 : len(elsePart)-1]) // Remove quotes for printing
} else {
i.Execute(elsePart)
}
}
default:
fmt.Println("bhul hoye gelo vai check kor ekbar: Unknown command")
}
}
func (i *Interpreter) evaluateExpression(tokens []string) (int, error) {
if len(tokens) == 1 {
if value, ok := i.variables[tokens[0]]; ok {
switch v := value.(type) {
case int:
return v, nil
case string:
return 0, fmt.Errorf("expected an integer, but got a string")
default:
return 0, fmt.Errorf("invalid type for evaluation")
}
}
return strconv.Atoi(tokens[0])
}
if len(tokens) == 3 {
left, err := i.getValue(tokens[0])
if err != nil {
return 0, err
}
right, err := i.getValue(tokens[2])
if err != nil {
return 0, err
}
switch tokens[1] {
case "+":
return left + right, nil
case "-":
return left - right, nil
case "*":
return left * right, nil
case "/":
if right == 0 {
return 0, fmt.Errorf("division by zero")
}
return left / right, nil
default:
return 0, fmt.Errorf("unknown operator '%s'", tokens[1])
}
}
return 0, fmt.Errorf("invalid expression")
}
func (i *Interpreter) evaluateCondition(condition string) (bool, error) {
tokens := strings.Fields(condition)
if len(tokens) != 3 {
return false, fmt.Errorf("invalid condition syntax")
}
left, err := i.getValue(tokens[0])
if err != nil {
return false, err
}
right, err := i.getValue(tokens[2])
if err != nil {
return false, err
}
switch tokens[1] {
case ">":
return left > right, nil
case "<":
return left < right, nil
case ">=":
return left >= right, nil
case "<=":
return left <= right, nil
case "==":
return left == right, nil
case "!=":
return left != right, nil
default:
return false, fmt.Errorf("unknown operator '%s'", tokens[1])
}
}
func (i *Interpreter) getValue(token string) (int, error) {
if value, err := strconv.Atoi(token); err == nil {
return value, nil
}
if value, exists := i.variables[token]; exists {
switch v := value.(type) {
case int:
return v, nil
case string:
return 0, fmt.Errorf("expected an integer, but got a string")
default:
return 0, fmt.Errorf("unknown variable '%s'", token)
}
}
return 0, fmt.Errorf("unknown variable '%s'", token)
}
func main() {
version := flag.Bool("v", false, "Print the version")
help := flag.Bool("h", false, "Display help")
flag.Parse()
if *version {
fmt.Println("Gola Compiler v1.0.2")
return
}
if *help {
fmt.Println("Usage: gola [options] <file>")
fmt.Println("Options:")
fmt.Println(" -v, --version Print the version of the compiler")
fmt.Println(" -h, --help Display help information")
return
}
args := flag.Args()
if len(args) > 0 {
fileName := args[0]
if !strings.HasSuffix(fileName, ".gola") {
fmt.Println("Error: Only .gola files are supported")
return
}
file, err := os.Open(fileName)
if err != nil {
fmt.Printf("Error: Unable to open file '%s'\n", fileName)
return
}
defer file.Close()
interpreter := NewInterpreter()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
interpreter.Execute(strings.TrimSpace(line))
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading file '%s': %v\n", fileName, err)
}
fmt.Println("Jay Shree Ram Bhai!")
return
}
fmt.Println(`Welcome to Gola - Bengali Edition!
:::::::: :::::::: ::: :::
:+: :+: :+: :+: :+: :+: :+:
+:+ +:+ +:+ +:+ +:+ +:+
:#: +#+ +:+ +#+ +#++:++#++:
+#+ +#+# +#+ +#+ +#+ +#+ +#+
#+# #+# #+# #+# #+# #+# #+#
######## ######## ########## ### ###
`)
fmt.Println("Type 'exit' to quit.")
scanner := bufio.NewScanner(os.Stdin)
interpreter := NewInterpreter()
for {
fmt.Print(">> ")
scanner.Scan()
line := scanner.Text()
if strings.TrimSpace(line) == "exit" {
break
}
interpreter.Execute(line)
}
fmt.Println("Jay Shree Ram Bhai!")
}