Skip to content

Commit

Permalink
Merge pull request #73 from skx/70-repl
Browse files Browse the repository at this point in the history
70 repl
  • Loading branch information
skx authored Oct 29, 2022
2 parents 5ffd9fc + 213661e commit 137dcfe
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ Remember that if you're running a Mac you'll need to remove the quarantine flag

## Usage

Once installed there are two ways to execute code:
Once installed there are three ways to execute code:

* By specifying an expression to execute upon the command-line:
* `yal -e '(print (os))'`
* By passing the name of a file containing lisp code to read and execute:
* `yal test.lisp`
* By launching the interpreter with zero arguments, which will launch the interactive REPL mode.

The yal interpreter allows (optional) documentation to be attached to functions, both those implemented in the core, and those which are added in lisp:

Expand Down
41 changes: 39 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package main

import (
"bufio"
"flag"
"fmt"
"math/rand"
Expand Down Expand Up @@ -250,8 +251,44 @@ func main() {
os.Exit(0)
}

// No arguments
//
// TODO REPL
// No arguments mean this is our REPL
//
fmt.Printf("YAL version %s\n", version)
reader := bufio.NewReader(os.Stdin)

src := ""
for {
if src == "" {
fmt.Printf("\n> ")
}

line, _ := reader.ReadString('\n')
src += line
src = strings.TrimSpace(src)

// Allow the user to exit
if src == "exit" || src == "quit" {
os.Exit(0)
}

open := strings.Count(src, "(")
close := strings.Count(src, ")")

if open < close {
fmt.Printf("Malformed expression: %v", src)
src = ""
continue
}
if open == close {

out := LISP.Execute(ENV, src)

// If the result wasn't nil then show it
if _, ok := out.(primitive.Nil); !ok {
fmt.Printf("%v\n", out)
}
src = ""
}
}
}

0 comments on commit 137dcfe

Please sign in to comment.