69 lines
1.2 KiB
Go
69 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.sharkk.net/Sharkk/Mako/compiler"
|
|
"git.sharkk.net/Sharkk/Mako/lexer"
|
|
"git.sharkk.net/Sharkk/Mako/parser"
|
|
"git.sharkk.net/Sharkk/Mako/vm"
|
|
)
|
|
|
|
func RunRepl() {
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
virtualMachine := vm.New()
|
|
|
|
fmt.Println("Mako REPL (type 'exit' to quit)")
|
|
for {
|
|
fmt.Print(">> ")
|
|
if !scanner.Scan() {
|
|
break
|
|
}
|
|
|
|
input := scanner.Text()
|
|
if input == "exit" {
|
|
break
|
|
}
|
|
|
|
ExecuteCode(input, virtualMachine)
|
|
}
|
|
}
|
|
|
|
func ExecuteCode(code string, virtualMachine *vm.VM) {
|
|
lex := lexer.New(code)
|
|
p := parser.New(lex)
|
|
program := p.ParseProgram()
|
|
|
|
if len(p.Errors()) > 0 {
|
|
for _, err := range p.Errors() {
|
|
fmt.Printf("Error: %s\n", err)
|
|
}
|
|
return
|
|
}
|
|
|
|
bytecode := compiler.Compile(program)
|
|
virtualMachine.Run(bytecode)
|
|
}
|
|
|
|
func main() {
|
|
args := os.Args[1:]
|
|
|
|
// If there's a command line argument, try to execute it as a file
|
|
if len(args) > 0 {
|
|
filename := args[0]
|
|
fileContent, err := os.ReadFile(filename)
|
|
if err != nil {
|
|
fmt.Printf("Error reading file %s: %v\n", filename, err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Execute the file content
|
|
ExecuteCode(string(fileContent), vm.New())
|
|
} else {
|
|
// No arguments, run the REPL
|
|
RunRepl()
|
|
}
|
|
}
|