Skip to content

Instantly share code, notes, and snippets.

@belisarius222
Last active August 26, 2018 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belisarius222/d4a6c23f0a61972e63401b30b97152ce to your computer and use it in GitHub Desktop.
Save belisarius222/d4a6c23f0a61972e63401b30b97152ce to your computer and use it in GitHub Desktop.
Nock CPS transform
|%
+$ nock-func $-(subject=* result=*)
+$ cps-formula $-([subject=* next=nock-func] result=*)
--
|%
++ nock-to-cps
|= fol=nock
^- cps-formula
::
?- fol
[b=^ c=^]
=/ hed-cps=cps-formula $(fol b)
=/ tal-cps=cps-formula $(fol c)
::
|= [a=* next=nock-func]
::
%+ hed-cps a
|= hed-result=*
::
%+ tal-cps a
|= tal-result=*
::
(next [hed-result tal-result])
::
[%0 b=@]
|= [a=* next=nock-func]
::
(next (value-at-axis a b))
::
::
[%1 b=*]
|= [a=* next=nock-func]
(next b)
::
[%2 b=nock c=nock]
=/ cons-cps $(fol [b c])
::
|= [a=* next=nock-func]
::
%+ cons-cps a
|= cons-result=*
::
?> ?=(^ cons-result)
=/ new-subject=* -.cons-result
=/ new-formula=* +.cons-result
::
=/ new-cps-formula ^^$(fol new-formula)
::
(new-cps-formula new-subject next)
::
[%3 b=nock]
|= [a=* next=nock-func]
(next .?(a))
::
[%4 b=nock]
|= [a=* next=nock-func]
(next .+(a))
::
[%5 b=nock]
|= [a=* next=nock-func]
(next .=(-.a +.a))
::
[%6 b=nock c=nock d=nock]
=/ cond-cps $(fol b)
=/ then-cps $(fol c)
=/ else-cps $(fol d)
::
|= [a=* next=nock-func]
::
%+ cond-cps a
|= cond-result=*
::
?: =(%.y cond-result)
(then-cps a next)
(else-cps a next)
::
[%7 b=nock c=nock]
=/ hed-cps=cps-formula $(fol b)
=/ tal-cps=cps-formula $(fol c)
::
|= [a=* next=nock-func]
::
%+ hed-cps a
|= a-prime=*
::
(tal-cps a-prime next)
::
[%8 b=nock c=nock]
=/ b-cps $(fol b)
=/ c-cps $(fol c)
::
|= [a=* next=nock-func]
::
%+ b-cps a
|= b-result=*
::
(c-cps [b-cps a] next)
::
[%9 b=@ c=nock]
=/ c-cps $(fol c)
::
|= [a=* next=nock-func]
::
%+ c-cps a
|= core=*
::
=/ arm-formula (value-at-axis core b)
::
=/ arm-cps ^^$(fol arm-formula)
::
(arm-cps core next)
::
[%10 b=* c=nock]
$(fol c) :: TODO actually handle hints
::
[%11 b=nock c=nock]
=/ b-cps $(fol b)
=/ c-cps $(fol c)
::
|= [a=* next=nock-func]
::
%+ b-cps a
|= b-result=*
::
%+ c-cps a
|= c-result=*
::
(next .^(* b-result c-result) :: TODO handle async
==
++ value-at-axis
|= [a=* b=@]
::
?: =(0 b)
!!
?: =(1 b)
a
=/ parent-cell $(b (div b 2))
?: =(0 (mod b 2))
-.parent-cell
+.parent-cell
--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment