Chapter 24.2 (Does Not Compile)
This commit is contained in:
parent
9025da8168
commit
a52c55eb9a
3 changed files with 32 additions and 10 deletions
|
@ -44,7 +44,12 @@ typedef struct {
|
|||
int depth;
|
||||
} Local;
|
||||
|
||||
typedef enum { TYPE_FUNCTION, TYPE_SCRIPT } FunctionType;
|
||||
|
||||
typedef struct {
|
||||
ObjFunction *function;
|
||||
FunctionType type;
|
||||
|
||||
Local locals[UINT8_COUNT];
|
||||
int localCount;
|
||||
int scopeDepth;
|
||||
|
@ -54,7 +59,7 @@ Parser parser;
|
|||
Compiler *current = NULL;
|
||||
Chunk *compilingChunk;
|
||||
|
||||
static Chunk *currentChunk() { return compilingChunk; }
|
||||
static Chunk *currentChunk() { return ¤t->function->chunk; }
|
||||
|
||||
static void errorAt(Token *token, const char *message) {
|
||||
if (parser.panicMode)
|
||||
|
@ -165,19 +170,33 @@ static void patchJump(int offset) {
|
|||
currentChunk()->code[offset + 1] = jump & 0xff;
|
||||
}
|
||||
|
||||
static void initCompiler(Compiler *compiler) {
|
||||
static void initCompiler(Compiler *compiler, FunctionType type) {
|
||||
compiler->function = NULL;
|
||||
compiler->type = type;
|
||||
compiler->localCount = 0;
|
||||
compiler->scopeDepth = 0;
|
||||
compiler->function = newFunction();
|
||||
current = compiler;
|
||||
|
||||
Local *local = ¤t->locals[current->localCount++];
|
||||
local->depth = 0;
|
||||
local->name.start = "";
|
||||
local->name.length = 0;
|
||||
}
|
||||
|
||||
static void endCompiler() {
|
||||
static ObjFunction *endCompiler() {
|
||||
emitReturn();
|
||||
ObjFunction *function = current->function;
|
||||
|
||||
#ifdef DEBUG_PRINT_CODE
|
||||
if (!parser.hadError) {
|
||||
disassembleChunk(currentChunk(), "code");
|
||||
disassembleChunk(currentChunk(), function->name != NULL
|
||||
? function->name->chars
|
||||
: "<script>");
|
||||
}
|
||||
#endif
|
||||
|
||||
return function;
|
||||
}
|
||||
|
||||
static void beginScope() { current->scopeDepth++; }
|
||||
|
@ -649,11 +668,10 @@ static void defineVariable(uint8_t global) {
|
|||
|
||||
static ParseRule *getRule(TokenType type) { return &rules[type]; }
|
||||
|
||||
bool compile(const char *source, Chunk *chunk) {
|
||||
ObjFunction *compile(const char *source) {
|
||||
initScanner(source);
|
||||
Compiler compiler;
|
||||
initCompiler(&compiler);
|
||||
compilingChunk = chunk;
|
||||
initCompiler(&compiler, TYPE_SCRIPT);
|
||||
|
||||
parser.hadError = false;
|
||||
parser.panicMode = false;
|
||||
|
@ -664,6 +682,6 @@ bool compile(const char *source, Chunk *chunk) {
|
|||
declaration();
|
||||
}
|
||||
|
||||
endCompiler();
|
||||
return !parser.hadError;
|
||||
ObjFunction *function = endCompiler();
|
||||
return parser.hadError ? NULL : function;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,6 @@
|
|||
#include "object.h"
|
||||
#include "vm.h"
|
||||
|
||||
bool compile(const char *source, Chunk *chunk);
|
||||
ObjFunction *compile(const char *source);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -69,6 +69,10 @@ ObjString *copyString(const char *chars, int length) {
|
|||
}
|
||||
|
||||
static void printFunction(ObjFunction *function) {
|
||||
if (function->name == NULL) {
|
||||
printf("<script>");
|
||||
return;
|
||||
}
|
||||
printf("<fn %s>", function->name->chars);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue