Skip to content

Instantly share code, notes, and snippets.

@RauliL
Created August 27, 2017 16:48
Show Gist options
  • Save RauliL/d9a8627778b04c94cb3b0852ba96660d to your computer and use it in GitHub Desktop.
Save RauliL/d9a8627778b04c94cb3b0852ba96660d to your computer and use it in GitHub Desktop.
Range implementation in Plorth + Class framework
: class
swap class-create swap tuck "-prototype" + tuck const " new" + compile swap
>symbol swap >word define
;
: class-extends
"prototype" swap @ nip rot "__proto__" swap ! class
;
: class-create
"fields" swap has-own?
(
"fields" swap @ array?
(
(
tuck dup
# Create setter.
"!" swap + swap >string >source " swap !" + compile swap rot !
# Create getter.
swap dup "@" swap + swap >string >source " swap @" + compile swap rot !
)
swap for-each
"fields" swap delete
)
( drop )
if-else
)
if
"prototype" {} !
;
"./class" import
{
"fields": [
"current",
"end",
"stepper",
"comparator",
],
"constructor": (
!end
!current
"+" >symbol swap !stepper
"<" >symbol swap !comparator
),
"reverse-stepper": (
@stepper >string
"-" >symbol swap
"+" >symbol swap
"+" = ?
),
"reverse-comparator": (
@comparator >string
">" >symbol swap
"<" >symbol swap
"<" = ?
),
"reverse": (
@current swap @end rot rot !end !current
reverse-stepper swap !stepper
reverse-comparator swap !comparator
),
"has-next?": (
dup @current swap @end swap @comparator nip call
),
"next": (
has-next?
(
dup
@current swap @stepper nip 1 swap call
dup rot !current swap
)
( "Range has reached it's end." range-error throw )
if-else
)
} "range" class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment