1616#include "../include/compiler.h"
1717
1818
19- void closeUpvalues (VM * vm , Value * last ) {
20- while (vm -> openUpvalues != NULL &&
21- vm -> openUpvalues -> location >= last ) {
22- ObjUpvalue * upvalue = vm -> openUpvalues ;
19+ void closeUpvalues (VM * pVM , Value * last ) {
20+ while (pVM -> openUpvalues != NULL &&
21+ pVM -> openUpvalues -> location >= last ) {
22+ ObjUpvalue * upvalue = pVM -> openUpvalues ;
2323 upvalue -> closed = * upvalue -> location ;
2424 upvalue -> location = & upvalue -> closed ;
25- vm -> openUpvalues = upvalue -> next ;
25+ pVM -> openUpvalues = upvalue -> next ;
2626 }
2727}
2828
29- ObjUpvalue * captureUpvalue (Value * local , VM * vm ) {
29+ ObjUpvalue * captureUpvalue (Value * local , VM * pVM ) {
3030 ObjUpvalue * prevUpvalue = NULL ;
31- ObjUpvalue * upvalue = vm -> openUpvalues ;
31+ ObjUpvalue * upvalue = pVM -> openUpvalues ;
3232 while (upvalue != NULL && upvalue -> location > local ) {
3333 prevUpvalue = upvalue ;
3434 upvalue = upvalue -> next ;
@@ -42,120 +42,120 @@ ObjUpvalue *captureUpvalue(Value *local, VM *vm) {
4242 createdUpvalue -> next = upvalue ;
4343
4444 if (prevUpvalue == NULL ) {
45- vm -> openUpvalues = createdUpvalue ;
45+ pVM -> openUpvalues = createdUpvalue ;
4646 } else {
4747 prevUpvalue -> next = createdUpvalue ;
4848 }
4949
5050 return createdUpvalue ;
5151}
5252
53- void defineMethod (ObjString * name , VM * vm ) {
54- Value method = peek (vm , 0 );
55- ObjClass * klass = AS_CLASS (peek (vm , 1 ));
53+ void defineMethod (ObjString * name , VM * pVM ) {
54+ Value method = peek (pVM , 0 );
55+ ObjClass * klass = AS_CLASS (peek (pVM , 1 ));
5656 if (IS_CLOSURE (method )) {
5757 AS_CLOSURE (method )-> function -> ownerClass = klass ;
5858 }
5959 tableSet (& klass -> methods , name , method );
60- pop (vm );
60+ pop (pVM );
6161}
6262
63- bool bindMethod (struct ObjClass * klass , ObjString * name , VM * vm ) {
63+ bool bindMethod (struct ObjClass * klass , ObjString * name , VM * pVM ) {
6464 Value method ;
6565 if (!tableGet (& klass -> methods , name , & method )) {
66- runtimeError (vm , "Undefined property '%s'." , name -> chars );
66+ runtimeError (pVM , "Undefined property '%s'." , name -> chars );
6767 return false;
6868 }
6969
70- ObjBoundMethod * bound = newBoundMethod (peek (vm , 0 ), AS_CLOSURE (method ));
71- pop (vm );
72- push (vm , OBJ_VAL (bound ));
70+ ObjBoundMethod * bound = newBoundMethod (peek (pVM , 0 ), AS_CLOSURE (method ));
71+ pop (pVM );
72+ push (pVM , OBJ_VAL (bound ));
7373 return true;
7474}
7575
76- bool call (ObjClosure * closure , int argCount , VM * vm ) {
76+ bool call (ObjClosure * closure , int argCount , VM * pVM ) {
7777 if (argCount != closure -> function -> arity ) {
78- runtimeError (vm , "Expected %d arguments but got %d." ,
78+ runtimeError (pVM , "Expected %d arguments but got %d." ,
7979 closure -> function -> arity , argCount );
8080 return false;
8181 }
8282
83- if (vm -> frameCount == FRAMES_MAX ) {
84- runtimeError (vm , "Stack overflow." );
83+ if (pVM -> frameCount == FRAMES_MAX ) {
84+ runtimeError (pVM , "Stack overflow." );
8585 return false;
8686 }
8787
88- CallFrame * frame = & vm -> frames [vm -> frameCount ++ ];
88+ CallFrame * frame = & pVM -> frames [pVM -> frameCount ++ ];
8989 frame -> closure = closure ;
9090 frame -> ip = closure -> function -> chunk .code ;
91- frame -> slots = vm -> stackTop - argCount - 1 ;
91+ frame -> slots = pVM -> stackTop - argCount - 1 ;
9292 return true;
9393}
9494
95- bool callValue (Value callee , int argCount , VM * vm ) {
95+ bool callValue (Value callee , int argCount , VM * pVM ) {
9696 if (IS_OBJ (callee )) {
9797 switch (OBJ_TYPE (callee )) {
9898 case OBJ_BOUND_METHOD : {
9999 ObjBoundMethod * bound = AS_BOUND_METHOD (callee );
100- vm -> stackTop [- argCount - 1 ] = bound -> receiver ;
101- return call (bound -> method , argCount , vm );
100+ pVM -> stackTop [- argCount - 1 ] = bound -> receiver ;
101+ return call (bound -> method , argCount , pVM );
102102 }
103103 case OBJ_CLASS : {
104104 ObjClass * klass = AS_CLASS (callee );
105- vm -> stackTop [- argCount - 1 ] = OBJ_VAL (newInstance (klass ));
105+ pVM -> stackTop [- argCount - 1 ] = OBJ_VAL (newInstance (klass ));
106106 Value initializer ;
107- if (tableGet (& klass -> methods , vm -> initString , & initializer )) {
108- return call (AS_CLOSURE (initializer ), argCount , vm );
107+ if (tableGet (& klass -> methods , pVM -> initString , & initializer )) {
108+ return call (AS_CLOSURE (initializer ), argCount , pVM );
109109 } else if (argCount != 0 ) {
110- runtimeError (vm , "Expected 0 arguments but got %d." , argCount );
110+ runtimeError (pVM , "Expected 0 arguments but got %d." , argCount );
111111 return false;
112112 }
113113 return true;
114114 }
115115 case OBJ_CLOSURE :
116- return call (AS_CLOSURE (callee ), argCount , vm );
116+ return call (AS_CLOSURE (callee ), argCount , pVM );
117117 case OBJ_NATIVE : {
118118 NativeFn native = AS_NATIVE (callee );
119- Value result = native (argCount , vm -> stackTop - argCount );
120- vm -> stackTop -= argCount + 1 ;
121- push (vm , result );
119+ Value result = native (argCount , pVM -> stackTop - argCount );
120+ pVM -> stackTop -= argCount + 1 ;
121+ push (pVM , result );
122122 return true;
123123 }
124124 default :
125125 break ; // Non-callable object type
126126 }
127127 }
128- runtimeError (vm , "Can only call functions and classes." );
128+ runtimeError (pVM , "Can only call functions and classes." );
129129 return false;
130130}
131131
132132bool invokeFromClass (struct ObjClass * klass , ObjString * name ,
133- int argCount , VM * vm ) {
133+ int argCount , VM * pVM ) {
134134 Value method ;
135135 if (!tableGet (& klass -> methods , name , & method )) {
136- runtimeError (vm , "Undefined property '%s'." , name -> chars );
136+ runtimeError (pVM , "Undefined property '%s'." , name -> chars );
137137 return false;
138138 }
139- return call (AS_CLOSURE (method ), argCount , vm );
139+ return call (AS_CLOSURE (method ), argCount , pVM );
140140}
141141
142- bool invoke (ObjString * name , int argCount , VM * vm ) {
143- Value receiver = peek (vm , argCount );
142+ bool invoke (ObjString * name , int argCount , VM * pVM ) {
143+ Value receiver = peek (pVM , argCount );
144144
145145 if (!IS_INSTANCE (receiver )) {
146- runtimeError (vm , "Only instances have methods." );
146+ runtimeError (pVM , "Only instances have methods." );
147147 return false;
148148 }
149149
150150 struct ObjInstance * instance = AS_INSTANCE (receiver );
151151
152152 Value value ;
153153 if (tableGet (& instance -> fields , name , & value )) {
154- vm -> stackTop [- argCount - 1 ] = value ;
155- return callValue (value , argCount , vm );
154+ pVM -> stackTop [- argCount - 1 ] = value ;
155+ return callValue (value , argCount , pVM );
156156 }
157157
158- return invokeFromClass (instance -> klass , name , argCount , vm );
158+ return invokeFromClass (instance -> klass , name , argCount , pVM );
159159}
160160
161161void appendToList (ObjList * list , Value value ) {
0 commit comments