// FORMAT TO MATH MODEL PARSER public IExpressionType UnitImportFromFormatToModel(IFormat input, int listPosition) { Context ctx = Context.getInstance(); ctx.getIn(input.getType().ToString() + listPosition); //спускаемся в контекст блока List <IExpressionType> unitExpressions = new List <IExpressionType>(); for (int o = 0; o < input.getOperands().Count; o++) // преобразуем все выражения внутри блока { switch (input.getOperands()[o].getType()) { case Types.FuncDefinition: case Types.WhileU: case Types.ConditionU: unitExpressions.Add(UnitImportFromFormatToModel(input.getOperands()[o], o)); break; default: unitExpressions.Add(ExpressionImportFromFormatToModel(input.getOperands()[o])); break; } } // преобразуем выражение - условие IExpressionType options = null; if (input.getType() != Types.FuncDefinition) { options = input.getOptions() == null ? null : ExpressionImportFromFormatToModel(input.getOptions()); } ctx.getOut(); // построение самого блока switch (input.getType()) { case Types.GlobalU: return(new GlobalUnit(unitExpressions, listPosition)); case Types.WhileU: return(new WhileUnit(unitExpressions, options, listPosition)); case Types.FuncDefinition: string name = input.getOptions().getOptions().getValue(); List <Variable> lst = new List <Variable>(); foreach (var v in input.getOptions().getOperands()) { lst.Add(new Variable(v.getValue())); } FunctionDefenition fd = new FunctionDefenition(unitExpressions, name, lst, listPosition); ctx.addFunction(name, ctx.getCurrPath(), fd); return(fd); default: throw new Exception("Error in importing" + input.getType()); } }
private IExpressionType doCustomFunc(Function f, FunctionDefenition fd) { Context ctx = Context.getInstance(); Computator comp = new Computator(); Simplification s = new Simplification(); // вычисления кастомных функций if (f.getOperands().Count != fd.getParams().Count) { throw new Exception("Not equal number of params"); } ctx.getIn(fd.getContext()); // get into right context bool[] wasBefore = new bool[fd.getParams().Count]; IExpressionType[] expressions = new IExpressionType[fd.getParams().Count]; for (int p = 0; p < fd.getParams().Count; p++) // add params to scope { if (ctx.exists(fd.getParams()[p].getValue(), ctx.getCurrPath()) == -1) { wasBefore[p] = false; ctx.addVariable(fd.getParams()[p].getValue(), ctx.getCurrPath(), f.getOperands()[p]); } else { wasBefore[p] = true; expressions[p] = ctx.getVariableValue(fd.getParams()[p].getValue(), ctx.getCurrPath()); ctx.changeVariable(fd.getParams()[p].getValue(), ctx.getCurrPath(), f.getOperands()[p]); } } List <IExpressionType> nl = new List <IExpressionType>(); foreach (var op in fd.getOperands()) { nl.Add(op.doOperation(comp)); } for (int p = 0; p < fd.getParams().Count; p++) //remove params from scope { if (!wasBefore[p]) { ctx.removeVariable(fd.getParams()[p].getValue(), ctx.getCurrPath()); } else { ctx.changeVariable(fd.getParams()[p].getValue(), ctx.getCurrPath(), expressions[p]); } } //OUT ctx.getOut(); IExpressionType n = nl[nl.Count - 1]; return((IMathExpression)(n.getType() == Types.FuncExpression ? s.simplify((Function)n) : n)); }
public IExpressionType doFuncDefenition(FunctionDefenition fd) { return(fd); }