๐ A Joy / Forth hybrid implemented in JS
/* inline comments */
\ comments
"string literals"
-123.45
"double" [ 2 * ] ;
"quadruple" [ double double ] ;
2 % 0 = [ "even" ] [ "odd" ] cond log
"foo" [ ( a b c ) a b + c b + c a ]
[ [ 15 divby => "FizzBuzz" log ] match
[ 5 divby => "Buzz" log ] match
[ 3 divby => "Fizz" log ] match
log ]
word | effect |
---|---|
drop | a -- () |
swap | a b -- b a |
dup | a -- a a |
rot | a b c -- b c a |
-rot | a b c -- c a b |
over | a b -- a b a |
nip | a b -- b |
tuck | a b -- b a b |
( a b -- a <> b )
-
-
- / mod rem div min max
-
( a -- a' ) 0- +1 -1 floor ceil round abs
clamp ( a min max -- a' )
( a b -- bool ) < <= >= > = != ( strict value equality ) === !== ( js reference equality ) loose= loose!= ( js loose equality )
( a min max -- bool ) <range< <=range< <range<= <=range<=
true false and ( a b -- a && b ) or ( a b -- a || b ) not ( a -- !a ) if ( bool [ifTrue] -- ...ifTrue | () ) cond ( bool [ifTrue] [ifFalse] -- ...ifTrue | ...ifFalse )
( str -- str' ) -trim- -trim trim- upcase downcase
( str pad size -- str' ) +pad pad+
replace ( str match replace -- str' ) matches ( str match -- [matches] ( returns [] for no match ) ) match? ( str match -- bool )
r[] ( string -- regex ) ( match one char in set ) r[^] ( string -- regex ) ( match one char not in set ) r- ( start end -- regex ) ( regex matching one char in range ) \w \d \b \s \W \D \B \S r. r$ r^ r+ r* r? r| r~ ( invert selection: \w r~ => \W ; "abc" r[] => "abc" r[^] ) ( capture groups are in regular brackets )
re ( [matchers] -- regex)
example: match email
[ \S r+ "@" \S r+ "." \S r+ ] re
~> /\S+@\S+\.\S/
[ "foo" "bar" r| ":" [ \w r+ ] ] re
~> /(foo|bar):(\w+)
num>str ( num -- str ) str>num ( str -- num ) num>fixed ( num precision -- str )
$0 $1 $2 ... copy that item on the stack ( aliases for <n> pick
)
delimiters: [ ]
[] ( empty stack )
push ( stk a -- stk' )
concat ( stk1 stk2 -- stk' )
top ( stk -- a)
rest ( stk -- 'stk )
split ( stk -- rest top )
in ( stk joiner -- stk' ) [ 1 2 3 ] [ 1 + ] in
=> [ 1 1 + 2 1 + 3 1 + ]
flatten ( stk -- stk' ) [ 1 2 + ] flatten
=> [ 3 ]
0 [ 1 + ] flatten
=> error
fold ( stk head joiner -- ... ) [ 1 2 3 ] [ 0 ] [ + ]
=> 6
map ( stk joiner -- stk' ) [ 1 2 3 ] [ 1 + ] map
=> [ 2 3 4 ]
0 var: #flatten : any? ( a stk -- a stk ok? ) over #flatten != ; : done ( a stk -- stk ) swap drop ; : flatten #flatten swap exec [] begin any? while cons repeat done ;