|
|
@@ -4,17 +4,9 @@
|
|
|
|
|
|
#include "debug.h"
|
|
|
#include <stdio.h>
|
|
|
-
|
|
|
-/**
|
|
|
- * @brief 打印指令
|
|
|
- * @param name 名称
|
|
|
- * @param offset 偏移
|
|
|
- * @return
|
|
|
- */
|
|
|
-static int simpleInstruction(const char *name, int offset) {
|
|
|
- printf("%s\n", name);
|
|
|
- return offset + 1;
|
|
|
-}
|
|
|
+#include "value.h"
|
|
|
+static int constantInstruction(const char *, Chunk *, int);
|
|
|
+static int simpleInstruction(const char *name, int offset);
|
|
|
|
|
|
void disassembleChunk(Chunk *chunk, const char *name) {
|
|
|
printf("== %s ==\n", name);
|
|
|
@@ -28,8 +20,33 @@ int disassembleInstruction(Chunk *chunk, int offset) {
|
|
|
|
|
|
uint8_t instruction = chunk->code[offset];
|
|
|
switch (instruction) {
|
|
|
+ case OP_CONSTANT:return constantInstruction("OP_CONSTANT", chunk, offset);
|
|
|
case OP_RETURN:return simpleInstruction("OP_RETURN", offset);
|
|
|
default:printf("Unknown opcode %d\n", instruction);
|
|
|
return offset + 1;
|
|
|
}
|
|
|
}
|
|
|
+/**
|
|
|
+ * @brief 打印常量指令
|
|
|
+ * @param name
|
|
|
+ * @param chunk
|
|
|
+ * @param offset
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+static int constantInstruction(const char *name, Chunk *chunk, int offset) {
|
|
|
+ uint8_t constant = chunk->code[offset + 1];
|
|
|
+ printf("%-16s %4d '", name, constant);
|
|
|
+ printValue(chunk->constants.values[constant]);
|
|
|
+ printf("'\n");
|
|
|
+ return offset + 2;
|
|
|
+}
|
|
|
+/**
|
|
|
+* @brief 打印指令
|
|
|
+* @param name 名称
|
|
|
+* @param offset 偏移
|
|
|
+* @return
|
|
|
+*/
|
|
|
+static int simpleInstruction(const char *name, int offset) {
|
|
|
+ printf("%s\n", name);
|
|
|
+ return offset + 1;
|
|
|
+}
|