Skip to content

Commit

Permalink
add repl
Browse files Browse the repository at this point in the history
  • Loading branch information
54L1M committed Jan 27, 2024
1 parent b183627 commit dd091c3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"os"
"os/user"

"github.com/54L1m/mil-lang/repl"
)

func main() {
user, err := user.Current()
if err != nil {
panic(err)
}
fmt.Printf("Hello %s! This is Mil-Lang!\n", user.Username)
fmt.Printf("Feel free to type in 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"

"github.com/54L1m/mil-lang/lexer"
"github.com/54L1m/mil-lang/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 dd091c3

Please sign in to comment.