From e2049d8b44ac97d3a47f8de8ad89f473214938d1 Mon Sep 17 00:00:00 2001 From: Bobby Date: Sun, 29 Oct 2023 16:44:23 -0400 Subject: Added REPL and main function --- main.go | 19 +++++++++++++++++++ repl/repl.go | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..deba212 --- /dev/null +++ b/main.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + "os" + "os/user" + "mana/repl" +) + +func main() { + user, err := user.Current() + + if err != nil { + panic(err) + } + + fmt.Printf("Hello %s! Welcome to Mana REPL!\n", user.Username) + repl.Start(os.Stdin, os.Stdout) +} diff --git a/repl/repl.go b/repl/repl.go index 9119db8..37acd49 100644 --- a/repl/repl.go +++ b/repl/repl.go @@ -1 +1,32 @@ -package repl \ No newline at end of file +package repl + +import ( + "bufio" + "fmt" + "io" + "mana/lexer" + "mana/tokens" +) + +// PROMPT is the prompt for the REPL. +const PROMPT = "=> " + +func Start(in io.Reader, out io.Writer) { + scanner := bufio.NewScanner(in) + + for { + fmt.Fprint(out, PROMPT) + scanned := scanner.Scan() + + if !scanned { + return + } + + line := scanner.Text() + l := lexer.New(line) + + for tok := l.NextToken(); tok.Type != tokens.EOF; tok = l.NextToken() { + fmt.Fprintf(out, "%+v\n", tok) + } + } +} -- cgit v1.2.3