Browse Source

§1.5 lexing REPL.

Frederic G. MARAND 5 years ago
parent
commit
a0a80b1afe
3 changed files with 62 additions and 0 deletions
  1. 12 0
      .idea/runConfigurations/REPL.xml
  2. 20 0
      main.go
  3. 30 0
      repl/repl.go

+ 12 - 0
.idea/runConfigurations/REPL.xml

@@ -0,0 +1,12 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="REPL" type="GoApplicationRunConfiguration" factoryName="Go Application" singleton="true">
+    <module name="waiig15" />
+    <working_directory value="$PROJECT_DIR$/" />
+    <go_parameters value="-i" />
+    <kind value="FILE" />
+    <filePath value="$PROJECT_DIR$/main.go" />
+    <package value="fgm/waiig15" />
+    <directory value="$PROJECT_DIR$/" />
+    <method v="2" />
+  </configuration>
+</component>

+ 20 - 0
main.go

@@ -0,0 +1,20 @@
+package main
+
+import (
+	"os/user"
+	"fmt"
+	"fgm/waiig15/repl"
+	"os"
+)
+
+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)
+
+}

+ 30 - 0
repl/repl.go

@@ -0,0 +1,30 @@
+package repl
+
+import (
+	"io"
+	"bufio"
+	"fmt"
+	"fgm/waiig15/lexer"
+	"fgm/waiig15/token"
+)
+
+const PROMPT = ">> "
+
+func Start(in io.Reader, out io.Writer) {
+	scanner := bufio.NewScanner(in)
+
+	for {
+		fmt.Print(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)
+		}
+	}
+}