Skip to content

Commit

Permalink
Added -Wpointer-arith to CFLAGS, read more
Browse files Browse the repository at this point in the history
I attempted to add '-Wpedantic' to CFLAGS, but it seems that my usage of
the variable length arrays within unions is causing an error that can't
be selectively disabled:

error: invalid use of structure with flexible array member [-Werror=pedantic]

This is the offending code: /source/toy_string.h#L9-L37

It seems that tagged unions, with VLAs within, is simply not allowed.
Unfortunately, my whole string system depends on it. I'll have to find some way
around it.

I've also updated the debugging output in repl/main.c.
  • Loading branch information
Ratstail91 committed Dec 12, 2024
1 parent cf9affe commit 7be63c8
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 149 deletions.
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 -Wformat=2
#CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2
#LIBS+=-lm
#LDFLAGS+=

Expand Down
133 changes: 31 additions & 102 deletions repl/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ unsigned char* readFile(char* path, int* size) {
}

//read the file
if (fread(buffer, sizeof(unsigned char), *size, file) < *size) {
if (fread(buffer, sizeof(unsigned char), *size, file) < (unsigned int)(*size)) {
fclose(file);
*size = -2; //singal a read error
return NULL;
Expand Down Expand Up @@ -130,10 +130,12 @@ static void assertFailureAndContinueCallback(const char* msg) {

static void noOpCallback(const char* msg) {
//NO-OP
(void)msg;
}

static void silentExitCallback(const char* msg) {
//NO-OP
(void)msg;
exit(-1);
}

Expand All @@ -151,6 +153,7 @@ typedef struct CmdLine {
} CmdLine;

void usageCmdLine(int argc, const char* argv[]) {
(void)argc;
printf("Usage: %s [ -h | -v | -f source.toy ]\n\n", argv[0]);
}

Expand All @@ -169,6 +172,8 @@ void helpCmdLine(int argc, const char* argv[]) {
}

void versionCmdLine(int argc, const char* argv[]) {
(void)argc;
(void)argv;
printf("The Toy Programming Language, Version %d.%d.%d %s\n\n", TOY_VERSION_MAJOR, TOY_VERSION_MINOR, TOY_VERSION_PATCH, TOY_VERSION_BUILD);

//copy/pasted from the license file - there's a way to include it directly, but it's too finnicky to bother
Expand Down Expand Up @@ -344,72 +349,37 @@ int repl(const char* filepath) {
static void debugStackPrint(Toy_Stack* stack) {
//DEBUG: if there's anything on the stack, print it
if (stack->count > 0) {
Toy_Bucket* stringBucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);

printf("Stack Dump\n-------------------------\ntype\tvalue\n");
for (int i = 0; i < stack->count; i++) {
Toy_Value v = ((Toy_Value*)(stack + 1))[i];
for (unsigned int i = 0; i < stack->count; i++) {
Toy_Value v = ((Toy_Value*)(stack + 1))[i]; //'stack + 1' is a naughty trick

//print type
printf("%s\t", Toy_private_getValueTypeAsCString(v.type));

v = Toy_unwrapValue(v);

switch(v.type) {
case TOY_VALUE_NULL:
printf("null");
break;

case TOY_VALUE_BOOLEAN:
printf("%s", TOY_VALUE_AS_BOOLEAN(v) ? "true" : "false");
break;

case TOY_VALUE_INTEGER:
printf("%d", TOY_VALUE_AS_INTEGER(v));
break;

case TOY_VALUE_FLOAT:
printf("%f", TOY_VALUE_AS_FLOAT(v));
break;

case TOY_VALUE_STRING: {
Toy_String* str = TOY_VALUE_AS_STRING(v);

//print based on type
if (str->type == TOY_STRING_NODE) {
char* buffer = Toy_getStringRawBuffer(str);
printf("%s", buffer);
free(buffer);
}
else if (str->type == TOY_STRING_LEAF) {
printf("%s", str->as.leaf.data);
}
else if (str->type == TOY_STRING_NAME) {
printf("%s", str->as.name.data);
}
break;
}

case TOY_VALUE_ARRAY:
case TOY_VALUE_TABLE:
case TOY_VALUE_FUNCTION:
case TOY_VALUE_OPAQUE:
case TOY_VALUE_TYPE:
case TOY_VALUE_ANY:
case TOY_VALUE_REFERENCE:
case TOY_VALUE_UNKNOWN:
printf("???");
break;
}
//print value
Toy_String* string = Toy_stringifyValue(&stringBucket, Toy_unwrapValue(v));
char* buffer = Toy_getStringRawBuffer(string);
printf("%s", buffer);
free(buffer);
Toy_freeString(string);

printf("\n");
}

Toy_freeBucket(&stringBucket);
}
}

static void debugScopePrint(Toy_Scope* scope, int depth) {
//DEBUG: if there's anything in the scope, print it
if (scope->table->count > 0) {
Toy_Bucket* stringBucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);

printf("Scope %d Dump\n-------------------------\ntype\tname\tvalue\n", depth);
for (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) == false) {
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) {
continue;
}

Expand All @@ -418,58 +388,17 @@ static void debugScopePrint(Toy_Scope* scope, int depth) {

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

k = Toy_unwrapValue(k);
v = Toy_unwrapValue(v);

switch(v.type) {
case TOY_VALUE_NULL:
printf("null");
break;

case TOY_VALUE_BOOLEAN:
printf("%s", TOY_VALUE_AS_BOOLEAN(v) ? "true" : "false");
break;

case TOY_VALUE_INTEGER:
printf("%d", TOY_VALUE_AS_INTEGER(v));
break;

case TOY_VALUE_FLOAT:
printf("%f", TOY_VALUE_AS_FLOAT(v));
break;

case TOY_VALUE_STRING: {
Toy_String* str = TOY_VALUE_AS_STRING(v);

//print based on type
if (str->type == TOY_STRING_NODE) {
char* buffer = Toy_getStringRawBuffer(str);
printf("%s", buffer);
free(buffer);
}
else if (str->type == TOY_STRING_LEAF) {
printf("%s", str->as.leaf.data);
}
else if (str->type == TOY_STRING_NAME) {
printf("%s\nWarning: The above value is a name string", str->as.name.data);
}
break;
}

case TOY_VALUE_ARRAY:
case TOY_VALUE_TABLE:
case TOY_VALUE_FUNCTION:
case TOY_VALUE_OPAQUE:
case TOY_VALUE_TYPE:
case TOY_VALUE_ANY:
case TOY_VALUE_REFERENCE:
case TOY_VALUE_UNKNOWN:
printf("???");
break;
}
//print value
Toy_String* string = Toy_stringifyValue(&stringBucket, Toy_unwrapValue(v));
char* buffer = Toy_getStringRawBuffer(string);
printf("%s", buffer);
free(buffer);
Toy_freeString(string);

printf("\n");
}

Toy_freeBucket(&stringBucket);
}

if (scope->next != NULL) {
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 -Wformat=2
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -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 -Wformat=2
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2
LIBS+=-lm
LDFLAGS+=

Expand Down
30 changes: 15 additions & 15 deletions source/toy_routine.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <string.h>

//utils
static void expand(void** handle, unsigned int* capacity, unsigned int* count, unsigned int amount) {
static void expand(unsigned char** handle, unsigned int* capacity, unsigned int* count, unsigned int amount) {
if ((*count) + amount > (*capacity)) {
while ((*count) + amount > (*capacity)) {
(*capacity) = (*capacity) < 8 ? 8 : (*capacity) * 2;
Expand All @@ -24,20 +24,20 @@ static void expand(void** handle, unsigned int* capacity, unsigned int* count, u
}
}

static void emitByte(void** handle, unsigned int* capacity, unsigned int* count, unsigned char byte) {
static void emitByte(unsigned char** handle, unsigned int* capacity, unsigned int* count, unsigned char byte) {
expand(handle, capacity, count, 1);
((unsigned char*)(*handle))[(*count)++] = byte;
}

static void emitInt(void** handle, unsigned int* capacity, unsigned int* count, unsigned int bytes) {
static void emitInt(unsigned char** handle, unsigned int* capacity, unsigned int* count, unsigned int bytes) {
char* ptr = (char*)&bytes;
emitByte(handle, capacity, count, *(ptr++));
emitByte(handle, capacity, count, *(ptr++));
emitByte(handle, capacity, count, *(ptr++));
emitByte(handle, capacity, count, *(ptr++));
}

static void emitFloat(void** handle, unsigned int* capacity, unsigned int* count, float bytes) {
static void emitFloat(unsigned char** handle, unsigned int* capacity, unsigned int* count, float bytes) {
char* ptr = (char*)&bytes;
emitByte(handle, capacity, count, *(ptr++));
emitByte(handle, capacity, count, *(ptr++));
Expand All @@ -47,19 +47,19 @@ static void emitFloat(void** handle, unsigned int* capacity, unsigned int* count

//write instructions based on the AST types
#define EMIT_BYTE(rt, part, byte) \
emitByte((void**)(&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), byte)
emitByte((&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), byte)
#define EMIT_INT(rt, part, bytes) \
emitInt((void**)(&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), bytes)
emitInt((&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), bytes)
#define EMIT_FLOAT(rt, part, bytes) \
emitFloat((void**)(&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), bytes)
emitFloat((&((*rt)->part)), &((*rt)->part##Capacity), &((*rt)->part##Count), bytes)

//skip bytes, but return the address
#define SKIP_BYTE(rt, part) (EMIT_BYTE(rt, part, 0), ((*rt)->part##Count - 1))
#define SKIP_INT(rt, part) (EMIT_INT(rt, part, 0), ((*rt)->part##Count - 4))

//overwrite a pre-existing position
#define OVERWRITE_INT(rt, part, addr, bytes) \
emitInt((void**)(&((*rt)->part)), &((*rt)->part##Capacity), &(addr), bytes);
emitInt((&((*rt)->part)), &((*rt)->part##Capacity), &(addr), bytes);

//simply get the address (always an integer)
#define CURRENT_ADDRESS(rt, part) ((*rt)->part##Count)
Expand All @@ -80,7 +80,7 @@ static unsigned int emitString(Toy_Routine** rt, Toy_String* str) {
unsigned int startAddr = (*rt)->dataCount;

//move the string into the data section
expand((void**)(&((*rt)->data)), &((*rt)->dataCapacity), &((*rt)->dataCount), length);
expand((&((*rt)->data)), &((*rt)->dataCapacity), &((*rt)->dataCount), length);

if (str->type == TOY_STRING_NODE) {
char* buffer = Toy_getStringRawBuffer(str);
Expand Down Expand Up @@ -750,7 +750,7 @@ static void* writeRoutine(Toy_Routine* rt, Toy_Ast* ast) {
}

//write the header and combine the parts
void* buffer = NULL;
unsigned char* buffer = NULL;
unsigned int capacity = 0, count = 0;
// int paramAddr = 0, subsAddr = 0;
int codeAddr = 0;
Expand All @@ -766,23 +766,23 @@ static void* writeRoutine(Toy_Routine* rt, Toy_Ast* ast) {
//generate blank spaces, cache their positions in the *Addr variables (for storing the start positions)
if (rt->paramCount > 0) {
// paramAddr = count;
emitInt((void**)&buffer, &capacity, &count, 0); //params
emitInt(&buffer, &capacity, &count, 0); //params
}
if (rt->codeCount > 0) {
codeAddr = count;
emitInt((void**)&buffer, &capacity, &count, 0); //code
emitInt(&buffer, &capacity, &count, 0); //code
}
if (rt->jumpsCount > 0) {
jumpsAddr = count;
emitInt((void**)&buffer, &capacity, &count, 0); //jumps
emitInt(&buffer, &capacity, &count, 0); //jumps
}
if (rt->dataCount > 0) {
dataAddr = count;
emitInt((void**)&buffer, &capacity, &count, 0); //data
emitInt(&buffer, &capacity, &count, 0); //data
}
if (rt->subsCount > 0) {
// subsAddr = count;
emitInt((void**)&buffer, &capacity, &count, 0); //subs
emitInt(&buffer, &capacity, &count, 0); //subs
}

//append various parts to the buffer
Expand Down
2 changes: 1 addition & 1 deletion source/toy_routine.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ typedef struct Toy_Routine {
unsigned int codeCapacity;
unsigned int codeCount;

unsigned int* jumps; //each 'jump' is the starting address of an element within 'data'
unsigned char* jumps; //each 'jump' is the starting address of an element within 'data'
unsigned int jumpsCapacity;
unsigned int jumpsCount;

Expand Down
2 changes: 1 addition & 1 deletion tests/benchmarks/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 -Wformat=2
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2
LIBS+=-lm
LDFLAGS+=

Expand Down
2 changes: 1 addition & 1 deletion tests/cases/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 -Wformat=2
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpointer-arith -Wformat=2
LIBS+=-lm
LDFLAGS+=

Expand Down
Loading

0 comments on commit 7be63c8

Please sign in to comment.