Beispiel #1
0
        public override void Action()
        {
            Expression w = (Expression)vars[IdName];

            if (w == null)
            {
                string err = "The name '" + IdName + "' does not exist in the current context ";
                //MessageBox.Show(err);
                throw new Exception(err);
            }

            Expression retExpr = null;

            if (expr.Type == ExpressionTypes.Number)
            {
                retExpr = new NumbExpr(Convert.ToDouble(expr.Value()));
            }
            else
            if (expr.Type == ExpressionTypes.String)
            {
                retExpr = new StrExpr(Convert.ToString(expr.Value()));
            }
            else
            if (expr.Type == ExpressionTypes.Var)
            {
                //ExpressionTypes exType=vars[]
                //retExpr = new StrExpr(Convert.ToString(expr.Value()));
                retExpr = (Expression)vars[((IDExpr)expr).Name];
            }

            vars[IdName] = retExpr;
        }
Beispiel #2
0
        private Expression matchSTROPERATOR(Expression litr1)
        {
            int operator_indx = match((int)TokenTags.STROPERATOR);

            Expression litr2 = expression();

            if (litr2.Type == ExpressionTypes.Number)
            {
                litr2 = new StrExpr(Convert.ToString(litr2.Value()));
            }
            Expression oprtr;

            switch (((Word)TokenList[operator_indx]).lexeme)
            {
            case "$+":
                oprtr = new Pluss(ExpressionTypes.String, litr1, litr2);
                break;

            case "$==":
                oprtr = new Equal(ExpressionTypes.String, litr1, litr2);
                break;

            case "$!=":
                oprtr = new NotEqual(ExpressionTypes.String, litr1, litr2);
                break;

            default: throw new Exception("Fatal error: wrong operator");
            }
            return(oprtr);
        }
Beispiel #3
0
        public override void Action()
        {
            Expression w = (Expression)vars[IdName];

            if (w != null)
            {
                string err = "Variable ' " + IdName + " 'is allready define in this scope";
                throw new Exception(err);
            }
            else
            {
                Expression retExpr = null;
                if (value.Type == ExpressionTypes.Number)
                {
                    retExpr = new NumbExpr(Convert.ToDouble(value.Value()));
                }
                else
                if (value.Type == ExpressionTypes.String)
                {
                    retExpr = new StrExpr(Convert.ToString(value.Value()));
                }
                else
                if (value.Type == ExpressionTypes.Var)
                {
                    retExpr = (Expression)vars[((IDExpr)value).Name];
                }
                vars.Add(this.IdName, retExpr);
            }
        }
Beispiel #4
0
        private Expression expression()
        {
            Expression expr = null;

            if (TokenListItem < TokenList.Count())
            {
                lookahead = (int)TokenList[TokenListItem].tag;

                switch (lookahead)
                {
                case (int)TokenTags.BOPERATOR:
                    int operator_indx = match((int)TokenTags.BOPERATOR);

                    Expression eNumb = expression();
                    if (eNumb.Type != ExpressionTypes.Number)
                    {
                        throw new Exception("Must be a number");
                    }

                    switch (((Word)TokenList[operator_indx]).lexeme)
                    {
                    case "+":
                        return(new NumbExpr(Convert.ToDouble(eNumb.Value())));

                    case "-":
                        return(new NumbExpr((-1) * Convert.ToDouble(eNumb.Value())));

                    default: throw new Exception("Invalid expression term '" + ((Word)TokenList[operator_indx]).lexeme + "'");
                    }

                case (int)TokenTags.NUMBER:
                    match((int)TokenTags.NUMBER);
                    Expression eNumb1 = new NumbExpr(((Numb)TokenList[TokenListItem - 1]).value);

                    if (lookahead == (int)TokenTags.STROPERATOR)
                    {
                        throw new Exception("Number operator expected;");
                    }

                    if (lookahead == (int)TokenTags.BOPERATOR)
                    {
                        return(matchBOPERATOR(eNumb1));
                    }
                    else
                    {
                        return(eNumb1);
                    }

                case (int)TokenTags.LITERAL:
                    match((int)TokenTags.LITERAL);
                    Expression litr1 = new StrExpr(((Word)TokenList[TokenListItem - 1]).lexeme);
                    if (lookahead == (int)TokenTags.BOPERATOR)
                    {
                        throw new Exception("String operator expected;");
                    }

                    if (lookahead == (int)TokenTags.STROPERATOR)
                    {
                        return(matchSTROPERATOR(litr1));
                    }
                    else
                    {
                        return(litr1);
                    }

                case (int)TokenTags.ID:
                    int IDindx = match((int)TokenTags.ID);

                    string str1 = ((Word)TokenList[IDindx]).lexeme;
                    IDExpr id1  = new IDExpr(str1, IDs);

                    if (lookahead == (int)TokenTags.BOPERATOR)
                    {
                        return(matchBOPERATOR(id1));
                    }

                    if (lookahead == (int)TokenTags.STROPERATOR)
                    {
                        return(matchSTROPERATOR(id1));
                    }

                    return(id1);

                case (int)TokenTags.OPENCIRKBRACKET:
                    match((int)TokenTags.OPENCIRKBRACKET);
                    expr = expression();
                    match((int)TokenTags.CLOSECIRKBRACKET);

                    if (lookahead == (int)TokenTags.BOPERATOR)
                    {
                        return(matchBOPERATOR(expr));
                    }

                    if (lookahead == (int)TokenTags.STROPERATOR)
                    {
                        return(matchSTROPERATOR(expr));
                    }

                    return(expr);

                case (int)TokenTags.CALL:
                    match((int)TokenTags.CALL);
                    int FunkNameTokeIndx = match((int)TokenTags.ID);

                    List <Expression> FunkParams = new List <Expression>();

                    match((int)TokenTags.OPENCIRKBRACKET);
                    while (lookahead != (int)TokenTags.CLOSECIRKBRACKET)
                    {
                        Expression param1 = expression();
                        FunkParams.Add(param1);
                        if (lookahead != (int)TokenTags.CLOSECIRKBRACKET)
                        {
                            match((int)TokenTags.COMMA);
                        }
                        else
                        {
                            break;
                        }
                    }
                    match((int)TokenTags.CLOSECIRKBRACKET);
                    string     FunkName = ((Word)TokenList[FunkNameTokeIndx]).lexeme + "#" + Convert.ToString(FunkParams.Count());
                    Expression func     = (matchFunk(FunkName)).Copy();

                    ((Function)func).SetParameters(FunkParams.ToArray());
                    return(func);

                default:
                    string err = "Syntax Error: not fited expression";
                    //MessageBox.Show(err);
                    throw new Exception(err);
                }
            }
            else
            {
                string err = "Out of range";
                //MessageBox.Show(err);
                throw new Exception(err);
            }
        }