Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (50 loc) · 1.28 KB
/
Copy pathmain.go
File metadata and controls
60 lines (50 loc) · 1.28 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
package main
import (
"fmt"
"os"
"github.com/xmasdev/melonlang/evaluator"
"github.com/xmasdev/melonlang/lexer"
"github.com/xmasdev/melonlang/parser"
"github.com/xmasdev/melonlang/repl"
)
func main() {
if len(os.Args) > 1 && os.Args[1] == "--repl" {
// Run REPL mode
fmt.Println("Welcome to MelonLang REPL!")
fmt.Println("Type 'exit' or 'quit' to exit")
fmt.Println()
repl.Start(os.Stdin, os.Stdout)
return
}
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: melonlang <script.mel> or melonlang --repl\n")
os.Exit(1)
}
// Execute script file
filename := os.Args[1]
content, err := os.ReadFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading file: %v\n", err)
os.Exit(1)
}
// Create lexer, parser, and evaluator
lex := lexer.New(string(content))
p := parser.New(lex)
program := p.ParseProgram()
// Check for parser errors
if len(p.Errors()) > 0 {
fmt.Fprintf(os.Stderr, "Parse errors:\n")
for _, msg := range p.Errors() {
fmt.Fprintf(os.Stderr, "\t%s\n", msg)
}
os.Exit(1)
}
// Evaluate the program
eval := evaluator.NewEvaluator()
result := eval.Eval(program)
// Handle any evaluation errors
if result != nil && result.Type() == "ERROR" {
fmt.Fprintf(os.Stderr, "Runtime error: %s\n", result.Inspect())
os.Exit(1)
}
}