/// <summary> /// /// </summary> /// <param name="parent"></param> /// <param name="res"></param> /// <param name="index"></param> /// <returns>the index of the end of the sequence</returns> private int CompileSequence(CalculatorToken parent, out CalculatorToken res, int index) { int end = -1; res = null; for (int i = index; i < _calculatorTokens.Count; i++) { var token = _calculatorTokens[i] as CalculatorTokenSequence; var seq = token; if (seq != null && seq.Sequence != CalculatorTokenSequence.TokenSequence.StartSequence) { continue; } end = CompileCalculatorToken(out res, i + 1) + 1; _calculatorTokens.RemoveRange(i, end - i); return i; } return end; }
/// <summary> /// /// </summary> /// <param name="token"></param> /// <param name="index"></param> /// <returns></returns> private int CompileCalculatorToken(out CalculatorToken token, int index) { token = _calculatorTokens[index]; if (token is CalculatorTokenBinaryOperator) { index = CompileBinaryOperator(token as CalculatorTokenBinaryOperator, index + 1); } else if (token is CalculatorTokenSequence) { CalculatorToken res; index = CompileSequence(token as CalculatorTokenSequence, out res, index + 1); token = res; } return index; }
/// <summary> /// /// </summary> /// <param name="token"></param> internal void AddCalculator(CalculatorToken token) { _calculatorTokens.Add(token); }
/// <summary> /// /// </summary> /// <param name="br"></param> /// <param name="option"></param> public override void Load(BinaryReader br, SaveOption option) { _operator = (BinaryOperator)br.ReadInt32(); _left = Calculator.CreateCalculatorToken(Calculator, br, option); _right = Calculator.CreateCalculatorToken(Calculator, br, option); }
/// <summary> /// /// </summary> /// <param name="el"></param> /// <param name="option"></param> public override void Load(XmlNode el, SaveOption option) { _operator = (BinaryOperator) int.Parse(el.Attributes["operator"].Value); XmlNode child = (XmlNode)el.SelectSingleNode("Left/Node"); _left = Calculator.CreateCalculatorToken(Calculator, child, option); child = (XmlNode)el.SelectSingleNode("Right/Node"); _right = Calculator.CreateCalculatorToken(Calculator, child, option); }
/// <summary> /// /// </summary> /// <param name="el"></param> /// <param name="option"></param> public void Load(XmlNode el, SaveOption option) { _root = null; XmlNode root = el.SelectSingleNode("Root/Node"); if (root != null) { _root = CreateCalculatorToken(this, root, option); } }