@@ -142,7 +142,7 @@ lox::Value VM::stack_peek() {
142142 return stack.back ();
143143}
144144
145- std::string VM::read_global_name () {
145+ std::string VM::read_constant_string () {
146146 lox::Value var_name_value = read_constant ();
147147 // This is technically unsafe since the constant could be any Value, but
148148 // by construction of the parser it should always be a string.
@@ -399,7 +399,7 @@ InterpretResult VM::run() {
399399 // have a DEFINE_GLOBAL instruction followed by the constant index.
400400 // When we get here we have already seen the DEFINE_GLOBAL
401401 // instruction, so we need to read the variable name.
402- std::string var_name = read_global_name ();
402+ std::string var_name = read_constant_string ();
403403 // After this, the parser will have emitted bytecode that pushes the
404404 // value of the variable onto the stack. So we need to pop that value.
405405 // (Or, following the book, just peek it now and pop it later, after
@@ -414,8 +414,14 @@ InterpretResult VM::run() {
414414 stack_pop (); // now pop the value
415415 break ;
416416 }
417+ case OpCode::CLASS : {
418+ std::string class_name = read_constant_string ();
419+ auto new_class = _gc.alloc <ObjClass>(class_name);
420+ stack_push (new_class);
421+ break ;
422+ }
417423 case OpCode::GET_GLOBAL : {
418- std::string var_name = read_global_name ();
424+ std::string var_name = read_constant_string ();
419425 // Now that we have the name of the variable, we can look it up in our
420426 // map
421427 auto it = globals.find (var_name);
@@ -426,7 +432,7 @@ InterpretResult VM::run() {
426432 break ;
427433 }
428434 case OpCode::SET_GLOBAL : {
429- std::string var_name = read_global_name ();
435+ std::string var_name = read_constant_string ();
430436 auto it = globals.find (var_name);
431437 if (it == globals.end ()) {
432438 error (" undefined variable '" + var_name + " '" );
0 commit comments