monkey/cmd/repl/main.go

42 lines
708 B
Go

package main
import (
"bufio"
"fmt"
"io"
"monkey/internal/lexer"
"monkey/internal/token"
"os"
"os/user"
)
const prompt = ">> "
func main() {
user, err := user.Current()
if err != nil {
panic(err)
}
fmt.Printf("Hello %s! This is the Monkey programming language!\n",
user.Username)
fmt.Printf("Feel free to type in commands\n")
start(os.Stdin, os.Stdout)
}
func start(in io.Reader, out io.Writer) {
scanner := bufio.NewScanner(in)
for {
fmt.Fprintf(out, prompt)
scanned := scanner.Scan()
if !scanned {
return
}
line := scanner.Text()
l := lexer.New(line)
for tok := l.NextToken(); tok.Type != token.EOF; tok = l.NextToken() {
fmt.Fprintf(out, "%+v\n", tok)
}
}
}