Exemple #1
0
        public EcmaComplication RunCode(TextReader reader)
        {
            EcmaComplication result = new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined());
            EcmaTokenizer    token  = new EcmaTokenizer(reader);

            while (token.Current().IsNot(TokenType.EOS))
            {
                result = EcmaEvulator.Evulate(this.State, this.State.GetProgroam(token));
            }
            return(result);
        }
Exemple #2
0
        internal static EcmaComplication Evulate(EcmaState state, EcmaStatment statment)
        {
            EcmaComplication c = new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined());

            switch (statment.Type)
            {
            case EcmaStatmentType.FunctionDec:
                return(CreateFuncDec(state, statment));

            case EcmaStatmentType.Expresion:
                return(new EcmaComplication(EcmaComplicationType.Normal, EvulateExpresion(state, statment.Expresion)));

            case EcmaStatmentType.Return:
                return(new EcmaComplication(EcmaComplicationType.Return, statment.Expresion == null ? EcmaValue.Undefined() : EvulateExpresion(state, statment.Expresion)));

            case EcmaStatmentType.If:
                if (Reference.GetValue(EvulateExpresion(state, statment.Expresion)).ToBoolean(state))
                {
                    return(Evulate(state, statment.Statment));
                }
                else
                {
                    if (statment.Else == null)
                    {
                        return(new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined()));
                    }
                    return(Evulate(state, statment.Else));
                }

            case EcmaStatmentType.Block:
                for (int i = 0; i < statment.Statments.Count; i++)
                {
                    c = Evulate(state, statment.Statments[i]);
                    if (c.Type != EcmaComplicationType.Normal)
                    {
                        return(c);
                    }
                }
                return(new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Null()));

            case EcmaStatmentType.Var:
                EcmaHeadObject putIn = state.GetScope()[state.GetScope().Length - 1];
                foreach (string key in statment.VarList.Keys)
                {
                    putIn.Put(key, statment.VarList[key] == null ? EcmaValue.Undefined() : Reference.GetValue(EvulateExpresion(state, statment.VarList[key])));
                }
                return(new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined()));

            case EcmaStatmentType.While:
                c = new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined());
                while (EvulateExpresion(state, statment.Expresion).ToBoolean(state))
                {
                    c = Evulate(state, statment.Statment);
                    if (c.Type == EcmaComplicationType.Break)
                    {
                        return(new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined()));
                    }
                    else if (c.Type == EcmaComplicationType.Continue)
                    {
                        c = new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined());
                    }
                    else if (c.Type == EcmaComplicationType.Return)
                    {
                        return(c);
                    }
                }
                return(c);

            case EcmaStatmentType.For:
                if (statment.Expresion != null)
                {
                    Reference.GetValue(EvulateExpresion(state, statment.Expresion));
                }
                while (statment.Second == null || Reference.GetValue(EvulateExpresion(state, statment.Second)).ToBoolean(state))
                {
                    c = Evulate(state, statment.Statment);
                    if (c.Type == EcmaComplicationType.Break)
                    {
                        return(new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined()));
                    }
                    else if (c.Type == EcmaComplicationType.Return)
                    {
                        return(c);
                    }
                    else if (c.Type == EcmaComplicationType.Continue)
                    {
                        c = new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined());
                    }

                    if (statment.Tree != null)
                    {
                        Reference.GetValue(EvulateExpresion(state, statment.Tree));
                    }
                }
                return(c);

            case EcmaStatmentType.ForIn:
                EcmaHeadObject obj = Reference.GetValue(EvulateExpresion(state, statment.Second)).ToObject(state);
                c = new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined());
                foreach (string key in obj.Property.Keys)
                {
                    if (obj.Property[key].DontEnum)
                    {
                        continue;
                    }

                    if (statment.Expresion.Type == ExpresionType.VarList)
                    {
                        Reference.PutValue(EvulateExpresion(state, statment.Expresion.Multi[0].Left), EcmaValue.String(key), state.GlobalObject);
                    }
                    else
                    {
                        Reference.PutValue(EvulateExpresion(state, statment.Expresion), EcmaValue.String(key), state.GlobalObject);
                    }
                    c = Evulate(state, statment.Statment);
                    if (c.Type != EcmaComplicationType.Normal)
                    {
                        if (c.Type == EcmaComplicationType.Break)
                        {
                            return(new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined()));
                        }
                        if (c.Type == EcmaComplicationType.Continue)
                        {
                            c = new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined());
                        }
                        if (c.Type == EcmaComplicationType.Return)
                        {
                            return(c);
                        }
                    }
                }
                return(c);

            case EcmaStatmentType.Continue:
                return(new EcmaComplication(EcmaComplicationType.Continue, EcmaValue.Undefined()));

            default:
                throw new EcmaRuntimeException("Evulator out of sync. Unknown statment type: " + statment.Type.ToString());
            }
        }