diff --git a/main.go b/main.go new file mode 100644 index 0000000..952d903 --- /dev/null +++ b/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "moodeng/repl" + "os" + "os/user" +) + +func main() { + user, err := user.Current() + if err != nil { + panic(err) + } + fmt.Printf("Hello %s! This is Moodeng!\n", user.Username) + fmt.Printf("You can tart typing commands...\n") + repl.Start(os.Stdin, os.Stdout) +} diff --git a/repl/repl.go b/repl/repl.go new file mode 100644 index 0000000..04abbde --- /dev/null +++ b/repl/repl.go @@ -0,0 +1,30 @@ +package repl + +import ( + "bufio" + "fmt" + "io" + "moodeng/lexer" + "moodeng/token" +) + +const PROMPT = ">> " + +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) + } + } +}