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

[WIP] Tree interpreter #1

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open

[WIP] Tree interpreter #1

wants to merge 22 commits into from

Conversation

porglezomp
Copy link
Contributor

Run the code by directly walking the AST

Holes are values that can be filled in later with the hole/fill
function. They're useful for manually implementing
tail-recursion-modulo-cons, which is where you fill in the second part
of a cons later so that you can make a tail call. It's possible to use
the hole function unsafely: if you make a cyclic structure it will never
be collected.

Unsafe example:

(let! rest (hole))
(let! all-ones (cons 1 rest))
(hole/fill rest all-ones)

now all-ones is a circular list!

Useful example:

(defn (list/map f xs)
  (let! result (hole))
  (loop ((xs xs) (spot result))
    (if (nil? xs) (break (hole/fill spot nil)))
    (let! new-spot (hole))
    (hole/fill spot (cons (f (car xs)) new-spot))
    (continue (cdr xs) new-spot))
  result)

Here we define map tail recursively, and without having to do any
post-processing like reversing the list.
Pretty-print tuples as #tuple or #tuple<tag>.
Add instructions for getting and setting the tuple tag.
Make struct generation set the tag inside the constructor.

Thanks to Roshni for pair programming on this, I had fun explaining it.
Rewrite (#tuple<cons> 1 (#tuple<cons> 2 ())) as (1 2) and
(#tuple<cons> 1 2) as (1 . 2).
For instance, a symbol like a will print as 'a in the alternate printing
mode. Now the repl prints using {:#} instead of {} to get the quoting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant