Pair down generated js a bit
This commit is contained in:
parent
cdf33bd6da
commit
8f69581893
27
ir.py
27
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:
|
||||
|
|
Loading…
Reference in a new issue