Skip to content

Commit 02a5f71

Browse files
committed
1.5 REPL (basic)
1 parent ccdbaac commit 02a5f71

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
// Package main implements the main process which invokes the interpreter's
4+
// REPL and waits for user input before lexing, parsing nad evaulating.
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"os/user"
10+
11+
"github.com/cedrickchee/hou/repl"
12+
)
13+
14+
func main() {
15+
user, err := user.Current()
16+
if err != nil {
17+
panic(err)
18+
}
19+
fmt.Printf("Hello %s! This is the Hou programming language!\n", user.Username)
20+
fmt.Printf("Feel free to type in commands\n")
21+
repl.Start(os.Stdin, os.Stdout)
22+
}

repl/repl.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package repl
2+
3+
// Package repl implements the Read-Eval-Print-Loop (REPL) or interactive console
4+
// by lexing, parsing and evaluating the input in the interpreter.
5+
6+
import (
7+
"bufio"
8+
"fmt"
9+
"io"
10+
11+
"github.com/cedrickchee/hou/lexer"
12+
"github.com/cedrickchee/hou/token"
13+
)
14+
15+
// PROMPT is the REPL prompt displayed for each input.
16+
const PROMPT = ">> "
17+
18+
// Start starts the REPL in a continuous loop.
19+
func Start(in io.Reader, out io.Writer) {
20+
scanner := bufio.NewScanner(in)
21+
22+
for {
23+
fmt.Printf(PROMPT)
24+
scanned := scanner.Scan()
25+
if !scanned {
26+
return
27+
}
28+
29+
line := scanner.Text()
30+
31+
// A REPL that tokenizes Monkey source code and prints the tokens.
32+
l := lexer.New(line)
33+
for tok := l.NextToken(); tok.Type != token.EOF; tok = l.NextToken() {
34+
fmt.Printf("%+v\n", tok)
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)