Skip to content

Instantly share code, notes, and snippets.

@nasser
Last active August 29, 2015 14:22
Show Gist options
  • Save nasser/ac9fa368c53fddd1e525 to your computer and use it in GitHub Desktop.
Save nasser/ac9fa368c53fddd1e525 to your computer and use it in GitHub Desktop.
Execution of range function, implemented in pure Rejoyce

Rejoyce

Rejoyce is a purely functional, persistent, concatenative language

It maintains an In queue and an Out stackThe In queue represents code yet to be executed, and is initially populated with the program to run

The Out stack represents data to be manipulated, and is initially empty

The two record the complete state of execution at any time

To evaluate a token, Rejoyce returns a completely new In and Out stack

A token can be a literal value, like 5

Or a word, like range

Or a quotation of literals and words, like [dup last 0 <=]

Or maps of words to definitions like { vec/wrap [[] swap conj] }

Literals, quotations, and maps are moved to the stack unmodified

Words are looked up on the stack for definitions, and are replaced by the first definition found

Words can produce new Out stacks, In queues, or both

Formally, every token in Rejoyce is a function of (In,Out) → (In',Out')

eval.joy

The eval.joy represents the complete evaluation of the code 5 range

Everything to the right of the ◆ is the In queue

Everything to the left of the ◆ is the Out stack

Every new line is the next step in the evaluation

Every line is a completely new In queue and Out stack

The core library maps, where the definitions of e.g. dup and <= are, are left out for brevity

They are at the bottom of the Out stack initially

The range.joy map is left out for brevity

It is at the top of the Out stack initially

Note evaluating a quotation happens by 'unwrapping it' and prepending it to the In queue

Note line 2 → 3, range is looked up and replaced in the In queue with its definition from range.joy

Note line 9 → 10, until loops by expanding to an unless with another until inside it's body

Note line 16 → 17, unless evaluates the quotation at the top of the stack if the boolean beneath it is false

range.joy

This is the map with the range function and its helpers

The / in vec/wrap is meant to simulate namespaces

When these words are looked up in eval.joy, the contents of their quotations are prepended to the In queue

