2023-11-18 12:21:52 -07:00
|
|
|
import os
|
|
|
|
import shutil
|
2023-11-18 20:13:15 -07:00
|
|
|
import warnings
|
2023-11-18 12:21:52 -07:00
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
|
|
|
from ast_parser.general_unparser import GeneralUnparser
|
|
|
|
from constants import *
|
|
|
|
|
|
|
|
|
2023-11-19 15:19:27 -07:00
|
|
|
def to_gazprea_type(ty: str): # TODO implement the compound types
|
2023-11-18 12:21:52 -07:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2023-11-18 20:13:15 -07:00
|
|
|
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 "-"
|
2023-11-21 23:03:12 -07:00
|
|
|
elif param == "addition" or param == "noop":
|
2023-11-18 20:13:15 -07:00
|
|
|
return "+"
|
|
|
|
elif param == "multiplication":
|
|
|
|
return "*"
|
|
|
|
elif param == "division":
|
|
|
|
return "/"
|
2023-11-22 10:26:25 -07:00
|
|
|
elif param == "modulo":
|
2023-11-18 20:13:15 -07:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2023-11-19 11:05:52 -07:00
|
|
|
class GazUnparser(GeneralUnparser):
|
2023-11-19 14:40:57 -07:00
|
|
|
def __init__(self, input_ast, from_xml: bool = False):
|
2023-11-19 15:19:27 -07:00
|
|
|
super().__init__(input=input_ast, from_xml=from_xml,
|
|
|
|
endline=";\n",
|
|
|
|
instream_end_delimiter=" <- std_input",
|
|
|
|
outstream_end_delimiter=" -> std_output", )
|
2023-11-19 14:40:57 -07:00
|
|
|
|
|
|
|
def format_variable(self, mut, ty, name, declaration: bool = False):
|
|
|
|
if declaration:
|
|
|
|
return "{} {} {}".format(mut, ty, name)
|
2023-11-18 12:21:52 -07:00
|
|
|
else:
|
2023-11-19 14:40:57 -07:00
|
|
|
return "{}".format(name)
|
2023-11-18 12:21:52 -07:00
|
|
|
|
2023-11-19 14:40:57 -07:00
|
|
|
def translate_value(self, val):
|
|
|
|
if val in ["True", "False"]:
|
|
|
|
return val.lower()
|
2023-11-18 12:21:52 -07:00
|
|
|
else:
|
2023-11-19 14:40:57 -07:00
|
|
|
return str(val)
|
2023-11-18 12:21:52 -07:00
|
|
|
|
2023-11-21 21:39:04 -07:00
|
|
|
def translate_op(self, param, ty=None):
|
2023-11-19 14:40:57 -07:00
|
|
|
return to_gaz_op(param)
|
2023-11-18 12:21:52 -07:00
|
|
|
|
2023-11-19 14:40:57 -07:00
|
|
|
def translate_type(self, ty):
|
|
|
|
return to_gazprea_type(ty)
|
2023-11-18 12:21:52 -07:00
|
|
|
|
2023-11-19 14:40:57 -07:00
|
|
|
def function_declaration(self, xml_tag, args, name, return_type):
|
|
|
|
return "{} {}{} {} ".format(
|
|
|
|
xml_tag,
|
|
|
|
name,
|
2023-11-18 12:21:52 -07:00
|
|
|
args,
|
|
|
|
return_type,
|
|
|
|
)
|
|
|
|
|
2023-11-19 14:40:57 -07:00
|
|
|
def format_single_arg(self, ty, name):
|
|
|
|
return "{} {}".format(ty, name)
|
2023-11-24 09:31:18 -07:00
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
pass
|