You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
751 B
Go

package lox
import (
"bufio"
"fmt"
"os"
)
var hadError = false
func RunFile(path string) {
bytes, err := os.ReadFile(path)
if err != nil {
fmt.Println("Error reading file", path)
os.Exit(74)
}
run(string(bytes))
if hadError {
os.Exit(65)
}
}
func RunPrompt() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("> ")
line, err := reader.ReadString('\n')
if err != nil {
fmt.Println(err)
os.Exit(74)
}
if line == "\n" {
break
}
run(line)
hadError = false
}
}
func Error(line int, message string) {
report(line, "", message)
}
func report(line int, where string, message string) {
fmt.Printf("[line %d] Error %s: %s\n", line, where, message)
}
func run(source string) {
fmt.Println(source)
}