From 8f695818932c745b07355b7a748897dd5e462b88 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Wed, 15 Mar 2023 14:06:57 -0400 Subject: [PATCH] Pair down generated js a bit --- ir.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/ir.py b/ir.py index c631699..532aad3 100644 --- a/ir.py +++ b/ir.py @@ -96,7 +96,7 @@ class IntPattern: return repr(self.value) def codegen(self, match_on: str) -> Tuple[Sequence[str], Collection[Tuple[str, str]]]: - return ((f'{match_on} === {self.value}',), tuple()) + return ((f'{match_on}=={self.value}',), tuple()) @dataclass(frozen=True) class SPattern: @@ -130,8 +130,8 @@ class SPattern: return 'S ' + repr(self.pred) def codegen(self, match_on: str) -> Tuple[Sequence[str], Collection[Tuple[str, str]]]: - pred_conditions, pred_bindings = self.pred.codegen(f'({match_on} - 1)') - return ((f'{match_on} > 0', *pred_conditions), pred_bindings) + pred_conditions, pred_bindings = self.pred.codegen(f'({match_on}-1)') + return ((f'{match_on}>0', *pred_conditions), pred_bindings) @dataclass(frozen=True) class ReplHole: @@ -239,28 +239,28 @@ class Function: return (':'.join( ( '&&'.join( - iter(pattern.codegen('$local_temp$')[0]) + iter(pattern.codegen('$')[0]) ) or '1' ) + '?' + ( '((' + ','.join( binding_name for (binding_name, binding_value) - in pattern.codegen('$local_temp$')[1] - ) + f') => ({branch.codegen()}))(' + ','.join( + in pattern.codegen('$')[1] + ) + f')=>({branch.codegen()}))(' + ','.join( binding_value for (binding_name, binding_value) - in pattern.codegen('$local_temp$')[1] + in pattern.codegen('$')[1] ) + ')' - if len(pattern.codegen('$local_temp$')[1]) else + if len(pattern.codegen('$')[1]) else branch.codegen() ) for (pattern, branch) in self.forms - ) or '0?undefined') + ':undefined' + ) or '0?[].b') + ':[].b' def codegen(self) -> str: - return '($local_temp$) => ' + self.codegen_inner() + return '$=>' + self.codegen_inner() def codegen_named(self, name) -> str: - return f'function {name}($local_temp$){{return {self.codegen_inner()}}}' + return f'function {name}($){{return {self.codegen_inner()}}}' def __repr__(self) -> str: return '{ ' + ', '.join('"' + repr(repr(p))[1:-1] + '" : ' + repr(e) for (p, e) in self.forms) + ' }' @@ -336,7 +336,10 @@ class Application: return f'[ {repr(self.first)}, {repr(self.arg)} ]' def codegen(self) -> str: - return f'({self.first.codegen()})({self.arg.codegen()})' + if isinstance(self.first, Function | Builtin) and self.arg.is_value(): + return unwrap_opt(self.first.try_apply(self.arg)).codegen() + else: + return f'({self.first.codegen()})({self.arg.codegen()})' @dataclass class Int: