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