Commit 0daaa65d authored by Christophe Benz's avatar Christophe Benz

Update transpiler (introduce lazy)

parent 0ccb5d18
......@@ -3,16 +3,29 @@
import math
from toolz.curried.operator import ge, gt, is_, ne
# M language functions
arr = round
inf = math.floor
null = is_(None)
positif = gt(0)
positif_ou_nul = ge(0)
present = ne(0)
def null(value):
return int(value == 0)
def positif(value):
return int(value > 0)
def positif_ou_nul(value):
return int(value >= 0)
def present(value):
return int(value != 0)
somme = sum
......@@ -15,7 +15,7 @@ import pprint
import textwrap
from m_language_parser.unloop_helpers import iter_unlooped_nodes
from toolz import mapcat
from toolz import interpose, mapcat
from . import core
......@@ -38,12 +38,22 @@ def visit_infix_expression(node, operators={}):
if value != UnboundLocalError:
yield index, value
tokens = (
tokens = [
visit_node(operand_or_operator)
if index == 0
else operators.get(operand_or_operator, operand_or_operator)
for index, operand_or_operator in interleave(node['operands'], node['operators'])
)
]
# Detect a specific case and transform it into a lazy expression in order to prevent a division by 0:
# a * b ... => a and a * b ...
if node['type'] == 'product_expression':
# last_operand_type = node['operands'][-1]['type']
# assert last_operand_type in ('function_call', 'integer', 'sum_expression', 'symbol'), last_operand_type
# if last_operand_type != 'integer':
tokens = interpose(
el='and',
seq=[visit_node(operand) for operand in node['operands'][:-1]] + [' '.join(map(str, tokens))],
)
return '({})'.format(' '.join(map(str, tokens)))
......
This diff is collapsed.
......@@ -214,16 +214,21 @@ def main():
filenames=['chap-*.json', 'res-ser*.json'],
),
)))
formulas_names = filter(
lambda formula_name: python_source_visitors.sanitized_variable_name(formula_name) in formula_source_by_name,
ordered_formulas_names,
)
# formulas_names = filter(
# lambda formula_name: python_source_visitors.sanitized_variable_name(formula_name) in formula_source_by_name,
# ordered_formulas_names,
# )
def get_formula_source(formula_name):
sanitized_formula_name = python_source_visitors.sanitized_variable_name(formula_name)
return formula_source_by_name[sanitized_formula_name] \
if sanitized_formula_name in formula_source_by_name \
else '# WARNING: the variable "{0}" is used in a formula at least, but is not defined.\n{0} = 0'.format(
sanitized_formula_name)
write_source_file(
file_name='formulas.py',
source=formulas_file_source(map(
lambda formula_name: formula_source_by_name[python_source_visitors.sanitized_variable_name(formula_name)],
formulas_names,
)),
source=formulas_file_source(map(get_formula_source, ordered_formulas_names)),
)
return 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment