runningwater 2 سال پیش
والد
کامیت
201d5da103
9فایلهای تغییر یافته به همراه143 افزوده شده و 21 حذف شده
  1. 7 0
      .idea/codeStyles/Project.xml
  2. 5 0
      .idea/codeStyles/codeStyleConfig.xml
  3. 6 0
      .idea/vcs.xml
  4. 7 1
      chunk.c
  5. 23 8
      chunk.h
  6. 28 11
      debug.c
  7. 12 1
      main.c
  8. 26 0
      value.c
  9. 29 0
      value.h

+ 7 - 0
.idea/codeStyles/Project.xml

@@ -0,0 +1,7 @@
+<component name="ProjectCodeStyleConfiguration">
+  <code_scheme name="Project" version="173">
+    <clangFormatSettings>
+      <option name="ENABLED" value="true" />
+    </clangFormatSettings>
+  </code_scheme>
+</component>

+ 5 - 0
.idea/codeStyles/codeStyleConfig.xml

@@ -0,0 +1,5 @@
+<component name="ProjectCodeStyleConfiguration">
+  <state>
+    <option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
+  </state>
+</component>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 7 - 1
chunk.c

@@ -9,9 +9,11 @@ void initChunk(Chunk *chunk) {
   chunk->count = 0;
   chunk->capacity = 0;
   chunk->code = NULL;
+  initValueArray(&chunk->constants);
 }
 void freeChunk(Chunk *chunk) {
   FREE_ARRAY(uint8_t, chunk->code, chunk->capacity);
+  freeValueArray(&chunk->constants);
   initChunk(chunk);
 }
 void writeChunk(Chunk *chunk, uint8_t byte) {
@@ -25,4 +27,8 @@ void writeChunk(Chunk *chunk, uint8_t byte) {
   // 赋值
   chunk->code[chunk->count] = byte;
   chunk->count++;
-}
+}
+int addConstant(Chunk *chunk, Value value) {
+  writeValueArray(&chunk->constants, value);
+  return chunk->constants.count - 1;
+}

+ 23 - 8
chunk.h

@@ -1,14 +1,23 @@
-//
-// Created by 李晓明 on 2023/6/14.
-//
+/**
+******************************************************************************
+* @file           : chunk.h
+* @author         : simon
+* @brief          : Chunks contain almost all of the information
+*                   that the runtime needs from the user’s source code
+* @attention      : None
+* @date           : 2023/8/16
+******************************************************************************
+*/
 
 #ifndef CLOX__CHUNK_H_
 #define CLOX__CHUNK_H_
 
 #include "common.h"
+#include "value.h"
 
 typedef enum {
-  OP_RETURN,
+  OP_CONSTANT,///<OP_CONSTANT (index)+>
+  OP_RETURN,  ///<OP_RETURN>
 } OpCode;
 
 //============================================================================
@@ -22,12 +31,18 @@ typedef enum {
 //7. Update the count.
 //============================================================================
 typedef struct {
-  int count;    // 使用量
-  int capacity; // 容量
-  uint8_t *code;// unsigned char*
+  int count;           // 使用量
+  int capacity;        // 容量
+  uint8_t *code;       // unsigned char*
+  ValueArray constants;// 常量池
 } Chunk;
 void initChunk(Chunk *chunk);
-void freeChunk(Chunk* chunk);
+void freeChunk(Chunk *chunk);
 void writeChunk(Chunk *chunk, uint8_t byte);
+/// 添加常量
+/// \param chunk 指令数组
+/// \param value 值
+/// \return index of constant 常量位置
+int addConstant(Chunk *chunk, Value value);
 
 #endif//CLOX__CHUNK_H_

+ 28 - 11
debug.c

@@ -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;
+}

+ 12 - 1
main.c

@@ -2,11 +2,22 @@
 #include "common.h"
 #include "debug.h"
 
+/*!
+ * @brief 程序主入口
+ * @param argc
+ * @param argv
+ * @return
+ */
 int main(int argc, char *argv[]) {
   Chunk chunk;
   initChunk(&chunk);
+
+  int constant = addConstant(&chunk, 1.2);
+  writeChunk(&chunk, OP_CONSTANT);
+  writeChunk(&chunk, constant);
+
   writeChunk(&chunk, OP_RETURN);
-  
+
   disassembleChunk(&chunk, "test chunk");
   freeChunk(&chunk);
   return 0;

+ 26 - 0
value.c

@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include "value.h"
+#include "memory.h"
+
+void initValueArray(ValueArray *array) {
+  array->values = NULL;
+  array->capacity = 0;
+  array->count = 0;
+};
+void writeValueArray(ValueArray *array, Value value) {
+  if (array->capacity < array->count + 1) {
+    int oldCapacity = array->capacity;
+    array->capacity = GROW_CAPACITY(oldCapacity);
+    array->values = GROW_ARRAY(Value, array->values, oldCapacity, array->capacity);
+  }
+
+  array->values[array->count] = value;
+  array->count++;
+};
+void freeValueArray(ValueArray *array) {
+  FREE_ARRAY(Value, array->values, array->capacity);
+  initValueArray(array);
+}
+void printValue(Value value) {
+  printf("%g", value);
+};

+ 29 - 0
value.h

@@ -0,0 +1,29 @@
+/**
+  ******************************************************************************
+  * @file           : value.h
+  * @author         : simon
+  * @brief          : 常量 support only double-precision, floating-point numbers
+  * @attention      : None
+  * @date           : 2023/8/16
+  ******************************************************************************
+  */
+
+#ifndef CLOX__VALUE_H_
+#define CLOX__VALUE_H_
+
+#include "common.h"
+
+typedef double Value;
+
+/// \brief 常量池
+typedef struct {
+  int capacity;
+  int count;
+  Value *values;
+} ValueArray;
+
+void initValueArray(ValueArray *array);
+void writeValueArray(ValueArray *array, Value value);
+void freeValueArray(ValueArray *array);
+void printValue(Value value);
+#endif //CLOX__VALUE_H_