runningwater 2 лет назад
Родитель
Сommit
4800a67ab9
9 измененных файлов с 24 добавлено и 179 удалено
  1. 1 0
      .gitignore
  2. 0 2
      chunk.h
  3. BIN
      cmake-build-debug/.ninja_deps
  4. 0 12
      cmake-build-debug/.ninja_log
  5. 0 160
      cmake-build-debug/build.ninja
  6. BIN
      cmake-build-debug/clox
  7. 10 4
      debug.c
  8. 1 1
      main.c
  9. 12 0
      memory.c

+ 1 - 0
.gitignore

@@ -66,3 +66,4 @@ CTestTestfile.cmake
 _deps
 .cmake
 
+cmake-build-debug

+ 0 - 2
chunk.h

@@ -21,13 +21,11 @@ typedef enum {
 //6. Store the element in the new array now that there is room.
 //7. Update the count.
 //============================================================================
-
 typedef struct {
   int count;    // 使用量
   int capacity; // 容量
   uint8_t *code;// unsigned char*
 } Chunk;
-
 void initChunk(Chunk *chunk);
 void freeChunk(Chunk* chunk);
 void writeChunk(Chunk *chunk, uint8_t byte);

BIN
cmake-build-debug/.ninja_deps


+ 0 - 12
cmake-build-debug/.ninja_log

@@ -1,12 +0,0 @@
-# ninja log v5
-1	232	1692160684471170639	CMakeFiles/clox.dir/main.c.o	5e2eec2c62253d48
-1	312	1692160684554141247	CMakeFiles/clox.dir/memory.c.o	d92a90604a706c49
-2	156	1692161095971768625	CMakeFiles/clox.dir/chunk.c.o	b1aab498a322315
-156	264	1692161096081595358	clox	29f69e464ff1869a
-8	145	1692166444718029449	CMakeFiles/clox.dir/main.c.o	5e2eec2c62253d48
-8	191	1692166444765372672	CMakeFiles/clox.dir/debug.c.o	61c95ef22fc4b638
-191	308	1692166444882165240	clox	4308b68b7c510dc0
-1	138	1692166505390799830	CMakeFiles/clox.dir/debug.c.o	61c95ef22fc4b638
-138	256	1692166505511508723	clox	4308b68b7c510dc0
-1	78	1692166571359693653	CMakeFiles/clox.dir/debug.c.o	61c95ef22fc4b638
-78	199	1692166571481700943	clox	4308b68b7c510dc0

Разница между файлами не показана из-за своего большого размера
+ 0 - 160
cmake-build-debug/build.ninja


BIN
cmake-build-debug/clox


+ 10 - 4
debug.c

@@ -4,10 +4,18 @@
 
 #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;
 }
+
 void disassembleChunk(Chunk *chunk, const char *name) {
   printf("== %s ==\n", name);
 
@@ -20,10 +28,8 @@ int disassembleInstruction(Chunk *chunk, int offset) {
 
   uint8_t instruction = chunk->code[offset];
   switch (instruction) {
-    case OP_RETURN:
-      return simpleInstruction("OP_RETURN", offset);
-    default:
-      printf("Unknown opcode %d\n", instruction);
+    case OP_RETURN:return simpleInstruction("OP_RETURN", offset);
+    default:printf("Unknown opcode %d\n", instruction);
       return offset + 1;
   }
 }

+ 1 - 1
main.c

@@ -6,7 +6,7 @@ int main(int argc, char *argv[]) {
   Chunk chunk;
   initChunk(&chunk);
   writeChunk(&chunk, OP_RETURN);
-
+  
   disassembleChunk(&chunk, "test chunk");
   freeChunk(&chunk);
   return 0;

+ 12 - 0
memory.c

@@ -12,6 +12,18 @@ void *reallocate(void *pointer, size_t oldSize, size_t newSize) {
     return NULL;
   }
 
+  /*
+  The realloc() function tries to change the size of the allocation pointed
+   to by ptr to size, and returns ptr.  If there is not enough room to
+   enlarge the memory allocation pointed to by ptr, realloc() creates a new
+   allocation, copies as much of the old data pointed to by ptr as will fit
+   to the new allocation, frees the old allocation, and returns a pointer to
+   the allocated memory.  If ptr is NULL, realloc() is identical to a call
+   to malloc() for size bytes.  If size is zero and ptr is not NULL, a new,
+   minimum sized object is allocated and the original object is freed.  When
+   extending a region allocated with calloc(3), realloc(3) does not guaran-
+   tee that the additional memory is also zero-filled
+   */
   void *result = realloc(pointer, newSize);
   if (result == NULL) exit(1);
   return result;