Skip to content

Instantly share code, notes, and snippets.

@okram
Last active March 13, 2020 11:03
Show Gist options
  • Save okram/d9609b0468ef203f8128e216e997c7c0 to your computer and use it in GitHub Desktop.
Save okram/d9609b0468ef203f8128e216e997c7c0 to your computer and use it in GitHub Desktop.
// general type casting w/ [as]
mmlang> 5[as,str]
==>'5'
mmlang> '5'[as,int]
==>5
mmlang> '5'[as,int][as,str]
==>'5'
// create records with [as] and traverser state
mmlang> int[plus,1]<x>[mult,10]<y>[as,rec['X'-><x>,'Y'-><y>,'Z'->[as,rec['XX' -> <x> + <y>]]]]
==>rec['X'-><x>,'Y'-><y>,'Z'->[as,rec['XX'-><x>[plus,<y>]]]]<=int[plus,1]<x>[mult,10]<y>[as,rec['X'-><x>,'Y'-><y>,'Z'->[as,rec['XX'-><x>[plus,<y>]]]]]
mmlang> 5[plus,1]<x>[mult,10]<y>[as,rec['X'-><x>,'Y'-><y>,'Z'->[as,rec['XX' -> <x> + <y>]]]]
==>['X'->6,'Y'->60,'Z'->['XX'->66]]
// aligned version
int[plus,1]<x>[mult,10]<y>[as,rec['X'-><x>,
'Y'-><y>,
'Z'->[as,rec['XX' -> <x> + <y>]]]]
// [as] is so important that it has a shorthand
mmlang> int[plus,3]str[plus,'4']
==>str<=int[plus,3][as,str][plus,'4']
mmlang> 5[plus,3]str[plus,'4']
==>'84'
// [a] type checks (? is its infix notation)
mmlang> 1[a,bool]
==>false
mmlang> 1?int
==>true
// [is] is the only inst whose opcode is also its infix operator
mmlang> int[is?int -> +10 | obj -> [id]]
==>int[choose,[int{?}<=int[is,bool<=int[a,int]]->int[plus,10]|int->int[id]]]
mmlang> 5[is?int -> +10 | obj -> [id]]
==>15
mmlang> '5'[is?int -> +10 | obj -> [id]]
==>'5'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment