runningwater 2 år sedan
incheckning
2cecc2b380
19 ändrade filer med 509 tillägg och 0 borttagningar
  1. 66 0
      .clang-format
  2. 68 0
      .gitignore
  3. 8 0
      .idea/.gitignore
  4. 2 0
      .idea/clox.iml
  5. 4 0
      .idea/misc.xml
  6. 8 0
      .idea/modules.xml
  7. 9 0
      CMakeLists.txt
  8. 28 0
      chunk.c
  9. 35 0
      chunk.h
  10. BIN
      cmake-build-debug/.ninja_deps
  11. 12 0
      cmake-build-debug/.ninja_log
  12. 160 0
      cmake-build-debug/build.ninja
  13. BIN
      cmake-build-debug/clox
  14. 12 0
      common.h
  15. 29 0
      debug.c
  16. 12 0
      debug.h
  17. 13 0
      main.c
  18. 18 0
      memory.c
  19. 25 0
      memory.h

+ 66 - 0
.clang-format

@@ -0,0 +1,66 @@
+# Generated from CLion C/C++ Code Style settings
+BasedOnStyle: LLVM
+AccessModifierOffset: -1
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: None
+AlignOperands: DontAlign
+AllowAllArgumentsOnNextLine: false
+AllowAllConstructorInitializersOnNextLine: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: Always
+AllowShortCaseLabelsOnASingleLine: true
+AllowShortFunctionsOnASingleLine: All
+AllowShortIfStatementsOnASingleLine: Always
+AllowShortLambdasOnASingleLine: All
+AllowShortLoopsOnASingleLine: true
+AlwaysBreakAfterReturnType: None
+AlwaysBreakTemplateDeclarations: Yes
+BreakBeforeBraces: Custom
+BraceWrapping:
+  AfterCaseLabel: false
+  AfterClass: false
+  AfterControlStatement: Never
+  AfterEnum: false
+  AfterFunction: false
+  AfterNamespace: false
+  AfterUnion: false
+  BeforeCatch: false
+  BeforeElse: false
+  IndentBraces: false
+  SplitEmptyFunction: false
+  SplitEmptyRecord: true
+BreakBeforeBinaryOperators: NonAssignment
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializers: BeforeColon
+BreakInheritanceList: BeforeColon
+ColumnLimit: 0
+CompactNamespaces: false
+ContinuationIndentWidth: 4
+IndentCaseLabels: true
+IndentPPDirectives: None
+IndentWidth: 2
+KeepEmptyLinesAtTheStartOfBlocks: true
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCSpaceAfterProperty: false
+ObjCSpaceBeforeProtocolList: false
+PointerAlignment: Right
+ReflowComments: false
+SpaceAfterCStyleCast: true
+SpaceAfterLogicalNot: false
+SpaceAfterTemplateKeyword: false
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCpp11BracedList: false
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatements
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 0
+SpacesInAngles: false
+SpacesInCStyleCastParentheses: false
+SpacesInContainerLiterals: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+TabWidth: 4
+UseTab: Never

+ 68 - 0
.gitignore

@@ -0,0 +1,68 @@
+### C template
+# Prerequisites
+*.d
+
+# Object files
+*.o
+*.ko
+*.obj
+*.elf
+
+# Linker output
+*.ilk
+*.map
+*.exp
+
+# Precompiled Headers
+*.gch
+*.pch
+
+# Libraries
+*.lib
+*.a
+*.la
+*.lo
+
+# Shared objects (inc. Windows DLLs)
+*.dll
+*.so
+*.so.*
+*.dylib
+
+# Executables
+*.exe
+*.out
+*.app
+*.i*86
+*.x86_64
+*.hex
+
+# Debug files
+*.dSYM/
+*.su
+*.idb
+*.pdb
+
+# Kernel Module Compile Results
+*.mod*
+*.cmd
+.tmp_versions/
+modules.order
+Module.symvers
+Mkfile.old
+dkms.conf
+
+### CMake template
+CMakeLists.txt.user
+CMakeCache.txt
+CMakeFiles
+CMakeScripts
+Testing
+Makefile
+cmake_install.cmake
+install_manifest.txt
+compile_commands.json
+CTestTestfile.cmake
+_deps
+.cmake
+

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 2 - 0
.idea/clox.iml

@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module classpath="CMake" type="CPP_MODULE" version="4" />

+ 4 - 0
.idea/misc.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
+</project>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/clox.iml" filepath="$PROJECT_DIR$/.idea/clox.iml" />
+    </modules>
+  </component>
+</project>

+ 9 - 0
CMakeLists.txt

@@ -0,0 +1,9 @@
+cmake_minimum_required(VERSION 3.25)
+# 项目名称
+project(clox C)
+
+set(CMAKE_C_STANDARD 99)
+
+file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
+# 添加源代码
+add_executable(clox ${files})

+ 28 - 0
chunk.c

