64 lines
991 B
Go
64 lines
991 B
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"varaq/varaq"
|
|
)
|
|
|
|
func main() {
|
|
argsCount := len(os.Args[1:])
|
|
if argsCount >= 1 {
|
|
runFile(os.Args[1])
|
|
} else {
|
|
repl()
|
|
}
|
|
os.Exit(0)
|
|
}
|
|
|
|
func repl() {
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
for fmt.Print("> "); scanner.Scan(); fmt.Print("> ") {
|
|
line := scanner.Text()
|
|
if len(line) == 0 {
|
|
return
|
|
}
|
|
err := interpret(line)
|
|
if err != nil {
|
|
fmt.Printf("%v\n", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func runFile(path string) {
|
|
code, err := ioutil.ReadFile(path) // the file is inside the local directory
|
|
if err != nil {
|
|
fmt.Printf("%v\n", err)
|
|
os.Exit(65)
|
|
}
|
|
|
|
err = interpret(string(code))
|
|
if err != nil {
|
|
fmt.Printf("%v\n", err)
|
|
os.Exit(65)
|
|
}
|
|
}
|
|
|
|
func interpret(src string) error {
|
|
tokens, err := varaq.Tokenize(src)
|
|
if err != nil {
|
|
return fmt.Errorf("%v", err)
|
|
}
|
|
|
|
code := varaq.Parse(tokens)
|
|
err = varaq.Interpret(code, os.Args[1:], os.Stdout)
|
|
if err != nil {
|
|
fmt.Printf("%v\n", err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|