Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public override Expression CloneExpr()
        {
            var newList = new KeyedElementList();
            foreach(var ke in Elements.FilteredChildren<KeyedElement>())
            {
                newList.AddChild(
                    new KeyedElement(
                        ke.Key == null ? null : ke.Key.CloneExpr(),
                        ke.Element.CloneExpr()));
            }

            return new CompositeLiteral(
                Type.CloneType(),
                newList);
        }
Ejemplo n.º 3
0
        private T DefineVariable <T>(string id, bool reference, AST.Type rawType) where T : Variable
        {
            int index = 0;

            if (typeof(T) == typeof(LocalVariable))
            {
                index = CurrentScope.LocalIndex++;
            }
            else if (typeof(T) == typeof(ParameterVariable))
            {
                index = CurrentScope.ParameterIndex++;
            }
            else if (typeof(T) == typeof(ReturnVariable))
            {
                index = CurrentScope.ReturnIndex++;
            }

            T inst = (T)Activator.CreateInstance(typeof(T), new object[] { index, reference });

            inst.ResolvedType = rawType.CloneType();
            CurrentScope.Vars.Add(id, inst);
            return(inst);
        }