diff options
| author | Bobby <[email protected]> | 2023-10-29 16:44:23 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2023-10-29 16:44:23 -0400 |
| commit | e2049d8b44ac97d3a47f8de8ad89f473214938d1 (patch) | |
| tree | 9cd66fd87ce50b73c8617f2fc8a53f484f484232 | |
| parent | abb94bc7da02ac69ade9a747a8a205741c96d35b (diff) | |
| download | mana-e2049d8b44ac97d3a47f8de8ad89f473214938d1.tar.xz mana-e2049d8b44ac97d3a47f8de8ad89f473214938d1.zip | |
Added REPL and main function
| -rw-r--r-- | main.go | 19 | ||||
| -rw-r--r-- | repl/repl.go | 33 |
2 files changed, 51 insertions, 1 deletions
@@ -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) + } + } +} |
