Initialize project
parent
a64a8fa826
commit
5063cdbf40
@ -1,3 +1,19 @@
|
|||||||
# golox
|
# golox
|
||||||
|
|
||||||
Lox implementation in Golang
|
Lox implementation in Golang
|
||||||
|
|
||||||
|
Build with:
|
||||||
|
|
||||||
|
```
|
||||||
|
go build -o bin/golox
|
||||||
|
```
|
||||||
|
|
||||||
|
Run with no arguments for prompt interpreter:
|
||||||
|
```
|
||||||
|
bin/golox
|
||||||
|
```
|
||||||
|
|
||||||
|
Or with script name for script execution:
|
||||||
|
```
|
||||||
|
bin/golox lox_script_name
|
||||||
|
```
|
||||||
|
|||||||
@ -0,0 +1,40 @@
|
|||||||
|
package lox
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RunFile(path string) {
|
||||||
|
bytes, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error reading file", path)
|
||||||
|
os.Exit(74)
|
||||||
|
}
|
||||||
|
run(string(bytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func run(source string) {
|
||||||
|
fmt.Println(source)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue