예제 #1
0
        private EcmaStatment GetReturn(EcmaTokenizer token)
        {
            EcmaStatment statment = new EcmaStatment(EcmaStatmentType.Return);

            if (token.Current().LineStart == token.Next().LineStart)
            {
                if (token.Current().IsNot(TokenType.Punctor, ";") && token.Current().IsNot(TokenType.EOS))
                {
                    statment.Expresion = Expresion(token);
                    if (token.Lines[token.Lines.Count - 2] == token.Current().LineStart)
                    {
                        if (token.Current().IsNot(TokenType.Punctor, ";") && token.Current().IsNot(TokenType.EOS))
                        {
                            token.Current().Excepect(TokenType.Punctor, ";");
                        }
                        token.Next();
                    }
                }
                else
                {
                    token.Next();
                }
            }
            return(statment);
        }
예제 #2
0
        private static EcmaComplication CreateFuncDec(EcmaState state, EcmaStatment statment)
        {
            EcmaValue func = EcmaValue.Object(new FunctionInstance(state, statment.Args, statment.Statment));

            state.GlobalObject.Put(statment.Name, func);
            return(new EcmaComplication(EcmaComplicationType.Normal, func));
        }
예제 #3
0
        private EcmaStatment GetFunctionDec(EcmaTokenizer token)
        {
            token.Next().Excepect(TokenType.Identify);
            EcmaStatment statment = new EcmaStatment(EcmaStatmentType.FunctionDec);

            statment.Name = token.Current().Context;
            token.Next().Excepect(TokenType.Punctor, "(");
            List <String> args = new List <String>();

            if (token.Next().IsNot(TokenType.Punctor, ")"))
            {
                token.Current().Excepect(TokenType.Identify);
                args.Add(token.Current().Context);
                while (token.Next().Is(TokenType.Punctor, ","))
                {
                    token.Next().Excepect(TokenType.Identify);
                    args.Add(token.Current().Context);
                }
            }

            token.Current().Excepect(TokenType.Punctor, ")");
            token.Next().Excepect(TokenType.Punctor, "{");
            statment.Args     = args.ToArray();
            statment.Statment = GetBlock(token);


            return(statment);
        }
예제 #4
0
        private EcmaStatment GetFor(EcmaTokenizer token)
        {
            token.Next().Excepect(TokenType.Punctor, "(");
            ExpresionData first = new ExpresionData(ExpresionType.Null);

            if (token.Next().IsNot(TokenType.Punctor, ";"))
            {
                if (token.Current().Is(TokenType.Keyword, "var"))
                {
                    token.Next();
                    first = VariableDeclarationList(token);
                }
                else
                {
                    first = Expresion(token);
                }
            }
            EcmaStatment current = null;

            if (token.Current().Is(TokenType.Keyword, "in"))
            {
                token.Next();
                current           = new EcmaStatment(EcmaStatmentType.ForIn);
                current.Expresion = first;
                current.Second    = Expresion(token);
            }
            else
            {
                token.Current().Excepect(TokenType.Punctor, ";");
                token.Next();
                current           = new EcmaStatment(EcmaStatmentType.For);
                current.Expresion = first;
                if (token.Current().IsNot(TokenType.Punctor, ";"))
                {
                    current.Second = Expresion(token);
                    token.Current().Excepect(TokenType.Punctor, ";");
                }
                else
                {
                    current.Second = new ExpresionData(ExpresionType.Null);
                }

                if (token.Next().IsNot(TokenType.Punctor, ")"))
                {
                    current.Tree = Expresion(token);
                }
                else
                {
                    current.Tree = new ExpresionData(ExpresionType.Null);
                }
            }

            token.Current().Excepect(TokenType.Punctor, ")");
            token.Next();
            current.Statment = GetStatment(token);
            return(current);
        }
예제 #5
0
        private EcmaStatment GetWhile(EcmaTokenizer token)
        {
            token.Next().Excepect(TokenType.Punctor, "(");
            token.Next();
            EcmaStatment statment = new EcmaStatment(EcmaStatmentType.While);

            statment.Expresion = Expresion(token);
            token.Current().Excepect(TokenType.Punctor, ")");
            token.Next();
            statment.Statment = GetStatment(token);
            return(statment);
        }
예제 #6
0
        private EcmaStatment GetIf(EcmaTokenizer token)
        {
            token.Next().Excepect(TokenType.Punctor, "(");
            token.Next();
            EcmaStatment statment = new EcmaStatment(EcmaStatmentType.If);

            statment.Expresion = Expresion(token);
            token.Current().Excepect(TokenType.Punctor, ")");
            token.Next();
            statment.Statment = GetStatment(token);
            if (token.Current().Is(TokenType.Keyword, "else"))
            {
                token.Next();
                statment.Else = GetStatment(token);
            }
            return(statment);
        }
예제 #7
0
        private EcmaStatment GetBlock(EcmaTokenizer token)
        {
            List <EcmaStatment> elem = new List <EcmaStatment>();

            if (token.Next().IsNot(TokenType.Punctor, "}"))
            {
                elem.Add(GetStatment(token));
                while (token.Current().IsNot(TokenType.Punctor, "}"))
                {
                    elem.Add(GetStatment(token));
                }
            }

            token.Current().Excepect(TokenType.Punctor, "}");
            token.Next();
            EcmaStatment statment = new EcmaStatment(EcmaStatmentType.Block);

            statment.Statments = elem;
            return(statment);
        }
예제 #8
0
        private EcmaStatment GetVar(EcmaTokenizer token)
        {
            Dictionary <string, ExpresionData> list = new Dictionary <string, ExpresionData>();

            token.Next().Excepect(TokenType.Identify);
            string identify = token.Current().Context;

            if (token.Next().Is(TokenType.Punctor, "="))
            {
                token.Next();
                list.Add(identify, Expresion(token));
            }
            else
            {
                list.Add(identify, null);
            }

            while (token.Current().Is(TokenType.Punctor, ","))
            {
                token.Next().Excepect(TokenType.Identify);
                identify = token.Current().Context;
                if (token.Next().Is(TokenType.Punctor, "="))
                {
                    token.Next();
                    list.Add(identify, Expresion(token));
                }
                else
                {
                    list.Add(identify, null);
                }
            }

            token.Current().Excepect(TokenType.Punctor, ";");
            token.Next();

            EcmaStatment statment = new EcmaStatment(EcmaStatmentType.Var);

            statment.VarList = list;
            return(statment);
        }
예제 #9
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());
            }
        }