From 7e0195b0a753bd990307587926bfc0fe11927f77 Mon Sep 17 00:00:00 2001 From: franck cuny Date: Sat, 11 Jan 2020 14:50:44 +0100 Subject: 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. --- users/fcuny/exp/monkey/pkg/repl/repl.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 users/fcuny/exp/monkey/pkg/repl/repl.go (limited to 'users/fcuny/exp/monkey/pkg/repl') 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) + } + } +} -- cgit v1.2.3 From 8437218bdaed90cab7374a752f8aaf128225aa1a Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Mon, 10 May 2021 19:21:39 -0700 Subject: lint: fix a few issues --- users/fcuny/exp/monkey/pkg/repl/repl.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'users/fcuny/exp/monkey/pkg/repl') diff --git a/users/fcuny/exp/monkey/pkg/repl/repl.go b/users/fcuny/exp/monkey/pkg/repl/repl.go index e8b3b1f..5e7b1d1 100644 --- a/users/fcuny/exp/monkey/pkg/repl/repl.go +++ b/users/fcuny/exp/monkey/pkg/repl/repl.go @@ -1,3 +1,4 @@ +// Package repl provides a REPL to the monkey language. package repl import ( @@ -13,7 +14,7 @@ const PROMPT = ">> " func Start(in io.Reader, out io.Writer) { scanner := bufio.NewScanner(in) for { - fmt.Printf(PROMPT) + fmt.Print(PROMPT) scanned := scanner.Scan() if !scanned { -- cgit v1.2.3