Skip to content

Stack Machine

Jayson Harshbarger edited this page Apr 17, 2016 · 7 revisions

Core words

get-log-level

gets the current logging level

( -> {string} )

set-log-level

sets the current logging level

( {string} -> )

q<

moves the top of the stack to the tail of the queue

( {any} -> )

q>

moves the tail of the queue to the top of the stack

( -> {any} )

stack

replaces the stack with a quote containing the current stack

( ... -> [ ... ] )

f♭> 1 2 3 stack
[ [ 1 2 3 ] ]

d++

increments the internal d counter, if d > 0 words are pushed to the stack as literals

f♭> d++ drop :d--
[ drop ]

d--

decrements the d counter

quote

pushes a quotation maker onto the stack

( -> #( )

dequote

collects stack items upto the last quote marker

( #( ... -> [ ... ] )

depth

pushes the size of the current stack

( -> {number} )

f♭> 1 2 3 depth
[ 1 2 3 3 ]

load

loads and runs a ff or json file

( {string} -> {any} )

Stack words

<- (stack)

replaces the stack with the item found at the top of the stack

( [A] -> A )

f♭> 1 2 [ 3 4 ] <-
[ 3 4 ]

-> (queue)

replaces the queue with the item found at the top of the stack

( [A] -> )

f♭> 1 2 [ 3 4 ] -> 5 6
[ 1 2 3 4 ]

undo

restores the stack to state before previous eval

auto-undo

set flag to auto-undo on error ( {boolean} -> )

Dictionary words

;

defines a word

( {string|atom} [A] -> )

f♭> sqr: [ dup * ] ;
[ ]

define

defines a set of words from an object

( {object} -> )

f♭> { sqr: "dup *" } define
[ ]

sto

stores a quote in the dictionary

( [A] {string|atom} -> )

f♭> [ dup * ] "sqr" sto
[ ]

def

defines a word

( [A] {string|atom} -> )

f♭> [ dup * ] "sqr" def
[ ]

memoize

memoize a defined word

( {string|atom} -> )

delete

deletes a defined word

( {string|atom} -> )

rcl

recalls the definion of a word

( {string|atom} -> [A] )

f♭> "sqr" rcl
[ [ dup * ] ]

expand

expand a quote

( [A] -> [a b c])

f♭> [ sqr ] expand
[ [ dup * ] ]

see

recalls the definition of a word as a formatted string

( {string|atom} -> {string} )

f♭> "sqr" see
[ '[ dup * ]' ]

words

returns a list of defined words

( -> {array} )

clr

clears the stack

( ... -> )

f♭> 1 2 3 clr
[  ]

\\

push the top of the queue to the stack

( -> {any} )

module functions, experimental

set-module

get-module

unset-module

sesssave

saves the current stack and dictionary as a JSON file

Child tasks

fork

evalues the quote in a child environment

( [A] -> [a] )

f♭> [ 1 2 * ] fork
[ [ 2 ] ]

spawn

evalues the quote in a child environment, returns a future

( [A] -> {future} )

await

evalues the quote in a child environment, waits for result

( [A] -> [a] )

send

pushes one element from stack to parent.

( A -> )

f♭> [ 1 2 3 send 4 ] fork
[ 3 [ 1 2 4 ] ]

return

pushes current stack to parent

( ... -> )

f♭> [ 1 2 3 return 4 ] fork
[ 1 2 3 [ 4 ] ]

suspend

stops execution, push queue to stack, loses other state

( ... -> )

f♭> [ 1 2 * suspend 3 4 *  ] fork
[ [ 2 3 4 * ] ]

all

executes each element in a child environment

( [ A B C ]-> [ [a] [b] [c] ])

race

executes each element in a child environment, returns first to finish

( [ A B C ]-> [x])