2022-09-03 23:15:35 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"varaq/varaq"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-07-12 00:30:26 -04:00
|
|
|
argsCount := len(os.Args[1:])
|
|
|
|
if argsCount >= 1 {
|
|
|
|
runFile(os.Args[1])
|
2022-09-03 23:15:35 -04:00
|
|
|
} else {
|
2024-07-12 00:30:26 -04:00
|
|
|
repl()
|
2022-09-03 23:15:35 -04:00
|
|
|
}
|
2024-07-12 00:30:26 -04:00
|
|
|
os.Exit(0)
|
2022-09-03 23:15:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func repl() {
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
2024-07-06 21:15:21 -04:00
|
|
|
for fmt.Print("> "); scanner.Scan(); fmt.Print("> ") {
|
2022-09-03 23:15:35 -04:00
|
|
|
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)
|
2023-03-18 22:26:32 -04:00
|
|
|
err = varaq.Interpret(code, os.Args[1:], os.Stdout)
|
2022-09-03 23:15:35 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%v\n", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|