private AST.OclExpression CreateImplicitPropertyIterator(AST.OclExpression expr, IToken token, Classifier sourceType, Property property) { if (TestNull(expr, property)) { return(new AST.ErrorExp(Library.Invalid)); } var codeSource = new CodeSource(token); VariableDeclaration varDecl = new VariableDeclaration("", sourceType, null); List <VariableDeclaration> varList = new List <VariableDeclaration>(); varList.Add(varDecl); AST.VariableExp localVar = new AST.VariableExp(varDecl) .SetCodeSource(codeSource); AST.PropertyCallExp localProp = CheckAmbiguous(new AST.PropertyCallExp(localVar, false, null, null, property)) .SetCodeSource(codeSource); //Napevno zafixovany navratovy typ collect // JM > kdyz je typ Set(Class) nebo Bag(Class) tak by mel implicit collect vracet Bag(Class) Classifier returnType; if (property.Type is CollectionType) { returnType = Library.CreateCollection(CollectionKind.Bag, ((CollectionType)property.Type).ElementType); } else { returnType = Library.CreateCollection(CollectionKind.Collection, property.Type); } return(new AST.IteratorExp(expr, localProp, @"collect", varList, returnType) .SetCodeSource(codeSource)); }
VariableDeclaration ProcessVarDef(AST.OclExpression expr, IToken name, Classifier type, ref int pushedVar) { if (TestNull(expr)) { return(new VariableDeclaration("", Library.Invalid, new AST.ErrorExp(Library.Invalid))); } Classifier finalType; if (type == null) { if (expr.Type is CollectionType) { finalType = ((CollectionType)expr.Type).ElementType; } else { finalType = expr.Type; } } else { finalType = type; } return(ProcessTypeDef(name, finalType, null, ref pushedVar)); }
VariableDeclaration ProcessImplicitVarDef(AST.OclExpression expr, ref int pushedVar) { if (TestNull(expr)) { return(new VariableDeclaration("", Library.Invalid, new AST.ErrorExp(Library.Invalid))); } Classifier type; if (expr.Type is CollectionType) { type = ((CollectionType)expr.Type).ElementType; } else { type = expr.Type; } VariableDeclaration decl = new VariableDeclaration("", type, null); //add variable to EnviromentStack var env = Environment.CreateFromClassifier(decl.PropertyType, decl); EnvironmentStack.Push(env); //inc pushedVar to future pop EnviromentStack //pushedVar is ref variable pushedVar++; return(decl); }
/// <summary> /// /// </summary> /// <param name="source">Source expression of iterotor. Itertor is calling on this expression.</param> /// <param name="isPre">Is marked by pre</param> /// <param name="refOperation">Called operation</param> /// <param name="args">Parameters of operation</param> /// <param name="environment">Environment</param> public OperationCallExp(OclExpression source, bool isPre, Operation refOperation, List <OclExpression> args, Environment environment = null) : base(source, isPre, refOperation.GetReturnType(args)) { this.ReferredOperation = refOperation; this.Arguments = args; this.Environment = environment; }
AST.OclExpression ProcessPropertyCall(AST.OclExpression expr, List <IToken> tokenPath, bool isPre) { if (TestNull(expr, tokenPath)) { return(new AST.ErrorExp(Library.Invalid)); } List <string> path = tokenPath.ToStringList(); if (expr.Type is CollectionType) { Classifier sourceType = ((CollectionType)expr.Type).ElementType; Property property = sourceType.LookupProperty(path[0]); if (property != null) { return(CreateImplicitPropertyIterator(expr, tokenPath[0], sourceType, property)); } } else { Property property = expr.Type.LookupProperty(path[0]); if (property != null) { //36a return(CheckAmbiguous(new AST.PropertyCallExp(expr, isPre, null, null, property)) .SetCodeSource(new CodeSource(tokenPath[0]))); } } Errors.AddError(new CodeErrorItem(String.Format(CompilerErrors.OCLAst_ProcessPropertyCall_PropertyNotExists_1, path[0]), tokenPath.First(), tokenPath.Last())); return(new AST.ErrorExp(Library.Invalid)); }
AST.OclExpression ProcessIterate(AST.OclExpression rootExpr, IToken itToken, VariableDeclaration iteratorDecl, VariableDeclaration accDecl, AST.OclExpression body) { if (TestNull(rootExpr, iteratorDecl, accDecl, body)) { return(new AST.ErrorExp(Library.Invalid)); } return(new AST.IterateExp(rootExpr, body, iteratorDecl, accDecl) .SetCodeSource(new CodeSource(itToken))); }
public LoopExp(OclExpression source, OclExpression body, VariableDeclaration iterator, Classifier type) : base(source, type) { this.Body = body; List <VariableDeclaration> its = new List <VariableDeclaration>(); its.Add(iterator); this.Iterator = new List <VariableDeclaration>(its); }
VariableDeclaration CreateVariableDeclaration(IToken name, Classifier type, AST.OclExpression value) { if (TestNull(name)) { return(new VariableDeclaration("", Library.Invalid, new AST.ErrorExp(Library.Invalid))); } Classifier finallType = type; return(new VariableDeclaration(name.Text, type, value)); }
AST.TupleLiteralExp CreateTupleLiteral(IToken rootToken, List <VariableDeclarationBag> vars) { if (TestNull(rootToken, vars)) { TupleType tupleTypeErr = new TupleType(TypesTable, new List <Property>()); TypesTable.RegisterType(tupleTypeErr); return(new AST.TupleLiteralExp(new Dictionary <string, AST.TupleLiteralPart>(), tupleTypeErr) .SetCodeSource(new CodeSource(rootToken))); } Dictionary <string, AST.TupleLiteralPart> parts = new Dictionary <string, AST.TupleLiteralPart>(); List <Property> tupleParts = new List <Property>(); foreach (var var in vars) { if (parts.ContainsKey(var.Name)) { Errors.AddError(new CodeErrorItem(String.Format(CompilerErrors.OCLAst_CreateTupleLiteral_Name_repeated_1, var.Name), rootToken, rootToken)); continue; } AST.OclExpression expr = var.Expression; if (var.Expression == null) { expr = new AST.ErrorExp(Library.Invalid); } Classifier type = var.Type; if (type == null) { type = expr.Type; } if (expr.Type.ConformsTo(type) == false) { Errors.AddError(new ErrorItem(CompilerErrors.OCLAst_CreateTupleLiteral_Type_does_not_comform_to_declared_type)); } //hodnota var newProterty = new Property(var.Name, PropertyType.One, type); var newPart = new AST.TupleLiteralPart(newProterty, expr); parts.Add(var.Name, newPart); //typ tupleParts.Add(newProterty); } TupleType tupleType = new TupleType(TypesTable, tupleParts); TypesTable.RegisterType(tupleType); return(new AST.TupleLiteralExp(parts, tupleType) .SetCodeSource(new CodeSource(rootToken)));; }
AST.OclExpression ProcessIteratorCall(AST.OclExpression expr, List <IToken> tokenPath, List <VariableDeclaration> decls, List <AST.OclExpression> args) { if (TestNull(expr, tokenPath, decls, args)) { return(new AST.ErrorExp(Library.Invalid)); } // apply oclAsSet opretion on not collect type if (expr.Type is CollectionType == false) { Operation asSetOp = expr.Type.LookupOperation(Library.OclAsSet, new Classifier[0]); if (asSetOp == null) { Errors.AddError(new ErrorItem(String.Format(CompilerErrors.OCLParser_OperationNotFound_1, Library.OclAsSet))); return(new AST.ErrorExp(Library.Invalid)); } expr = new AST.OperationCallExp(expr, false, asSetOp, new List <AST.OclExpression>(), Environment) .SetCodeSource(new CodeSource(tokenPath[0])); } List <string> path = tokenPath.ToStringList(); if (path.Count != 1) { Errors.AddError(new CodeErrorItem(string.Format(CompilerErrors.OCLAst_ProcessIteratorCall_Unknow_iterator_operation), tokenPath.First(), tokenPath.Last())); return(new AST.ErrorExp(Library.Invalid)); } if (args.Count > 1) { Errors.AddError(new CodeErrorItem(CompilerErrors.OCLAst_ProcessIteratorCall_Iterator_ma_jenom_jedno_tělo_výrazu, tokenPath.First(), tokenPath.Last())); } string name = path[0]; IteratorOperation iteratorOperation = ((CollectionType)(expr.Type)).LookupIteratorOperation(name); // Iterator variable muze byt NULL -> pak se pouziji defaultni nazvy ... neni implementovano if (iteratorOperation != null) { // Pozadovany typ na telo iteratoru, podle pouzite funkce Classifier bodyType = iteratorOperation.BodyType(expr.Type as CollectionType, args[0].Type, TypesTable); if (args[0].Type.ConformsTo(bodyType) == false) { Errors.AddError(new CodeErrorItem(CompilerErrors.OCLAst_ProcessIteratorCall_Body_type_inconsistency, tokenPath.First(), tokenPath.Last())); } //Navratovy typ iteratoru podle pouzite operace Classifier returnType = iteratorOperation.ExpressionType(expr.Type as CollectionType, args[0].Type, TypesTable); return(new AST.IteratorExp(expr, args[0], name, decls, returnType) .SetCodeSource(new CodeSource(tokenPath[0]))); } Errors.AddError(new CodeErrorItem(string.Format(CompilerErrors.OCLAst_ProcessIteratorCall_Bad_iterator_operation_1, name), tokenPath.First(), tokenPath.Last())); return(new AST.ErrorExp(Library.Invalid)); }
AST.OclExpression ProcessOperationCall(AST.OclExpression expr, List <IToken> tokenPath, bool isPre, List <AST.OclExpression> args) { if (TestNull(expr, tokenPath)) { return(new AST.ErrorExp(Library.Invalid)); } // v pripade ze funkce ma nula argumentu neprovedese vytvoreni prazdneho listu v gramatice if (args == null) { args = new List <AST.OclExpression>(); } List <string> path = tokenPath.ToStringList(); if (path.Count == 1) { if (expr.Type is CollectionType) { //25b Classifier sourceType = ((CollectionType)expr.Type).ElementType; Operation op = sourceType.LookupOperation(path[0], args.Select(arg => arg.Type)); if (op != null) { return(CreateImplicitCollectIterator(expr, tokenPath[0], args, sourceType, op)); } } // JM 2.5. 2012 if (expr is AST.TypeExp) { Operation op = (((AST.TypeExp)expr).ReferredType).LookupOperation(path[0], args.Select(arg => arg.Type)); if (op != null) { return(new AST.OperationCallExp(expr, isPre, op, args, Environment) .SetCodeSource(new CodeSource(tokenPath[0]))); } } else { //35eg IEnumerable <Classifier> parameterTypes = args.Select(arg => arg.Type); Operation op = expr.Type.LookupOperation(path[0], parameterTypes); if (op != null) { return(new AST.OperationCallExp(expr, isPre, op, args, Environment) .SetCodeSource(new CodeSource(tokenPath[0]))); } } } Errors.AddError(new CodeErrorItem(string.Format(CompilerErrors.OCLAst_ProcessOperationCall_OperationNotExists_1, path.First()), tokenPath.First(), tokenPath.Last())); return(new AST.ErrorExp(Library.Invalid)); }
AST.ClassLiteralExp CreateClassLiteral(IToken rootToken, List <VariableDeclarationBag> vars, List <IToken> tokenPath) { Classifier createdClass = ResolveTypePathName(tokenPath); if (createdClass == null || createdClass == Library.Invalid) { Errors.AddError(new CodeErrorItem(String.Format("Unable to create type '{0}'.", tokenPath.ConcatWithSeparator(@"::")), rootToken, rootToken)); } Dictionary <string, AST.TupleLiteralPart> parts = new Dictionary <string, AST.TupleLiteralPart>(); List <Property> classParts = new List <Property>(); foreach (var var in vars) { if (parts.ContainsKey(var.Name)) { Errors.AddError(new CodeErrorItem(String.Format(CompilerErrors.OCLAst_CreateTupleLiteral_Name_repeated_1, var.Name), rootToken, rootToken)); continue; } AST.OclExpression expr = var.Expression; if (var.Expression == null) { expr = new AST.ErrorExp(Library.Invalid); } Classifier type = var.Type; if (type == null) { type = expr.Type; } if (expr.Type.ConformsTo(type) == false) { Errors.AddError(new ErrorItem(CompilerErrors.OCLAst_CreateTupleLiteral_Type_does_not_comform_to_declared_type)); } //hodnota var newProterty = new Property(var.Name, PropertyType.One, type); var newPart = new AST.TupleLiteralPart(newProterty, expr); parts.Add(var.Name, newPart); //typ classParts.Add(newProterty); } return(new AST.ClassLiteralExp(parts, createdClass) .SetCodeSource(new CodeSource(rootToken))); }
AST.OclExpression CreateLet(IToken letToken, VariableDeclaration decl, AST.OclExpression inExpr) { if (TestNull(decl, inExpr)) { return(new AST.ErrorExp(Library.Invalid)); } //?????? proc to tady bylo //if (decl == null) { // return inExpr; //} return(new AST.LetExp(decl, inExpr) .SetCodeSource(new CodeSource(letToken)));; }
bool IsIteratorOperation(AST.OclExpression expr, List <IToken> tokenPath) { if (tokenPath == null && tokenPath.Count != 1) { return(false); } if (expr.Type is InvalidType) { return(false); } string name = tokenPath[0].Text; IteratorOperation iteratorOperation = ((CollectionType)(expr.Type)).LookupIteratorOperation(name); return(iteratorOperation != null); }
VariableDeclaration ProcessTypeDef(IToken name, Classifier type, AST.OclExpression initExpr, ref int pushedVar) { if (TestNull(name)) { return(new VariableDeclaration("", Library.Invalid, new AST.ErrorExp(Library.Invalid))); } VariableDeclaration decl = new VariableDeclaration(name.Text, type, null); //add variable to EnviromentStack var env = Environment.AddElement(decl.Name, decl.PropertyType, decl, true); EnvironmentStack.Push(env); //inc pushedVar to future pop EnviromentStack //pushedVar is ref variable pushedVar++; return(decl); }
AST.OclExpression ProcessCollectionOperationCall(AST.OclExpression expr, List <IToken> tokenPath, List <AST.OclExpression> args) { if (TestNull(expr, tokenPath)) { return(new AST.ErrorExp(Library.Invalid)); } // v pripade ze funkce ma nula argumentu neprovedese vytvoreni prazdneho listu v gramatice if (args == null) { args = new List <AST.OclExpression>(); } List <string> path = tokenPath.ToStringList(); if (path.Count != 1) { Errors.AddError(new CodeErrorItem(CompilerErrors.OCLAst_ProcessIteratorCall_Unknow_iterator_operation, tokenPath.First(), tokenPath.Last())); return(new AST.ErrorExp(Library.Invalid)); } string name = path[0]; // apply oclAsSet opretion on not collect type if (expr.Type is CollectionType == false) { Operation asSetOp = expr.Type.LookupOperation(Library.OclAsSet, new Classifier[0]); if (asSetOp == null) { Errors.AddError(new ErrorItem(String.Format(CompilerErrors.OCLParser_OperationNotFound_1, Library.OclAsSet))); return(new AST.ErrorExp(Library.Invalid)); } expr = new AST.OperationCallExp(expr, false, asSetOp, new List <AST.OclExpression>(), Environment) .SetCodeSource(new CodeSource(tokenPath[0])); } Operation collectionOperation = expr.Type.LookupOperation(name, args.Select(arg => arg.Type)); if (collectionOperation != null) // 36b { return(new AST.OperationCallExp(expr, false, collectionOperation, args, Environment) .SetCodeSource(new CodeSource(tokenPath[0]))); } Errors.AddError(new CodeErrorItem(String.Format(CompilerErrors.OCLAst_ProcessCollectionOperationCall_Unknown_collection_operation, name), tokenPath.First(), tokenPath.Last())); return(new AST.ErrorExp(Library.Invalid)); }
AST.OclExpression UnaryOperation(IToken name, AST.OclExpression exp1) { if (TestNull(name, exp1)) { return(new AST.ErrorExp(Library.Invalid)); } Operation op = exp1.Type.LookupOperation(name.Text, new Classifier[] { }); if (op == null) { Errors.AddError(new CodeErrorItem(String.Format(CompilerErrors.OCLAst_InfixOperation_NotDefined_2, exp1.Type, name), name, name)); return(new AST.ErrorExp(Library.Any)); } return(new AST.OperationCallExp(exp1, false, op, new List <AST.OclExpression>(new AST.OclExpression[] { }), Environment) .SetCodeSource(new CodeSource(name))); }
VariableDeclaration ProcessAccDef(IToken name, Classifier type, AST.OclExpression initExpr, ref int pushedVar) { if (TestNull(name, initExpr)) { return(new VariableDeclaration("", Library.Any, new AST.ErrorExp(Library.Invalid))); } Classifier finalType = initExpr.Type; if (type != null) { if (initExpr.Type.ConformsTo(type) == false) { Errors.AddError(new ErrorItem(CompilerErrors.OCLAst_ProcessAccDef_Init_value_do_not_conform_to_variable_type)); } finalType = type; } return(ProcessTypeDef(name, finalType, initExpr, ref pushedVar)); }
AST.OclExpression CreateIf(IToken ifTok, AST.OclExpression condition, AST.OclExpression th, AST.OclExpression el) { if (TestNull(ifTok, condition, th, el)) { return(new AST.ErrorExp(Library.Invalid)); } if (condition.Type.ConformsTo(Library.Boolean) == false) { Errors.AddError(new CodeErrorItem(CompilerErrors.OCLAst_CreateIf_Condition_of_IF_must_conform_to_bool, ifTok, ifTok)); } //EnvironmentStack.Pop(); Classifier exprType = th.Type.CommonSuperType(el.Type); return(new AST.IfExp(exprType, condition, th, el) .SetCodeSource(new CodeSource(ifTok)));; }
private AST.OclExpression CreateImplicitCollectIterator(AST.OclExpression expr, IToken token, List <AST.OclExpression> args, Classifier sourceType, Operation op) { if (TestNull(expr, args, sourceType, op)) { return(new AST.ErrorExp(Library.Invalid)); } var codeSource = new CodeSource(token); VariableDeclaration varDecl = new VariableDeclaration(string.Empty, sourceType, null); List <VariableDeclaration> varList = new List <VariableDeclaration>(); varList.Add(varDecl); AST.VariableExp localVar = new AST.VariableExp(varDecl) .SetCodeSource(codeSource); AST.OperationCallExp localOp = new AST.OperationCallExp(localVar, false, op, args, Environment) .SetCodeSource(codeSource); //Napevno zafixovany navratovy typ collect Classifier returnType = Library.CreateCollection(CollectionKind.Collection, op.ReturnType); return(new AST.IteratorExp(expr, localOp, @"collect", varList, returnType) .SetCodeSource(codeSource)); }
public LoopExp(OclExpression source, OclExpression body, List <VariableDeclaration> iterators, Classifier type) : base(source, type) { this.Body = body; this.Iterator = iterators; }
public NavigationCallExp(OclExpression source, bool isPre, Property navigationSource, OclExpression qualifier, Classifier returnType) : base(source, isPre, returnType) { this.NavigationSource = navigationSource; this.Qualifier = qualifier; }
/// <summary> /// /// </summary> /// <param name="type">Return type of If expression.</param> /// <param name="condition">If condition</param> /// <param name="thenExpr"></param> /// <param name="elseExpr"></param> public IfExp(Classifier type, OclExpression condition, OclExpression thenExpr, OclExpression elseExpr) : base(type) { this.Condition = condition; this.ThenExpression = thenExpr; this.ElseExpression = elseExpr; }
public string AstToString(OclExpression expr) { sb = new StringBuilder(); expr.Accept(this); return(sb.ToString()); }
/// <summary> /// /// </summary> /// <param name="source">Source expression of iterotor. Itertor is calling on this expression.</param> /// <param name="isPre">Is marked by pre</param> /// <param name="navigationSource">Source of navigation. Example: expr.navigationSource::referedProperty</param> /// <param name="qualifier">Qulifier of property. Example: expr.referedProperty[qualifier]</param> /// <param name="referredProperty">Destination property</param> public PropertyCallExp(OclExpression source, bool isPre, Property navigationSource, OclExpression qualifier, Property referredProperty) : base(source, isPre, navigationSource, qualifier, referredProperty.Type) { this.ReferredProperty = referredProperty; }
/// <summary> /// /// </summary> /// <param name="variable">Substitute variable</param> /// <param name="inExpr">Substitute expression in expression</param> public LetExp(Types.VariableDeclaration variable, OclExpression inExpr) : base(inExpr.Type) { this.Variable = variable; this.InExpression = inExpr; }
/// <summary> /// /// </summary> /// <param name="source">Source expression of iterotor. Itertor is calling on this expression.</param> /// <param name="body">Iterate expression</param> /// <param name="iterator">Iterator variable</param> /// <param name="resultVariable">Accumulate variable</param> public IterateExp(OclExpression source, OclExpression body, VariableDeclaration iterator, VariableDeclaration resultVariable) : base(source, body, iterator, resultVariable.PropertyType) { this.Result = resultVariable; }
public CallExp(OclExpression source, Classifier type) : base(type) { this.Source = source; }
public PropertyInitialization(OclExpression initializationExpression) { InitializationExpression = initializationExpression; }
public InvariantWithMessage(OclExpression constraint) { Constraint = constraint; }