commit 3998ef0a3c4bfaf929176c7140a94274a5fc774e Author: Fluffy Date: Sun Sep 22 17:50:20 2024 +0100 Initial commit diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..702b90f --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/Fluffy-Bean/GoLox + +go 1.23.1 diff --git a/main.go b/main.go new file mode 100644 index 0000000..353e791 --- /dev/null +++ b/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strings" +) + +func main() { + if len(os.Args) > 2 { + fmt.Println("Usage: lox [script]") + os.Exit(1) + } else if len(os.Args) == 2 { + parseFile(os.Args[1]) + } else { + parsePrompt() + } +} + +func parseFile(path string) { + file, err := os.ReadFile(path) + if err != nil { + fmt.Println("Could not open file") + os.Exit(1) + } + + scanner := NewScanner() + scanner.Parse(string(file)) +} + +func parsePrompt() { + reader := bufio.NewReader(os.Stdin) + scanner := NewScanner() + + for { + fmt.Print("> ") + + text, _ := reader.ReadString('\n') + text = strings.TrimSpace(text) + if text == "exit" { + return + } + + scanner.Parse(text) + } +} diff --git a/scanner.go b/scanner.go new file mode 100644 index 0000000..9180bf5 --- /dev/null +++ b/scanner.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" +) + +type Scanner struct { + tokens []Token +} + +func (s *Scanner) GetTokens() []Token { + return s.tokens +} + +func (s *Scanner) Parse(source string) { + fmt.Println(source) +} + +func NewScanner() *Scanner { + return &Scanner{} +} diff --git a/scripts/example.lox b/scripts/example.lox new file mode 100644 index 0000000..b2527ad --- /dev/null +++ b/scripts/example.lox @@ -0,0 +1 @@ +print "Hello, World!"; diff --git a/tokens.go b/tokens.go new file mode 100644 index 0000000..ba02770 --- /dev/null +++ b/tokens.go @@ -0,0 +1,5 @@ +package main + +type Token struct { + token string +}