private TypeDescriptor GetAssignmentDesc(TypeDescriptor name, TypeDescriptor exp) { // assign integer value int valueInt = 0; // TODO: setting to 0 is not a good solution // fails if type check is not accurate NumberTypeDescriptor expNum = exp as NumberTypeDescriptor; PrimitiveTypeIntDescriptor expInt = exp as PrimitiveTypeIntDescriptor; if (expNum != null || expInt != null) { if (expNum != null) { valueInt = expNum.Num; } if (expInt != null) { valueInt = expInt.Value; } NumberTypeDescriptor nameNum = name as NumberTypeDescriptor; PrimitiveTypeIntDescriptor nameInt = name as PrimitiveTypeIntDescriptor; if (nameNum != null) { nameNum.Num = valueInt; return(nameNum); } if (nameInt != null) { nameInt.Value = valueInt; return(nameInt); } } // assign bool value PrimitiveTypeBooleanDescriptor expBoolean = exp as PrimitiveTypeBooleanDescriptor; PrimitiveTypeBooleanDescriptor nameBoolean = name as PrimitiveTypeBooleanDescriptor; if (expBoolean != null && nameBoolean != null) { nameBoolean.Value = expBoolean.Value; return(nameBoolean); } // assign string value LiteralTypeDescriptor expLiteral = exp as LiteralTypeDescriptor; LiteralTypeDescriptor nameLiteral = name as LiteralTypeDescriptor; if (expLiteral != null && nameLiteral != null) { nameLiteral.Value = expLiteral.Value; return(nameLiteral); } // assign an error if value unassignable return(new ErrorDescriptor("Assignment of value from " + exp.GetType().Name + " to " + name.GetType().Name + " failed")); }
private string GetIlType(TypeDescriptor desc, AbstractNode typeSpec) { // either the return type is a primitive (can get from the signature) PrimitiveTypeIntDescriptor intDesc = desc as PrimitiveTypeIntDescriptor; NumberTypeDescriptor numDesc = desc as NumberTypeDescriptor; if (intDesc != null || numDesc != null) { return("int32"); } PrimitiveTypeStringDescriptor stringDesc = desc as PrimitiveTypeStringDescriptor; LiteralTypeDescriptor litDesc = desc as LiteralTypeDescriptor; if (stringDesc != null || litDesc != null) { return("string"); } PrimitiveTypeBooleanDescriptor boolDesc = desc as PrimitiveTypeBooleanDescriptor; if (boolDesc != null) { return("bool"); } PrimitiveTypeVoidDescriptor voidDesc = desc as PrimitiveTypeVoidDescriptor; if (voidDesc != null) { return("void"); } // or it is a qualified name (can extract from the node) QualifiedName qName = typeSpec as QualifiedName; if (qName != null) { return(qName.GetStringName()); } // or it is an error ErrorDescriptor err = desc as ErrorDescriptor; if (err != null) { return(err.Message); } return(""); }
private void VisitNode(IterationStatement node) { AbstractNode whileExp = node.Child; AbstractNode bodyStmt = whileExp.Sib; IterationStatementDescriptor iterDesc = new IterationStatementDescriptor(); // while expression whileExp.Accept(this); iterDesc.WhileDescriptor = whileExp.TypeDescriptor; PrimitiveTypeBooleanDescriptor whileBoolDesc = whileExp.TypeDescriptor as PrimitiveTypeBooleanDescriptor; if (whileBoolDesc == null) { iterDesc.TypeDescriptor = new ErrorDescriptor("If statement " + "does not evaluate to a Boolean expression. (Has type: " + GetSimpleName(whileExp.TypeDescriptor) + ")"); } // if body stmt empty, infinite loop error bodyStmt.Accept(this); bodyStmt.TypeDescriptor = bodyStmt.Child.TypeDescriptor; iterDesc.BodyDescriptor = bodyStmt.TypeDescriptor; // propagate up errors as needed if (iterDesc.WhileDescriptor is ErrorDescriptor || iterDesc.BodyDescriptor is ErrorDescriptor) { if (iterDesc.WhileDescriptor is ErrorDescriptor) { iterDesc.TypeDescriptor = iterDesc.WhileDescriptor; if (iterDesc.BodyDescriptor is ErrorDescriptor) { ((ErrorDescriptor)iterDesc.TypeDescriptor).CombineErrors( (ErrorDescriptor)iterDesc.BodyDescriptor); } } else { iterDesc.TypeDescriptor = iterDesc.BodyDescriptor; } } // otherwise assign appropriate iteration statement descriptor else { iterDesc.TypeDescriptor = iterDesc.WhileDescriptor; } node.TypeDescriptor = iterDesc; }
private string GetIlType(TypeDescriptor desc) { // either the return type is a primitive (can get from the signature) PrimitiveTypeIntDescriptor intDesc = desc as PrimitiveTypeIntDescriptor; NumberTypeDescriptor numDesc = desc as NumberTypeDescriptor; if (intDesc != null || numDesc != null) { return("int32"); } PrimitiveTypeStringDescriptor stringDesc = desc as PrimitiveTypeStringDescriptor; LiteralTypeDescriptor litDesc = desc as LiteralTypeDescriptor; if (stringDesc != null || litDesc != null) { return("string"); } PrimitiveTypeBooleanDescriptor boolDesc = desc as PrimitiveTypeBooleanDescriptor; if (boolDesc != null) { return("bool"); } PrimitiveTypeVoidDescriptor voidDesc = desc as PrimitiveTypeVoidDescriptor; if (voidDesc != null) { return("void"); } // or it is an error ErrorDescriptor err = desc as ErrorDescriptor; if (err != null) { return(err.Message); } return(""); }
private void PrintValue(AbstractNode node) { if (node.TypeDescriptor != null) { Console.ForegroundColor = ConsoleColor.DarkMagenta; NumberTypeDescriptor num = node.TypeDescriptor as NumberTypeDescriptor; if (num != null) { Console.Write(" " + num.Num); return; } PrimitiveTypeIntDescriptor primInt = node.TypeDescriptor as PrimitiveTypeIntDescriptor; if (primInt != null) { Console.Write(" " + primInt.Value); return; } PrimitiveTypeBooleanDescriptor primBool = node.TypeDescriptor as PrimitiveTypeBooleanDescriptor; if (primBool != null) { Console.Write(" " + primBool.Value); return; } LiteralTypeDescriptor lit = node.TypeDescriptor as LiteralTypeDescriptor; if (lit != null) { Console.Write(" " + lit.Value); return; } Console.ResetColor(); } }
public PrimitiveTypeBoolean() { TypeDescriptor = new PrimitiveTypeBooleanDescriptor(); }
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; }