Skip to content

Commit

Permalink
Reworked Toy_String as a union, enabled -Wpedantic
Browse files Browse the repository at this point in the history
Toy now fits into the C spec.

Fixed #158

Addendum: MacOS test caught an error:

error: a function declaration without a prototype is deprecated in all versions of C

That took 3 attempts to fix correctly.

Addendum: 'No new line at the end of file' are you shitting me?
  • Loading branch information
Ratstail91 committed Dec 15, 2024
1 parent 93fce94 commit a28053d
Show file tree
Hide file tree
Showing 41 changed files with 327 additions and 314 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ typedef struct Toy_Thing {
int member;
} Toy_Thing;
TOY_API void Toy_useThing();
TOY_API void Toy_useThing(void);
#define TOY_USE_OTHER_THING() Toy_private_useOtherThing()
TOY_API void Toy_private_useOtherThing();
TOY_API void Toy_private_useOtherThing(void);
```

## Data Type Sizes
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#compiler settings reference
#CC=gcc
#CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2
#CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpedantic -Wformat=2
#LIBS+=-lm
#LDFLAGS+=

Expand Down
4 changes: 2 additions & 2 deletions repl/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,14 @@ static void debugScopePrint(Toy_Scope* scope, int depth) {

printf("Scope %d Dump\n-------------------------\ntype\tname\tvalue\n", depth);
for (unsigned int i = 0; i < scope->table->capacity; i++) {
if ( (TOY_VALUE_IS_STRING(scope->table->data[i].key) && TOY_VALUE_AS_STRING(scope->table->data[i].key)->type == TOY_STRING_NAME) != true) {
if ( (TOY_VALUE_IS_STRING(scope->table->data[i].key) && TOY_VALUE_AS_STRING(scope->table->data[i].key)->info.type == TOY_STRING_NAME) != true) {
continue;
}

Toy_Value k = scope->table->data[i].key;
Toy_Value v = scope->table->data[i].value;

printf("%s\t%s\t", Toy_private_getValueTypeAsCString(v.type), TOY_VALUE_AS_STRING(k)->as.name.data);
printf("%s\t%s\t", Toy_private_getValueTypeAsCString(v.type), TOY_VALUE_AS_STRING(k)->name.data);

//print value
Toy_String* string = Toy_stringifyValue(&stringBucket, Toy_unwrapValue(v));
Expand Down
2 changes: 1 addition & 1 deletion repl/makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#compiler settings
CC=gcc
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpedantic -Wformat=2
LIBS+=-lm -lToy
LDFLAGS+=-Wl,-rpath,'$$ORIGIN'

Expand Down
2 changes: 1 addition & 1 deletion source/makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#compiler settings
CC=gcc
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpedantic -Wformat=2
LIBS+=-lm
LDFLAGS+=

Expand Down
2 changes: 1 addition & 1 deletion source/toy_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
//defined separately, as compilation can take several seconds, invalidating the comparisons of the given macros
static const char* build = __DATE__ " " __TIME__ ", incomplete Toy v2.x";

const char* Toy_private_version_build() {
const char* Toy_private_version_build(void) {
return build;
}
2 changes: 1 addition & 1 deletion source/toy_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@

//defined as a function, for technical reasons
#define TOY_VERSION_BUILD Toy_private_version_build()
TOY_API const char* Toy_private_version_build();
TOY_API const char* Toy_private_version_build(void);

6 changes: 3 additions & 3 deletions source/toy_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ void Toy_setAssertFailureCallback(Toy_callbackType cb) {
assertCallback = cb;
}

void Toy_resetPrintCallback() {
void Toy_resetPrintCallback(void) {
printCallback = outDefault;
}

void Toy_resetErrorCallback() {
void Toy_resetErrorCallback(void) {
errorCallback = errDefault;
}

void Toy_resetAssertFailureCallback() {
void Toy_resetAssertFailureCallback(void) {
assertCallback = assertDefault;
}
6 changes: 3 additions & 3 deletions source/toy_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TOY_API void Toy_setPrintCallback(Toy_callbackType cb);
TOY_API void Toy_setErrorCallback(Toy_callbackType cb);
TOY_API void Toy_setAssertFailureCallback(Toy_callbackType cb);

TOY_API void Toy_resetPrintCallback();
TOY_API void Toy_resetErrorCallback();
TOY_API void Toy_resetAssertFailureCallback();
TOY_API void Toy_resetPrintCallback(void);
TOY_API void Toy_resetErrorCallback(void);
TOY_API void Toy_resetAssertFailureCallback(void);

28 changes: 14 additions & 14 deletions source/toy_routine.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static void emitToJumpTable(Toy_Routine** rt, unsigned int startAddr) {

static unsigned int emitString(Toy_Routine** rt, Toy_String* str) {
//4-byte alignment
unsigned int length = str->length + 1;
unsigned int length = str->info.length + 1;
if (length % 4 != 0) {
length += 4 - (length % 4); //ceil
}
Expand All @@ -82,16 +82,16 @@ static unsigned int emitString(Toy_Routine** rt, Toy_String* str) {
//move the string into the data section
expand((&((*rt)->data)), &((*rt)->dataCapacity), &((*rt)->dataCount), length);

if (str->type == TOY_STRING_NODE) {
if (str->info.type == TOY_STRING_NODE) {
char* buffer = Toy_getStringRawBuffer(str);
memcpy((*rt)->data + (*rt)->dataCount, buffer, str->length + 1);
memcpy((*rt)->data + (*rt)->dataCount, buffer, str->info.length + 1);
free(buffer);
}
else if (str->type == TOY_STRING_LEAF) {
memcpy((*rt)->data + (*rt)->dataCount, str->as.leaf.data, str->length + 1);
else if (str->info.type == TOY_STRING_LEAF) {
memcpy((*rt)->data + (*rt)->dataCount, str->leaf.data, str->info.length + 1);
}
else if (str->type == TOY_STRING_NAME) {
memcpy((*rt)->data + (*rt)->dataCount, str->as.name.data, str->length + 1);
else if (str->info.type == TOY_STRING_NAME) {
memcpy((*rt)->data + (*rt)->dataCount, str->name.data, str->info.length + 1);
}

(*rt)->dataCount += length;
Expand Down Expand Up @@ -443,9 +443,9 @@ static unsigned int writeInstructionVarDeclare(Toy_Routine** rt, Toy_AstVarDecla

//delcare with the given name string
EMIT_BYTE(rt, code, TOY_OPCODE_DECLARE);
EMIT_BYTE(rt, code, Toy_getNameStringType(ast.name));
EMIT_BYTE(rt, code, ast.name->length); //quick optimisation to skip a 'strlen()' call
EMIT_BYTE(rt, code, Toy_getNameStringConstant(ast.name) ? 1 : 0); //check for constness
EMIT_BYTE(rt, code, Toy_getNameStringVarType(ast.name));
EMIT_BYTE(rt, code, ast.name->info.length); //quick optimisation to skip a 'strlen()' call
EMIT_BYTE(rt, code, Toy_getNameStringVarConstant(ast.name) ? 1 : 0); //check for constness

emitString(rt, ast.name);

Expand All @@ -472,15 +472,15 @@ static unsigned int writeInstructionAssign(Toy_Routine** rt, Toy_AstVarAssign as
}

//target is a name string
if (ast.target->type == TOY_AST_VALUE && TOY_VALUE_IS_STRING(ast.target->value.value) && TOY_VALUE_AS_STRING(ast.target->value.value)->type == TOY_STRING_NAME) {
if (ast.target->type == TOY_AST_VALUE && TOY_VALUE_IS_STRING(ast.target->value.value) && TOY_VALUE_AS_STRING(ast.target->value.value)->info.type == TOY_STRING_NAME) {
//name string
Toy_String* target = TOY_VALUE_AS_STRING(ast.target->value.value);

//emit the name string
EMIT_BYTE(rt, code, TOY_OPCODE_READ);
EMIT_BYTE(rt, code, TOY_VALUE_STRING);
EMIT_BYTE(rt, code, TOY_STRING_NAME);
EMIT_BYTE(rt, code, target->length); //store the length (max 255)
EMIT_BYTE(rt, code, target->info.length); //store the length (max 255)

emitString(rt, target);
}
Expand Down Expand Up @@ -590,7 +590,7 @@ static unsigned int writeInstructionAssign(Toy_Routine** rt, Toy_AstVarAssign as
}

static unsigned int writeInstructionAccess(Toy_Routine** rt, Toy_AstVarAccess ast) {
if (!(ast.child->type == TOY_AST_VALUE && TOY_VALUE_IS_STRING(ast.child->value.value) && TOY_VALUE_AS_STRING(ast.child->value.value)->type == TOY_STRING_NAME)) {
if (!(ast.child->type == TOY_AST_VALUE && TOY_VALUE_IS_STRING(ast.child->value.value) && TOY_VALUE_AS_STRING(ast.child->value.value)->info.type == TOY_STRING_NAME)) {
fprintf(stderr, TOY_CC_ERROR "COMPILER ERROR: Found a non-name-string in a value node when trying to write access\n" TOY_CC_RESET);
exit(-1);
}
Expand All @@ -601,7 +601,7 @@ static unsigned int writeInstructionAccess(Toy_Routine** rt, Toy_AstVarAccess as
EMIT_BYTE(rt, code, TOY_OPCODE_READ);
EMIT_BYTE(rt, code, TOY_VALUE_STRING);
EMIT_BYTE(rt, code, TOY_STRING_NAME);
EMIT_BYTE(rt, code, name->length); //store the length (max 255)
EMIT_BYTE(rt, code, name->info.length); //store the length (max 255)

emitString(rt, name);

Expand Down
44 changes: 22 additions & 22 deletions source/toy_scope.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,33 +92,33 @@ Toy_Scope* Toy_deepCopyScope(Toy_Bucket** bucketHandle, Toy_Scope* scope) {
}

void Toy_declareScope(Toy_Scope* scope, Toy_String* key, Toy_Value value) {
if (key->type != TOY_STRING_NAME) {
if (key->info.type != TOY_STRING_NAME) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Toy_Scope only allows name strings as keys\n" TOY_CC_RESET);
exit(-1);
}

Toy_TableEntry* entryPtr = lookupScope(scope, key, Toy_hashString(key), false);

if (entryPtr != NULL) {
char buffer[key->length + 256];
sprintf(buffer, "Can't redefine a variable: %s", key->as.name.data);
char buffer[key->info.length + 256];
sprintf(buffer, "Can't redefine a variable: %s", key->name.data);
Toy_error(buffer);
return;
}

//type check
Toy_ValueType kt = Toy_getNameStringType(key);
Toy_ValueType kt = Toy_getNameStringVarType(key);
if (kt != TOY_VALUE_ANY && value.type != TOY_VALUE_NULL && kt != value.type && value.type != TOY_VALUE_REFERENCE) {
char buffer[key->length + 256];
sprintf(buffer, "Incorrect value type in declaration of '%s' (expected %s, got %s)", key->as.name.data, Toy_private_getValueTypeAsCString(kt), Toy_private_getValueTypeAsCString(value.type));
char buffer[key->info.length + 256];
sprintf(buffer, "Incorrect value type in declaration of '%s' (expected %s, got %s)", key->name.data, Toy_private_getValueTypeAsCString(kt), Toy_private_getValueTypeAsCString(value.type));
Toy_error(buffer);
return;
}

//constness check
if (Toy_getNameStringConstant(key) && value.type == TOY_VALUE_NULL) {
char buffer[key->length + 256];
sprintf(buffer, "Can't declare %s as const with value 'null'", key->as.name.data);
if (Toy_getNameStringVarConstant(key) && value.type == TOY_VALUE_NULL) {
char buffer[key->info.length + 256];
sprintf(buffer, "Can't declare %s as const with value 'null'", key->name.data);
Toy_error(buffer);
return;
}
Expand All @@ -129,33 +129,33 @@ void Toy_declareScope(Toy_Scope* scope, Toy_String* key, Toy_Value value) {

//TODO: check for clearign old values
void Toy_assignScope(Toy_Scope* scope, Toy_String* key, Toy_Value value) {
if (key->type != TOY_STRING_NAME) {
if (key->info.type != TOY_STRING_NAME) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Toy_Scope only allows name strings as keys\n" TOY_CC_RESET);
exit(-1);
}

Toy_TableEntry* entryPtr = lookupScope(scope, key, Toy_hashString(key), true);

if (entryPtr == NULL) {
char buffer[key->length + 256];
sprintf(buffer, "Undefined variable: %s\n", key->as.name.data);
char buffer[key->info.length + 256];
sprintf(buffer, "Undefined variable: %s\n", key->name.data);
Toy_error(buffer);
return;
}

//type check
Toy_ValueType kt = Toy_getNameStringType( TOY_VALUE_AS_STRING(entryPtr->key) );
Toy_ValueType kt = Toy_getNameStringVarType( TOY_VALUE_AS_STRING(entryPtr->key) );
if (kt != TOY_VALUE_ANY && value.type != TOY_VALUE_NULL && kt != value.type && value.type != TOY_VALUE_REFERENCE) {
char buffer[key->length + 256];
sprintf(buffer, "Incorrect value type in assignment of '%s' (expected %s, got %s)", key->as.name.data, Toy_private_getValueTypeAsCString(kt), Toy_private_getValueTypeAsCString(value.type));
char buffer[key->info.length + 256];
sprintf(buffer, "Incorrect value type in assignment of '%s' (expected %s, got %s)", key->name.data, Toy_private_getValueTypeAsCString(kt), Toy_private_getValueTypeAsCString(value.type));
Toy_error(buffer);
return;
}

//constness check
if (Toy_getNameStringConstant( TOY_VALUE_AS_STRING(entryPtr->key) )) {
char buffer[key->length + 256];
sprintf(buffer, "Can't assign to const %s", key->as.name.data);
if (Toy_getNameStringVarConstant( TOY_VALUE_AS_STRING(entryPtr->key) )) {
char buffer[key->info.length + 256];
sprintf(buffer, "Can't assign to const %s", key->name.data);
Toy_error(buffer);
return;
}
Expand All @@ -164,16 +164,16 @@ void Toy_assignScope(Toy_Scope* scope, Toy_String* key, Toy_Value value) {
}

Toy_Value* Toy_accessScopeAsPointer(Toy_Scope* scope, Toy_String* key) {
if (key->type != TOY_STRING_NAME) {
if (key->info.type != TOY_STRING_NAME) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Toy_Scope only allows name strings as keys\n" TOY_CC_RESET);
exit(-1);
}

Toy_TableEntry* entryPtr = lookupScope(scope, key, Toy_hashString(key), true);

if (entryPtr == NULL) {
char buffer[key->length + 256];
sprintf(buffer, "Undefined variable: %s\n", key->as.name.data);
char buffer[key->info.length + 256];
sprintf(buffer, "Undefined variable: %s\n", key->name.data);
Toy_error(buffer);
NULL;
}
Expand All @@ -182,7 +182,7 @@ Toy_Value* Toy_accessScopeAsPointer(Toy_Scope* scope, Toy_String* key) {
}

bool Toy_isDeclaredScope(Toy_Scope* scope, Toy_String* key) {
if (key->type != TOY_STRING_NAME) {
if (key->info.type != TOY_STRING_NAME) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Toy_Scope only allows name strings as keys\n" TOY_CC_RESET);
exit(-1);
}
Expand Down
2 changes: 1 addition & 1 deletion source/toy_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stdio.h>
#include <stdlib.h>

Toy_Stack* Toy_allocateStack() {
Toy_Stack* Toy_allocateStack(void) {
Toy_Stack* stack = malloc(TOY_STACK_INITIAL_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack));

if (stack == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion source/toy_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ typedef struct Toy_Stack { //32 | 64 BITNESS
Toy_Value data[]; //- | -
} Toy_Stack; //8 | 8

TOY_API Toy_Stack* Toy_allocateStack();
TOY_API Toy_Stack* Toy_allocateStack(void);
TOY_API void Toy_freeStack(Toy_Stack* stack);

TOY_API void Toy_pushStack(Toy_Stack** stackHandle, Toy_Value value);
Expand Down
Loading

0 comments on commit a28053d

Please sign in to comment.