about summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@fcuny.net>2020-01-11 14:50:44 +0100
committerfranck cuny <franck@fcuny.net>2020-01-11 14:50:44 +0100
commit7e0195b0a753bd990307587926bfc0fe11927f77 (patch)
tree04e08f67def9a2c7b18f6fda4266f0129aa0c6e2
parentlexer: support tokens for equal and not equal. (diff)
downloadworld-7e0195b0a753bd990307587926bfc0fe11927f77.tar.gz
repl: support a simple REPL for some early testing
The REPL reads the input, send it to the lexer, and prints the token to
STDOUT. For now nothing else is done since we still don't parse the
tokens.
Diffstat (limited to '')
-rw-r--r--users/fcuny/exp/monkey/cmd/repl/main.go12
-rw-r--r--users/fcuny/exp/monkey/pkg/repl/repl.go29
2 files changed, 41 insertions, 0 deletions
diff --git a/users/fcuny/exp/monkey/cmd/repl/main.go b/users/fcuny/exp/monkey/cmd/repl/main.go
new file mode 100644
index 0000000..46b865c
--- /dev/null
+++ b/users/fcuny/exp/monkey/cmd/repl/main.go
@@ -0,0 +1,12 @@
+package main
+
+import (
+	"fmt"
+	"monkey/pkg/repl"
+	"os"
+)
+
+func main() {
+	fmt.Printf("Welcome to monkey's REPL.")
+	repl.Start(os.Stdin, os.Stdout)
+}
diff --git a/users/fcuny/exp/monkey/pkg/repl/repl.go b/users/fcuny/exp/monkey/pkg/repl/repl.go
new file mode 100644
index 0000000..e8b3b1f
--- /dev/null
+++ b/users/fcuny/exp/monkey/pkg/repl/repl.go
@@ -0,0 +1,29 @@
+package repl
+
+import (
+	"bufio"
+	"fmt"
+	"io"
+	lexer "monkey/pkg/lexer"
+	token "monkey/pkg/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)
+		}
+	}
+}