Compare commits
No commits in common. "0bd4ed0606bdbcaba19556d5c41a01418e0eed3a" and "0d96551e82de8a108d7442733f6c7e07fb80ee86" have entirely different histories.
0bd4ed0606
...
0d96551e82
|
@ -3,10 +3,8 @@ 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
|
||||
from constants import *
|
||||
|
||||
import keyword
|
||||
|
@ -51,8 +49,6 @@ class AstGenerator:
|
|||
self.current_nesting_depth = 0
|
||||
self.current_control_flow_nesting_depth = 0
|
||||
|
||||
self.py_unparser = None
|
||||
|
||||
self._init_numlines()
|
||||
|
||||
def _init_numlines(self):
|
||||
|
@ -68,7 +64,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'], [[], ['modulo'], ['not']],
|
||||
get_numberlines('expression-weights', ['brackets', 'arithmetic', 'unary'], [[], [], ['not']],
|
||||
self.settings))
|
||||
self.float_unary = ['negation', 'noop']
|
||||
self.char_op_options, self.char_op_cutoffs, self.char_op_numline = (
|
||||
|
@ -258,8 +254,7 @@ class AstGenerator:
|
|||
self._generate_from_options(cutoffs, number_line, options)
|
||||
|
||||
def _generate_expression(self, expr_type: list[str], number_line,
|
||||
cutoffs, options, unary=None, comparison: bool = False, eval_res: bool = False,
|
||||
constraint=None):
|
||||
cutoffs, options, unary=None, comparison: bool = False):
|
||||
"""
|
||||
@brief Generate an expression
|
||||
|
||||
|
@ -278,13 +273,13 @@ class AstGenerator:
|
|||
# Check the expression depth against settings
|
||||
if self.current_nesting_depth > self.settings['generation-options']['max-nesting-depth'] or random.random() < \
|
||||
self.settings['block-termination-probability']:
|
||||
self.generate_literal(random.choice(expr_type), constraint)
|
||||
self.generate_literal(random.choice(expr_type))
|
||||
self.current_nesting_depth -= 1
|
||||
return
|
||||
|
||||
# Generate
|
||||
op = _choose_option(cutoffs, number_line, options)
|
||||
self._generate_expr(comparison, expr_type, op, unary, eval_res, constraint)
|
||||
self._generate_expr(comparison, expr_type, op, unary)
|
||||
|
||||
# Return to parent
|
||||
self.current_nesting_depth -= 1
|
||||
|
@ -315,7 +310,7 @@ class AstGenerator:
|
|||
# Return to parent
|
||||
self.current_ast_element = parent
|
||||
|
||||
def generate_binary(self, op, op_type, eval_res=None, constraint=None):
|
||||
def generate_binary(self, op, op_type):
|
||||
"""
|
||||
@brief Generate a binary operation
|
||||
|
||||
|
@ -334,19 +329,13 @@ class AstGenerator:
|
|||
self.make_element(GAZ_OPERATOR_TAG, args)
|
||||
|
||||
# Gnereate lhs and rhs
|
||||
self.generate_xhs(GAZ_LHS_TAG, op_type, constraint)
|
||||
|
||||
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)))
|
||||
self.generate_xhs(GAZ_LHS_TAG, op_type)
|
||||
self.generate_xhs(GAZ_RHS_TAG, op_type)
|
||||
|
||||
# Return to parent
|
||||
self.current_ast_element = parent
|
||||
|
||||
def generate_bracket(self, op_type, constraint=None):
|
||||
def generate_bracket(self, op_type):
|
||||
"""
|
||||
@brief Generate a bracket operation
|
||||
|
||||
|
@ -358,12 +347,12 @@ class AstGenerator:
|
|||
self.make_element(GAZ_BRACKET_TAG, args)
|
||||
|
||||
# Generate the expression in the brackets
|
||||
self.generate_xhs(GAZ_RHS_TAG, op_type, constraint)
|
||||
self.generate_xhs(GAZ_RHS_TAG, op_type)
|
||||
|
||||
# Return to parent
|
||||
self.current_ast_element = parent
|
||||
|
||||
def generate_xhs(self, handedness, op_type, is_zero=False, constraint=None):
|
||||
def generate_xhs(self, handedness, op_type, is_zero=False):
|
||||
"""
|
||||
@brief generate a lhs or a rhs depending on handedness
|
||||
|
||||
|
@ -375,13 +364,11 @@ class AstGenerator:
|
|||
|
||||
self.make_element(handedness, [])
|
||||
|
||||
element = self.generate_expression(op_type, is_zero=is_zero, constraint=constraint)
|
||||
self.generate_expression(op_type, is_zero=is_zero)
|
||||
|
||||
self.current_ast_element = parent
|
||||
|
||||
return element
|
||||
|
||||
def generate_unary(self, op, op_type=ANY_TYPE, constraint=None):
|
||||
def generate_unary(self, op, op_type=ANY_TYPE):
|
||||
"""
|
||||
@brief Generate a unary operation
|
||||
|
||||
|
@ -394,7 +381,7 @@ class AstGenerator:
|
|||
]
|
||||
self.make_element(GAZ_UNARY_OPERATOR_TAG, args)
|
||||
|
||||
self.generate_xhs(GAZ_RHS_TAG, op_type, constraint)
|
||||
self.generate_xhs(GAZ_RHS_TAG, op_type)
|
||||
|
||||
self.current_ast_element = parent
|
||||
|
||||
|
@ -535,17 +522,16 @@ class AstGenerator:
|
|||
else:
|
||||
return Variable(self.get_name(GAZ_VAR_TAG), var_type, mut)
|
||||
|
||||
def generate_literal(self, var_type: str, value=None, constraint: tuple[str, str] | None = None):
|
||||
def generate_literal(self, var_type: str, value=None):
|
||||
"""
|
||||
@brief generate a literal
|
||||
|
||||
@param var_type: Type of the literal
|
||||
@param value: optional value of the literal
|
||||
@param constraint: optional constraint
|
||||
@return: None
|
||||
"""
|
||||
if value is None:
|
||||
value = self.get_value(var_type, constraint)
|
||||
value = self.get_value(var_type)
|
||||
else:
|
||||
value = value
|
||||
|
||||
|
@ -581,7 +567,7 @@ class AstGenerator:
|
|||
self.current_scope = current_scope
|
||||
self.current_ast_element = current_element
|
||||
|
||||
def generate_expression(self, expr_type: str, is_zero=False, constraint=None):
|
||||
def generate_expression(self, expr_type: str, is_zero=False):
|
||||
"""
|
||||
@brief generate an expression
|
||||
|
||||
|
@ -593,16 +579,16 @@ class AstGenerator:
|
|||
self.generate_literal(expr_type, value=0)
|
||||
return
|
||||
elif expr_type == GAZ_INT_KEY or expr_type == GAZ_FLOAT_KEY:
|
||||
self.generate_int_expr(constraint)
|
||||
self.generate_int_expr()
|
||||
elif expr_type == GAZ_BOOL_KEY:
|
||||
if random.random() < 0.5:
|
||||
self.generate_bool_expr(constraint)
|
||||
self.generate_bool_expr()
|
||||
else:
|
||||
self.generate_comp_expr(constraint)
|
||||
self.generate_comp_expr()
|
||||
elif expr_type == GAZ_CHAR_KEY:
|
||||
self.generate_char_expr(constraint)
|
||||
self.generate_char_expr()
|
||||
elif expr_type == GAZ_FLOAT_KEY:
|
||||
self.generate_float_expr(constraint)
|
||||
self.generate_float_expr()
|
||||
elif expr_type == ANY_TYPE: # TODO implement the choice of any type
|
||||
ty = self.get_type(GAZ_RHS_TAG)
|
||||
self.generate_expression(ty)
|
||||
|
@ -627,39 +613,39 @@ class AstGenerator:
|
|||
def generate_arg(self):
|
||||
return Argument(self.get_name(GAZ_VAR_TAG), self.get_type(GAZ_VAR_TAG))
|
||||
|
||||
def generate_int_expr(self, constraint=None):
|
||||
def generate_int_expr(self):
|
||||
self._generate_expression([GAZ_INT_KEY],
|
||||
self.int_op_numline,
|
||||
self.int_op_cutoffs,
|
||||
self.int_op_options,
|
||||
self.int_unary)
|
||||
|
||||
def generate_float_expr(self, constraint=None):
|
||||
def generate_float_expr(self):
|
||||
self._generate_expression([GAZ_FLOAT_KEY, GAZ_INT_KEY],
|
||||
self.float_op_numline,
|
||||
self.float_op_cutoffs,
|
||||
self.float_op_options,
|
||||
self.float_unary)
|
||||
|
||||
def generate_bool_expr(self, constraint=None):
|
||||
def generate_bool_expr(self):
|
||||
self._generate_expression([GAZ_BOOL_KEY],
|
||||
self.bool_op_numline,
|
||||
self.bool_op_cutoffs,
|
||||
self.bool_op_options,
|
||||
self.bool_unary)
|
||||
|
||||
def generate_char_expr(self, constraint=None):
|
||||
def generate_char_expr(self):
|
||||
self._generate_expression([GAZ_CHAR_KEY],
|
||||
self.char_op_numline,
|
||||
self.char_op_cutoffs,
|
||||
self.char_op_options)
|
||||
|
||||
def generate_comp_expr(self, constraint=None):
|
||||
def generate_comp_expr(self):
|
||||
self._generate_expression([GAZ_BOOL_KEY],
|
||||
self.comp_op_numline,
|
||||
self.comp_op_cutoffs,
|
||||
self.comp_op_options,
|
||||
comparison=True, eval_res=self.get_truth())
|
||||
comparison=True) #, evals=self.get_truth())
|
||||
|
||||
def push_scope(self, xml_element: ET.Element = None):
|
||||
scope = Scope(self.current_scope)
|
||||
|
@ -703,7 +689,7 @@ class AstGenerator:
|
|||
if res < cutoffs[i]:
|
||||
return ops[i] # TODO everything should be fast faied
|
||||
|
||||
def get_value(self, type, constraint: tuple[str, str] | None = None):
|
||||
def get_value(self, type):
|
||||
if type == GAZ_INT_KEY:
|
||||
if self.settings["properties"]["generate-max-int"]:
|
||||
return random.randint(-2147483648, 2147483647)
|
||||
|
@ -903,16 +889,16 @@ class AstGenerator:
|
|||
break
|
||||
break
|
||||
|
||||
def _generate_expr(self, comparison, expr_type, op, unary, eval_res=None, constraint=None):
|
||||
def _generate_expr(self, comparison, expr_type, op, unary):
|
||||
if op in unary:
|
||||
self.generate_unary(op, random.choice(expr_type), constraint)
|
||||
self.generate_unary(op, random.choice(expr_type))
|
||||
elif op == GAZ_BRACKET_TAG:
|
||||
self.generate_bracket(random.choice(expr_type), constraint)
|
||||
self.generate_bracket(random.choice(expr_type))
|
||||
elif comparison:
|
||||
if op in ['equality', 'inequality']:
|
||||
self.generate_binary(op, random.choice([GAZ_INT_KEY, GAZ_FLOAT_KEY, GAZ_CHAR_KEY]), eval_res, constraint)
|
||||
self.generate_binary(op, random.choice([GAZ_INT_KEY, GAZ_FLOAT_KEY, GAZ_CHAR_KEY]))
|
||||
else:
|
||||
self.generate_binary(op, random.choice([GAZ_INT_KEY, GAZ_FLOAT_KEY]), eval_res, constraint)
|
||||
self.generate_binary(op, random.choice([GAZ_INT_KEY, GAZ_FLOAT_KEY]))
|
||||
else:
|
||||
self.generate_binary(op, random.choice(expr_type))
|
||||
|
||||
|
@ -937,6 +923,3 @@ class AstGenerator:
|
|||
self.current_ast_element.append(var.xml)
|
||||
|
||||
self.current_ast_element = parent
|
||||
|
||||
def get_truth(self):
|
||||
return random.random() < self.settings['misc-weights']['conditional-true']
|
||||
|
|
|
@ -1,66 +0,0 @@
|
|||
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
|
|
@ -90,7 +90,9 @@ misc-weights:
|
|||
type-qualifier-weights:
|
||||
const: 10
|
||||
var: 60
|
||||
conditional-true: 0.9 # probability for a conditional to be true
|
||||
conditional-eval:
|
||||
true: 50
|
||||
false: 50
|
||||
|
||||
block-termination-probability: 0.2 # probability for a block to terminate
|
||||
|
||||
|
|
Loading…
Reference in a new issue