コード例 #1
0
        /**
         * Compiles an expression.
         *
         * @param e       the expression
         * @param tabs    number of tabulations
         */
        private void compileEXPR(IExpr e, int tabs)
        {
            if (e == null)
            {
                return;
            }

            int TAG = e.getTAG();

            switch (TAG)
            {
            case (Token.T_INT):
                buffer.Append(((Number)e).getIntValue() + "");
                break;

            case (Token.T_FLOAT):
                buffer.Append((((Number)e).getFloatValue() + "f").Replace(',', '.'));
                break;

            case (Token.T_DOUBLE):
                buffer.Append((((Number)e).getDoubleValue() + "").Replace(',', '.'));
                break;

            case (Token.T_CHAR):
                buffer.Append("'" + ((TChar)e).getChar() + "'");
                break;

            case (Token.T_STRING):
                buffer.Append("\"" + ((TString)e).getString() + "\"");
                break;

            case (Token.T_TRUE):
                buffer.Append("true");
                break;

            case (Token.T_FALSE):
                buffer.Append("false");
                break;

            case (Token.T_CAST):
                Cast cast = (Cast)e;
                buffer.Append("(" + cast.getType());
                buffer.Append(") ");
                compileEXPR(cast.getExpression(), tabs);

                break;

            case (Token.T_IDENTIFIER):
                Identifier ide = (Identifier)e;

                if (ide.isWaitingAsync() && !inside_async)
                {
                    ide.setWaitingAsync(false);
                    buffer.Append("(" + ide.getName() + " = task_" + ide.getName() + ".Result)");
                }
                else
                {
                    buffer.Append(ide.getName());
                }

                break;

            case (Token.T_CALL):
                Call c = (Call)e;

                String name;
                if (!map.TryGetValue(c.getName(), out name))
                {
                    name = c.getName();
                }

                buffer.Append(name + "(");

                List <IExpr> args = c.getArgs();
                if (args != null)
                {
                    buffer.Append(" ");

                    for (int i = 0; i < args.Count; i++)
                    {
                        buffer.Append((i == 0) ? "" : ", ");
                        compileEXPR(args[i], tabs);
                    }

                    buffer.Append(" ");
                }

                buffer.Append(")");

                while ((args = c.getCall()) != null)
                {
                    buffer.Append("( ");
                    for (int i = 0; i < args.Count; i++)
                    {
                        buffer.Append((i == 0) ? "" : ", ");
                        compileEXPR(args[i], 0);
                    }
                    buffer.Append(" )");
                }

                break;

            case (Token.T_ASYNC):
                buffer.Append("Task.Factory.StartNew(() =>\n");

                addTabs(tabs + 1);
                buffer.Append("{\n");

                Async a = (Async)e;
                inside_async = true;
                compileBLOCK(a.getBlock(), tabs + 2);
                inside_async = false;

                buffer.Append("\n");
                addTabs(tabs + 1);
                buffer.Append("})");

                break;

            case (Token.T_DASYNC):
                buffer.Append("Task.Factory.StartNew(() =>\n");

                addTabs(tabs + 1);
                buffer.Append("{\n");

                DAsync da = (DAsync)e;

                inside_async = true;

                List <IStatement> statements = da.getStatementsList();
                if (statements != null)
                {
                    for (int i = 0; i < statements.Count; i++)
                    {
                        if (statements[i].getTAG() == Token.T_RETURN)
                        {
                            addTabs(tabs + 2);
                            buffer.Append("return client.getRemoteServer( " + ((Identifier)da.getRemoteAddress()).getName() + " ).");
                            compileEXPR(((Return)statements[i]).getExpr(), 0);
                            buffer.Append(";");
                        }
                        else
                        {
                            compileSTATEMENT(statements[i], tabs + 2);
                        }
                        buffer.Append("\n");
                    }
                }

                inside_async = false;

                addTabs(tabs + 1);
                buffer.Append("})");

                break;

            case (Token.T_FUN):
                Function f = (Function)e;

                Types type = f.getReturnType();
                if (type.getType() == Token.T_VOID)
                {
                    buffer.Append("new Action<");
                }
                else
                {
                    buffer.Append("new Func<");
                }

                List <Identifier> parameters = f.getParams();
                int size = 0;
                if (parameters != null)
                {
                    size = parameters.Count;
                    for (int i = 0; i < size; i++)
                    {
                        buffer.Append(((i > 0) ? ", " : "") + parameters[i].getType());
                    }
                }

                if (type.getType() != Token.T_VOID)
                {
                    buffer.Append(((size > 0) ? ", " : "") + type);
                }

                buffer.Append(">((");

                if (parameters != null)
                {
                    for (int i = 0; i < parameters.Count; i++)
                    {
                        if (i > 0)
                        {
                            buffer.Append(", ");
                        }

                        compileEXPR(parameters[i], tabs + 1);
                    }
                }

                buffer.Append(") =>\n");
                addTabs(tabs + 3);
                buffer.Append("{\n");

                bool backup_global = is_global;
                is_global = false;
                compileBLOCK(f.getBlock(), tabs + 4);
                is_global = backup_global;

                buffer.Append("\n");
                addTabs(tabs + 3);
                buffer.Append("})");

                break;

            case (Token.T_INCR):
            case (Token.T_DECR):
                Expr expr = (Expr)e;
                bool par  = e.getParenthesis();

                if (par)
                {
                    buffer.Append("(");
                }

                compileEXPR(expr.getExpr1(), tabs);

                buffer.Append(Token.getTokenValue(TAG));

                if (par)
                {
                    buffer.Append(")");
                }

                break;

            case (Token.T_AND):
            case (Token.T_OR):
            case (Token.T_NOT):
            case (Token.T_EQUALS):
            case (Token.T_DIFFERENT):
            case (Token.T_GREATER):
            case (Token.T_GREATER_OR_EQUAL):
            case (Token.T_LESS):
            case (Token.T_LESS_OR_EQUAL):
            case (Token.T_PLUS):
            case (Token.T_PLUS_EQUALS):
            case (Token.T_MINUS):
            case (Token.T_MINUS_EQUALS):
            case (Token.T_DIVIDE):
            case (Token.T_DIVIDE_EQUALS):
            case (Token.T_MULTIPLY):
            case (Token.T_MULTIPLY_EQUALS):
                expr = (Expr)e;
                par  = e.getParenthesis();

                if (par)
                {
                    buffer.Append("(");
                }

                compileEXPR(expr.getExpr1(), tabs);

                buffer.Append(" " + Token.getTokenValue(TAG) + " ");

                compileEXPR(expr.getExpr2(), tabs);

                if (par)
                {
                    buffer.Append(")");
                }

                break;
            }
        }