Tweaked string quotes

This commit is contained in:
2022-08-11 13:45:13 +01:00
parent 32ac46d9a0
commit 5c93bf663a
4 changed files with 21 additions and 10 deletions

View File

@@ -14,14 +14,14 @@ void initCompiler(Compiler* compiler) {
compiler->capacity = 0; compiler->capacity = 0;
compiler->count = 0; compiler->count = 0;
//default atomic literals //default atomic literals (commented out, because not needed atm - might need them later)
Literal n = TO_NULL_LITERAL; // Literal n = TO_NULL_LITERAL;
Literal t = TO_BOOLEAN_LITERAL(true); // Literal t = TO_BOOLEAN_LITERAL(true);
Literal f = TO_BOOLEAN_LITERAL(false); // Literal f = TO_BOOLEAN_LITERAL(false);
pushLiteralArray(&compiler->literalCache, n); // pushLiteralArray(&compiler->literalCache, n);
pushLiteralArray(&compiler->literalCache, t); // pushLiteralArray(&compiler->literalCache, t);
pushLiteralArray(&compiler->literalCache, f); // pushLiteralArray(&compiler->literalCache, f);
} }
//separated out, so it can be recursive //separated out, so it can be recursive

View File

@@ -17,6 +17,9 @@ static char* globalPrintBuffer = NULL;
static size_t globalPrintCapacity = 0; static size_t globalPrintCapacity = 0;
static size_t globalPrintCount = 0; static size_t globalPrintCount = 0;
//BUGFIX: string quotes shouldn't show when just printing strings, but should show when printing them as members of something else
static char quotes = 0; //set to 0 to not show string quotes
static void printToBuffer(const char* str) { static void printToBuffer(const char* str) {
while (strlen(str) + globalPrintCount > globalPrintCapacity) { while (strlen(str) + globalPrintCount > globalPrintCapacity) {
int oldCapacity = globalPrintCapacity; int oldCapacity = globalPrintCapacity;
@@ -60,7 +63,12 @@ void printLiteralCustom(Literal literal, void (printFn)(const char*)) {
case LITERAL_STRING: { case LITERAL_STRING: {
char buffer[4096]; char buffer[4096];
snprintf(buffer, 4096, "\"%.*s\"", STRLEN(literal), AS_STRING(literal)); if (!quotes) {
snprintf(buffer, 4096, "%.*s", STRLEN(literal), AS_STRING(literal));
}
else {
snprintf(buffer, 4096, "%c%.*s%c", quotes, STRLEN(literal), AS_STRING(literal), quotes);
}
printFn(buffer); printFn(buffer);
} }
break; break;
@@ -79,6 +87,7 @@ void printLiteralCustom(Literal literal, void (printFn)(const char*)) {
//print the contents to the global buffer //print the contents to the global buffer
printToBuffer("["); printToBuffer("[");
for (int i = 0; i < ptr->count; i++) { for (int i = 0; i < ptr->count; i++) {
quotes = '"';
printLiteralCustom(ptr->literals[i], printToBuffer); printLiteralCustom(ptr->literals[i], printToBuffer);
if (i + 1 < ptr->count) { if (i + 1 < ptr->count) {
@@ -99,6 +108,7 @@ void printLiteralCustom(Literal literal, void (printFn)(const char*)) {
//finally, output and cleanup //finally, output and cleanup
printFn(printBuffer); printFn(printBuffer);
FREE_ARRAY(char, printBuffer, printCapacity); FREE_ARRAY(char, printBuffer, printCapacity);
quotes = 0;
} }
break; break;
@@ -125,8 +135,10 @@ void printLiteralCustom(Literal literal, void (printFn)(const char*)) {
printToBuffer(","); printToBuffer(",");
} }
quotes = '"';
printLiteralCustom(ptr->entries[i].key, printToBuffer); printLiteralCustom(ptr->entries[i].key, printToBuffer);
printToBuffer(":"); printToBuffer(":");
quotes = '"';
printLiteralCustom(ptr->entries[i].value, printToBuffer); printLiteralCustom(ptr->entries[i].value, printToBuffer);
} }
@@ -149,6 +161,7 @@ void printLiteralCustom(Literal literal, void (printFn)(const char*)) {
//finally, output and cleanup //finally, output and cleanup
printFn(printBuffer); printFn(printBuffer);
FREE_ARRAY(char, printBuffer, printCapacity); FREE_ARRAY(char, printBuffer, printCapacity);
quotes = 0;
} }
break; break;

View File

@@ -1,6 +1,5 @@
#pragma once #pragma once
#include "common.h"
#include "literal.h" #include "literal.h"
//TODO: benchmark this //TODO: benchmark this

View File

@@ -1,6 +1,5 @@
#pragma once #pragma once
#include "common.h"
#include "literal_dictionary.h" #include "literal_dictionary.h"
typedef struct Scope { typedef struct Scope {