add basic repl
This commit is contained in:
19
main.go
Normal file
19
main.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"monkey/repl"
|
||||
"os"
|
||||
user2 "os/user"
|
||||
)
|
||||
|
||||
func main() {
|
||||
user, err := user2.Current()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("Hello %s! This is the Monkey programmig language!\n", user.Username)
|
||||
fmt.Printf("Feel free to type in commands\n")
|
||||
repl.Start(os.Stdin, os.Stdout)
|
||||
}
|
||||
30
repl/repl.go
Normal file
30
repl/repl.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package repl
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"monkey/lexer"
|
||||
"monkey/token"
|
||||
)
|
||||
|
||||
const PROMPT = ">> "
|
||||
|
||||
func Start(in io.Reader, out io.Writer) {
|
||||
scanner := bufio.NewScanner(in)
|
||||
|
||||
for {
|
||||
fmt.Fprintf(out, 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.Fprintf(out, "%+v\n", tok)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user