gazprea-fuzzer-python/ast_parser/gaz_unparser.py
2023-11-24 09:31:18 -07:00

104 lines
2.7 KiB
Python

import os
import shutil
import warnings
import xml.etree.ElementTree as ET
from ast_parser.general_unparser import GeneralUnparser
from constants import *
def to_gazprea_type(ty: str): # TODO implement the compound types
if ty == GAZ_INT_KEY:
return "integer"
elif ty == GAZ_BOOL_KEY:
return "boolean"
elif ty == GAZ_FLOAT_KEY:
return "real"
elif ty == GAZ_CHAR_KEY:
return "character"
elif ty == GAZ_STRING_KEY:
return "string[*]"
else:
raise Exception("Unknown type: " + ty)
def _unparse_arg(node):
return "{} {}".format(to_gazprea_type(node.get(GAZ_TY_KEY)), node.get(GAZ_NAME_KEY))
def to_gaz_value(val):
if val in ["True", "False"]:
return val.lower()
else:
return str(val)
def to_gaz_op(param):
if param == "negation" or param == "subtraction":
return "-"
elif param == "addition" or param == "noop":
return "+"
elif param == "multiplication":
return "*"
elif param == "division":
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 ">="
else:
warnings.warn("Warning, unknown operator: " + param)
return param
class GazUnparser(GeneralUnparser):
def __init__(self, input_ast, from_xml: bool = False):
super().__init__(input=input_ast, from_xml=from_xml,
endline=";\n",
instream_end_delimiter=" <- std_input",
outstream_end_delimiter=" -> std_output", )
def format_variable(self, mut, ty, name, declaration: bool = False):
if declaration:
return "{} {} {}".format(mut, ty, name)
else:
return "{}".format(name)
def translate_value(self, val):
if val in ["True", "False"]:
return val.lower()
else:
return str(val)
def translate_op(self, param, ty=None):
return to_gaz_op(param)
def translate_type(self, ty):
return to_gazprea_type(ty)
def function_declaration(self, xml_tag, args, name, return_type):
return "{} {}{} {} ".format(
xml_tag,
name,
args,
return_type,
)
def format_single_arg(self, ty, name):
return "{} {}".format(ty, name)
def setup(self):
pass