REPL
This commit is contained in:
parent
7e9304ed11
commit
b8d8e30f0f
18
main.go
18
main.go
@ -1 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/user"
|
||||
|
||||
"gitlab.com/Tyrel/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.Printf("Feel free to type in commands\n")
|
||||
repl.Start(os.Stdin, os.Stdout)
|
||||
}
|
||||
|
31
repl/repl.go
Normal file
31
repl/repl.go
Normal file
@ -0,0 +1,31 @@
|
||||
package repl
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"gitlab.com/Tyrel/monkey/lexer"
|
||||
"gitlab.com/Tyrel/monkey/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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user