Last active
April 23, 2020 00:33
-
-
Save johnchandlerburnham/9284890adfb07ff82512329af262ccf9 to your computer and use it in GitHub Desktop.
Compiling a parser to JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Parse.test : String | |
// Parse.parse<String> | |
// | Parse.tokens("a"); | |
// | "a"; | |
module.exports = (function (){ | |
var F64 = new Float64Array(1); | |
var U32 = new Uint32Array(F64.buffer); | |
var F64_get = (x,i)=>((F64[0]=x),(i<32?(U32[0]>>>i)&1:(U32[1]>>>(i-32)&1))); | |
var F64_set = (x,i)=>((F64[0]=x),(i<32?(U32[0]=U32[0]|(1<<i)):(U32[1]=U32[1]|(1<<(i-32)))),F64[0]); | |
var inst_unit = x=>x(1); | |
var elim_unit = x=>v=>v; | |
var inst_bool = x=>x(true)(false); | |
var elim_bool = x=>t=>f=>x?t:f; | |
var inst_nat = x=>x(0n)(p=>1n+p); | |
var elim_nat = x=>z=>s=>x===0n?z:s(x-1n); | |
var inst_bits = x=>x('')(p=>'0'+p)(p=>'1'+p); | |
var elim_bits = x=>be=>b0=>b1=>(x.length?(x[0]==='0'?b0(x.slice(1)):b1(x.slice(1))):be); | |
var inst_u16 = x=>x(w=>(function R(x,k){return x(0)(p=>R(p,k*2))(p=>k+R(p,k*2))})(w,1)); | |
var elim_u16 = x=>u=>u((function R(i){return we=>w0=>w1=>i===16?we:((x>>>i)&1?w1:w0)(R(i+1))})(0)); | |
var inst_u32 = x=>x(w=>(function R(x,k){return x(0)(p=>R(p,k*2))(p=>k+R(p,k*2))})(w,1)); | |
var elim_u32 = x=>u=>u((function R(i){return we=>w0=>w1=>i===32?we:((x>>>i)&1?w1:w0)(R(i+1))})(0)); | |
var inst_f64 = x=>x(w=>(function R(x,i){return x(0)(p=>R(p,i+1))(p=>F64_set(R(p,i+1),i))})(w,0)); | |
var elim_f64 = x=>u=>u((function R(i){return we=>w0=>w1=>i===64?we:(F64_get(x,i)?w1:w0)(R(i+1))})(0)); | |
var inst_string = x=>x('')(h=>t=>String.fromCharCode(h)+t); | |
var elim_string = x=>n=>c=>x===''?n:c(x.charCodeAt(0))(x.slice(1)); | |
var $Parser$Reply = ($S=>($E=>($A=>(void 0)))); | |
var $Parser$Reply$empty_value = ($state=>($value=>($empty_value=>($=>($=>($=>$empty_value($state)($value))))))); | |
var $Parser$Reply$empty_error = ($state=>($error=>($=>($empty_error=>($=>($=>$empty_error($state)($error))))))); | |
var $Parser$Reply$eaten_value = ($state=>($value=>($=>($=>($eaten_value=>($=>$eaten_value($state)($value))))))); | |
var $Parser$Reply$eaten_error = ($state=>($error=>($=>($=>($=>($eaten_error=>$eaten_error($state)($error))))))); | |
var $Parser$run = ($p=>($s=>$p($s)($Parser$Reply$empty_value)($Parser$Reply$empty_error)($Parser$Reply$eaten_value)($Parser$Reply$eaten_error))); | |
var $The = ($A=>($x=>(void 0))); | |
var $Parser$Error = ($E=>(void 0)); | |
var $Parser$type = ($S=>($E=>($A=>($reply=>(void 0))))); | |
var $The$term = ($x=>($term=>$term($x))); | |
var $Parser$parse = ($p=>($s=>$Parser$run($p)($s)(($=>($x=>$x)))(($=>($e=>$The$term($e))))(($=>($x=>$x)))(($=>($e=>$The$term($e)))))); | |
var $Nat$zero = inst_nat(($z=>($s=>$z))); | |
var $List$nil = ($nil=>($cons=>$nil)); | |
var $Parser$State$default = ($input=>($state=>($make=>$make($input)($Nat$zero)($List$nil)($state)))); | |
var $Unit$new = inst_unit(($new=>$new)); | |
var $Parse$State$default = ($input=>$Parser$State$default($input)($Unit$new)); | |
var $Parse$parse = ($p=>($s=>$Parser$parse($p)($Parse$State$default($s)))); | |
var $Nat$succ = ($n=>inst_nat(($z=>($s=>$s($n))))); | |
var $String$length = ($str=>elim_string($str)($Nat$zero)(($head=>($tail=>$Nat$succ($String$length($tail)))))); | |
var $Maybe = ($A=>(void 0)); | |
var $Pair = ($A=>($B=>(void 0))); | |
var $Maybe$some = ($value=>($=>($some=>$some($value)))); | |
var $Pair$new = ($a=>($b=>($pair=>$pair($a)($b)))); | |
var $String$nil = inst_string(($nil=>($=>$nil))); | |
var $Maybe$none = ($none=>($=>$none)); | |
var $String$cons = ($head=>($tail=>inst_string(($=>($cons=>$cons($head)($tail)))))); | |
var $String$take_n$aux = ($n=>($str=>($res=>elim_string($res)(elim_nat($n)($Maybe$some($Pair$new($str)($String$nil)))(($=>$Maybe$none)))(($head=>($tail=>elim_nat($n)($Maybe$some($Pair$new($str)($String$nil)))(($pred=>$String$take_n$aux($pred)($String$cons($head)($str))($tail))))))))); | |
var $String$reverse$go = ($str=>($res=>elim_string($str)($res)(($str$head=>($str$tail=>$String$reverse$go($str$tail)($String$cons($str$head)($res))))))); | |
var $String$reverse = ($str=>$String$reverse$go($str)($String$nil)); | |
var $String$take_n = ($n=>($str=>$String$take_n$aux($n)($String$nil)($str)($Maybe$none)(($p=>$p(($x=>($y=>$Maybe$some($Pair$new($String$reverse($x))($y))))))))); | |
var $Parser$Error$Item$end_of_input = ($=>($=>($end_of_input=>$end_of_input))); | |
var $List$cons = ($head=>($tail=>($nil=>($cons=>$cons($head)($tail))))); | |
var $List$single = ($x=>$List$cons($x)($List$nil)); | |
var $Parser$Error$Item$label = ($xs=>($=>($label=>($=>$label($xs))))); | |
var $Parser$Error$make_trivial = ($position=>($unexpect=>($expected=>($trivial=>($=>$trivial($position)($unexpect)($expected)))))); | |
var $Parser$State$make = ($input=>($index=>($error=>($state=>($make=>$make($input)($index)($error)($state)))))); | |
var $Nat$add = a=>b=>a+b; | |
var $Bool$true = inst_bool(($t=>($f=>$t))); | |
var $Bool$false = inst_bool(($t=>($f=>$f))); | |
var $Bool$and = a=>b=>a&&b; | |
var $Cmp$ltn = ($ltn=>($eql=>($gtn=>$ltn))); | |
var $Cmp$gtn = ($ltn=>($eql=>($gtn=>$gtn))); | |
var $Word$cmp$aux = ($a=>($b=>($c=>$a(($b=>$c))(($a$pred=>($b=>$b(($a$pred=>$c))(($b$pred=>($a$pred=>$Word$cmp$aux($a$pred)($b$pred)($c))))(($b$pred=>($a$pred=>$Word$cmp$aux($a$pred)($b$pred)($Cmp$ltn))))($a$pred))))(($a$pred=>($b=>$b(($a$pred=>$c))(($b$pred=>($a$pred=>$Word$cmp$aux($a$pred)($b$pred)($Cmp$gtn))))(($b$pred=>($a$pred=>$Word$cmp$aux($a$pred)($b$pred)($c))))($a$pred))))($b)))); | |
var $Cmp$eql = ($ltn=>($eql=>($gtn=>$eql))); | |
var $Word$cmp = ($a=>($b=>$Word$cmp$aux($a)($b)($Cmp$eql))); | |
var $Word$eql = ($a=>($b=>$Word$cmp($a)($b)($Bool$false)($Bool$true)($Bool$false))); | |
var $Nat$0 = $Nat$zero; | |
var $Nat$1 = $Nat$succ($Nat$0); | |
var $Nat$2 = $Nat$succ($Nat$1); | |
var $Nat$3 = $Nat$succ($Nat$2); | |
var $Nat$4 = $Nat$succ($Nat$3); | |
var $Nat$5 = $Nat$succ($Nat$4); | |
var $Nat$6 = $Nat$succ($Nat$5); | |
var $Nat$7 = $Nat$succ($Nat$6); | |
var $Nat$8 = $Nat$succ($Nat$7); | |
var $Nat$9 = $Nat$succ($Nat$8); | |
var $Nat$10 = $Nat$succ($Nat$9); | |
var $Nat$11 = $Nat$succ($Nat$10); | |
var $Nat$12 = $Nat$succ($Nat$11); | |
var $Nat$13 = $Nat$succ($Nat$12); | |
var $Nat$14 = $Nat$succ($Nat$13); | |
var $Nat$15 = $Nat$succ($Nat$14); | |
var $Nat$16 = $Nat$succ($Nat$15); | |
var $U16$eql = a=>b=>a===b; | |
var $String$eql = ($a=>($b=>elim_string($a)(elim_string($b)($Bool$true)(($b$head=>($b$tail=>$Bool$false))))(($a$head=>($a$tail=>elim_string($b)($Bool$false)(($b$head=>($b$tail=>$Bool$and($U16$eql($a$head)($b$head))($String$eql($a$tail)($b$tail)))))))))); | |
var $Parser$tokens = ($str=>($s1=>($empty_value=>($empty_error=>($eaten_value=>($eaten_error=>$s1(($input=>($index=>($error=>($state=>($len=>$String$take_n($len)($input)(($unex=>($expe=>$empty_error($s1)($Parser$Error$make_trivial($Nat$zero)($unex)($expe)))($List$single($Parser$Error$Item$label($str))))($Maybe$some($Parser$Error$Item$end_of_input)))(($p=>$p(($took=>($rest=>($s2=>elim_bool($String$eql($str)($took))(elim_string($str)($empty_value($s2)($str))(($=>($=>$eaten_value($s2)($str)))))(($unex_label=>($expe_label=>($unex=>($expe=>$empty_error($s1)($Parser$Error$make_trivial($index)($unex)($expe)))($List$single($expe_label)))($Maybe$some($unex_label)))($Parser$Error$Item$label($str)))($Parser$Error$Item$label($took))))($Parser$State$make($rest)($Nat$add($index)($len))($error)($state))))))))($String$length($str))))))))))))); | |
var $Parse$tokens = ($str=>$Parser$tokens($str)); | |
var $Word$0 = ($wo=>($we=>($w0=>($w1=>$w0($wo))))); | |
var $Word$1 = ($wo=>($we=>($w0=>($w1=>$w1($wo))))); | |
var $Word$nil = ($we=>($w0=>($w1=>$we))); | |
var $U16$new = ($a=>inst_u16(($u16=>$u16($a)))); | |
var $Char$new = ($b0=>($b1=>($b2=>($b3=>($b4=>($b5=>($b6=>($b7=>($b8=>($b9=>($bA=>($bB=>($bC=>($bD=>($bE=>($bF=>($kF=>($kE=>($kD=>($kC=>($kB=>($kA=>($k9=>($k8=>($k7=>($k6=>($k5=>($k4=>($k3=>($k2=>($k1=>($k0=>($k_=>$U16$new($k0($k1($k2($k3($k4($k5($k6($k7($k8($k9($kA($kB($kC($kD($kE($kF($k_))))))))))))))))))($Word$nil))($bF($Word$0)($Word$1)))($bE($Word$0)($Word$1)))($bD($Word$0)($Word$1)))($bC($Word$0)($Word$1)))($bB($Word$0)($Word$1)))($bA($Word$0)($Word$1)))($b9($Word$0)($Word$1)))($b8($Word$0)($Word$1)))($b7($Word$0)($Word$1)))($b6($Word$0)($Word$1)))($b5($Word$0)($Word$1)))($b4($Word$0)($Word$1)))($b3($Word$0)($Word$1)))($b2($Word$0)($Word$1)))($b1($Word$0)($Word$1)))($b0($Word$0)($Word$1)))))))))))))))))); | |
var $Bit$0 = ($o=>($i=>$o)); | |
var $Bit$1 = ($o=>($i=>$i)); | |
var $Parse$test = $Parse$parse($Parse$tokens("a"))("a"); | |
return { | |
'Parser.Reply': $Parser$Reply, | |
'Parser.Reply.empty_value': $Parser$Reply$empty_value, | |
'Parser.Reply.empty_error': $Parser$Reply$empty_error, | |
'Parser.Reply.eaten_value': $Parser$Reply$eaten_value, | |
'Parser.Reply.eaten_error': $Parser$Reply$eaten_error, | |
'Parser.run': $Parser$run, | |
'The': $The, | |
'Parser.Error': $Parser$Error, | |
'Parser.type': $Parser$type, | |
'The.term': $The$term, | |
'Parser.parse': $Parser$parse, | |
'Nat.zero': $Nat$zero, | |
'List.nil': $List$nil, | |
'Parser.State.default': $Parser$State$default, | |
'Unit.new': $Unit$new, | |
'Parse.State.default': $Parse$State$default, | |
'Parse.parse': $Parse$parse, | |
'Nat.succ': $Nat$succ, | |
'String.length': $String$length, | |
'Maybe': $Maybe, | |
'Pair': $Pair, | |
'Maybe.some': $Maybe$some, | |
'Pair.new': $Pair$new, | |
'String.nil': $String$nil, | |
'Maybe.none': $Maybe$none, | |
'String.cons': $String$cons, | |
'String.take_n.aux': $String$take_n$aux, | |
'String.reverse.go': $String$reverse$go, | |
'String.reverse': $String$reverse, | |
'String.take_n': $String$take_n, | |
'Parser.Error.Item.end_of_input': $Parser$Error$Item$end_of_input, | |
'List.cons': $List$cons, | |
'List.single': $List$single, | |
'Parser.Error.Item.label': $Parser$Error$Item$label, | |
'Parser.Error.make_trivial': $Parser$Error$make_trivial, | |
'Parser.State.make': $Parser$State$make, | |
'Nat.add': $Nat$add, | |
'Bool.true': $Bool$true, | |
'Bool.false': $Bool$false, | |
'Bool.and': $Bool$and, | |
'Cmp.ltn': $Cmp$ltn, | |
'Cmp.gtn': $Cmp$gtn, | |
'Word.cmp.aux': $Word$cmp$aux, | |
'Cmp.eql': $Cmp$eql, | |
'Word.cmp': $Word$cmp, | |
'Word.eql': $Word$eql, | |
'Nat.0': $Nat$0, | |
'Nat.1': $Nat$1, | |
'Nat.2': $Nat$2, | |
'Nat.3': $Nat$3, | |
'Nat.4': $Nat$4, | |
'Nat.5': $Nat$5, | |
'Nat.6': $Nat$6, | |
'Nat.7': $Nat$7, | |
'Nat.8': $Nat$8, | |
'Nat.9': $Nat$9, | |
'Nat.10': $Nat$10, | |
'Nat.11': $Nat$11, | |
'Nat.12': $Nat$12, | |
'Nat.13': $Nat$13, | |
'Nat.14': $Nat$14, | |
'Nat.15': $Nat$15, | |
'Nat.16': $Nat$16, | |
'U16.eql': $U16$eql, | |
'String.eql': $String$eql, | |
'Parser.tokens': $Parser$tokens, | |
'Parse.tokens': $Parse$tokens, | |
'Word.0': $Word$0, | |
'Word.1': $Word$1, | |
'Word.nil': $Word$nil, | |
'U16.new': $U16$new, | |
'Char.new': $Char$new, | |
'Bit.0': $Bit$0, | |
'Bit.1': $Bit$1, | |
'Parse.test': $Parse$test, | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment