furo-forth-stack

furo-forth-stack #

@furo/util v2.1.19
import '@furo/util/src/furo-forth-stack.js';
exports FuroForthStack js
exports <furo-forth-stack> custom-element-definition
superclass LitElement

summary forth like stack

furo-forth-stack is a declarative stack, inspired by the forth stack.

https://hackaday.com/2017/01/27/forth-the-hackers-language/ http://wiki.laptop.org/go/Forth_stack_operators http://galileo.phys.virginia.edu/classes/551.jvn.fall01/primer.htm#stacks

Attributes and Properties #

size #

default: 0

Current size of the stack

Events #

stack-size-changed #

at-stack-size-changedNumber

Fired when the stack size changes with Integer with the current size of the stack.

rotated #

at-rotatedthe top element

Fired when stack was rotated

stack-changed #

at-stack-changedthe top element

Fired when the stack contents changes after put, drop,…

swapped #

at-swappedvoid

Fired when stack was swapped

empty #

at-emptyvoid

Fired when stack gets empty

Methods #

clearStack #

clearStack() ⟹ void

*fn-clear-stack

Empties the stack and set the stack-size to 0



put #

put(e `` ) ⟹ void

`` fn-put

Add an element to the stack

  • e

swap #

swap() ⟹ void

*fn-swap

swap ( n1 n2 – n2 n1 )

swap, as you may have guessed, swaps the top two elements of the stack. For example:

1 2 3 4 swap will give you:

1 2 4 3 <- Top



drop #

drop() ⟹ void

*fn-drop

drop ( n – )

drop simply drops the top element of the stack. Running:

1 2 3 drop gives you a stack of:

1 2 <- Top



dup #

dup() ⟹ void

*fn-dup

dup ( n – n n )

dup is short for “duplicate” – it duplicates the top element of the stack. For example, try this out:

1 2 3 dup

You should end up with the following stack:

1 2 3 3 <- Top



over #

over() ⟹ void

*fn-over

over ( n1 n2 – n1 n2 n1 )

over is a bit less obvious: it takes the second element from the top of the stack and duplicates it to the top of the stack. Running this:

1 2 3 over will result in this:

1 2 3 2 <- Top



rot #

rot() ⟹ void

*fn-rot

rot ( n1 n2 n3 – n2 n3 n1 )

Finally, rot “rotates” the top three elements of the stack. The third element from the top of the stack gets moved to the top of the stack, pushing the other two elements down.

1 2 3 rot gives you:

2 3 1 <- Top



rrot #

rrot() ⟹ void

*fn-rrot

rrot ( n1 n2 n3 – n3 n1 n2 )

Reverse rotation or right rotation rrot “rotates” the elements of the stack inverse to rot. The top elemen the stack gets moved to the bottom of the stack.

1 2 3 rot gives you:

3 1 2 <- Top