pj1/pj1-go/pj1.go

64 lines
1,006 B
Go
Raw Normal View History

package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"git.bonsai.cool/kayprish/pj1/pj1-go/lexer"
"git.bonsai.cool/kayprish/pj1/pj1-go/util"
)
// func main() {
// if len(os.Args) > 2 {
// fmt.Println("Usage: pj1-go [script]")
// os.Exit(64)
// } else if len(os.Args) == 2 {
// runFile(os.Args[0])
// } else {
// runPrompt()
// }
// }
func runFile(path string) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println(err)
return
}
run(string(bytes[:]))
if util.HadError {
os.Exit(65)
}
}
func runPrompt() {
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("> ")
if !scanner.Scan() {
break
}
line := scanner.Text()
fmt.Println(line)
run(line)
util.HadError = false
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
}
func run(source string) {
l := lexer.NewLexer(source)
l.ScanTokens()
var tokens []lexer.Token = l.Tokens
for _, token := range tokens {
fmt.Println(token)
}
}