Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#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;
}