Ver Fonte

Simplify

Kajetan Johannes Hammerle há 1 semana atrás
pai
commit
ce696535b0
11 ficheiros alterados com 243 adições e 347 exclusões
  1. 0 1
      CMakeLists.txt
  2. 1 30
      src/Buffer.c
  3. 0 13
      src/Buffer.h
  4. 163 26
      src/Code.c
  5. 4 20
      src/Code.h
  6. 0 166
      src/CodeRunner.c
  7. 52 32
      src/Compiler.c
  8. 4 44
      src/Main.c
  9. 6 2
      src/Memory.c
  10. 11 11
      src/Tokenizer.c
  11. 2 2
      src/Tokenizer.h

+ 0 - 1
CMakeLists.txt

@@ -6,7 +6,6 @@ set(CMAKE_C_STANDARD 23)
 set(SRC
     "src/Main.c"
     "src/Code.c"
-    "src/CodeRunner.c"
     "src/Error.c"
     "src/Compiler.c"
     "src/Tokenizer.c"

+ 1 - 30
src/Buffer.c

@@ -7,10 +7,7 @@
 #include "Memory.h"
 
 void bufferInit(Buffer* b) {
-    b->data = nullptr;
-    b->maxIndex = 0;
-    b->readIndex = 0;
-    b->writeIndex = 0;
+    *b = (Buffer){};
 }
 
 void bufferDestroy(Buffer* b) {
@@ -57,14 +54,8 @@ bool bufferRead(Buffer* b, void* p, size_t n) {
         return bufferRead(b, t, sizeof(type));   \
     }
 
-BUFFER_READ_WRITE_IMPL(I8, i8)
-BUFFER_READ_WRITE_IMPL(I16, i16)
 BUFFER_READ_WRITE_IMPL(I32, i32)
-BUFFER_READ_WRITE_IMPL(I64, i64)
 BUFFER_READ_WRITE_IMPL(U8, u8)
-BUFFER_READ_WRITE_IMPL(U16, u16)
-BUFFER_READ_WRITE_IMPL(U32, u32)
-BUFFER_READ_WRITE_IMPL(U64, u64)
 
 bool bufferWriteString(Buffer* b, const char* s) {
     size_t l = strlen(s) + 1;
@@ -84,23 +75,3 @@ const char* bufferReadString(Buffer* b) {
     b->readIndex += l;
     return s;
 }
-
-bool bufferIsEmpty(const Buffer* b) {
-    return b->readIndex >= b->writeIndex;
-}
-
-size_t bufferGetReadIndex(const Buffer* b) {
-    return b->readIndex;
-}
-
-void bufferSetReadIndex(Buffer* b, size_t n) {
-    b->readIndex = n;
-}
-
-size_t bufferGetWriteIndex(const Buffer* b) {
-    return b->writeIndex;
-}
-
-void bufferSetWriteIndex(Buffer* b, size_t n) {
-    b->writeIndex = n;
-}

+ 0 - 13
src/Buffer.h

@@ -20,23 +20,10 @@ void bufferReset(Buffer* b);
     [[nodiscard]] bool bufferWrite##Type(Buffer* b, type t); \
     [[nodiscard]] bool bufferRead##Type(Buffer* b, type* t)
 
-BUFFER_READ_WRITE(I8, i8);
-BUFFER_READ_WRITE(I16, i16);
 BUFFER_READ_WRITE(I32, i32);
-BUFFER_READ_WRITE(I64, i64);
 BUFFER_READ_WRITE(U8, u8);
-BUFFER_READ_WRITE(U16, u16);
-BUFFER_READ_WRITE(U32, u32);
-BUFFER_READ_WRITE(U64, u64);
 
 [[nodiscard]] bool bufferWriteString(Buffer* b, const char* s);
 const char* bufferReadString(Buffer* b);
 
-bool bufferIsEmpty(const Buffer* b);
-
-size_t bufferGetReadIndex(const Buffer* b);
-void bufferSetReadIndex(Buffer* b, size_t n);
-size_t bufferGetWriteIndex(const Buffer* b);
-void bufferSetWriteIndex(Buffer* b, size_t n);
-
 #endif

+ 163 - 26
src/Code.c

@@ -1,5 +1,12 @@
 #include "Code.h"
 
+#include <stdio.h>
+#include <string.h>
+
+#include "Code.h"
+#include "Constants.h"
+#include "Memory.h"
+
 void codeInit(Code* c) {
     *c = (Code){};
     bufferInit(&c->code);
@@ -7,6 +14,7 @@ void codeInit(Code* c) {
 
 void codeDestroy(Code* c) {
     bufferDestroy(&c->code);
+    memoryFree(c->stack);
     *c = (Code){};
 }
 
@@ -14,52 +22,181 @@ void codeReset(Code* c) {
     bufferReset(&c->code);
 }
 
-bool codePushInstruction(Code* c, Instruction i) {
-    return bufferWriteU8(&c->code, i);
+#define POP_VALUE(name)     \
+    Value name;             \
+    if(iPopValue(c, &name)) \
+    return true
+
+#define INT_VALUE(value) ((Value){.type = VT_INT32, .data = (value)})
+#define CSTRING_VALUE(value)                               \
+    ((Value){.type = VT_CONSTANT_STRING, .data = (value)})
+
+#define SET_ERROR(format, ...)                \
+    snprintf(                                 \
+        c->error.text, sizeof(c->error.text), \
+        format __VA_OPT__(, ) __VA_ARGS__)
+
+[[nodiscard]] static bool codeReadI32(Code* c, i32* i) {
+    return bufferReadI32(&c->code, i);
+}
+
+[[nodiscard]] static const char* codeReadConstantString(Code* c) {
+    return bufferReadString(&c->code);
 }
 
-bool codePushI64(Code* c, i64 i) {
-    return bufferWriteI64(&c->code, i);
+static void codeSetPosition(Code* c, size_t pos) {
+    c->code.readIndex = pos;
 }
 
-bool codePushSize(Code* c, size_t i) {
-    return bufferWrite(&c->code, &i, sizeof(i));
+static bool iPushValue(Code* c, Value v) {
+    while(c->stackIndex >= c->maxStackSize) {
+        size_t newSize = c->maxStackSize <= 0 ? 16 : (c->maxStackSize * 5) / 4;
+        if(newSize >= MAX_STACK_VALUES) {
+            SET_ERROR("Stack overflow");
+            return true;
+        }
+        Value* newValues = memoryAllocate(sizeof(Value) * newSize);
+        if(newValues == nullptr) {
+            SET_ERROR("Out of memory for stack");
+            return true;
+        }
+        memcpy(newValues, c->stack, sizeof(Value) * c->stackIndex);
+        memoryFree(c->stack);
+        c->stack = newValues;
+        c->maxStackSize = newSize;
+    }
+    c->stack[c->stackIndex++] = v;
+    return false;
 }
 
-bool codePushConstantString(Code* c, const char* s) {
-    return bufferWriteString(&c->code, s);
+static bool iPopValue(Code* c, Value* v) {
+    if(c->stackIndex <= 0) {
+        SET_ERROR("Pop on empty stack");
+        return true;
+    }
+    *v = c->stack[--c->stackIndex];
+    return false;
 }
 
-Instruction codeReadInstruction(Code* c) {
-    Instruction i = STOP;
-    (void)bufferReadU8(&c->code, &i);
-    return i;
+static bool iAdd(Code* c) {
+    POP_VALUE(a);
+    POP_VALUE(b);
+    return iPushValue(c, INT_VALUE(a.data + b.data));
 }
 
-bool codeReadI64(Code* c, i64* i) {
-    return bufferReadI64(&c->code, i);
+static bool iPushConstantString(Code* c) {
+    const char* s = codeReadConstantString(c);
+    i32 address = (i32)((const u8*)s - c->code.data);
+    return iPushValue(c, CSTRING_VALUE(address));
 }
 
-bool codeReadSize(Code* c, size_t* s) {
-    return bufferRead(&c->code, s, sizeof(size_t));
+static bool iPushInt(Code* c) {
+    i32 i = 0;
+    if(codeReadI32(c, &i)) {
+        SET_ERROR("PushInt without value");
+        return true;
+    }
+    return iPushValue(c, INT_VALUE(i));
 }
 
-const char* codeReadConstantString(Code* c) {
-    return bufferReadString(&c->code);
+static bool iPrint(Code* c) {
+    POP_VALUE(a);
+    switch(a.type) {
+        case VT_INT32: printf("%d", a.data); break;
+        case VT_CONSTANT_STRING: printf("%s", c->code.data + a.data); break;
+    }
+    return false;
+}
+
+static bool iPrintNewline() {
+    putchar('\n');
+    return false;
+}
+
+static bool iJumpIf(Code* c) {
+    POP_VALUE(a);
+    i32 jumpPos = 0;
+    if(codeReadI32(c, &jumpPos)) {
+        SET_ERROR("JumpIf without position");
+        return true;
+    }
+    if(a.data == 0) {
+        codeSetPosition(c, (size_t)jumpPos);
+    }
+    return false;
+}
+
+static bool iReadVariable(Code* c) {
+    i32 address = 0;
+    if(codeReadI32(c, &address)) {
+        SET_ERROR("ReadVariable without address");
+        return true;
+    } else if((size_t)address >= c->stackIndex) {
+        SET_ERROR("ReadVariable with invalid address");
+        return true;
+    }
+    return iPushValue(c, c->stack[address]);
+}
+
+static bool iSetVariable(Code* c) {
+    i32 address = 0;
+    if(codeReadI32(c, &address)) {
+        SET_ERROR("SetVariable without address");
+        return true;
+    } else if((size_t)address >= c->stackIndex) {
+        SET_ERROR("SetVariable with invalid address");
+        return true;
+    }
+    POP_VALUE(a);
+    c->stack[address] = a;
+    return false;
+}
+
+static bool iPushStackVariables(Code* c) {
+    i32 amount = 0;
+    if(codeReadI32(c, &amount)) {
+        SET_ERROR("PushStackVariables without amount");
+        return true;
+    }
+    Value v = INT_VALUE(0);
+    while(amount > 0) {
+        if(iPushValue(c, v)) {
+            return true;
+        }
+        amount--;
+    }
+    return false;
 }
 
-size_t codeGetWritePosition(const Code* c) {
-    return bufferGetWriteIndex(&c->code);
+static bool execute(Code* c, Instruction command) {
+    switch(command) {
+        case ADD: return iAdd(c);
+        case PUSH_CONSTANT_STRING: return iPushConstantString(c);
+        case PUSH_INT64: return iPushInt(c);
+        case PRINT: return iPrint(c);
+        case PRINT_NEWLINE: return iPrintNewline();
+        case JUMP_ON_0: return iJumpIf(c);
+        case READ_VARIABLE: return iReadVariable(c);
+        case SET_VARIABLE: return iSetVariable(c);
+        case PUSH_STACK_VARIABLES: return iPushStackVariables(c);
+        case STOP: return true;
+    }
+    return false;
 }
 
-void codeSetWritePosition(Code* c, size_t pos) {
-    bufferSetWriteIndex(&c->code, pos);
+void codeRun(Code* c) {
+    while(true) {
+        Instruction i = STOP;
+        if(bufferReadU8(&c->code, &i) || execute(c, i)) {
+            return;
+        }
+    }
 }
 
-size_t codeGetPosition(const Code* c) {
-    return bufferGetReadIndex(&c->code);
+bool codeHasRunError(const Code* code) {
+    return hasError(&code->error);
 }
 
-void codeSetPosition(Code* c, size_t pos) {
-    bufferSetReadIndex(&c->code, pos);
+const char* codeGetRunError(const Code* code) {
+    return code->error.text;
 }

+ 4 - 20
src/Code.h

@@ -5,20 +5,18 @@
 #include "Error.h"
 #include "Types.h"
 
-typedef enum : u8 { VT_INT64, VT_CONSTANT_STRING } ValueType;
+typedef enum : u8 { VT_INT32, VT_CONSTANT_STRING } ValueType;
 
 typedef struct {
     ValueType type;
     u8 reserved1;
     u8 reserved2;
     u8 reserved3;
-
-    union {
-        i64 intValue;
-        const char* constantStringValue;
-    };
+    i32 data;
 } Value;
 
+static_assert(sizeof(Value) == 8);
+
 typedef enum : u8 {
     ADD,
     PUSH_CONSTANT_STRING,
@@ -48,18 +46,4 @@ void codeRun(Code* code);
 bool codeHasRunError(const Code* code);
 const char* codeGetRunError(const Code* code);
 
-[[nodiscard]] bool codePushInstruction(Code* code, Instruction i);
-[[nodiscard]] bool codePushI64(Code* code, i64 i);
-[[nodiscard]] bool codePushSize(Code* code, size_t i);
-[[nodiscard]] bool codePushConstantString(Code* code, const char* c);
-size_t codeGetWritePosition(const Code* code);
-void codeSetWritePosition(Code* code, size_t pos);
-
-[[nodiscard]] Instruction codeReadInstruction(Code* code);
-[[nodiscard]] bool codeReadI64(Code* code, i64* i);
-[[nodiscard]] bool codeReadSize(Code* code, size_t* s);
-[[nodiscard]] const char* codeReadConstantString(Code* code);
-size_t codeGetPosition(const Code* code);
-void codeSetPosition(Code* code, size_t pos);
-
 #endif

+ 0 - 166
src/CodeRunner.c

@@ -1,166 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-
-#include "Code.h"
-#include "Constants.h"
-#include "Memory.h"
-
-#define POP_VALUE(name)     \
-    Value name;             \
-    if(iPopValue(c, &name)) \
-    return true
-
-#define INT_VALUE(value) ((Value){.type = VT_INT64, .intValue = (value)})
-#define CSTRING_VALUE(value)                                              \
-    ((Value){.type = VT_CONSTANT_STRING, .constantStringValue = (value)})
-
-#define SET_ERROR(format, ...)                \
-    snprintf(                                 \
-        c->error.text, sizeof(c->error.text), \
-        format __VA_OPT__(, ) __VA_ARGS__)
-
-static bool iPushValue(Code* c, Value v) {
-    while(c->stackIndex >= c->maxStackSize) {
-        size_t newSize = c->maxStackSize <= 0 ? 16 : (c->maxStackSize * 5) / 4;
-        if(newSize >= MAX_STACK_VALUES) {
-            SET_ERROR("Stack overflow");
-            return true;
-        }
-        Value* newValues = memoryAllocate(sizeof(Value) * newSize);
-        if(newValues == nullptr) {
-            SET_ERROR("Out of memory for stack");
-            return true;
-        }
-        memcpy(newValues, c->stack, sizeof(Value) * c->stackIndex);
-        memoryFree(c->stack);
-        c->stack = newValues;
-        c->maxStackSize = newSize;
-    }
-    c->stack[c->stackIndex++] = v;
-    return false;
-}
-
-static bool iPopValue(Code* c, Value* v) {
-    if(c->stackIndex <= 0) {
-        SET_ERROR("Pop on empty stack");
-        return true;
-    }
-    *v = c->stack[--c->stackIndex];
-    return false;
-}
-
-static bool iAdd(Code* c) {
-    POP_VALUE(a);
-    POP_VALUE(b);
-    return iPushValue(c, INT_VALUE(a.intValue + b.intValue));
-}
-
-static bool iPushConstantString(Code* c) {
-    return iPushValue(c, CSTRING_VALUE(codeReadConstantString(c)));
-}
-
-static bool iPushInt(Code* c) {
-    i64 i = 0;
-    if(codeReadI64(c, &i)) {
-        SET_ERROR("PushInt without value");
-        return true;
-    }
-    return iPushValue(c, INT_VALUE(i));
-}
-
-static bool iPrint(Code* c) {
-    POP_VALUE(a);
-    switch(a.type) {
-        case VT_INT64: printf("%ld", a.intValue); break;
-        case VT_CONSTANT_STRING: printf("%s", a.constantStringValue); break;
-    }
-    return false;
-}
-
-static bool iPrintNewline() {
-    putchar('\n');
-    return false;
-}
-
-static bool iJumpIf(Code* c) {
-    POP_VALUE(a);
-    size_t jumpPos = 0;
-    if(codeReadSize(c, &jumpPos)) {
-        SET_ERROR("JumpIf without position");
-        return true;
-    }
-    if(a.intValue == 0) {
-        codeSetPosition(c, jumpPos);
-    }
-    return false;
-}
-
-static bool iReadVariable(Code* c) {
-    i64 address = 0;
-    if(codeReadI64(c, &address)) {
-        SET_ERROR("ReadVariable without address");
-        return true;
-    } else if((size_t)address >= c->stackIndex) {
-        SET_ERROR("ReadVariable with invalid address");
-        return true;
-    }
-    return iPushValue(c, c->stack[address]);
-}
-
-static bool iSetVariable(Code* c) {
-    i64 address = 0;
-    if(codeReadI64(c, &address)) {
-        SET_ERROR("SetVariable without address");
-        return true;
-    } else if((size_t)address >= c->stackIndex) {
-        SET_ERROR("SetVariable with invalid address");
-        return true;
-    }
-    POP_VALUE(a);
-    c->stack[address] = a;
-    return false;
-}
-
-static bool iPushStackVariables(Code* c) {
-    i64 amount = 0;
-    if(codeReadI64(c, &amount)) {
-        SET_ERROR("PushStackVariables without amount");
-        return true;
-    }
-    Value v = INT_VALUE(0);
-    while(amount > 0) {
-        if(iPushValue(c, v)) {
-            return true;
-        }
-        amount--;
-    }
-    return false;
-}
-
-static bool execute(Code* c, Instruction command) {
-    switch(command) {
-        case ADD: return iAdd(c);
-        case PUSH_CONSTANT_STRING: return iPushConstantString(c);
-        case PUSH_INT64: return iPushInt(c);
-        case PRINT: return iPrint(c);
-        case PRINT_NEWLINE: return iPrintNewline();
-        case JUMP_ON_0: return iJumpIf(c);
-        case READ_VARIABLE: return iReadVariable(c);
-        case SET_VARIABLE: return iSetVariable(c);
-        case PUSH_STACK_VARIABLES: return iPushStackVariables(c);
-        case STOP: return true;
-    }
-    return false;
-}
-
-void codeRun(Code* c) {
-    while(!execute(c, codeReadInstruction(c))) {}
-}
-
-bool codeHasRunError(const Code* code) {
-    return hasError(&code->error);
-}
-
-const char* codeGetRunError(const Code* code) {
-    return code->error.text;
-}

+ 52 - 32
src/Compiler.c

@@ -31,12 +31,38 @@ typedef struct {
         c->line __VA_OPT__(, ) __VA_ARGS__);                       \
     longjmp(c->jump, 1)
 
-#define CODE(command)                                \
-    do {                                             \
-        if(command) {                                \
-            THROW_ERROR("Too less memory for code"); \
-        }                                            \
-    } while(false)
+[[noreturn]] static void throwOutOfCodeMemory(Context* c) {
+    THROW_ERROR("Too less memory for code");
+}
+
+static void codePushInstruction(Context* c, Instruction i) {
+    if(bufferWriteU8(&c->code->code, i)) {
+        throwOutOfCodeMemory(c);
+    }
+}
+
+static void codePushI32(Context* c, i32 i) {
+    if(bufferWriteI32(&c->code->code, i)) {
+        throwOutOfCodeMemory(c);
+    }
+}
+
+static void codePushConstantString(Context* c, const char* s) {
+    if(bufferWriteString(&c->code->code, s)) {
+        throwOutOfCodeMemory(c);
+    }
+}
+
+static size_t codeGetWritePosition(const Context* c) {
+    return c->code->code.writeIndex;
+}
+
+static void codeRewriteI32(Context* c, size_t pos, i32 i) {
+    size_t oldPos = codeGetWritePosition(c);
+    c->code->code.writeIndex = pos;
+    codePushI32(c, i);
+    c->code->code.writeIndex = oldPos;
+}
 
 static i32 addVariable(Context* c, const char* name) {
     for(Variable* v = c->variables; v != nullptr; v = v->next) {
@@ -89,15 +115,15 @@ static Token consumeNewline(Context* c) {
 static void compileConstant(Context* c) {
     Token token = tokenizerNext(c->tokenizer);
     if(token.type == STRING) {
-        CODE(codePushInstruction(c->code, PUSH_CONSTANT_STRING));
-        CODE(codePushConstantString(c->code, token.stringValue));
-    } else if(token.type == INT64) {
-        CODE(codePushInstruction(c->code, PUSH_INT64));
-        CODE(codePushI64(c->code, token.intValue));
+        codePushInstruction(c, PUSH_CONSTANT_STRING);
+        codePushConstantString(c, token.stringValue);
+    } else if(token.type == INT32) {
+        codePushInstruction(c, PUSH_INT64);
+        codePushI32(c, token.intValue);
     } else if(token.type == LITERAL) {
         i32 index = addVariable(c, token.stringValue);
-        CODE(codePushInstruction(c->code, READ_VARIABLE));
-        CODE(codePushI64(c->code, index));
+        codePushInstruction(c, READ_VARIABLE);
+        codePushI32(c, index);
     } else {
         unexpectedToken(c, token);
     }
@@ -108,7 +134,7 @@ static void compileAdd(Context* c) {
     while(tokenizerPeek(c->tokenizer).type == PLUS) {
         tokenizerNext(c->tokenizer);
         compileConstant(c);
-        CODE(codePushInstruction(c->code, ADD));
+        codePushInstruction(c, ADD);
     }
 }
 
@@ -120,9 +146,9 @@ static void compileLine(Context* c, Token token);
 
 static void compileIf(Context* c) {
     compileExpression(c);
-    CODE(codePushInstruction(c->code, JUMP_ON_0));
-    size_t posIndex = codeGetWritePosition(c->code);
-    CODE(codePushSize(c->code, 0));
+    codePushInstruction(c, JUMP_ON_0);
+    size_t posIndex = codeGetWritePosition(c);
+    codePushI32(c, 0);
     consumeToken(c, THEN);
     consumeNewline(c);
     while(true) {
@@ -133,18 +159,15 @@ static void compileIf(Context* c) {
     }
     consumeToken(c, ENDIF);
     consumeNewline(c);
-    size_t endIndex = codeGetWritePosition(c->code);
-    codeSetWritePosition(c->code, posIndex);
-    CODE(codePushSize(c->code, endIndex));
-    codeSetWritePosition(c->code, endIndex);
+    codeRewriteI32(c, posIndex, (i32)codeGetWritePosition(c));
 }
 
 static void compileSetVariable(Context* c, const char* name) {
     consumeToken(c, EQUAL);
     compileExpression(c);
     i32 index = addVariable(c, name);
-    CODE(codePushInstruction(c->code, SET_VARIABLE));
-    CODE(codePushI64(c->code, index));
+    codePushInstruction(c, SET_VARIABLE);
+    codePushI32(c, index);
 }
 
 static void compileLine(Context* c, Token token) {
@@ -161,10 +184,10 @@ static void compileLine(Context* c, Token token) {
     if(strcmp(s, "print") == 0) {
         while(tokenizerPeek(c->tokenizer).type != NEWLINE) {
             compileExpression(c);
-            CODE(codePushInstruction(c->code, PRINT));
+            codePushInstruction(c, PRINT);
         }
         consumeNewline(c);
-        CODE(codePushInstruction(c->code, PRINT_NEWLINE));
+        codePushInstruction(c, PRINT_NEWLINE);
     } else if(tokenizerPeek(c->tokenizer).type == EQUAL) {
         compileSetVariable(c, s);
     } else {
@@ -174,9 +197,9 @@ static void compileLine(Context* c, Token token) {
 
 static void parseTokens(Context* c) {
     codeReset(c->code);
-    CODE(codePushInstruction(c->code, PUSH_STACK_VARIABLES));
-    size_t stackVarsLoc = codeGetWritePosition(c->code);
-    CODE(codePushI64(c->code, 0));
+    codePushInstruction(c, PUSH_STACK_VARIABLES);
+    size_t stackVarsLoc = codeGetWritePosition(c);
+    codePushI32(c, 0);
 
     while(true) {
         Token token = tokenizerNext(c->tokenizer);
@@ -186,12 +209,9 @@ static void parseTokens(Context* c) {
         compileLine(c, token);
     }
 
-    size_t oldPos = codeGetWritePosition(c->code);
-    codeSetWritePosition(c->code, stackVarsLoc);
     if(c->variables != nullptr) {
-        CODE(codePushI64(c->code, c->variables->index + 1));
+        codeRewriteI32(c, stackVarsLoc, c->variables->index + 1);
     }
-    codeSetWritePosition(c->code, oldPos);
 }
 
 Error compileFile(Tokenizer* t, Code* code) {

+ 4 - 44
src/Main.c

@@ -13,28 +13,10 @@ int main(int argCount, const char** args) {
 
     static char heap[1000];
     memoryInit(heap, sizeof(heap));
-    // memoryDump();
-
-    // char* c1 = memoryAllocate(16);
-    // char* c2 = memoryAllocate(22);
-    // char* c3 = memoryAllocate(32);
-    // if(c1 == nullptr || c2 == nullptr || c3 == nullptr) {
-    //     return 0;
-    // }
-    // memset(c1, 1, 16);
-    // memset(c2, 2, 22);
-    // memset(c3, 3, 32);
-    // memoryDump();
-    // memoryFree(c2);
-    // memoryDump();
-    // memoryFree(c3);
-    // memoryDump();
-    // memoryFree(c1);
-    // memoryDump();
+    memoryDump();
 
     Tokenizer t;
     Error e = tokenizerInit(&t, args[1]);
-    memoryDump();
     if(hasError(&e)) {
         puts(e.text);
         tokenizerDestroy(&t);
@@ -48,36 +30,14 @@ int main(int argCount, const char** args) {
     if(hasError(&e)) {
         puts(e.text);
     } else {
+        memoryDump();
         codeRun(&code);
         if(codeHasRunError(&code)) {
             puts(codeGetRunError(&code));
         }
+        memoryDump();
     }
-
     codeDestroy(&code);
-
-    // while(true) {
-    //     Token token = tokenizerNext(&t);
-    //     if(tokenizerHasError(&t)) {
-    //         puts(tokenizerGetError(&t));
-    //         break;
-    //     }
-    //     char buffer[256];
-    //     tokenizerPrintToken(&token, buffer, sizeof(buffer));
-    //     puts(buffer);
-    //     if(token.type == END) {
-    //         break;
-    //     }
-    // }
-
-    //  char line[256];
-    //  while(true) {
-    //      fgets(line, sizeof(line), stdin);
-    //      if(strcmp(line, "quit\n") == 0) {
-    //          break;
-    //      }
-    //      puts(line);
-    //  }
-    //  puts("quit");
+    memoryDump();
     return 0;
 }

+ 6 - 2
src/Memory.c

@@ -2,6 +2,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 typedef struct {
     u32 units;
@@ -114,10 +115,13 @@ void memoryDump() {
     //     }
     // }
     // fputc('\n', stderr);
-    fputs("-------------------\n", stderr);
+    fputs("Free Slots ", stderr);
+    u32 sum = 0;
     Slot* current = memoryNextSlot(slots);
     while(current != nullptr) {
-        fprintf(stderr, "Free slot %u\n", current->units);
+        fprintf(stderr, "%u, ", current->units);
+        sum += current->units;
         current = memoryNextSlot(current);
     }
+    fprintf(stderr, " = %u\n", sum);
 }

+ 11 - 11
src/Tokenizer.c

@@ -2,8 +2,8 @@
 
 #include <assert.h>
 #include <errno.h>
+#include <limits.h>
 #include <setjmp.h>
-#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -106,11 +106,11 @@ static const char* tokenizerAddNumber(TState* t, const char* s) {
     char* end = nullptr;
     errno = 0;
     i64 i = strtoll(number, &end, 10);
-    if(errno != 0 || *end != '\0') {
+    if(errno != 0 || *end != '\0' || i > INT32_MAX || i < INT32_MIN) {
         tInvalidNumber(t);
     }
-    tAddToken(t, INT64);
-    if(bufferWriteI64(&t->tokenizer->buffer, i)) {
+    tAddToken(t, INT32);
+    if(bufferWriteI32(&t->tokenizer->buffer, (i32)i)) {
         tTooMuchTokens(t);
     }
     return s;
@@ -187,9 +187,9 @@ static const char* tReadString(Tokenizer* t) {
     return c;
 }
 
-static i64 tReadInt64(Tokenizer* t) {
-    i64 i = 0;
-    if(bufferReadI64(&t->buffer, &i)) {
+static i32 tReadInt32(Tokenizer* t) {
+    i32 i = 0;
+    if(bufferReadI32(&t->buffer, &i)) {
         assert(false);
     }
     return i;
@@ -203,16 +203,16 @@ Token tokenizerNext(Tokenizer* t) {
     switch(token.type) {
         case STRING:
         case LITERAL: token.stringValue = tReadString(t); break;
-        case INT64: token.intValue = tReadInt64(t); break;
+        case INT32: token.intValue = tReadInt32(t); break;
         default: break;
     }
     return token;
 }
 
 Token tokenizerPeek(Tokenizer* t) {
-    size_t index = bufferGetReadIndex(&t->buffer);
+    size_t index = t->buffer.readIndex;
     Token token = tokenizerNext(t);
-    bufferSetReadIndex(&t->buffer, index);
+    t->buffer.readIndex = index;
     return token;
 }
 
@@ -247,7 +247,7 @@ void tokenizerDestroy(Tokenizer* t) {
 void tokenizerPrintToken(const Token* token, char* buffer, size_t n) {
     switch(token->type) {
         TOKEN_PRINTER(LITERAL, "Literal(%s)", token->stringValue);
-        TOKEN_PRINTER(INT64, "Int64(%ld)", token->intValue);
+        TOKEN_PRINTER(INT32, "Int32(%d)", token->intValue);
         TOKEN_PRINTER(STRING, "String(%s)", token->stringValue);
         TOKEN_PRINTER(PLUS, "Plus");
         TOKEN_PRINTER(EQUAL, "Equal");

+ 2 - 2
src/Tokenizer.h

@@ -6,7 +6,7 @@
 
 typedef enum : u8 {
     LITERAL,
-    INT64,
+    INT32,
     STRING,
     PLUS,
     EQUAL,
@@ -22,7 +22,7 @@ typedef struct {
 
     union {
         const char* stringValue;
-        i64 intValue;
+        i32 intValue;
     };
 } Token;