Tried and failed to add constraints, I'm going to try a different method

Took 11 minutes
This commit is contained in:
ayrton 2023-11-25 09:03:30 -07:00
parent db38110622
commit 6d36c5c967
2 changed files with 73 additions and 4 deletions

View file

@ -3,6 +3,7 @@ import warnings
from english_words import get_english_words_set
from ast_generator.tiny_py_unparser import TinyPyUnparser
from ast_generator.utils import *
from ast_generator.utils import filter_options, _choose_option
from ast_parser.python_unparser import PythonUnparser
@ -67,7 +68,7 @@ class AstGenerator:
settings=self.settings))
self.bool_unary = ['not']
self.float_op_options, self.float_op_cutoffs, self.float_op_numline = (
get_numberlines('expression-weights', ['brackets', 'arithmetic', 'unary'], [[], [], ['not']],
get_numberlines('expression-weights', ['brackets', 'arithmetic', 'unary'], [[], ['modulo'], ['not']],
self.settings))
self.float_unary = ['negation', 'noop']
self.char_op_options, self.char_op_cutoffs, self.char_op_numline = (
@ -335,8 +336,10 @@ class AstGenerator:
# Gnereate lhs and rhs
self.generate_xhs(GAZ_LHS_TAG, op_type, constraint)
self.py_unparser = PythonUnparser(self.current_ast_element.find(GAZ_LHS_TAG), True)
self.py_unparser = TinyPyUnparser(self.current_ast_element.find(GAZ_LHS_TAG), True)
self.py_unparser.unparse()
print(self.py_unparser.source)
self.generate_xhs(GAZ_RHS_TAG, op_type, constraint=(op, eval(self.py_unparser.source)))
@ -378,7 +381,7 @@ class AstGenerator:
return element
def generate_unary(self, op, op_type=ANY_TYPE):
def generate_unary(self, op, op_type=ANY_TYPE, constraint=None):
"""
@brief Generate a unary operation
@ -391,7 +394,7 @@ class AstGenerator:
]
self.make_element(GAZ_UNARY_OPERATOR_TAG, args)
self.generate_xhs(GAZ_RHS_TAG, op_type)
self.generate_xhs(GAZ_RHS_TAG, op_type, constraint)
self.current_ast_element = parent

View file

@ -0,0 +1,66 @@
from ast_parser.general_unparser import GeneralUnparser
from ast_parser.python_unparser import PythonUnparser, to_python_type, to_python_op
from constants import GAZ_TY_KEY, GAZ_TRUE_BLOCK_TAG, GAZ_FALSE_BLOCK_TAG
import xml.etree.ElementTree as ET
class TinyPyUnparser(GeneralUnparser):
def __init__(self, ast: ET.Element, debug=False):
super().__init__(ast, debug,
endline='\n',
outstream_begin_delimiter="gprint(",
outstream_end_delimiter=", end='')",
function_return_type_indicator_predicate="->",
loop_start_delimiter="while ",
loop_end_delimiter=":",
conditional_case_delimiter="elif ",
conditional_start_delimiter="if ",
conditional_else_delimiter="else:",
conditional_end_delimiter=":",
block_start_delimiter="",
block_end_delimiter="", # TODO can this contain the pass?
strip_conditionals=True)
def format_variable(self, mut, ty, name, declaration: bool = False):
if declaration:
return "{}: {}".format(name, ty)
else:
return "{}".format(name)
def translate_value(self, val):
return str(val)
def translate_op(self, param, ty=None):
return to_python_op(param, ty)
def translate_type(self, ty):
return to_python_type(ty)
def function_declaration(self, xml_tag, args, name, return_type):
return "def {}{} {}:".format(
name,
args,
return_type,
)
def format_single_arg(self, ty, name):
return "{}: {}".format(name, ty)
def unparse_block(self, node):
# super().unparse_block(node)
self.source += f"{self.block_delimiters[0]}\n"
self.indentation += 4
for child in node:
self.unparse_node(child)
self.source += self.indentation_character * self.indentation + "pass\n"
self.indentation -= 4
if node.get(GAZ_TY_KEY) is None:
self.source += f"{self.block_delimiters[1]}\n\n"
elif node.get(GAZ_TY_KEY) in [GAZ_TRUE_BLOCK_TAG, GAZ_FALSE_BLOCK_TAG]:
self.source += f"{self.block_delimiters[1]}"
def unparse(self):
super().unparse()
def setup(self):
pass