示例#1
0
        private System.Type ResolveType(AST.Type t)
        {
            switch (t)
            {
            case BuiltinType bt:
                return(ResolveBuiltinType(bt.Type));

            default:
                return(null);
            }
        }
示例#2
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);
        }
示例#3
0
        private AST.Type CanBeType(Node n)
        {
            bool isPointer = false;

            while (n is ExpressionWrapper)
            {
                n = (n as ExpressionWrapper).Expr;
            }

            if (n is UnaryExpression && (n as UnaryExpression).Op == EUnaryOp.Dereference)
            {
                isPointer = true;
                n         = (n as UnaryExpression).Expr;
            }

            AST.Type ret = null;
            switch (n)
            {
            case IdentifierExpression ie:
                if (m_registeredTypes.Contains(ie.Identifier))
                {
                    ret = new TypeName(ie.Identifier);
                }
                break;

            case SelectorExpression se:
                var fullname = ProcessSelector(se);
                if (m_registeredTypes.Contains(fullname))
                {
                    ret = new TypeName(fullname);
                }
                break;

            default:
                break;
            }

            if (isPointer && ret != null)
            {
                ret = new PointerType(ret);
            }
            return(ret);
        }
示例#4
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);
        }
示例#5
0
        private void StoreIndirect(AST.Type type)
        {
            switch (type)
            {
            case BuiltinType bt:
                switch (bt.Type)
                {
                case EBuiltinType.Uint8:
                    m_inst.AddChild(new Instruction(EInstruction.Stind_I1));
                    break;

                case EBuiltinType.Uint16:
                    m_inst.AddChild(new Instruction(EInstruction.Stind_I2));
                    break;

                case EBuiltinType.Uint32:
                    m_inst.AddChild(new Instruction(EInstruction.Stind_I4));
                    break;

                case EBuiltinType.Uint64:
                    m_inst.AddChild(new Instruction(EInstruction.Stind_I8));
                    break;

                case EBuiltinType.Int8:
                    m_inst.AddChild(new Instruction(EInstruction.Stind_I1));
                    break;

                case EBuiltinType.Int16:
                    m_inst.AddChild(new Instruction(EInstruction.Stind_I2));
                    break;

                case EBuiltinType.Int32:
                    m_inst.AddChild(new Instruction(EInstruction.Stind_I4));
                    break;

                case EBuiltinType.Int64:
                    m_inst.AddChild(new Instruction(EInstruction.Stind_I8));
                    break;

                case EBuiltinType.Float32:
                    m_inst.AddChild(new Instruction(EInstruction.Stind_R4));
                    break;

                case EBuiltinType.Float64:
                    m_inst.AddChild(new Instruction(EInstruction.Stind_R8));
                    break;

                case EBuiltinType.Uintptr:
                    m_inst.AddChild(new Instruction(EInstruction.Stind_I8));
                    break;

                default:
                    m_inst.AddChild(new Instruction(EInstruction.StindRef));
                    break;
                }
                break;

            default:
                m_inst.AddChild(new Instruction(EInstruction.StindRef));
                break;
            }
        }