mirror of
https://github.com/Fluffy-Bean/GoLox.git
synced 2024-12-26 09:06:05 +00:00
Initial commit
This commit is contained in:
commit
3998ef0a3c
47
main.go
Normal file
47
main.go
Normal 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
21
scanner.go
Normal 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
1
scripts/example.lox
Normal file
|
@ -0,0 +1 @@
|
|||
print "Hello, World!";
|
Loading…
Reference in a new issue