Commit d05c8212 authored by Christophe Benz's avatar Christophe Benz

Update transpiler

parent fe27105b
from .generated.chap_1 import *
from .generated.chap_2 import *
......@@ -42,15 +42,17 @@ def value_to_python_source(value):
return pprint.pformat(value, indent=4, width=120)
def write_source(file_name, source):
def write_source(file_name, original_files, source):
header = """\
# -*- coding: utf-8 -*-
# WARNING: This file is automatically generated by a script. No not modify it by hand!
# Original files are "{}"
"""
""".format(original_files)
global args
file_path = os.path.join(generated_dir_path, file_name)
with open(file_path, 'w') as output_file:
......@@ -147,6 +149,17 @@ def float_to_python_source(node):
return node['value']
def formula_to_python_source(node):
expression_source = node_to_python_source(node['expression'])
symbols = []
find_node(node['expression'], out=symbols, type='symbol')
return 'def {name}({parameters}):\n return {expression}\n\n'.format(
expression=expression_source,
name=node['name'],
parameters=', '.join(sorted(set(symbol['value'] for symbol in symbols))),
)
def function_call_to_python_source(node):
return '{name}({arguments})'.format(
arguments=', '.join(map(node_to_python_source, node['arguments'])),
......@@ -163,24 +176,16 @@ def loop_expression_to_python_source(node):
return node_to_python_source(node['expression'])
def pour_formula_to_python_source(node):
import ipdb; ipdb.set_trace()
def product_expression_to_python_source(node):
return infix_expression_to_python_source(node)
def regle_to_python_source(node):
def iter_formulas():
for formula_node in node['formulas']:
assert formula_node['type'] == 'formula', formula_node
expression_node = formula_node['expression']
expression_source = node_to_python_source(expression_node)
symbols = []
find_node(expression_node, out=symbols, type='symbol')
yield 'def {name}({parameters}):\n return {expression}\n\n'.format(
expression=expression_source,
name=formula_node['name'],
parameters=', '.join(sorted(set(symbol['value'] for symbol in symbols))),
)
return '\n'.join(iter_formulas())
return '\n'.join(map(node_to_python_source, node['formulas']))
def sum_expression_to_python_source(node):
......@@ -202,18 +207,18 @@ def ternary_operator_to_python_source(node):
# File transpilation functions
def chap_1_to_python_source(application):
def chap_to_python_source(application, json_file_name):
regle_nodes = list(filter(
lambda node: application in node['applications'],
read_ast_json_file(os.path.join(args.json_dir, 'chap-1.json')),
read_ast_json_file(os.path.join(args.json_dir, json_file_name)),
))
source = '\n'.join(map(node_to_python_source, regle_nodes))
formulas_names = [
formula_node['name']
for regle_node in regle_nodes
for formula_node in regle_node['formulas']
]
assert len(formulas_names) == len(set(formulas_names)), formulas_names
source = '\n'.join(map(node_to_python_source, regle_nodes))
return source
......@@ -243,14 +248,16 @@ def main():
variable_definition_by_name = tgvH_json_to_python_source()
write_source(
file_name='variables_definitions.py',
original_files='tgvH.json and tgvH.m',
source='variable_definition_by_name = ' + value_to_python_source(variable_definition_by_name),
)
chap_1_source = chap_1_to_python_source(args.application)
write_source(
file_name='chap_1.py',
source=chap_1_source,
)
for index in range(1, 4):
write_source(
file_name='chap_{}.py'.format(index),
original_files='chap-{index}.json and chap-{index}.m'.format(index=index),
source=chap_to_python_source(application=args.application, json_file_name='chap-{}.json'.format(index)),
)
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