Skip to content

Commit

Permalink
implement repl to test the lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
mcapell committed Aug 22, 2024
1 parent 9ed1b70 commit 3afda8e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
20 changes: 20 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
"os"
"os/user"

"github.com/mcapell/go-monkey/repl"
)

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.Println("Feel free to type in commands")
repl.Start(os.Stdin, os.Stdout)
}
33 changes: 33 additions & 0 deletions repl/repl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package repl

import (
"bufio"
"fmt"
"io"

"github.com/mcapell/go-monkey/lexer"
"github.com/mcapell/go-monkey/token"
)

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 != token.EOF; tok = l.NextToken() {
fmt.Fprintf(out, "%+v\n", tok)
}

}
}

0 comments on commit 3afda8e

Please sign in to comment.