Add REPL boilerplate.
Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
parent
dcf5cb336a
commit
04dfd62600
2 changed files with 45 additions and 0 deletions
19
cmd/repl/main.go
Normal file
19
cmd/repl/main.go
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/user"
|
||||||
|
|
||||||
|
"code.jmug.me/jmug/interpreter-in-go/pkg/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("Go ahead, type something :)")
|
||||||
|
repl.Start(os.Stdin, os.Stdout)
|
||||||
|
}
|
||||||
26
pkg/repl/repl.go
Normal file
26
pkg/repl/repl.go
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
package repl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"code.jmug.me/jmug/interpreter-in-go/pkg/lexer"
|
||||||
|
"code.jmug.me/jmug/interpreter-in-go/pkg/token"
|
||||||
|
)
|
||||||
|
|
||||||
|
const PROMPT = ">> "
|
||||||
|
|
||||||
|
func Start(in io.Reader, out io.Writer) {
|
||||||
|
scanner := bufio.NewScanner(in)
|
||||||
|
for {
|
||||||
|
fmt.Fprint(out, PROMPT)
|
||||||
|
if !scanner.Scan() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
l := lexer.New(scanner.Text())
|
||||||
|
for tok := l.NextToken(); tok.Type != token.EOF; tok = l.NextToken() {
|
||||||
|
fmt.Fprintf(out, "%+v\n", tok)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue