runningwater преди 3 години
родител
ревизия
71c8c4e893
променени са 2 файла, в които са добавени 54 реда и са изтрити 0 реда
  1. 17 0
      main.go
  2. 37 0
      repl/repl.go

+ 17 - 0
main.go

@@ -1,8 +1,25 @@
 package main
 
+import (
+    "fmt"
+    "github/runnignwater/monkey/repl"
+    "os"
+    _user "os/user"
+)
+
 /**
  * @Author: simon
  * @Author: ynwdlxm@163.com
  * @Date: 2022/10/2 上午10:55
  * @Desc:
  */
+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)
+}

+ 37 - 0
repl/repl.go

@@ -0,0 +1,37 @@
+package repl
+
+import (
+    "bufio"
+    "fmt"
+    "github/runnignwater/monkey/lexer"
+    "github/runnignwater/monkey/token"
+    "io"
+)
+
+/**
+ * @Author: simon
+ * @Author: ynwdlxm@163.com
+ * @Date: 2022/10/2 下午5:41
+ * @Desc: REPL that tokenizes Monkey source code and prints the tokens
+ */
+
+const PROMPT = ">>> "
+
+func Start(in io.Reader, out io.Writer) {
+    scanner := bufio.NewScanner(in)
+
+    for {
+        fmt.Printf(PROMPT)
+        scan := scanner.Scan()
+        if !scan {
+            return
+        }
+        line := scanner.Text()
+
+        l := lexer.New(line)
+
+        for tok := l.NextToken(); tok.Type != token.EOF; tok = l.NextToken() {
+            fmt.Printf("%+v\n", tok)
+        }
+    }
+}