@@ -0,0 +1,28 @@
+//
+// Created by 李晓明 on 2023/6/14.
+//
+#include "chunk.h"
+#include "memory.h"
+#include <stdlib.h>
+
+void initChunk(Chunk *chunk) {
+  chunk->count = 0;
+  chunk->capacity = 0;
+  chunk->code = NULL;
+}
+void freeChunk(Chunk *chunk) {
+  FREE_ARRAY(uint8_t, chunk->code, chunk->capacity);
+  initChunk(chunk);
+}
+void writeChunk(Chunk *chunk, uint8_t byte) {
+  // 容量不够
+  if (chunk->capacity < chunk->count + 1) {
+    int oldCapacity = chunk->capacity;
+    chunk->capacity = GROW_CAPACITY(oldCapacity);
+    chunk->code = GROW_ARRAY(uint8_t, chunk->code, oldCapacity, chunk->capacity);
+  }
+
+  // 赋值
+  chunk->code[chunk->count] = byte;
+  chunk->count++;
+}

+ 35 - 0
chunk.h

@@ -0,0 +1,35 @@
+//
+// Created by 李晓明 on 2023/6/14.
+//
+
+#ifndef CLOX__CHUNK_H_
+#define CLOX__CHUNK_H_
+
+#include "common.h"
+
+typedef enum {
+  OP_RETURN,
+} OpCode;
+
+//============================================================================
+// Dynamic array of instructions 扩容步骤
+//1. Allocate a new array with more capacity.
+//2. Copy the existing elements from the old array to the new one.
+//3. Store the new capacity.
+//4. Delete the old array.
+//5. Update code to point to the new array.
+//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);
+
+#endif//CLOX__CHUNK_H_

BIN
cmake-build-debug/.ninja_deps


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

@@ -0,0 +1,12 @@
+# 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

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 160 - 0
cmake-build-debug/build.ninja


BIN
cmake-build-debug/clox


+ 12 - 0
common.h

@@ -0,0 +1,12 @@
+//
+// Created by 李晓明 on 2023/6/14.
+//
+
+#ifndef CLOX__COMMON_H_
+#define CLOX__COMMON_H_
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#endif//CLOX__COMMON_H_

+ 29 - 0
debug.c

@@ -0,0 +1,29 @@
+//
+// Created by 李晓明 on 2023/8/16.
+//
+
+#include "debug.h"
+#include <stdio.h>
+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);
+
+  for (int offset = 0; offset < chunk->count;) {
+    offset = disassembleInstruction(chunk, offset);
+  }
+}
+int disassembleInstruction(Chunk *chunk, int offset) {
+  printf("%04d  ", offset);
+
+  uint8_t instruction = chunk->code[offset];
+  switch (instruction) {
+    case OP_RETURN:
+      return simpleInstruction("OP_RETURN", offset);
+    default:
+      printf("Unknown opcode %d\n", instruction);
+      return offset + 1;
+  }
+}

+ 12 - 0
debug.h

@@ -0,0 +1,12 @@
+//
+// Created by 李晓明 on 2023/8/16.
+//
+
+#ifndef CLOX_DEBUG_H
+#define CLOX_DEBUG_H
+#include "chunk.h"
+
+void disassembleChunk(Chunk *chunk, const char *name);
+int disassembleInstruction(Chunk *chunk, int offset);
+
+#endif//CLOX_DEBUG_H

+ 13 - 0
main.c

@@ -0,0 +1,13 @@
+#include "chunk.h"
+#include "common.h"
+#include "debug.h"
+
+int main(int argc, char *argv[]) {
+  Chunk chunk;
+  initChunk(&chunk);
+  writeChunk(&chunk, OP_RETURN);
+
+  disassembleChunk(&chunk, "test chunk");
+  freeChunk(&chunk);
+  return 0;
+}

+ 18 - 0
memory.c

@@ -0,0 +1,18 @@
+#include "memory.h"
+#include <stdlib.h>
+
+//    oldSize	 newSize	               Operation
+//    0	        Non‑zero	            Allocate new block.
+//    Non‑zero	0	                    Free allocation.
+//    Non‑zero	Smaller than oldSize	Shrink existing allocation.
+//    Non‑zero	Larger than oldSize	    Grow existing allocation.
+void *reallocate(void *pointer, size_t oldSize, size_t newSize) {
+  if (newSize == 0) {
+    free(pointer);
+    return NULL;
+  }
+
+  void *result = realloc(pointer, newSize);
+  if (result == NULL) exit(1);
+  return result;
+}

+ 25 - 0
memory.h

@@ -0,0 +1,25 @@
+//
+// Created by 李晓明 on 2023/8/16.
+//
+
+#ifndef CLOX_MEMORY_H
+#define CLOX_MEMORY_H
+
+#include "common.h"
+
+#define GROW_CAPACITY(capacity) \
+  ((capacity) < 8 ? 8 : (capacity) *2)
+
+#define GROW_ARRAY(type, pointer, oldCount, newCount) \
+  (type *) reallocate(pointer, sizeof(type) * (oldCount), sizeof(type) * (newCount))
+
+#define FREE_ARRAY(type, pointer, oldCount) \
+  reallocate(pointer, sizeof(type) * (oldCount), 0)
+
+//    oldSize	 newSize	               Operation
+//    0	        Non‑zero	            Allocate new block.
+//    Non‑zero	0	                    Free allocation.
+//    Non‑zero	Smaller than oldSize	Shrink existing allocation.
+//    Non‑zero	Larger than oldSize	    Grow existing allocation.
+void *reallocate(void *pointer, size_t oldSize, size_t newSize);
+#endif//CLOX_MEMORY_H