Cahapter 22.1
This commit is contained in:
parent
26b418d254
commit
df1985ae84
2 changed files with 22 additions and 0 deletions
|
@ -8,4 +8,6 @@
|
||||||
#define DEBUG_PRINT_CODE
|
#define DEBUG_PRINT_CODE
|
||||||
#define DEBUG_TRACE_EXECUTION
|
#define DEBUG_TRACE_EXECUTION
|
||||||
|
|
||||||
|
#define UINT8_COUNT (UINT8_MAX + 1)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -38,7 +38,19 @@ typedef struct {
|
||||||
Precedence precedence;
|
Precedence precedence;
|
||||||
} ParseRule;
|
} ParseRule;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Token name;
|
||||||
|
int depth;
|
||||||
|
} Local;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Local locals[UINT8_COUNT];
|
||||||
|
int localCount;
|
||||||
|
int scopeDepth;
|
||||||
|
} Compiler;
|
||||||
|
|
||||||
Parser parser;
|
Parser parser;
|
||||||
|
Compiler *current = NULL;
|
||||||
Chunk *compilingChunk;
|
Chunk *compilingChunk;
|
||||||
|
|
||||||
static Chunk *currentChunk() { return compilingChunk; }
|
static Chunk *currentChunk() { return compilingChunk; }
|
||||||
|
@ -122,6 +134,12 @@ static void emitConstant(Value value) {
|
||||||
emitBytes(OP_CONSTANT, makeConstant(value));
|
emitBytes(OP_CONSTANT, makeConstant(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void initCompiler(Compiler *compiler) {
|
||||||
|
compiler->localCount = 0;
|
||||||
|
compiler->scopeDepth = 0;
|
||||||
|
current = compiler;
|
||||||
|
}
|
||||||
|
|
||||||
static void endCompiler() {
|
static void endCompiler() {
|
||||||
emitReturn();
|
emitReturn();
|
||||||
#ifdef DEBUG_PRINT_CODE
|
#ifdef DEBUG_PRINT_CODE
|
||||||
|
@ -398,6 +416,8 @@ static ParseRule *getRule(TokenType type) { return &rules[type]; }
|
||||||
|
|
||||||
bool compile(const char *source, Chunk *chunk) {
|
bool compile(const char *source, Chunk *chunk) {
|
||||||
initScanner(source);
|
initScanner(source);
|
||||||
|
Compiler compiler;
|
||||||
|
initCompiler(&compiler);
|
||||||
compilingChunk = chunk;
|
compilingChunk = chunk;
|
||||||
|
|
||||||
parser.hadError = false;
|
parser.hadError = false;
|
||||||
|
|
Loading…
Reference in a new issue