Esempio n. 1
0
        /// <summary>
        /// Adds a variable to the global directory.
        /// Called when a new variable ID is read in global scope.
        /// </summary>
        /// <param name="newGlobalVariable"></param>
        /// <returns>The variable address in the global directory where the variable was stored.</returns>
        public static int AddGlobalVariable(Variable newGlobalVariable)
        {
            // retrieve the memory address where the variable will live
            int memorySpace = GetNextAvailable(MemoryScope.global, newGlobalVariable.GetDataType());

            // if memory space is insufficient, throw and exception
            if (memorySpace == -1)
            {
                throw new Exception("Out of global memory");
            }

            // pass on the memory address meant for the variable
            newGlobalVariable.SetMemoryAddress(memorySpace);

            // try to add variable to global directory
            try
            {
                FunctionDirectory.GlobalFunction().AddGlobalVariable(newGlobalVariable);
                if (newGlobalVariable.GetDataType() == SemanticCubeUtilities.DataTypes.number)
                {
                    try { memorySpace = SetMemory(memorySpace, 0); }
                    catch (Exception e) { throw new Exception(e.Message); }
                }
                else if (newGlobalVariable.GetDataType() == SemanticCubeUtilities.DataTypes.text)
                {
                    try { memorySpace = SetMemory(memorySpace, ""); }
                    catch (Exception e) { throw new Exception(e.Message); }
                }
                else if (newGlobalVariable.GetDataType() == SemanticCubeUtilities.DataTypes.boolean)
                {
                    try { memorySpace = SetMemory(memorySpace, false); }
                    catch (Exception e) { throw new Exception(e.Message); }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return(memorySpace);
        }
Esempio n. 2
0
        /// <summary>
        /// Loads an asset into memory.
        /// Called by <see cref="Parser"/> when an Asset is read.
        /// </summary>
        /// <param name="memoryAddress"></param>
        /// <param name="asset"></param>
        /// <returns>The memory address where the asset was set.</returns>
        public static int SetAssetInMemory(Asset asset)
        {
            int memoryAddress = GetNextAssetAvailable();

            if (memoryAddress == -1)
            {
                throw new Exception("Out of asset memory");
            }
            asset.SetMemoryAddress(memoryAddress);

            // try to add asset to global asset directory
            try
            {
                if (currentAssetAddress + Enum.GetNames(typeof(AssetAttributes)).Length <= highestAssetAddress)
                {
                    FunctionDirectory.GlobalFunction().AddAssetVariable(asset);

                    // add attribute values to memory
                    memoryGlobalAssets[memoryAddress + (int)AssetAttributes.id]       = asset.GetID();
                    memoryGlobalAssets[memoryAddress + (int)AssetAttributes.x]        = asset.GetX();
                    memoryGlobalAssets[memoryAddress + (int)AssetAttributes.y]        = asset.GetY();
                    memoryGlobalAssets[memoryAddress + (int)AssetAttributes.width]    = asset.GetWidth();
                    memoryGlobalAssets[memoryAddress + (int)AssetAttributes.height]   = asset.GetHeight();
                    memoryGlobalAssets[memoryAddress + (int)AssetAttributes.rotation] = asset.GetRotation();
                    memoryGlobalAssets[memoryAddress + (int)AssetAttributes.number]   = asset.GetNumber();
                    memoryGlobalAssets[memoryAddress + (int)AssetAttributes.label]    = asset.GetLabel();

                    currentAssetAddress += Enum.GetNames(typeof(AssetAttributes)).Length;
                }

                return(memoryAddress);
            }
            catch (Exception)
            {
                throw;
            }
        }