示例#1
0
        public void Parser(string input)
        {
            try
            {
                Match match = Regex.Match(input, regex_match);

                if (match.Success)
                {
                    // check to see if we are assigning a constant
                    if (char.Parse(match.Groups[2].Value) == '=')
                    {
                        string key = match.Groups[1].Value;
                        int    val = int.Parse(match.Groups[3].Value);
                        consts.AddConstantsToDictionary(key, val);
                        storedConstant = true;
                    }
                    else
                    {
                        int _term1;
                        if (!int.TryParse(match.Groups[1].Value, out _term1))
                        {
                            string key = match.Groups[1].Value;
                            _term1 = consts.GetConstant(key);
                        }
                        term_1 = _term1;

                        _operator = char.Parse(match.Groups[2].Value);

                        int _term2;
                        if (!int.TryParse(match.Groups[3].Value, out _term2))
                        {
                            string key = match.Groups[3].Value;
                            _term2 = consts.GetConstant(key);
                        }
                        term_2 = _term2;
                    }
                }
                else
                {
                    throw new InvalidOperationException("Try a 2 term expression");
                }
            }
            catch (InvalidOperationException)
            {
                invalidEntry = true;
            }
        }