Generation Error Mitigation #4

Merged
aCompetentBean merged 3 commits from ayrton/generation-errors into main 2023-11-24 15:47:48 -07:00
5 changed files with 66 additions and 11 deletions
Showing only changes of commit 1c91f6ac86 - Show all commits

View file

@ -156,6 +156,8 @@ class AstGenerator:
self.generate_return(return_type=return_type, return_value=return_value) self.generate_return(return_type=return_type, return_value=return_value)
if self.settings['generation-options']['generate-dead-code']: if self.settings['generation-options']['generate-dead-code']:
self.generate_statements(exclude='declaration') self.generate_statements(exclude='declaration')
self.print_all_current_scope_vars()
self.pop_scope() self.pop_scope()
self.current_ast_element = parent self.current_ast_element = parent
@ -643,7 +645,7 @@ class AstGenerator:
self.comp_op_numline, self.comp_op_numline,
self.comp_op_cutoffs, self.comp_op_cutoffs,
self.comp_op_options, self.comp_op_options,
comparison=True) comparison=True) #, evals=self.get_truth())
def push_scope(self, xml_element: ET.Element = None): def push_scope(self, xml_element: ET.Element = None):
scope = Scope(self.current_scope) scope = Scope(self.current_scope)
@ -899,3 +901,25 @@ class AstGenerator:
self.generate_binary(op, random.choice([GAZ_INT_KEY, GAZ_FLOAT_KEY])) self.generate_binary(op, random.choice([GAZ_INT_KEY, GAZ_FLOAT_KEY]))
else: else:
self.generate_binary(op, random.choice(expr_type)) self.generate_binary(op, random.choice(expr_type))
def print_all_current_scope_vars(self):
for key, value in self.current_scope.symbols.items():
if isinstance(value, Variable):
self.print_variable(value)
def print_variable(self, var: Variable):
"""
@brief generate an outstream for a variable
@param var: the variable to print
@return:
"""
parent = self.current_ast_element
args = [
("type", GAZ_OUT_STREAM),
]
self.make_element(GAZ_STREAM_TAG, args)
self.current_ast_element.append(var.xml)
self.current_ast_element = parent

View file

@ -9,7 +9,7 @@ generation-options:
max-globals: 5 # maximum number of global variables max-globals: 5 # maximum number of global variables
properties: properties:
max-range-length: 5 # maximum length of ranges, vectors and tuples, (AxA matrices can exist) max-range-length: 5 # maximum length of ranges, vectors and tuples, (AxA matrices can exist)
use-english-words: True # use english words instead of random names (this may limit the maximum number of names) use-english-words: True # use english words instead of random names (if we run out, we switch to random)
id-length: # length of identifiers id-length: # length of identifiers
min: 1 min: 1
max: 5 max: 5
@ -68,12 +68,12 @@ statement-weights: # set to 0 for any statements y
in-stream: 5 in-stream: 5
type-weights: type-weights:
value-types: atomic-types:
integer: 50 int: 50 # TODO change these to the gaz types
real: 50 float: 50
boolean: 50 bool: 50
character: 50 char: 50
void: 10 void: 0 # TODO add support for void
composite-types: composite-types:
vector: 20 vector: 20
tuple: 5 tuple: 5
@ -90,6 +90,9 @@ misc-weights:
type-qualifier-weights: type-qualifier-weights:
const: 10 const: 10
var: 60 var: 60
conditional-eval:
true: 50
false: 50
block-termination-probability: 0.2 # probability for a block to terminate block-termination-probability: 0.2 # probability for a block to terminate

View file

@ -309,6 +309,22 @@ class TestGeneration(unittest.TestCase):
return res return res
def test_print_all_current_scope_vars(self):
self.ast_gen.ast = ET.Element("block")
self.ast_gen.current_ast_element = self.ast_gen.ast
self.ast_gen.generate_declaration(mut='var')
self.ast_gen.generate_declaration(mut='var')
self.ast_gen.generate_declaration(mut='var')
self.ast_gen.generate_declaration(mut='var')
self.ast_gen.print_all_current_scope_vars()
print(ET.tostring(self.ast_gen.ast))
streams = self.ast_gen.ast.findall("stream")
self.assertEqual(4, len(streams))
def write_ast(self): def write_ast(self):
dom = xml.dom.minidom.parseString(ET.tostring(self.ast_gen.ast).decode('utf-8')) dom = xml.dom.minidom.parseString(ET.tostring(self.ast_gen.ast).decode('utf-8'))
pretty: str = dom.toprettyxml() pretty: str = dom.toprettyxml()

View file

@ -28,6 +28,11 @@ class GazpreaFuzzer:
self.ground_truth = None self.ground_truth = None
self.out = None self.out = None
try:
os.makedirs('debug/ast')
except FileExistsError:
pass
def fuzz(self): def fuzz(self):
self.generator.generate_ast() self.generator.generate_ast()
self.write_ast() # FIXME sometimes this is none self.write_ast() # FIXME sometimes this is none

View file

@ -61,6 +61,7 @@ class Fuzzer():
pass pass
i = 0 i = 0
min_i = 0
while i < self.batch: while i < self.batch:
try: try:
self.fuzzer.fuzz() self.fuzzer.fuzz()
@ -71,8 +72,10 @@ class Fuzzer():
"".format(r)) "".format(r))
with open(f"{fuzzer_asterr}/{r}.xml", 'w') as f: with open(f"{fuzzer_asterr}/{r}.xml", 'w') as f:
f.write(xml.dom.minidom.parseString(ET.tostring(self.fuzzer.ast).decode('utf-8')).toprettyxml()) f.write(xml.dom.minidom.parseString(ET.tostring(self.fuzzer.ast).decode('utf-8')).toprettyxml())
if i - 1 >= min_i:
i -= 1 i -= 1
else:
i = min_i
continue continue
dom = xml.dom.minidom.parseString(ET.tostring(self.fuzzer.ast).decode('utf-8')) dom = xml.dom.minidom.parseString(ET.tostring(self.fuzzer.ast).decode('utf-8'))
pretty: str = dom.toprettyxml() pretty: str = dom.toprettyxml()
@ -89,7 +92,10 @@ class Fuzzer():
os.system("rm -f {}/{}_{}.py".format(fuzzer_ground_truth, self.file_name, i)) os.system("rm -f {}/{}_{}.py".format(fuzzer_ground_truth, self.file_name, i))
os.system("rm -f {}/{}_{}.py".format(fuzzer_outputs, self.file_name, i)) os.system("rm -f {}/{}_{}.py".format(fuzzer_outputs, self.file_name, i))
warnings.warn("Runtime error encountered, retrying") warnings.warn("Runtime error encountered, retrying")
i -= 1 if i - 1 >= min_i:
i -= 1
else:
i = min_i
continue continue
except KeyboardInterrupt: except KeyboardInterrupt:
r = random.randint(0, 1000000) r = random.randint(0, 1000000)
@ -109,6 +115,7 @@ class Fuzzer():
# with open("fuzzer/outputs/{}.out".format(i), 'w') as f: # with open("fuzzer/outputs/{}.out".format(i), 'w') as f:
# f.write(self.fuzzer.out) # f.write(self.fuzzer.out)
i += 1 i += 1
min_i = i
if __name__ == '__main__': if __name__ == '__main__':