ソースを参照

Shrink string tokens

Kajetan Johannes Hammerle 2 週間 前
コミット
66825fbd3e
4 ファイル変更20 行追加10 行削除
  1. 9 3
      src/Buffer.c
  2. 3 1
      src/Constants.h
  3. 1 1
      src/Main.c
  4. 7 5
      src/Tokenizer.c

+ 9 - 3
src/Buffer.c

@@ -1,7 +1,9 @@
 #include "Buffer.h"
 
+#include <assert.h>
 #include <string.h>
 
+#include "Constants.h"
 #include "Memory.h"
 
 void bufferInit(Buffer* b) {
@@ -66,12 +68,16 @@ BUFFER_READ_WRITE_IMPL(U64, u64)
 
 bool bufferWriteString(Buffer* b, const char* s) {
     size_t l = strlen(s) + 1;
-    return bufferWrite(b, &l, sizeof(l)) || bufferWrite(b, s, l);
+    assert(l <= MAX_STRING_LENGTH && l <= MAX_LITERAL_LENGTH);
+    static_assert(MAX_STRING_LENGTH <= 0xFF);
+    static_assert(MAX_LITERAL_LENGTH <= 0xFF);
+    u8 u = l & 0xFF;
+    return bufferWriteU8(b, u) || bufferWrite(b, s, u);
 }
 
 const char* bufferReadString(Buffer* b) {
-    size_t l = 0;
-    if(bufferRead(b, &l, sizeof(l)) || b->readIndex + l > b->writeIndex) {
+    u8 l = 0;
+    if(bufferReadU8(b, &l) || b->readIndex + l > b->writeIndex) {
         return nullptr;
     }
     const char* s = (char*)(b->data + b->readIndex);

+ 3 - 1
src/Constants.h

@@ -3,7 +3,9 @@
 
 #include <stddef.h>
 
-[[maybe_unused]] constexpr size_t MAX_CODE = 1024 * 1024 * 2;
 [[maybe_unused]] constexpr size_t MAX_VALUES = 1024;
+[[maybe_unused]] constexpr size_t MAX_LINE_LENGTH = 128;
+[[maybe_unused]] constexpr size_t MAX_LITERAL_LENGTH = 64;
+[[maybe_unused]] constexpr size_t MAX_STRING_LENGTH = 64;
 
 #endif

+ 1 - 1
src/Main.c

@@ -25,7 +25,7 @@ int main(int argCount, const char** args) {
         return 0;
     }
 
-    static char heap[2000];
+    static char heap[1000];
     memoryInit(heap, sizeof(heap));
     // memoryDump();
 

+ 7 - 5
src/Tokenizer.c

@@ -8,6 +8,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "Constants.h"
+
 typedef struct {
     int line;
     FILE* file;
@@ -58,7 +60,7 @@ static bool isTokenEnd(char c) {
 
 static const char* tokenizerAddLiteral(TState* t, const char* s) {
     size_t index = 0;
-    char buffer[256] = {};
+    char buffer[MAX_LITERAL_LENGTH] = {};
     buffer[index++] = *s;
     while(true) {
         char c = *(++s);
@@ -67,7 +69,7 @@ static const char* tokenizerAddLiteral(TState* t, const char* s) {
         } else if(!isAlphaNumeric(c)) {
             tInvalidToken(t, c);
         } else if(index >= sizeof(buffer) - 1) {
-            tTooMuchTokens(t);
+            THROW_ERROR("Too long literal");
         }
         buffer[index++] = c;
     }
@@ -117,7 +119,7 @@ static const char* tokenizerAddNumber(TState* t, const char* s) {
 static const char* tokenizerAddString(TState* t, const char* s) {
     tAddToken(t, STRING);
     size_t index = 0;
-    char buffer[256] = {};
+    char buffer[MAX_STRING_LENGTH] = {};
     while(true) {
         char c = *(++s);
         if(c == '\0') {
@@ -126,7 +128,7 @@ static const char* tokenizerAddString(TState* t, const char* s) {
             s++;
             break;
         } else if(index >= sizeof(buffer) - 1) {
-            tTooMuchTokens(t);
+            THROW_ERROR("Too long string");
         }
         buffer[index++] = c;
     }
@@ -164,7 +166,7 @@ static void tParseLineString(TState* t, const char* s) {
 static void tParseLines(TState* t) {
     while(true) {
         t->line++;
-        char line[256] = {};
+        char line[MAX_LINE_LENGTH] = {};
         if(fgets(line, sizeof(line), t->file) == nullptr) {
             return;
         }