commit b2cb2a1c4d76faa74a9a398f498bc0d866628f0b
parent 5b743e7aeec89865a7c1723048b9bddb79b9f07b
Author: bsandro <email@bsandro.tech>
Date: Sat, 11 Jun 2022 23:31:28 +0300
Lexer demo - interactive demo binary
Diffstat:
3 files changed, 48 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1 @@
+interp
diff --git a/main.go b/main.go
@@ -0,0 +1,17 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "os/user"
+ "interp/repl"
+)
+
+func main() {
+ user, err := user.Current()
+ if err != nil {
+ panic(err)
+ }
+ fmt.Printf("ehlo %s, this is a potato interpretator cleverly named 'interp'\n", user.Username)
+ repl.Start(os.Stdin, os.Stdout)
+}
diff --git a/repl/repl.go b/repl/repl.go
@@ -0,0 +1,30 @@
+package repl
+
+import (
+ "bufio"
+ "fmt"
+ "io"
+ "interp/lexer"
+ "interp/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)
+ }
+ }
+}