Wire up file running.
This commit is contained in:
parent
ad1a8cac34
commit
d7c5a19f85
3 changed files with 15 additions and 5 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
|
@ -9,12 +10,17 @@ import (
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
argc := len(os.Args)
|
argc := len(os.Args)
|
||||||
if argc > 2 {
|
switch {
|
||||||
|
case argc > 2:
|
||||||
fmt.Println("Usage: golox [script]")
|
fmt.Println("Usage: golox [script]")
|
||||||
os.Exit(64)
|
os.Exit(64)
|
||||||
} else if argc == 2 {
|
case argc == 2:
|
||||||
|
err := runner.RunFile(os.Args[1])
|
||||||
} else {
|
if errors.Is(err, runner.ErrInvalidScriptFile) {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
default:
|
||||||
runner.RunPrompt()
|
runner.RunPrompt()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,13 @@ package runner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrInvalidScriptFile = errors.New("could not read script file")
|
||||||
|
|
||||||
func RunPrompt() {
|
func RunPrompt() {
|
||||||
s := bufio.NewScanner(os.Stdin)
|
s := bufio.NewScanner(os.Stdin)
|
||||||
fmt.Print("> ")
|
fmt.Print("> ")
|
||||||
|
|
@ -20,7 +23,7 @@ func RunPrompt() {
|
||||||
func RunFile(path string) error {
|
func RunFile(path string) error {
|
||||||
fBytes, err := os.ReadFile(path)
|
fBytes, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not read script file: %w", err)
|
return errors.Join(ErrInvalidScriptFile, err)
|
||||||
}
|
}
|
||||||
Run(string(fBytes))
|
Run(string(fBytes))
|
||||||
// TODO: check hadError and exit with a 65 code if so.
|
// TODO: check hadError and exit with a 65 code if so.
|
||||||
|
|
|
||||||
1
golox/test.lox
Normal file
1
golox/test.lox
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
{,{.(;)-}}*
|
||||||
Loading…
Add table
Add a link
Reference in a new issue