Initialize project

main
oabrivard 1 year ago
parent a64a8fa826
commit 5063cdbf40

6
.gitignore vendored

@ -14,10 +14,9 @@
settings.json settings.json
# ---> Go # ---> Go
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins # Binaries for programs and plugins
bin/
*.exe *.exe
*.exe~ *.exe~
*.dll *.dll
@ -36,3 +35,4 @@ settings.json
# Go workspace file # Go workspace file
go.work go.work
# <--- Go

@ -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,3 @@
module golox
go 1.23.2

@ -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)
}

@ -0,0 +1,21 @@
package main
import (
"fmt"
"os"
"golox/lox"
)
func main() {
nbArgs := len(os.Args)
if nbArgs > 2 {
fmt.Println("Usage: golox [script]")
os.Exit(64)
} else if nbArgs == 2 {
lox.RunFile(os.Args[1])
} else {
lox.RunPrompt()
}
}
Loading…
Cancel
Save