Skip to content
Snippets Groups Projects
bc_interpreter.c 2.57 KiB
Newer Older
  • Learn to ignore specific revisions
  • #include <stdio.h>
    #include <inttypes.h>
    #include <assert.h>
    
    #include "bc_interpreter.h"
    
    // void * heap_alloc_alligned ( Heap * heap, size_t len, size_t align ) {
    //   size_t pos = (size_t) heap -> next;
    //   size_t rem = pos % align;
    //   if ( rem )
    //     heap -> next = heap -> next + align - rem;
    //   if ( heap -> next + len >= heap -> end )
    //     return NULL;  
    //   void * ret = heap -> next;
    //   heap -> next += len;
    //   return ret;
    // }
    // 
    // void * heap_alloc ( Heap * heap, size_t len ) {
    //   size_t pos = (size_t) heap -> next;
    //   size_t rem = pos % 8;
    //   if ( rem )
    //     heap -> next = heap -> next + 8 - rem;
    //   if ( heap -> next + len >= heap -> end )
    //     return NULL;  
    //   void * ret = heap -> next;
    //   heap -> next += len;
    //   return ret;
    // }
    // 
    // void heap_init ( Heap * heap, size_t heap_size ) {
    //   heap -> begin = (u8*) malloc ( heap_size );
    //   heap -> next = heap -> begin;
    //   heap -> end = heap -> begin + heap_size;
    // }
    // 
    // void heap_destroy ( Heap * heap ) {
    //   free ( heap -> begin );
    // }
    
    //Value * make_null ( ASTInterpreterState * state ) {
    //  NullValue * value = heap_alloc ( state -> heap, sizeof (NullValue) );
    //  *value = (NullValue) { .kind = (Value) { .kind = VALUE_NULL } };
    //  return (Value*) value;
    //}
    //
    //Value * make_int ( ASTInterpreterState * state, i32 val ) {
    //  IntValue * value = heap_alloc ( state -> heap, sizeof (IntValue) );
    //  *value = (IntValue) { .kind = (Value) { .kind = VALUE_INTEGER }, .integer = val };
    //  return (Value*) value;
    //}
    //
    //Value * make_bool ( ASTInterpreterState * state, bool val ) {
    //  BoolValue * value = heap_alloc ( state -> heap, sizeof (BoolValue) );
    //  *value = (BoolValue) { .kind = (Value) { .kind = VALUE_BOOLEAN }, .boolean = val };
    //  return (Value*) value;
    //}
    
    static inline u8 read_byte ( Str str, size_t * pos ) {
      return str . str [ *pos++ ];
    }
    
    void read_header ( Str input, size_t * pos ) {
      u8 bytes [4];
      for ( size_t i = 0; i < 4; ++i ) 
        bytes [ i ] = read_byte ( input, pos );
      Str header;
      header . str = bytes;
      header . len = 4;
      assert ( str_eq ( header, STR ( "FML\n" ) ) );
    }
    
    Constant * read_constant_pool ( Str input, size_t * pos ) {
      u8 count_bytes [ 2 ] = { read_byte ( input, pos ), read_byte ( input, pos ) };
      uint16_t elements = count_bytes [ 1 ] << 8 | count_bytes [ 0 ];
      printf ( "%u", elements );
      return NULL;
    }
    
    Program parse_program ( Str input ) {
      Program program;
      // read + check header
      size_t pos = 0;
      read_header ( input, &pos );
      // read constant pool 
      program . constant_pool = read_constant_pool ( input, &pos );
    
      return program;
    }