11#include "vm.h"
2- #include "bloat .h"
2+ #include "common .h"
33#include "chunk.h"
44#include "value.h"
55#include "object.h"
6-
76#include <stdio.h>
87#include <stdarg.h>
98#include <string.h>
109
1110static InterpretResult run (VM * vm ) {
1211 #define READ_BYTE () (*vm->ip++)
13- #define READ_CONSTANT () (vm->chunk->constants[READ_BYTE()])
12+ #define READ_CONSTANT () (vm->chunk->constants.values [READ_BYTE()])
1413 #define BINARY_OP (op ) \
1514 do { \
16- if (!IS_NUMBER(peek(0)) || !IS_NUMBER(peek(1))) { \
17- runtime_error("Operands must be numbers."); \
15+ if (!IS_NUMBER(peek(vm, 0)) || !IS_NUMBER(peek(vm, 1))) { \
16+ runtime_error(vm, "Operands must be numbers."); \
1817 return INTERPRET_RUNTIME_ERROR; \
1918 } \
2019 double b = AS_NUMBER(pop(vm)); \
@@ -51,7 +50,7 @@ static InterpretResult run(VM* vm) {
5150 case OP_NEGATE : {
5251 Value value = pop (vm );
5352 if (!IS_NUMBER (value )) {
54- runtime_error ("Operand must be a number." );
53+ runtime_error (vm , "Operand must be a number." );
5554 return INTERPRET_RUNTIME_ERROR ;
5655 }
5756 push (vm , NUMBER_VAL (- AS_NUMBER (value )));
@@ -127,16 +126,15 @@ bool values_equal(Value a, Value b) {
127126 }
128127}
129128
130- Value peek (int offset ) {
131- VM * vm ;
132- if ((intptr_t )(vm -> stack_top - offset - 1 ) < 0 ) {
133- runtime_error ("Stack underflow!" );
129+ Value peek (VM * vm , int offset ) {
130+ if ((vm -> stack_top - vm -> stack ) <= offset ) {
131+ runtime_error (vm , "Stack underflow!" );
134132 return NIL_VAL ;
135133 }
136- return vm -> stack [(int )( vm -> stack_top - offset - 1 ) ];
134+ return vm -> stack [(vm -> stack_top - vm -> stack ) - offset - 1 ];
137135}
138136
139- void * reallocate (void * pointer , size_t oldSize , size_t newSize ) {
137+ void * reallocate (VM * vm , void * pointer , size_t oldSize , size_t newSize ) {
140138 vm -> bytes_allocated += newSize - oldSize ;
141139
142140 if (newSize > oldSize ) {
@@ -156,30 +154,32 @@ void* reallocate(void* pointer, size_t oldSize, size_t newSize) {
156154}
157155
158156void collect_garbage (VM * vm ) {
157+ // Mark phase
159158 for (Value * slot = vm -> stack ; slot < vm -> stack_top ; slot ++ ) {
160159 mark_value (vm , * slot );
161160 }
162161
163- Object * * object = & vm -> objects ;
162+ // Sweep phase
163+ Obj * * object = & vm -> objects ;
164164 while (* object != NULL ) {
165- if (!(* object )-> is_marked ) {
166- Object * unreached = * object ;
165+ if (!(* object )-> marked ) {
166+ Obj * unreached = * object ;
167167 * object = unreached -> next ;
168- free_object (unreached );
168+ free_object (vm , unreached );
169169 } else {
170- (* object )-> is_marked = false;
170+ (* object )-> marked = false;
171171 object = & (* object )-> next ;
172172 }
173173 }
174174
175175 vm -> next_gc = vm -> bytes_allocated * 2 ;
176176}
177177
178- void mark_object (VM * vm , Object * object ) {
178+ void mark_object (VM * vm , Obj * object ) {
179179 if (object == NULL ) return ;
180- if (object -> is_marked ) return ;
180+ if (object -> marked ) return ;
181181
182- object -> is_marked = true;
182+ object -> marked = true;
183183 // If the object contains references to other objects,
184184 // they should be marked here
185185}
@@ -190,7 +190,7 @@ void mark_value(VM* vm, Value value) {
190190 }
191191}
192192
193- void runtime_error (const char * format , ...) {
193+ void runtime_error (VM * vm , const char * format , ...) {
194194 va_list args ;
195195 va_start (args , format );
196196 vfprintf (stderr , format , args );
@@ -199,10 +199,10 @@ void runtime_error(const char* format, ...) {
199199}
200200
201201void free_objects (VM * vm ) {
202- Object * object = vm -> objects ;
202+ Obj * object = vm -> objects ;
203203 while (object != NULL ) {
204- Object * next = object -> next ;
205- free_object (object );
204+ Obj * next = object -> next ;
205+ free_object (vm , object );
206206 object = next ;
207207 }
208208}
0 commit comments