◆ 5 range
5 ◆ range
5 ◆ vec/wrap [vec/conj-dec] [vec/last-neg?] until
5 ◆ [] swap conj [vec/conj-dec] [vec/last-neg?] until
5 [] ◆ swap conj [vec/conj-dec] [vec/last-neg?] until
[] 5 ◆ conj [vec/conj-dec] [vec/last-neg?] until
[5] ◆ [vec/conj-dec] [vec/last-neg?] until
[5] [vec/conj-dec] ◆ [vec/last-neg?] until
[5] [vec/conj-dec] [vec/last-neg?] ◆ until
[5] ◆ vec/last-neg? [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5] ◆ dup last 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5] [5] ◆ last 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5] 5 ◆ 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5] 5 0 ◆ <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5] false ◆ [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5] false [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] ◆ unless
[5] ◆ vec/conj-dec [vec/conj-dec] [vec/last-neg?] until
[5] ◆ dup last dec conj [vec/conj-dec] [vec/last-neg?] until
[5] [5] ◆ last dec conj [vec/conj-dec] [vec/last-neg?] until
[5] 5 ◆ dec conj [vec/conj-dec] [vec/last-neg?] until
[5] 4 ◆ conj [vec/conj-dec] [vec/last-neg?] until
[5 4] ◆ [vec/conj-dec] [vec/last-neg?] until
[5 4] [vec/conj-dec] ◆ [vec/last-neg?] until
[5 4] [vec/conj-dec] [vec/last-neg?] ◆ until
[5 4] ◆ vec/last-neg? [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4] ◆ dup last 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4] [5 4] ◆ last 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4] 4 ◆ 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4] 4 0 ◆ <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4] false ◆ [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4] false [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] ◆ unless
[5 4] ◆ vec/conj-dec [vec/conj-dec] [vec/last-neg?] until
[5 4] ◆ dup last dec conj [vec/conj-dec] [vec/last-neg?] until
[5 4] [5 4] ◆ last dec conj [vec/conj-dec] [vec/last-neg?] until
[5 4] 4 ◆ dec conj [vec/conj-dec] [vec/last-neg?] until
[5 4] 3 ◆ conj [vec/conj-dec] [vec/last-neg?] until
[5 4 3] ◆ [vec/conj-dec] [vec/last-neg?] until
[5 4 3] [vec/conj-dec] ◆ [vec/last-neg?] until
[5 4 3] [vec/conj-dec] [vec/last-neg?] ◆ until
[5 4 3] ◆ vec/last-neg? [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3] ◆ dup last 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3] [5 4 3] ◆ last 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3] 3 ◆ 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3] 3 0 ◆ <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3] false ◆ [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3] false [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] ◆ unless
[5 4 3] ◆ vec/conj-dec [vec/conj-dec] [vec/last-neg?] until
[5 4 3] ◆ dup last dec conj [vec/conj-dec] [vec/last-neg?] until
[5 4 3] [5 4 3] ◆ last dec conj [vec/conj-dec] [vec/last-neg?] until
[5 4 3] 3 ◆ dec conj [vec/conj-dec] [vec/last-neg?] until
[5 4 3] 2 ◆ conj [vec/conj-dec] [vec/last-neg?] until
[5 4 3 2] ◆ [vec/conj-dec] [vec/last-neg?] until
[5 4 3 2] [vec/conj-dec] ◆ [vec/last-neg?] until
[5 4 3 2] [vec/conj-dec] [vec/last-neg?] ◆ until
[5 4 3 2] ◆ vec/last-neg? [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2] ◆ dup last 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2] [5 4 3 2] ◆ last 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2] 2 ◆ 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2] 2 0 ◆ <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2] false ◆ [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2] false [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] ◆ unless
[5 4 3 2] ◆ vec/conj-dec [vec/conj-dec] [vec/last-neg?] until
[5 4 3 2] ◆ dup last dec conj [vec/conj-dec] [vec/last-neg?] until
[5 4 3 2] [5 4 3 2] ◆ last dec conj [vec/conj-dec] [vec/last-neg?] until
[5 4 3 2] 2 ◆ dec conj [vec/conj-dec] [vec/last-neg?] until
[5 4 3 2] 1 ◆ conj [vec/conj-dec] [vec/last-neg?] until
[5 4 3 2 1] ◆ [vec/conj-dec] [vec/last-neg?] until
[5 4 3 2 1] [vec/conj-dec] ◆ [vec/last-neg?] until
[5 4 3 2 1] [vec/conj-dec] [vec/last-neg?] ◆ until
[5 4 3 2 1] ◆ vec/last-neg? [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2 1] ◆ dup last 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2 1] [5 4 3 2 1] ◆ last 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2 1] 1 ◆ 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2 1] 1 0 ◆ <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2 1] false ◆ [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2 1] false [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] ◆ unless
[5 4 3 2 1] ◆ vec/conj-dec [vec/conj-dec] [vec/last-neg?] until
[5 4 3 2 1] ◆ dup last dec conj [vec/conj-dec] [vec/last-neg?] until
[5 4 3 2 1] [5 4 3 2 1] ◆ last dec conj [vec/conj-dec] [vec/last-neg?] until
[5 4 3 2 1] 1 ◆ dec conj [vec/conj-dec] [vec/last-neg?] until
[5 4 3 2 1] 0 ◆ conj [vec/conj-dec] [vec/last-neg?] until
[5 4 3 2 1 0] ◆ [vec/conj-dec] [vec/last-neg?] until
[5 4 3 2 1 0] [vec/conj-dec] ◆ [vec/last-neg?] until
[5 4 3 2 1 0] [vec/conj-dec] [vec/last-neg?] ◆ until
[5 4 3 2 1 0] ◆ vec/last-neg? [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2 1 0] ◆ dup last 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2 1 0] [5 4 3 2 1 0] ◆ last 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2 1 0] 0 ◆ 0 <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2 1 0] 0 0 ◆ <= [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2 1 0] true ◆ [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] unless
[5 4 3 2 1 0] true [vec/conj-dec [vec/conj-dec] [vec/last-neg?] until] ◆ unless
[5 4 3 2 1 0] ◆
{ vec/last-neg? [dup last 0 <=]
vec/conj-dec [dup last dec conj]
vec/conj-inc [dup last inc conj]
vec/wrap [[] swap conj]
range [vec/wrap [vec/conj-dec] [vec/last-neg?] until] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment