コード例 #1
0
ファイル: Exp1Parser.cs プロジェクト: kai-liki/Study
        private string[] GetAnXExp(Context context, string[] tokens)
        {
            List<string> target = new List<string>();

            foreach (string token in tokens)
            {
                if (context.XName.Equals(token))
                {
                    target.Add(token);
                }
                else if (context.Variables.Keys.Contains(token))
                {
                    target.AddRange(GetAnXExp(context, token));
                }
                else
                {
                    if (Regex.IsMatch(token, REGEX_NUMBER) || Regex.IsMatch(token, REGEX_OPERATOR))
                    {
                        target.Add(token);
                    }
                    else
                    {
                        throw new Exception(string.Format("Variable {0} has never been defined.", token));
                    }
                }
            }

            return target.ToArray();
        }
コード例 #2
0
ファイル: LinearTaxEngine.cs プロジェクト: kai-liki/Study
        private static Result Calculate(Context context)
        {
            decimal? x;
            Equation e = context.CalcEquation;
            if (e != null)
            {
                ANXResult lresult = e.LeftTokens.Calculate(context.XName);
                ANXResult rresult = e.RightTokens.Calculate(context.XName);
                e.Item0 = lresult.Item0 - rresult.Item0;
                e.Item1 = lresult.Item1 - rresult.Item1;
                x = e.GetX();
            }
            else
            {
                x = null;
            }
            Dictionary<string, ResultInfo> results = new Dictionary<string, ResultInfo>();
            foreach (KeyValuePair<string, Variable> vp in context.Variables)
            {
                string vname = vp.Key;
                Variable v = vp.Value;
                ResultInfo resultinfo = new ResultInfo();
                resultinfo.Name = vname;
                resultinfo.Amount = v.GetVariableValue(x, context.XName);
                results[vname] = resultinfo;
            }

            Result result = new Result(results, x);
            return result;
        }
コード例 #3
0
ファイル: Exp1Parser.cs プロジェクト: kai-liki/Study
        public void Parse(Context context)
        {
            context.Status = ContextStatus.Exp1Parsing;

            varstatus = new Dictionary<Variable, VariableStatus>();

            StringBuilder sb = new StringBuilder();
            foreach (string varname in context.Variables.Keys)
            {
                sb.Clear();
                context.Variables[varname].Tokens = GetAnXExp(context, varname);
                foreach (string token in context.Variables[varname].Tokens)
                {
                    sb.Append(token);
                }
                context.Variables[varname].Exp1 = sb.ToString();
            }

            Equation equation = context.CalcEquation;
            if (equation != null)
            {
                equation.LeftTokens = GetAnXExp(context, equation.LeftTokens);
                sb.Clear();
                foreach (string token in equation.LeftTokens)
                {
                    sb.Append(token);
                }
                equation.LExp = sb.ToString();

                equation.RightTokens = GetAnXExp(context, equation.RightTokens);
                sb.Clear();
                foreach (string token in equation.RightTokens)
                {
                    sb.Append(token);
                }
                equation.RExp = sb.ToString();
            }
            context.Status = ContextStatus.Exp1Parsed;
        }
コード例 #4
0
ファイル: RPNParser.cs プロジェクト: kai-liki/Study
        public void Parse(Context context)
        {
            context.Status = ContextStatus.RPNParsing;
            StringBuilder sb = new StringBuilder();
            foreach (Variable v in context.Variables.Values)
            {
                sb.Clear();
                v.Tokens = new CTree(v.Tokens, 0, v.Tokens.Length - 1).PostorderTrav();
                foreach (string token in v.Tokens)
                {
                    sb.Append(token);
                }
                v.RPN = sb.ToString();
            }

            Equation e = context.CalcEquation;
            if (e != null)
            {
                e.LeftTokens = new CTree(e.LeftTokens, 0, e.LeftTokens.Length - 1).PostorderTrav();
                sb.Clear();
                foreach (string token in e.LeftTokens)
                {
                    sb.Append(token);
                }
                e.LRPN = sb.ToString();

                e.RightTokens = new CTree(e.RightTokens, 0, e.RightTokens.Length - 1).PostorderTrav();
                sb.Clear();
                foreach (string token in e.RightTokens)
                {
                    sb.Append(token);
                }
                e.RRPN = sb.ToString();
            }

            context.Status = ContextStatus.RPNParsed;
        }
