Exemplo n.º 1
0
        public override AstNode Visit(EnumDefinition node)
        {
            // Update the scope.
            PushScope(node.GetScope());

            // Visit his children.
            VisitList(node.GetChildren());

            // Restore the scope.
            PopScope();

            return node;
        }
Exemplo n.º 2
0
        public override AstNode Visit(EnumDefinition node)
        {
            // Push the scope.
            PushScope(node.GetScope());

            // Use a default type of const int.
            IChelaType baseType = ChelaType.GetIntType();
            IChelaType enumType = ConstantType.Create(baseType);

            // Declare the value field.
            Structure building = node.GetStructure();
            FieldVariable valueField =
                new FieldVariable("m_value", MemberFlags.Public, baseType, building);
            building.AddField(valueField);

            // Used for implicit prev + 1.
            IChelaType enumConst = ConstantType.Create(building);
            TypeNode enumIntExpr = new TypeNode(TypeKind.Other, node.GetPosition());
            enumIntExpr.SetOtherType(enumType);

            // Visit the constant defininitions.
            EnumConstantDefinition prev = null;
            AstNode child = node.GetChildren();
            while(child != null)
            {
                // Cast the child.
                EnumConstantDefinition constDef = (EnumConstantDefinition)child;

                // Set the child type.
                constDef.SetCoercionType(enumType);
                constDef.SetNodeType(enumConst);

                // If there isn't a constant expression, use previous + 1 or 0.
                if(constDef.GetValue() == null)
                {
                    TokenPosition constPos = constDef.GetPosition();
                    if(prev != null)
                    {
                        // Previous + 1
                        Expression prevExpr = new CastOperation(enumIntExpr,
                                                                new VariableReference(prev.GetName(), constPos),
                                                                constPos);
                        Expression implicitVal = new BinaryOperation(BinaryOperation.OpAdd,
                                                                     prevExpr,
                                                                     new ByteConstant(1, constPos),
                                                                     constPos);
                        constDef.SetValue(implicitVal);
                    }
                    else
                    {
                        // First element is 0
                        constDef.SetValue(new ByteConstant(0, constPos));
                    }
                }

                // Visit the constant definition.
                constDef.Accept(this);

                // Process the next constant.
                prev = constDef;
                child = child.GetNext();
            }

            // Restore the scope.
            PopScope();

            return node;
        }