From 4d8609c38e5a7ee0bb0871930d62be51760a6fdc Mon Sep 17 00:00:00 2001 From: ayrton Date: Tue, 21 Nov 2023 23:03:12 -0700 Subject: [PATCH] Added correct comparisons and implemented all basic types Took 27 minutes --- ast_generator/ast_generator.py | 14 +++++++++++++- ast_parser/gaz_unparser.py | 2 +- config.yaml | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/ast_generator/ast_generator.py b/ast_generator/ast_generator.py index 388a2ae..1684719 100644 --- a/ast_generator/ast_generator.py +++ b/ast_generator/ast_generator.py @@ -332,6 +332,11 @@ class AstGenerator: self.generate_unary(op, random.choice(expr_type)) elif op == GAZ_BRACKET_TAG: 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])) + else: + self.generate_binary(op, random.choice([GAZ_INT_KEY, GAZ_FLOAT_KEY])) else: self.generate_binary(op, random.choice(expr_type)) @@ -532,7 +537,12 @@ class AstGenerator: if expr_type == GAZ_INT_KEY or expr_type == GAZ_FLOAT_KEY: self.generate_int_expr() elif expr_type == GAZ_BOOL_KEY: - self.generate_bool_expr() + if random.random() < 0.5: + self.generate_bool_expr() + else: + self.generate_comp_expr() + elif expr_type == GAZ_CHAR_KEY: + self.generate_char_expr() elif expr_type == ANY_TYPE: # TODO implement the choice of any type self.generate_int_expr() else: @@ -603,6 +613,8 @@ class AstGenerator: return random.uniform(-1000, 1000) elif type == GAZ_BOOL_KEY: return random.choice([True, False]) + elif type == GAZ_CHAR_KEY: + return "'" + random.choice(string.ascii_letters) + "'" else: raise TypeError("Unimplemented generator for type: " + type) diff --git a/ast_parser/gaz_unparser.py b/ast_parser/gaz_unparser.py index 4f129e8..10ea831 100644 --- a/ast_parser/gaz_unparser.py +++ b/ast_parser/gaz_unparser.py @@ -36,7 +36,7 @@ def to_gaz_value(val): def to_gaz_op(param): if param == "negation" or param == "subtraction": return "-" - elif param == "addition": + elif param == "addition" or param == "noop": return "+" elif param == "multiplication": return "*" diff --git a/config.yaml b/config.yaml index 68329d3..a953c4b 100644 --- a/config.yaml +++ b/config.yaml @@ -5,6 +5,7 @@ generation-options: max-conditionals-loops: 5 # maximum number of loops/conditionals per routine max-number-of-routines: 5 # maximum number of routines (main will always be generated) generate-dead-code: True # generate dead code + max-loop-iterations: 100 # maximum number of iterations in a loop properties: 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)