Code.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #ifndef BASIC_CODE_H
  2. #define BASIC_CODE_H
  3. #include "Buffer.h"
  4. #include "Error.h"
  5. #include "Types.h"
  6. typedef enum : u8 { VT_INT32, VT_ARRAY, VT_CONSTANT_STRING } ValueType;
  7. typedef struct {
  8. ValueType type;
  9. u8 visitMarker;
  10. u8 reserved2;
  11. u8 reserved3;
  12. i32 data;
  13. } Value;
  14. typedef struct Allocation Allocation;
  15. struct Allocation {
  16. Allocation* next;
  17. Value values[];
  18. };
  19. #define INT_VALUE(value) ((Value){.type = VT_INT32, .data = (value)})
  20. static_assert(sizeof(Value) == 8);
  21. typedef enum : u8 {
  22. ADD,
  23. SUB,
  24. MUL,
  25. DIV,
  26. PUSH_CONSTANT_STRING,
  27. PUSH_INT32,
  28. POP,
  29. JUMP,
  30. JUMP_ON_0,
  31. JUMP_SUB,
  32. RETURN_VALUE,
  33. READ_VARIABLE,
  34. READ_VARIABLE_ARRAY,
  35. SET_VARIABLE,
  36. SET_VARIABLE_ARRAY,
  37. PUSH_STACK_VARIABLES,
  38. AND,
  39. OR,
  40. NOT,
  41. EQUAL,
  42. NOT_EQUAL,
  43. GREATER,
  44. SMALLER,
  45. GREATER_OR_EQUAL,
  46. SMALLER_OR_EQUAL,
  47. CALL_SYSTEM,
  48. STOP
  49. } Instruction;
  50. typedef struct {
  51. Buffer code;
  52. Error error;
  53. Value* stack;
  54. Allocation* allocations;
  55. size_t maxStackSize;
  56. size_t stackIndex;
  57. i32 localVariableIndex;
  58. } Code;
  59. void codeInit(Code* code);
  60. void codeDestroy(Code* code);
  61. void codeReset(Code* code);
  62. void codeRun(Code* code);
  63. bool codeHasRunError(const Code* code);
  64. const char* codeGetRunError(const Code* code);
  65. void codeRunCollector(Code* code);
  66. void codeDump(const Code* code);
  67. #define SET_ERROR(format, ...) \
  68. snprintf( \
  69. c->error.text, sizeof(c->error.text), "%zu | " format, \
  70. c->code.readIndex __VA_OPT__(, ) __VA_ARGS__)
  71. #endif