Skip to content

Commit

Permalink
1.5: Implement REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
nibral committed Jan 10, 2019
1 parent 0b77bb3 commit efce289
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
19 changes: 19 additions & 0 deletions main.go
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"monkey_interpreter/repl"
"os"
"os/user"
)

func main() {
replUser, err := user.Current()
if err != nil {
panic(err)
}

fmt.Printf("Hello %s! This is the Monkey programming language!\n", replUser.Username)
fmt.Printf("Feel free to type commands\n")
repl.Start(os.Stdin, os.Stdout)
}
31 changes: 31 additions & 0 deletions repl/repl.go
@@ -0,0 +1,31 @@
package repl

import (
"bufio"
"fmt"
"io"
"monkey_interpreter/lexer"
"monkey_interpreter/token"
)

const PROMPT = ">> "

func Start(in io.Reader, out io.Writer) {
scanner := bufio.NewScanner(in)

for {
fmt.Printf(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.Printf("%+v\n", tok)
}
}
}

0 comments on commit efce289

Please sign in to comment.