Unify code for ending compiler.

There's now a single code path for the end of a chunk of bytecode, so
we can eventually put code there for capturing closures.
This commit is contained in:
Bob Nystrom
2013-12-01 14:59:56 -08:00
parent 19811143a0
commit e4043d69b4
3 changed files with 78 additions and 27 deletions
+24 -7
View File
@@ -28,17 +28,21 @@ typedef enum
// Pop a superclass off the stack, then push a new class that extends it.
CODE_SUBCLASS,
// Add a method for symbol [arg1] with body stored in constant [arg2] to the
// class on the top of stack. Does not modify the stack.
// Define a method for symbol [arg] whose body is the function popped off the
// top of the stack. The class receiving the method is on top of the stack
// (after the function is popped off) and remains after this.
CODE_METHOD_INSTANCE,
// Add a method for symbol [arg1] with body stored in constant [arg2] to the
// metaclass of the class on the top of stack. Does not modify the stack.
// Define a method for symbol [arg] whose body is the function popped off the
// top of the stack. The class receiving the method is the metaclass of the
// class on top of the stack (after the function is popped off) and remains
// after this.
CODE_METHOD_STATIC,
// Add a constructor method for symbol [arg1] with body stored in constant
// [arg2] to the metaclass of the class on the top of stack. Does not modify
// the stack.
// Define a constructor method for symbol [arg] whose body is the function
// popped off the top of the stack. The class receiving the method is the
// metaclass of the class on top of the stack (after the function is popped
// off) and remains after this.
CODE_METHOD_CTOR,
// Create a new list with [arg] elements. The top [arg] values on the stack
@@ -115,6 +119,19 @@ typedef enum
CODE_END
} Code;
// TODO(bob): add opcode for closure
// functions with no upvalues can just be loaded as constants like normal
// functions which do have upvalues need a CODE_CLOSURE op. it takes the
// constant index of the fn as an argument.
// it loads that fn, then wraps it in a closure object.
// then it captures all of the upvalues.
// those can either be upvalues for locals, or upvalues that close over an
// outer upvalue.
//
// then add ops for loading and storing an upvalue. take upvalue index as arg.
typedef struct
{
// TODO(bob): Make this dynamically sized.