//http://pl.wikipedia.org/wiki/Odwrotna_notacja_polska#Algorytm_obliczenia_warto.C5.9Bci_wyra.C5.BCenia_ONP private variable CalulateRPNStatement(List<Token> tokensInRPN, IDictionary<string, variable> localVariables, IDictionary<string, variable> inheritedVariables) { Stack<Token> stack = new Stack<Token>(); foreach (Token token in tokensInRPN) { if (token.GetType() == typeof(variable)) { stack.Push(token); } else if (token.GetType() == typeof(LeftSideOper) || token.GetType() == typeof(RightSideOper)) { if (stack.Count < ((Oper)token).noOfArguments) throw new ApplicationException("no of needed arguments is bigger than no of tokens on stack"); variable[] parameters = new variable[((Oper)token).noOfArguments]; for (int i = 0; i < ((Oper)token).noOfArguments; i++) { if(stack.Peek().GetType() != typeof(variable)) throw new ApplicationException("wrong no of arguments in stack"); parameters[i] = (variable)stack.Pop(); } stack.Push(((Oper)token).Execute(parameters)); } else if (token.GetType() == typeof(Function)) { if (stack.Count < ((Function)token).agrumgensNo) throw new ApplicationException("no of needed arguments is bigger than no of tokens on stack"); variable[] arguments = new variable[((Function)token).agrumgensNo]; for (int i = 0; i < ((Function)token).agrumgensNo; i++) { if (stack.Peek().GetType() != typeof(Function)) throw new ApplicationException("wrong no of arguments in stack"); arguments[i] = (variable)stack.Pop(); } stack.Push(((Function)token).Execute(arguments)); } else { throw new ApplicationException("unhandled token"); } } if (stack.Count != 1) throw new ApplicationException("there should be only 1 value on stack"); if (stack.Peek().GetType() != typeof(variable)) throw new ApplicationException("last token on stack should be a variable"); return (variable)stack.Pop(); }
public override variable Execute(variable[] arguments) { switch (code) { case "+": return new variable(arguments[1].value + arguments[0].value); case "*": return new variable(arguments[1].value * arguments[0].value); default: throw new ApplicationException("unresolved operator"); } }
public abstract variable Execute(variable[] arguments);
public override variable Execute(variable[] arguments) { throw new ApplicationException("you cannot execute bracket!"); }
public override variable Execute(variable[] arguments) { switch (code) { case "=": arguments[1].value = arguments[0].value; return arguments[1]; default: throw new ApplicationException("unresolved operator"); } }
public variable Execute(variable[] arguments) { throw new NotImplementedException(); }