public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Create new scope var childScope = new TigerScope(scope, "Let"); //Check children DeclarationListNode.CheckSemantics(childScope, report); InstructionSequenceNode.CheckSemantics(childScope, report); if (!DeclarationListNode.IsOK || !InstructionSequenceNode.IsOK) { return; } TigerType = InstructionSequenceNode.TigerType; //Check return type if (!childScope.ValidReturnType(TigerType)) { report.AddError(SemanticErrors.InvalidReturnType(this, TigerType)); } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children LValueNode.CheckSemantics(scope, report); IndexExpressionNode.CheckSemantics(scope, report); if (!LValueNode.IsOK || !IndexExpressionNode.IsOK) { return; } //Check children types if (!(LValueNode.TigerType is ArrayType)) { report.AddError(SemanticErrors.NonArrayType(LValueNode, LValueNode.TigerType)); return; } if (!TigerType.Int.Assignable(IndexExpressionNode.TigerType)) { report.AddError(SemanticErrors.ArrayIndexerInvalidType(IndexExpressionNode, IndexExpressionNode.TigerType)); return; } TigerType = ((ArrayType)LValueNode.TigerType).ContentType; }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children Condition.CheckSemantics(scope, report); InstructionNode.CheckSemantics(scope, report); if (!Condition.IsOK || !InstructionNode.IsOK) { return; } IsOK = true; //Check children types if (!TigerType.AreCompatible(Condition.TigerType, TigerType.Int)) { report.AddError(SemanticErrors.InvalidConditionType(Condition, Condition.TigerType)); IsOK = false; } if (!TigerType.AreCompatible(InstructionNode.TigerType, TigerType.Void)) { report.AddError(SemanticErrors.InvalidWhileBodyType(InstructionNode)); IsOK = false; } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children LValueNode.CheckSemantics(scope, report); AttributeNode.CheckSemantics(scope, report); if (!LValueNode.IsOK || !AttributeNode.IsOK) { return; } //Check children types if (!(LValueNode.TigerType is RecordType)) { report.AddError(SemanticErrors.NonRecordType(LValueNode, LValueNode.TigerType)); } else { var field = ((RecordType)LValueNode.TigerType).RecordFields.FirstOrDefault(f => f.Name == AttributeNode.Name); if (field == null) { report.AddError(SemanticErrors.InvalidField(AttributeNode, AttributeNode.Name, LValueNode.TigerType.Name)); } else { TigerType = field.TigerType; } } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Checking children Condition.CheckSemantics(scope, report); IfBlock.CheckSemantics(scope, report); ElseBlock.CheckSemantics(scope, report); if (!Condition.IsOK || !IfBlock.IsOK || !ElseBlock.IsOK) { return; } TigerType = IfBlock.TigerType; //Checking children types if (!TigerType.AreCompatible(Condition.TigerType, TigerType.Int)) { report.AddError(SemanticErrors.InvalidConditionType(Condition, Condition.TigerType)); } if (!TigerType.AreCompatible(IfBlock.TigerType, ElseBlock.TigerType)) { report.AddError(SemanticErrors.IncompatibleIfElseReturnType(ElseBlock, IfBlock.TigerType, ElseBlock.TigerType)); } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children LValueNode.CheckSemantics(scope, report); ExpressionNode.CheckSemantics(scope, report); if (!LValueNode.IsOK || !ExpressionNode.IsOK) { return; } //Check children types if (!LValueNode.TigerType.Assignable(ExpressionNode.TigerType)) { report.AddError(SemanticErrors.InvalidAssignType(ExpressionNode, ExpressionNode.TigerType, LValueNode.TigerType)); } var variableAccess = LValueNode as VariableAccessNode; if (variableAccess != null && !variableAccess.VariableInfo.Assignable) { report.AddError(SemanticErrors.NonAssignableVariable(LValueNode, variableAccess.VariableInfo.Name)); } IsOK = true; }
public void CheckBodySemantics(TigerScope scope, Report report) { //If CheckSemantics failed (FunctionInfo was not created) return if (!IsOK) { return; } IsOK = false; //Create function scope FunctionBodyNode.CheckSemantics(FunctionScope, report); if (!FunctionBodyNode.IsOK) { return; } IsOK = true; if (ReturnTypeNode != null && !ReturnTypeNode.TigerType.Assignable(FunctionBodyNode.TigerType)) { report.AddError(SemanticErrors.IncompatibleFunctionReturnTypeBody(FunctionBodyNode, ReturnTypeNode.TigerType, FunctionBodyNode.TigerType)); } else if (ReturnTypeNode == null && !TigerType.AreOfSameType(TigerType.Void, FunctionBodyNode.TigerType)) { report.AddError(SemanticErrors.IncompatibleFunctionReturnTypeBody(FunctionBodyNode, TigerType.Void, FunctionBodyNode.TigerType)); } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children IdNode.CheckSemantics(scope, report); if (!IdNode.IsOK) { return; } //Check type name existence if (!scope.TypeNameAvailable(IdNode.Name)) { report.AddError(SemanticErrors.TypeNameAlreadyInUse(IdNode, IdNode.Name)); } else { RecordTigerType = new RecordType(IdNode.Name, ContainingScope.Name, null); scope.DefineIncompleteType(IdNode.Name, RecordTigerType); } IsOK = true; }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children IdNode.CheckSemantics(scope, report); TypeNode.CheckSemantics(scope, report); ExpressionNode.CheckSemantics(scope, report); if (!IdNode.IsOK || !TypeNode.IsOK || !ExpressionNode.IsOK) { return; } //Check use of variable name if (!scope.VariableNameAvailable(IdNode.Name)) { report.AddError(SemanticErrors.VariableNameAlreadyInUse(IdNode, IdNode.Name)); return; } //Check children types if (!TypeNode.TigerType.Assignable(ExpressionNode.TigerType)) { report.AddError(SemanticErrors.InvalidAssignType(ExpressionNode, TypeNode.TigerType, ExpressionNode.TigerType)); } //Add variable to scope VariableInfo = scope.DefineVariable(IdNode.Name, TypeNode.TigerType, ContainingScope); IsOK = true; }
public override void CheckBodySemantics(TigerScope scope, Report report) { //Check children IdNode.CheckSemantics(scope, report); FieldDeclarationNodes.ToList().ForEach(f => f.CheckSemantics(scope, report)); var usedNames = new List <string>(); foreach (var f in FieldDeclarationNodes) { if (usedNames.Contains(f.IdNode.Name)) { report.AddError(SemanticErrors.DuplicateFieldDeclaration(f, IdNode.Name, f.IdNode.Name)); } usedNames.Add(f.IdNode.Name); } //If type was not added to scope (if CheckSemantics failed) return if (!IsOK || FieldDeclarationNodes.Any(f => !f.IsOK)) { IsOK = false; return; } RecordTigerType = scope.FindType(IdNode.Name) as RecordType; IsOK = true; }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children LeftOperandNode.CheckSemantics(scope, report); RightOperandNode.CheckSemantics(scope, report); if (!LeftOperandNode.IsOK || !RightOperandNode.IsOK) { return; } //Check children types if (TigerType.AreCompatible(LeftOperandNode.TigerType, TigerType.Int) && TigerType.AreCompatible(RightOperandNode.TigerType, TigerType.Int)) { TigerType = TigerType.Int; } else if (!TigerType.AreCompatible(LeftOperandNode.TigerType, TigerType.Int)) { report.AddError(SemanticErrors.LogicalOperandInvalidType(LeftOperandNode, LeftOperandNode.TigerType)); } else { report.AddError(SemanticErrors.LogicalOperandInvalidType(RightOperandNode, RightOperandNode.TigerType)); } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children Condition.CheckSemantics(scope, report); IfBlock.CheckSemantics(scope, report); if (!Condition.IsOK && !IfBlock.IsOK) { return; } TigerType = TigerType.Void; //Check condition type if (!TigerType.AreCompatible(TigerType.Int, Condition.TigerType)) { report.AddError(SemanticErrors.InvalidConditionType(Condition, Condition.TigerType)); TigerType = TigerType.Error; } if (!TigerType.AreCompatible(TigerType.Void, IfBlock.TigerType)) { report.AddError(SemanticErrors.InvalidIfBodyType(IfBlock)); TigerType = TigerType.Error; } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; Value = Parse(); TigerType = TigerType.String; }
public TypeResolution(TypeDeclarationNode[] typeDeclarationNodes, TigerScope scope, Report report) { TypeDeclarationNodes = typeDeclarationNodes; Dictionary = new Dictionary <string, DisjointSet>(); Sets = new DisjointSet[TypeDeclarationNodes.Length]; Scope = scope; Report = report; }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children DeclarationBlockNodes.ToList().ForEach(d => d.CheckSemantics(scope, report)); IsOK = DeclarationBlockNodes.All(d => d.IsOK); }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; InstructionNode.CheckSemantics(scope, report); TigerType = InstructionNode.TigerType; IsOK = InstructionNode.IsOK; }
public override void CheckBodySemantics(TigerScope scope, Report report) { TypeAccessNode.CheckSemantics(scope, report); //If type was not added to scope (if CheckSemantics failed) return if (!TypeAccessNode.IsOK || !IsOK) { IsOK = false; return; } IsOK = true; }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; IdNode.CheckSemantics(scope, report); TypeNode.CheckSemantics(scope, report); if (!IdNode.IsOK || !TypeNode.IsOK) { return; } TigerType = TypeNode.TigerType; }
public override void CheckBodySemantics(TigerScope scope, Report report) { TypeAccessNode.CheckSemantics(scope, report); //If type was not added to scope (if CheckSemantics failed) return if (!TypeAccessNode.IsOK || !IsOK) { IsOK = false; return; } scope.CompleteType(IdNode.Name, new ArrayType(IdNode.Name, TypeAccessNode.TigerType)); IsOK = true; }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children IdNode.CheckSemantics(scope, report); ExpressionNode.CheckSemantics(scope, report); if (!IdNode.IsOK || !ExpressionNode.IsOK) { return; } TigerType = ExpressionNode.TigerType; }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Checking children VariableDeclarationNodes.ToList().ForEach(v => v.CheckSemantics(scope, report)); IsOK = true; foreach (var v in VariableDeclarationNodes.Where(v => !v.IsOK && !(v is ExplicitVariableDeclarationNode))) { scope.RemoveVariable(v.IdNode.Name); IsOK = false; } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; int val; if (!int.TryParse(Text, out val)) { report.AddError(SemanticErrors.InvalidIntegerConstant(this, Text)); } else { TigerType = TigerType.Int; Value = val; } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check type existence var type = scope.FindType(IdNode.Name); //Checking existence of array if (type != null) { TigerType = type is SimpleType ? ((SimpleType)type).ActualType : type; } else { report.AddError(SemanticErrors.NonExistentTypeReference(IdNode, IdNode.Name)); } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children InstructionNodes.ToList().ForEach(i => i.CheckSemantics(scope, report)); if (InstructionNodes.Any(e => !e.IsOK)) { return; } //If type is not void (no break found inside) assign last instruction type if (!TigerType.AreCompatible(TigerType, TigerType.Void)) { TigerType = InstructionNodes.Length == 0 ? TigerType.Void : InstructionNodes.Last().TigerType; } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children IdNode.CheckSemantics(scope, report); FromExpression.CheckSemantics(scope, report); ToExpression.CheckSemantics(scope, report); if (!IdNode.IsOK || !FromExpression.IsOK || !ToExpression.IsOK) { return; } //Check loop bounds type if (!TigerType.AreCompatible(FromExpression.TigerType, TigerType.Int) || !TigerType.AreCompatible(ToExpression.TigerType, TigerType.Int)) { report.AddError(!TigerType.AreCompatible(FromExpression.TigerType, TigerType.Int) ? SemanticErrors.InvalidForBoundType(FromExpression, FromExpression.TigerType) : SemanticErrors.InvalidForBoundType(ToExpression, ToExpression.TigerType)); return; } IsOK = true; //Define new scope TigerScope childScope = new TigerScope(scope, "For"); VariableInfo = childScope.DefineVariable(IdNode.Name, TigerType.Int, ContainingScope, false); DoInstruction.CheckSemantics(childScope, report); if (!DoInstruction.IsOK) { return; } if (!TigerType.AreCompatible(DoInstruction.TigerType, TigerType.Void)) { report.AddError(SemanticErrors.InvalidForBodyType(DoInstruction)); IsOK = false; } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children TypeNode.CheckSemantics(scope, report); RecordFieldInitNodes.ToList().ForEach(f => f.CheckSemantics(scope, report)); if (!TypeNode.IsOK || RecordFieldInitNodes.Any(f => !f.IsOK)) { return; } //Check children types if (!(TypeNode.TigerType is RecordType)) { report.AddError(SemanticErrors.NonRecordType(TypeNode, TypeNode.TigerType)); } else { TigerType = TypeNode.TigerType; var fields = ((RecordType)TypeNode.TigerType).RecordFields; //Check fields length if (fields.Length != RecordFieldInitNodes.Length) { report.AddError(SemanticErrors.WrongNumberOfFields(TypeNode, TypeNode.TigerType.Name, fields.Length, RecordFieldInitNodes.Length)); } //Check field names and types for (int i = 0; i < Math.Min(fields.Length, RecordFieldInitNodes.Length); i++) { if (fields[i].Name != RecordFieldInitNodes[i].IdNode.Name) { report.AddError(SemanticErrors.WrongFieldPosition(RecordFieldInitNodes[i].IdNode, fields[i].Name, RecordFieldInitNodes[i].IdNode.Name)); } if (!fields[i].TigerType.Assignable(RecordFieldInitNodes[i].TigerType)) { report.AddError(SemanticErrors.InvalidFieldType(RecordFieldInitNodes[i].IdNode, fields[i].TigerType, RecordFieldInitNodes[i].TigerType)); } } } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Checking children IdNode.CheckSemantics(scope, report); Arguments.ToList().ForEach(e => e.CheckSemantics(scope, report)); if (!IdNode.IsOK || Arguments.Any(e => !e.IsOK)) { return; } //Checking existence of function FunctionInfo = scope.FindFunction(IdNode.Name); if (FunctionInfo == null) { report.AddError(SemanticErrors.NonExistentFunctionReference(IdNode, IdNode.Name)); } else { TigerType = FunctionInfo.ReturnType; //Checking parameters if (Arguments.Length != FunctionInfo.ParameterNumber) { report.AddError(SemanticErrors.WrongNumberOfParameters(IdNode, FunctionInfo.Name, FunctionInfo.ParameterNumber, Arguments.Length)); } else { for (int i = 0; i < Arguments.Length; i++) { if (!FunctionInfo.Parameters[i].Assignable(Arguments[i].TigerType)) { report.AddError(SemanticErrors.InvalidArgumentType(Arguments[i], Arguments[i].TigerType, FunctionInfo.Parameters[i])); } } } } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Cheking children (function body semantics not checked) FunctionDeclarationNodes.ToList().ForEach(f => f.CheckSemantics(scope, report)); //Cheking children function body (notice all functions in block are already declared) FunctionDeclarationNodes.ToList().ForEach(f => f.CheckBodySemantics(scope, report)); foreach (var f in FunctionDeclarationNodes.Where(f => !f.IsOK && f.ReturnTypeNode == null)) { scope.RemoveVariable(f.IdNode.Name); IsOK = false; } if (FunctionDeclarationNodes.All(f => f.IsOK)) { IsOK = true; } }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; IsOK = true; //Check if break is in correct position var nodes = GetNodesToRoot().TakeWhile(x => !(x is MethodDeclarationNode)).ToList(); Owner = (nodes.FirstOrDefault(x => x is IBreakableNode)) as IBreakableNode; if (Owner == null) { report.AddError(SemanticErrors.BreakInIncorrectPostion(this)); return; } //Set containing InstructionSequenceNodes' types to void nodes.TakeWhile(x => !(x is IBreakableNode)).Where(n => n is InstructionSequenceNode).ToList() .ForEach(n => ((InstructionSequenceNode)n).TigerType = TigerType.Void); }
public override void CheckSemantics(TigerScope scope, Report report) { ContainingScope = scope; //Check children IdNode.CheckSemantics(scope, report); FunctionScope = new TigerScope(scope, IdNode.Name); ParameterDeclarationNodes.ToList().ForEach(p => p.CheckSemantics(FunctionScope, report)); if (!IdNode.IsOK || ParameterDeclarationNodes.Any(p => !p.IsOK)) { return; } //Check existence of function name if (!scope.FunctionNameAvailable(IdNode.Name)) { report.AddError(SemanticErrors.FunctionNameAlreadyInUse(IdNode, IdNode.Name)); return; } if (ReturnTypeNode != null) { ReturnTypeNode.CheckSemantics(scope, report); if (!ReturnTypeNode.IsOK) { return; } FunctionInfo = scope.DefineFunction(IdNode.Name, ReturnTypeNode.TigerType, ParameterDeclarationNodes.Select(p => p.TypeNode.TigerType).ToArray(), FunctionScope); } else { FunctionInfo = scope.DefineFunction(IdNode.Name, TigerType.Void, ParameterDeclarationNodes.Select(p => p.TypeNode.TigerType).ToArray(), FunctionScope); } IsOK = true; }