gazprea-fuzzer-python/ast_generator/tiny_py_unparse.py

102 lines
2.9 KiB
Python
Raw Normal View History

from ast_parser.general_unparser import GeneralUnparser
import warnings
from xml.etree import ElementTree as ET
from ast_parser.gaz_unparser import GazUnparser
from ast_parser.general_unparser import GeneralUnparser
from constants import *
def to_python_type(ty):
if ty == GAZ_INT_KEY:
return "int"
elif ty == GAZ_BOOL_KEY:
return "bool"
elif ty == GAZ_FLOAT_KEY:
return "float"
elif ty == GAZ_CHAR_KEY:
return "str"
elif ty == GAZ_STRING_KEY:
return "str"
else:
raise Exception("Unknown type: " + ty)
def to_python_op(param, ty):
if param == "negation" or param == "subtraction":
return "-"
elif param == "addition" or param == "noop":
return "+"
elif param == "multiplication":
return "*"
elif param == "division" and ty != GAZ_INT_KEY:
return "/"
elif param == "division" and ty == GAZ_INT_KEY:
return "//"
elif param == "modulo":
return "%"
elif param == "power":
return "**"
elif param == "equality":
return "=="
elif param == "inequality":
return "!="
elif param == "less-than":
return "<"
elif param == "less-than-or-equal":
return "<="
elif param == "greater-than":
return ">"
elif param == "greater-than-or-equal":
return ">="
elif param == "xor":
return "!="
else:
warnings.warn("Warning, unknown operator: " + param)
return param
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)
self.source = ''
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 unparse_block(self, node):
raise TypeError("Cannot unparse blocks for this type of evaluation")
def setup(self):
pass