コード例 #1
0
        public override Base VisitAssignment([NotNull] GolangParser.AssignmentContext context)
        {
            var left  = context.expression_list(0).Accept(this) as ExpressionList;
            var right = context.expression_list(1).Accept(this) as ExpressionList;
            var type  = context.assign_op().Accept(this) as AssignmentTypeWrapper;

            var ret = new StatementList();

            if (type.Type == AssignmentType.Normal && left.NumChildren() > 1 && right.NumChildren() == 1 && right.GetChild(0) is InvocationExpression)
            {
                ret.AddChild(
                    new ReturnAssignment(
                        left,
                        right.GetChild <InvocationExpression>(0)));
            }
            else
            {
                while (left.NumChildren() > 0)
                {
                    ret.AddChild(
                        new Assignment(
                            left.GetChild <Expression>(0),
                            right.GetChild <Expression>(0),
                            type.Type));
                }
            }

            return(ret);
        }
コード例 #2
0
        public override Base VisitStatement_list([NotNull] GolangParser.Statement_listContext context)
        {
            var ret = new StatementList();

            foreach (var child in context.children)
            {
                if (child is GolangParser.StatementContext)
                {
                    var raw = child.Accept(this);
                    switch (raw)
                    {
                    case Statement s:
                        ret.AddChild(s);
                        break;

                    case StatementList list:
                        while (list.NumChildren() > 0)
                        {
                            ret.AddChild(list.GetChild <Statement>(0));
                        }
                        break;
                    }
                }
            }

            return(ret);
        }
コード例 #3
0
        public override Base VisitFor_clause([NotNull] GolangParser.For_clauseContext context)
        {
            StatementList preamble    = null;
            var           rawPreamble = context.simple_statement(0) != null?context.simple_statement(0).Accept(this) : null;

            switch (rawPreamble)
            {
            case Statement s:
                preamble = new StatementList();
                preamble.AddChild(s);
                break;

            case StatementList sl:
                preamble = sl;
                break;

            default:
                break;
            }

            return(new IterativeClause(
                       preamble,
                       context.expression() != null ? context.expression().Accept(this) as Expression : null,
                       context.simple_statement(1) != null ? context.simple_statement(1).Accept(this) as Statement : null));
        }
コード例 #4
0
        public override Base VisitShort_var_decl([NotNull] GolangParser.Short_var_declContext context)
        {
            var ret = new StatementList();

            var identList = context.identifier_list().Accept(this) as RawNodeList;
            var exprList  = context.expression_list().Accept(this) as ExpressionList;

            if (identList.Items.Count > 1 && exprList.NumChildren() == 1 && exprList.GetChild(0) is InvocationExpression)
            {
                var invoke = exprList.GetChild <InvocationExpression>(0);
                var idents = new ExpressionList();

                int index = 0;
                foreach (var ident in identList.Items)
                {
                    idents.AddChild(new IdentifierExpression(ident.Text));
                    var vd = new VarDeclaration(ident.Text,
                                                new ReturnType(
                                                    invoke,
                                                    index++));
                    m_currentScope.AddVarDeclaration(vd);
                }


                ret.AddChild(
                    new ReturnAssignment(
                        idents,
                        invoke));
            }
            else
            {
                for (int i = 0; i < identList.Items.Count; i++)
                {
                    var vd = new VarDeclaration(identList.Items[i].Text, new ExpressionType(exprList.GetChild <Expression>(0)));
                    m_currentScope.AddVarDeclaration(vd);
                    ret.AddChild(
                        new Assignment(
                            new IdentifierExpression(vd.Identifier),
                            exprList.GetChild <Expression>(0),
                            AssignmentType.Normal));
                }
            }

            return(ret);
        }
コード例 #5
0
        public override Base VisitVar_spec([NotNull] GolangParser.Var_specContext context)
        {
            var identList = context.identifier_list().Accept(this) as RawNodeList;

            AST.Type type = null;
            if (context.type() != null)
            {
                type = context.type().Accept(this) as AST.Type;
            }

            ExpressionList exprList = null;

            if (context.expression_list() != null)
            {
                exprList = context.expression_list().Accept(this) as ExpressionList;
            }

            var sl = new StatementList();

            for (int i = 0; i < identList.Items.Count; i++)
            {
                VarDeclaration vd;
                if (type != null)
                {
                    vd = new VarDeclaration(identList.Items[i].Text, type.CloneType());
                }
                else //expr list should be defined here
                {
                    vd = new VarDeclaration(identList.Items[i].Text, new ExpressionType(exprList.GetChild <Expression>(0)));
                }

                m_currentScope.AddVarDeclaration(vd);
                if (exprList != null)
                {
                    var assign = new Assignment(
                        new IdentifierExpression(identList.Items[i].Text),
                        exprList.GetChild <Expression>(0),
                        AssignmentType.Normal);

                    if (m_currentScope == m_currentPackage)
                    {
                        m_currentPackage.AddStaticInitializer(assign);
                    }
                    else
                    {
                        sl.AddChild(assign);
                    }
                }
            }
            return(sl);
        }