Esempio n. 1
0
        private int DisassembleConstant(string name, GearsChunk chunk, int offset, EGearsOpCode constantType, Action <string> writeLine)
        {
            int constantIndex = (chunk.ReadCode(ref offset) << 8) + chunk.ReadCode(ref offset);

            switch (constantType)
            {
            case OP_LOAD_CONSTANT: {
                GearsValue value = chunk.ReadConstantValue(constantIndex);
                writeLine($"{name} const[{constantIndex}] ({value})");
            }
            break;

            case OP_LOAD_STRING: {
                string value = chunk.Strings.ReadStringConstant(constantIndex);
                writeLine($"{name} string[{constantIndex}] ({value})");
            }
            break;

            case OP_LOAD_FUNCTION: {
                string value = chunk.ReadConstantValueAsBitStr(constantIndex);
                writeLine($"{name} bitstr[{constantIndex}] ({value})");
            }
            break;
            }
            return(offset);
        }
Esempio n. 2
0
        /// <summary>
        /// Adds the given value to the chunk's constant table.
        /// Returns the index of that constant in the constant table.
        /// </summary>
        private int MakeValueConstant(GearsValue value)
        {
            for (int i = 0; i < _Chunk.SizeConstant; i++)
            {
                if ((ulong)_Chunk.ReadConstantValue(i) == (ulong)value)
                {
                    return(i);
                }
            }
            int index = _Chunk.WriteConstantValue(value);

            if (index > short.MaxValue)
            {
                throw new CompilerException(Tokens.Peek(), "Too many constants in one chunk.");
            }
            return(index);
        }