Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

70 repl #73

Merged
merged 2 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = ""
}
}
}