def wf_ICALL(node, type, scope): newNode = Node("ICALL") newNode.addAttribute(node.attributes[0]) item = lookup(node.attributes[0], scope) if item != None: (NA, varname, arguments, returnType, offset, scopefound) = item else: print("Function " + node.attributes[0] + " Not found") sys.exit() arguments = flatten(arguments) if 2 * (len(node.children)) == len(arguments): value = 1 for child in node.children: if arguments[value] == "M_int": newNode.addChild(wf_I_expr(child, "Int", scope)) elif (arguments[value] == "M_bool"): newNode.addChild(wf_I_expr(child, "Bool", scope)) else: print("Invalid Argument Type") sys.exit() value += 2 return newNode else: print("Improper number of arguments in function call") if (returnType == "M_int" and (type == "Int" or type == "Print")): return newNode elif (returnType == "M_bool" and (type == "Bool" or type == "Print")): return newNode else: print("Function call return type does not match type of calling code") sys.exit()
def wf_iBLOCK(node, scope): newNode = Node("iBlock") tally = 0 for child in node.children: if child.name == "M_fun": newNode.addChild(wf_I_fbody(child, scope)) elif child.name == "M_var": tally += 1 else: newNode.addChild(wf_I_stmt(child, scope)) newNode.addAttribute(tally) return newNode
def wf_I_prog(node): if node.name == "M_prog": newNode = Node("IPROG") table = getTable("Prog") newNode.addAttribute(table.numberOfLocal) for child in node.children: if (child.name == "M_fun"): newNode.addChild(wf_I_fbody(child, 0)) for child in node.children: if (child.name != "M_var" and child.name != "M_fun"): newNode.addChild(wf_I_stmt(child, 0)) return newNode
def wf_IASS(node, scope): newNode = Node("IASS") item = lookup(node.attributes[0], scope) if (item == None): print("Variable " + node.attributes[0] + " not found for assignment") sys.exit() else: (type, varname, varType, offset, scopefound) = item newNode.addAttribute(node.attributes[0]) newNode.addAttribute(offset) if (varType == "M_int"): newNode.addChild(wf_I_expr(node.children[0], "Int", scope)) elif (varType == "M_bool"): newNode.addChild(wf_I_expr(node.children[0], "Bool", scope)) return newNode
def wf_I_fbody(current, scope): for child in current.children: if child.name == "M_block": scope = scope + 1 newNode = Node("IFUN") table = getTable(current.attributes[0]) if (table == None): print("function " + current.attributes[0] + " not defined in current scope") sys.exit() newNode.addAttribute(current.attributes[0]) newNode.addAttribute(table.numberOfLocal) newNode.addAttribute(table.numberOfArguments) for tiny in child.children: if (tiny.name == "M_return"): newNode.addChild( wf_iRETURN(tiny, current.attributes[0], scope)) elif (tiny.name == "M_fun"): newNode.addChild(wf_I_fbody(tiny, scope)) elif (tiny.name != "M_var" and tiny.name != "M_fun"): newNode.addChild(wf_I_stmt(tiny, scope)) return newNode
def wf_I_expr(node, type, scope): if (node.name == "M_num"): if type == "Int" or type == "Print": newNode = Node("INUM") newNode.addAttribute(int(node.attributes[0])) return newNode else: print("Incompatible types") sys.exit() elif (node.name == "M_bl"): if type == "Bool" or type == "Print": newNode = Node("IBOOL") newNode.addAttribute(node.attributes[0]) return newNode else: print("Incompatible types") sys.exit() elif (node.name == "M_id"): newNode = wf_IID(node, type, scope) return newNode else: return wf_I_APP(node, type, scope)
def wf_read(current, scope): item = lookup(current.attributes[0], scope) if (item == None): print("variable " + current.attributes[0] + " not found in this scope") sys.exit() else: (type, varname, varType, offset, scopefound) = item if (varType == "M_int"): newNode = Node("iREAD_I") newNode.addAttribute(current.attributes[0]) newNode.addAttribute(offset) return newNode elif (varType == "M_bool"): newNode = Node("iREAD_B") newNode.addAttribute(current.attributes[0]) newNode.addAttribute(offset) return newNode
def wf_IID(node, type, scope): item = lookup(node.attributes[0], scope) if (item == None): print("ID " + node.attributes[0] + " not found in this scope") sys.exit() else: (STtype, varname, varType, offset, scopefound) = item if (scopefound != scope): scopefound = 0 else: scopefound = 1 if (varType == "M_int" and (type == "Int" or type == "Print")): newNode = Node("IID") newNode.addAttribute(node.attributes[0]) newNode.addAttribute(offset) newNode.addAttribute(scopefound) return newNode elif (varType == "M_bool" and (type == "Bool" or type == "Print")): newNode = Node("IID") newNode.addAttribute(node.attributes[0]) newNode.addAttribute(offset) newNode.addAttribute(scopefound) return newNode else: print("Variable " + node.attributes[0] + " not the required type of " + type) sys.exit()