コード例 #5
0
ファイル: ExpParser.cs プロジェクト: kai-liki/Study
        public Context Parse(string text)
        {
            StringBuilder sb = new StringBuilder();
            char[] buffer = new char[1];

            Context context = new Context(text);

            context.Status = ContextStatus.ExpParsing;

            using (StringReader sr = new StringReader(text))
            {
                while (sr.Peek() != -1)
                {
                    string line = sr.ReadLine().Trim();

                    if (line.Length <= 0)
                    {
                        continue;
                    }
                    else if (line.StartsWith(DEFINESYMBOL.ToString()))
                    {
                        AssertMatch(line, REGEX_DEFINEEXPRESSION, "Invalid X name defination");
                        if (line.Length <= 1)
                        {
                            throw new Exception("Invalid X name defination");
                        }
                        line = line.Substring(1);
                        AssertMatch(line, REGEX_VARNAME, "Invalid X name");
                        if (context.Variables.Count > 0 || context.CalcEquation != null)
                        {
                            throw new Exception("X name defination must be put in front of any variable defination or equation defination.");
                        }
                        xname = line;
                        context.UpdateXName(this);
                    }
                    else if (line.StartsWith(EQUATIONOPEN.ToString()))
                    {
                        AssertMatch(line, REGEX_FULLEQUATION, "Invalid equation defination");

                        line = line.Substring(1, line.Length - 2).Trim();

                        AssertMatch(line, REGEX_EQUATION, "Invalid equation");

                        Equation equation = new Equation();
                        equation.Exp = line;

                        string[] exps = line.Split('=');
                        string leftexp = exps[0].Trim();
                        string rightexp = exps[1].Trim();
                        equation.LeftTokens = Tokenize(leftexp);
                        equation.RightTokens = Tokenize(rightexp);

                        context.CalcEquation = equation;
                    }
                    else
                    {
                        AssertMatch(line, REGEX_ASSIGNMENT, "Invalid variable assignment");
                        
                        string[] exps = line.Split('=');
                        string varname = exps[0].Trim();
                        string expression = exps[1].Trim();

                        AssertMatch(expression, REGEX_EXPRESSION, "Invalid variable expression");

                        if (context.Variables.Keys.Contains(varname))
                        {
                            throw new Exception(string.Format("Variable {0} has already been defined.", varname));
                        }

                        Variable variable =new Variable();
                        variable.Exp = expression;
                        variable.Tokens = Tokenize(expression);
                        context.Variables[varname] = variable;
                        
                    }
                }
            }

            context.Status = ContextStatus.ExpParsed;
            return context;
        }
コード例 #6
0
ファイル: Exp1Parser.cs プロジェクト: kai-liki/Study
        private string[] GetAnXExp(Context context, string varname)
        {
            if(!context.Variables.Keys.Contains(varname) && !context.XName.Equals(varname))
            {
                throw new Exception(string.Format("Variable {0} has not been defined.", varname));
            }

            Variable variable = context.Variables[varname];
            if (varstatus.Keys.Contains(variable) && varstatus[variable] == VariableStatus.Exp1Parsed)
            {
                return variable.Tokens;
            }
            else if (varstatus.Keys.Contains(variable) && varstatus[variable] == VariableStatus.Exp1Parsing)
            {
                throw new Exception(string.Format("Loop occur for variable {0}", varname));
            }
            else
            {
                varstatus[variable] = VariableStatus.Exp1Parsing;
            }

            List<string> target = new List<string>();

            string[] tokens = variable.Tokens;

            foreach (string token in tokens)
            {
                if (context.XName.Equals(token))
                {
                    target.Add(token);
                }
                else if (context.Variables.Keys.Contains(token))
                {
                    target.AddRange(GetAnXExp(context, token));
                }
                else
                {
                    if (Regex.IsMatch(token, REGEX_NUMBER) || Regex.IsMatch(token, REGEX_OPERATOR))
                    {
                        target.Add(token);
                    }
                    else
                    {
                        throw new Exception(string.Format("Variable {0} has never been defined.", token));
                    }
                }
            }

            variable.Tokens = target.ToArray();
            varstatus[variable] = VariableStatus.Exp1Parsed;
            return variable.Tokens;
        }