Ejemplo n.º 1
0
        private static void Command(ListController <TokenModel> lc)
        {
            if (lc.TryGetNext(out var first))
            {
                if (Keywords.Types.Contains(first.Text))
                {
                    var    varType = first.Text;
                    string varName = Variable(lc);
                    VarTable.Add(varName, varType);
                }
                else if (VarTable.Contains(first.Text))
                {
                    if (!lc.TryGetNext(out var eq))
                    {
                        throw new ParseException("= expected", lc.GetPrevious(), true);
                    }

                    if (eq.Text != "=")
                    {
                        throw new ParseException("= expected", lc.GetPrevious(), true);
                    }
                    AssingNode asNode = new AssingNode();
                    asNode.Left = new VarNode()
                    {
                        Variable = VarTable.Get(first.Text),
                    };


                    if (!lc.TryGetNext(out var next))
                    {
                        throw new ParseException("Function or expression expected", lc.GetPrevious(), true);
                    }

                    if (FuncTable.Contains(next.Text))
                    {
                        lc.SetBack();
                        asNode.Right = Func(lc);
                    }
                    else if (VarTable.Contains(next.Text) || next.Text.All(c => Constants.Numbers.Contains(c)))
                    {
                        lc.SetBack();
                        //todo arithmetic
                        throw new NotImplementedException("Arithmetic don't included, for now");
                    }
                    else
                    {
                        throw new ParseException("Function or expression expected", lc.GetPrevious(), true);
                    }
                }
            }
        }
Ejemplo n.º 2
0
 public static TokenModel TryGetNext(ListController <TokenModel> lc, string errorMessage)
 {
     if (lc.TryGetNext(out var ret))
     {
         return(ret);
     }
     throw new ParseException(errorMessage, lc.GetPrevious(), true);
 }
Ejemplo n.º 3
0
        private static FunctionNode Func(ListController <TokenModel> lc)
        {
            if (!lc.TryGetNext(out var funcName))
            {
                throw new ParseException("Function expected", lc.GetPrevious(), true);
            }

            if (!FuncTable.Contains(funcName.Text))
            {
                throw new ParseException("Unknown function", lc.GetPrevious(), true);
            }

            var resNode = new FunctionNode();

            resNode.FuncName = funcName.Text;
            if (!lc.TryGetNext(out var opNest))
            {
                throw new ParseException();
            }
            else
            {
                if (opNest.Text == "(")
                {
                    while (true)
                    {
                        if (lc.TryGetNext(out var next))
                        {
                            if (next.Text == ")")
                            {
                                break;
                            }

                            if ()
                            {
                            }
                        }
                    }
                }
            }

            //todo parse tokens to node
        }
Ejemplo n.º 4
0
        public static void Lang(ListController <TokenModel> lc)
        {
            while (true)
            {
                Command(lc);
                if (!lc.TryGetNext(out var sc))
                {
                    throw new ParseException("; expected", lc.GetPrevious(), true);
                }

                if (sc.Text != ";")
                {
                    throw new ParseException("; expected", lc.GetPrevious(), true);
                }

                if (!lc.TryGetNext(out _))
                {
                    return;
                }

                lc.SetBack();
            }
        }
Ejemplo n.º 5
0
        private static string Variable(ListController <TokenModel> lc)
        {
            if (!lc.TryGetNext(out var variable))
            {
                throw new ParseException("Variable name expected", lc.GetPrevious(), true);
            }

            if (!Constants.Alphabet.Contains(variable.Text[0]))
            {
                throw new ParseException($"Invalid token '{variable.Text[0]}' in variable name", variable);
            }

            return(variable.Text);
        }