Initial commit

This commit is contained in:
Michał 2024-09-22 17:50:20 +01:00
commit 3998ef0a3c
5 changed files with 77 additions and 0 deletions

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module github.com/Fluffy-Bean/GoLox
go 1.23.1

47
main.go Normal file
View file

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

21
scanner.go Normal file
View file

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

1
scripts/example.lox Normal file
View file

@ -0,0 +1 @@
print "Hello, World!";

5
tokens.go Normal file
View file

@ -0,0 +1,5 @@
package main
type Token struct {
token string
}