// 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()); } }
public IExpressionType ExpressionImportFromFormatToModel(IFormat expression) { List <IExpressionType> operands = new List <IExpressionType>(); Context ctx = Context.getInstance(); if (expression.getOperands() != null) { foreach (var operand in expression.getOperands()) { operands.Add(ExpressionImportFromFormatToModel(operand)); } } switch (expression.getType()) { case Types.Variable: Variable newVar = new Variable(expression.getValue()); ctx.addVariable(newVar.getValue(), ctx.getCurrPath(), null); return(newVar); case Types.Number: return(new Number(Convert.ToDouble(expression.getValue()))); case Types.VarDefinition: Equation ne = new Equation(operands, false); ctx.changeVariable(((Variable)operands[0]).getValue(), ctx.getCurrPath(), ne); return(ne); case Types.SuspendedVarDefinition: Equation nde = new Equation(operands, true); ctx.changeVariable(((Variable)operands[0]).getValue(), ctx.getCurrPath(), nde); return(nde); case Types.List: return(new MathList(operands)); case Types.Interval: return(new Interval(operands)); case Types.FuncExpression: return(new Function(expression.getType(), operands, expression.getOptions().getValue())); default: return(new Function(expression.getType(), operands, "")); } }