Exemplo n.º 1
0
 public static int addConstant(ref Chunk_t chunk, Value_t value)
 {
     VM.push(value);
     Value.writeValueArray(ref chunk.constants, value);
     VM.pop();
     return(chunk.constants.count - 1);
 }
Exemplo n.º 2
0
 public static void initChunk(ref Chunk_t chunk)
 {
     chunk.count    = 0;
     chunk.capacity = 0;
     chunk.code     = null;
     chunk.lines    = null;
     Value.initiValueArray(ref chunk.constants);
 }
Exemplo n.º 3
0
        private static void emitByte(byte byte_)
        {
            Chunk_t _chunk = currentChunk();

            Chunk.writeChunk(ref _chunk, byte_, nearbyToken.line);

            current.function.chunk = _chunk; // workaround
        }
Exemplo n.º 4
0
        public static void freeChunk(ref Chunk_t chunk)
        {
            Memory.FREE_ARRAY <byte>(typeof(byte), ref chunk.code, chunk.capacity);
            Memory.FREE_ARRAY <int>(typeof(int), ref chunk.lines, chunk.capacity);

            Value.freeValueArray(ref chunk.constants);

            initChunk(ref chunk);
        }
Exemplo n.º 5
0
        public static void writeChunk(ref Chunk_t chunk, byte byte_, int line)
        {
            if (chunk.capacity < chunk.count + 1)
            {
                int oldCapacity = chunk.capacity;
                chunk.capacity = Memory.GROW_CAPACITY(oldCapacity);
                chunk.code     = (byte[])Memory.GROW_ARRAY <byte>(ref chunk.code, typeof(byte), oldCapacity, chunk.capacity);
                chunk.lines    = (int[])Memory.GROW_ARRAY <int>(ref chunk.lines, typeof(int), oldCapacity, chunk.capacity);
            }

            chunk.code[chunk.count]  = byte_;
            chunk.lines[chunk.count] = line;
            chunk.count++;
        }
Exemplo n.º 6
0
        private static byte makeConstant(Value_t value)
        {
            Chunk_t _chunk   = currentChunk();
            int     constant = Chunk.addConstant(ref _chunk, value);

            if (constant > byte.MaxValue)
            {
                error("Too many constants in one chunk.");
                return(0);
            }

            current.function.chunk = _chunk; // work around
            return((byte)constant);
        }
Exemplo n.º 7
0
        private static ObjFunction endCompiler()
        {
            emitReturn();
            ObjFunction function = current.function;

#if DEBUG_PRINT_CODE
            if (!hadError)
            {
                Chunk_t _chunk = currentChunk();
                Debug.disassembleChunk(ref _chunk, function.name != null ? function.name.chars : "<script>\0".ToCharArray());
            }
#endif

            current = current.enclosing;
            return(function);
        }
Exemplo n.º 8
0
 public static void writeChunk(ref Chunk_t chunk, OpCode opcode, int line)
 {
     writeChunk(ref chunk, (byte)opcode, line);
 }