aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom Willemse2022-01-22 22:01:54 -0800
committerGravatar Tom Willemse2022-01-22 22:01:54 -0800
commita52c55eb9af3e29a912d41a48071e830b0971fd5 (patch)
tree579f8d8e934f63c67aef9a8da60d60fe136bd928
parent9025da8168e505e2b0e32b89a0d94db935dace93 (diff)
downloadcrafting-interpreters-a52c55eb9af3e29a912d41a48071e830b0971fd5.tar.gz
crafting-interpreters-a52c55eb9af3e29a912d41a48071e830b0971fd5.zip
Chapter 24.2 (Does Not Compile)
-rw-r--r--clox/src/compiler.c36
-rw-r--r--clox/src/compiler.h2
-rw-r--r--clox/src/object.c4
3 files changed, 32 insertions, 10 deletions
diff --git a/clox/src/compiler.c b/clox/src/compiler.c
index aed21f7..44eede8 100644
--- a/clox/src/compiler.c
+++ b/clox/src/compiler.c
@@ -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 &current->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 = &current->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;
}
diff --git a/clox/src/compiler.h b/clox/src/compiler.h
index e7023a9..7de1c20 100644
--- a/clox/src/compiler.h
+++ b/clox/src/compiler.h
@@ -4,6 +4,6 @@
#include "object.h"
#include "vm.h"
-bool compile(const char *source, Chunk *chunk);
+ObjFunction *compile(const char *source);
#endif
diff --git a/clox/src/object.c b/clox/src/object.c
index 7170352..2df533e 100644
--- a/clox/src/object.c
+++ b/clox/src/object.c
@@ -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);
}