public override bool Validate(L1Specializer.Metadata.SymbolTableLight table) { if (DeclareVariable != String.Empty) { if (VariableType.TypeEnum != VariableTypeEnum.Integer && VariableType.TypeEnum != VariableTypeEnum.Char) { CompilerServices.AddError( Location, "Cycle variable must be type of int or char!" ); return false; } if (table.TryGetSymbol(DeclareVariable) != null) { CompilerServices.AddError( Location, "Declared cycle variable already exists!" ); return false; } bool valid = Init.Validate(table); if (valid) { if (!CompilerServices.IsAssignable(VariableType, Init.ResultType)) { CompilerServices.AddError( Location, "Cycle variable initialization expression has different type!" ); return false; } } else return false; } else { if (Init.LeftNode.LeafType != ExpressionLeafType.VariableAccess) { CompilerServices.AddError( Location, "Error in cycle definition: no variable reference presented!" ); return false; } bool valid = Init.Validate(table); if (!valid) return false; } bool validStep = Step.Validate(table); bool validEnd = EndValue.Validate(table); if (validEnd && validStep) { if (Step.ResultType.TypeEnum != VariableTypeEnum.Integer) { CompilerServices.AddError( Location, "Cycle step must be type of int!" ); return false; } if (EndValue.ResultType.TypeEnum != VariableTypeEnum.Integer && EndValue.ResultType.TypeEnum != VariableTypeEnum.Char) { CompilerServices.AddError( Location, "Cycle end value must be type of int or char!" ); return false; } } else return false; SymbolTableLight newTable = new SymbolTableLight(table); if (DeclareVariable != String.Empty) { newTable.AddSymbol(new SymbolLight(DeclareVariable, VariableType)); } bool res = CompilerServices.ValidateStatementList(Statements, newTable); return res; }
public static void SemanticAnalise(L1Program program) { foreach (FunctionDefinition functionDef in program.Functions) { if (functionDef.IsEmbedded) continue; SymbolTableLight table = new SymbolTableLight(); foreach (FunctionParameter parameter in functionDef.Header.Parameters) { SymbolLight symbol = new SymbolLight(parameter.Name, parameter.Type); table.AddSymbol(symbol); } f_currentFunction = functionDef; CheckForLabelsDuplicates(f_currentFunction.Statements); ValidateStatementList(functionDef.Statements, table); if (functionDef.Header.ReturnType != null && !HasReturn(functionDef.Statements)) { CompilerServices.AddError( functionDef.Location, "Not all branches of execution return value" ); } } }