public void VisitNode(AbstractNode node) { VisitChildren(node); }
private void VisitNode(MethodCall node) { AbstractNode methodReference = node.Child; AbstractNode argumentList = methodReference.Sib; // may be null TypeDescriptor descriptor; QualifiedName qualifiedName = methodReference as QualifiedName; if (qualifiedName == null) { descriptor = new ErrorDescriptor ("Only Qualified Name supported for Method Call reference"); } else { // get parameters from method call List <TypeDescriptor> argListTypes = GetParameterTypes(argumentList as ArgumentList); // get parameters (signature) from declared method Attributes attr = Table.lookup(qualifiedName.GetStringName()); MethodTypeDescriptor methodDescriptor = attr.TypeDescriptor as MethodTypeDescriptor; if (methodDescriptor != null) { SignatureDescriptor methodSignature = methodDescriptor.Signature; if (methodSignature != null && ParametersMatch(methodSignature, argListTypes)) { // method descriptor for only current signature MethodTypeDescriptor temp = new MethodTypeDescriptor(); temp.ReturnType = methodDescriptor.ReturnType; temp.Signature.ParameterTypes = argListTypes; temp.Signature.Next = null; descriptor = temp; } else if (methodSignature == null) { descriptor = new ErrorDescriptor ("No signature found for method: " + qualifiedName.GetStringName()); } else { descriptor = new ErrorDescriptor ("No method signature found matching: (" + String.Join(", ", argListTypes) + ")"); } } else { descriptor = new ErrorDescriptor("Method not declared: " + qualifiedName.GetStringName()); } node.TypeDescriptor = descriptor; Attributes methodCallAttr = new Attr(descriptor); methodCallAttr.Kind = Kind.MethodType; node.AttributesRef = methodCallAttr; } }
private void VisitNode(SelectionStatement node) { AbstractNode ifExp = node.Child; AbstractNode thanStmt = ifExp.Sib; AbstractNode elseStmt = thanStmt.Sib; // may be null SelectionStatementDescriptor ssDesc = new SelectionStatementDescriptor(); String errMsg = ""; // if expression ifExp.Accept(this); ssDesc.IfDescriptor = ifExp.TypeDescriptor; PrimitiveTypeBooleanDescriptor ifBoolDesc = ifExp.TypeDescriptor as PrimitiveTypeBooleanDescriptor; if (ifBoolDesc == null) { ifExp.TypeDescriptor = new ErrorDescriptor("If statement " + "does not evaluate to a Boolean expression. (Has type: " + ifExp.TypeDescriptor.GetType().Name + ")"); } // than statement thanStmt.Accept(this); thanStmt.TypeDescriptor = thanStmt.Child.TypeDescriptor; ssDesc.ThanDescriptor = thanStmt.TypeDescriptor; if (!IsCompatibleStatement(thanStmt.TypeDescriptor)) { if (errMsg.Length > 0) { errMsg += "\n"; } errMsg += "Non-compatible THAN statement type: " + thanStmt.TypeDescriptor.GetType().Name; } // else statement if (elseStmt != null) { elseStmt.Accept(this); ssDesc.HasElseStmt = true; elseStmt.TypeDescriptor = elseStmt.Child.TypeDescriptor; ssDesc.ElseDescriptor = elseStmt.TypeDescriptor; if (!IsCompatibleStatement(elseStmt.TypeDescriptor)) { if (errMsg.Length > 0) { errMsg += "\n"; } errMsg += "Non-compatible ELSE statement type: " + elseStmt.TypeDescriptor.GetType().Name; } } else { ssDesc.HasElseStmt = false; } // if any components have errors, propogate them up the tree if (ifExp.TypeDescriptor is ErrorDescriptor || thanStmt.TypeDescriptor is ErrorDescriptor || elseStmt?.TypeDescriptor is ErrorDescriptor) { // creates an error containing any and all errors lower in tree if (ifExp.TypeDescriptor is ErrorDescriptor) { ssDesc.TypeDescriptor = ifExp.TypeDescriptor; if (thanStmt.TypeDescriptor is ErrorDescriptor) { ((ErrorDescriptor)ssDesc.TypeDescriptor).CombineErrors ((ErrorDescriptor)thanStmt.TypeDescriptor); } if (elseStmt?.TypeDescriptor is ErrorDescriptor) { ((ErrorDescriptor)ssDesc.TypeDescriptor).CombineErrors ((ErrorDescriptor)elseStmt.TypeDescriptor); } } else if (thanStmt.TypeDescriptor is ErrorDescriptor) { ssDesc.TypeDescriptor = thanStmt.TypeDescriptor; if (elseStmt?.TypeDescriptor is ErrorDescriptor) { ((ErrorDescriptor)ssDesc.TypeDescriptor).CombineErrors ((ErrorDescriptor)elseStmt.TypeDescriptor); } } else { ssDesc.TypeDescriptor = elseStmt.TypeDescriptor; } } // if IF/THAN/ELSE was incompatible, create new error else if (errMsg.Length > 0) { ssDesc.TypeDescriptor = new ErrorDescriptor(errMsg); } // otherwise assign evaluated boolean to selection statement desc else { ssDesc.TypeDescriptor = ifExp.TypeDescriptor; } node.TypeDescriptor = ssDesc; }
private TypeDescriptor AssignmentExp(AbstractNode qualName, AbstractNode exp) { QualifiedName name = qualName as QualifiedName; Expression expression = exp as Expression; PrimaryExpression primaryExp = exp as PrimaryExpression; TypeDescriptor nameDesc; if (name != null && (expression != null || primaryExp != null)) { Attributes nameAttr = Table.lookup(name.GetStringName()); qualName.AttributesRef = nameAttr; qualName.TypeDescriptor = nameAttr.TypeDescriptor; exp.Accept(this); // Check for errors // if both types are Error Descriptors, combine errors ErrorDescriptor nameErrDesc = nameAttr.TypeDescriptor as ErrorDescriptor; ErrorDescriptor expErrDesc = exp.TypeDescriptor as ErrorDescriptor; if (nameErrDesc != null && expErrDesc != null) { nameDesc = nameErrDesc.CombineErrors(expErrDesc); } // if one or the other is an error, propagate the error up else if (nameErrDesc != null) { nameDesc = nameAttr.TypeDescriptor; } else if (expErrDesc != null) { nameDesc = exp.TypeDescriptor; } // check that the variable being assigned to is assignable else if (nameAttr.IsAssignable) { // if types compatible, assign successfully assigned type if (TypesCompatible(nameAttr.TypeDescriptor, exp.TypeDescriptor)) { nameDesc = GetAssignmentDesc(nameAttr.TypeDescriptor, exp.TypeDescriptor); } // otherwise, assign new error for incompatible types else { nameDesc = new ErrorDescriptor("Incompatible types: " + "cannot assign " + GetSimpleName(exp.TypeDescriptor) + " to " + GetSimpleName(nameAttr.TypeDescriptor) + " variable"); } } // variable is not assignable else { nameDesc = new ErrorDescriptor(nameAttr + " is not assigable. Cannot assign as " + GetSimpleName(exp.TypeDescriptor)); } } // Assignment not made up of correct parts else { string message = ""; if (name == null && expression == null) { message += "EQUALS expression expects 'QualifiedName' " + "on LHS, but has: " + qualName.GetType().Name + " & 'Expression' or 'PrimaryExression' on " + "RHS, but has " + exp.GetType().Name; } else if (name == null) { message += "EQUALS expression expects 'QualifiedName' on" + " LHS, but has: " + qualName.GetType().Name; } else { message += "EQUALS expression expects 'Expression' or " + "'PrimaryExpression' on RHS, but has: " + exp.GetType().Name; } nameDesc = new ErrorDescriptor(message); } return(nameDesc); }
public void CreateIlFileContent(AbstractNode node) { node?.Accept(this); }
private string GetIlType(AbstractNode node) { return(GetIlType(node.TypeDescriptor, node)); }
private string GetIlTypeParam(AbstractNode node) { AbstractNode typeSpecifier = node.Child; return($"{GetIlType(typeSpecifier)}"); }
private new void VisitNode(AbstractNode node) { VisitChildren(node); }
private void VisitNode(MethodDeclaration node) { AbstractNode modifiers = node.Child; AbstractNode typeSpecifier = modifiers.Sib; AbstractNode methodDeclarator = typeSpecifier.Sib; AbstractNode methodBody = methodDeclarator.Sib; AbstractNode retType = typeSpecifier.Child; if (retType is PrimitiveTypeVoid) { ((PrimitiveTypeVoid)retType).Accept(TypeVisitor); } else if (retType is PrimitiveTypeBoolean) { ((PrimitiveTypeBoolean)retType).Accept(TypeVisitor); } else if (retType is PrimitiveTypeInt) { ((PrimitiveTypeInt)retType).Accept(TypeVisitor); } else if (retType is QualifiedName) { ((QualifiedName)retType).Accept(TypeVisitor); } else { string msg = "Return type of a method must be a " + "PrimitiveType or QualifiedName (found: " + GetSimpleName(retType.TypeDescriptor); retType.TypeDescriptor = new ErrorDescriptor(msg); } AbstractNode methodDeclaratorName = methodDeclarator.Child; AbstractNode parameterList = methodDeclaratorName.Sib; // may be null MethodTypeDescriptor methDesc = new MethodTypeDescriptor(); methDesc.ReturnType = retType.TypeDescriptor; methDesc.Modifiers = ((Modifiers)modifiers).ModifierTokens; methDesc.Locals = new ScopeTable(); methDesc.IsDefinedIn = CurrentClass; Attributes attr = new Attr(methDesc); attr.Kind = Kind.MethodType; string name = ((Identifier)methodDeclaratorName).ID; Table.enter(name, attr); node.TypeDescriptor = attr.TypeDescriptor; node.AttributesRef = attr; Table.openScope(methDesc.Locals); MethodTypeDescriptor oldCurrentMethod = CurrentMethod; CurrentMethod = methDesc; if (parameterList != null) { parameterList.Accept(this); methDesc.Signature.ParameterTypes = ((ParameterListTypeDescriptor) parameterList.TypeDescriptor).ParamTypeDescriptors; attr.TypeDescriptor = methDesc; Table.updateValue(name, attr); node.TypeDescriptor = methDesc; node.AttributesRef = Table.lookup(name); } methodBody.Accept(this); CurrentMethod = oldCurrentMethod; Table.closeScope(); }