Skip to content

Commit

Permalink
Merge pull request #7 from akos011221/feat/repl
Browse files Browse the repository at this point in the history
Adding basic REPL
  • Loading branch information
akos011221 authored Oct 6, 2024
2 parents 876c127 + 973daa3 commit 99acd5d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"fmt"
"moodeng/repl"
"os"
"os/user"
)

func main() {
user, err := user.Current()
if err != nil {
panic(err)
}
fmt.Printf("Hello %s! This is Moodeng!\n", user.Username)
fmt.Printf("You can tart typing commands...\n")
repl.Start(os.Stdin, os.Stdout)
}
30 changes: 30 additions & 0 deletions repl/repl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package repl

import (
"bufio"
"fmt"
"io"
"moodeng/lexer"
"moodeng/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)
}
}
}

0 comments on commit 99acd5d

Please sign in to comment.