From 5063cdbf4062ea99962f974cd04c5816a587ca7f Mon Sep 17 00:00:00 2001 From: oabrivard Date: Thu, 31 Oct 2024 12:00:18 +0100 Subject: [PATCH] Initialize project --- .gitignore | 6 +++--- README.md | 18 +++++++++++++++++- go.mod | 3 +++ lox/lox.go | 40 ++++++++++++++++++++++++++++++++++++++++ main.go | 21 +++++++++++++++++++++ 5 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 go.mod create mode 100644 lox/lox.go create mode 100644 main.go diff --git a/.gitignore b/.gitignore index a09fef2..78a0187 100644 --- a/.gitignore +++ b/.gitignore @@ -14,10 +14,9 @@ settings.json # ---> 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 +bin/ *.exe *.exe~ *.dll @@ -36,3 +35,4 @@ settings.json # Go workspace file go.work +# <--- Go diff --git a/README.md b/README.md index 2a8ece6..ce3c4e9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,19 @@ # golox -Lox implementation in Golang \ No newline at end of file +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 +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4418f9c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module golox + +go 1.23.2 diff --git a/lox/lox.go b/lox/lox.go new file mode 100644 index 0000000..f4148dd --- /dev/null +++ b/lox/lox.go @@ -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) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..b539208 --- /dev/null +++ b/main.go @@ -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() + } +}