Fiddling with -Wpedantic

This commit is contained in:
2022-08-07 21:39:21 +01:00
parent 3b89e216cc
commit ceeca9d40f
9 changed files with 50 additions and 20 deletions

View File

@@ -2,6 +2,8 @@
#include "memory.h" #include "memory.h"
#include <stdio.h>
void initCompiler(Compiler* compiler) { void initCompiler(Compiler* compiler) {
initLiteralArray(&compiler->literalCache); initLiteralArray(&compiler->literalCache);
compiler->bytecode = NULL; compiler->bytecode = NULL;
@@ -30,6 +32,11 @@ void writeCompiler(Compiler* compiler, Node* node) {
//determine node type //determine node type
switch(node->type) { switch(node->type) {
//TODO: more types, like variables, etc. //TODO: more types, like variables, etc.
case NODE_ERROR: {
printf("[internal] NODE_ERROR encountered in writeCompiler()");
compiler->bytecode[compiler->count++] = OP_EOF; //1 byte
}
break;
case NODE_LITERAL: { case NODE_LITERAL: {
//ensure the literal is in the cache //ensure the literal is in the cache
@@ -83,19 +90,19 @@ void freeCompiler(Compiler* compiler) {
compiler->count = 0; compiler->count = 0;
} }
static void emitByte(char** collationPtr, int* capacityPtr, int* countPtr, unsigned char byte) { static void emitByte(unsigned char** collationPtr, int* capacityPtr, int* countPtr, unsigned char byte) {
//grow the array //grow the array
if (*countPtr + 1 > *capacityPtr) { if (*countPtr + 1 > *capacityPtr) {
int oldCapacity = *capacityPtr; int oldCapacity = *capacityPtr;
*capacityPtr = GROW_CAPACITY(*capacityPtr); *capacityPtr = GROW_CAPACITY(*capacityPtr);
*collationPtr = GROW_ARRAY(char, *collationPtr, oldCapacity, *capacityPtr); *collationPtr = GROW_ARRAY(unsigned char, *collationPtr, oldCapacity, *capacityPtr);
} }
//append to the collation //append to the collation
(*collationPtr)[(*countPtr)++] = byte; (*collationPtr)[(*countPtr)++] = byte;
} }
static void emitShort(char** collationPtr, int* capacityPtr, int* countPtr, unsigned short bytes) { static void emitShort(unsigned char** collationPtr, int* capacityPtr, int* countPtr, unsigned short bytes) {
char* ptr = (char*)&bytes; char* ptr = (char*)&bytes;
emitByte(collationPtr, capacityPtr, countPtr, *ptr); emitByte(collationPtr, capacityPtr, countPtr, *ptr);
@@ -103,7 +110,7 @@ static void emitShort(char** collationPtr, int* capacityPtr, int* countPtr, unsi
emitByte(collationPtr, capacityPtr, countPtr, *ptr); emitByte(collationPtr, capacityPtr, countPtr, *ptr);
} }
static void emitInt(char** collationPtr, int* capacityPtr, int* countPtr, int bytes) { static void emitInt(unsigned char** collationPtr, int* capacityPtr, int* countPtr, int bytes) {
char* ptr = (char*)&bytes; char* ptr = (char*)&bytes;
emitByte(collationPtr, capacityPtr, countPtr, *ptr); emitByte(collationPtr, capacityPtr, countPtr, *ptr);
@@ -115,7 +122,7 @@ static void emitInt(char** collationPtr, int* capacityPtr, int* countPtr, int by
emitByte(collationPtr, capacityPtr, countPtr, *ptr); emitByte(collationPtr, capacityPtr, countPtr, *ptr);
} }
static void emitFloat(char** collationPtr, int* capacityPtr, int* countPtr, float bytes) { static void emitFloat(unsigned char** collationPtr, int* capacityPtr, int* countPtr, float bytes) {
char* ptr = (char*)&bytes; char* ptr = (char*)&bytes;
emitByte(collationPtr, capacityPtr, countPtr, *ptr); emitByte(collationPtr, capacityPtr, countPtr, *ptr);
@@ -128,10 +135,10 @@ static void emitFloat(char** collationPtr, int* capacityPtr, int* countPtr, floa
} }
//return the result //return the result
char* collateCompiler(Compiler* compiler, int* size) { unsigned char* collateCompiler(Compiler* compiler, int* size) {
int capacity = GROW_CAPACITY(0); int capacity = GROW_CAPACITY(0);
int count = 0; int count = 0;
char* collation = ALLOCATE(char, capacity); unsigned char* collation = ALLOCATE(unsigned char, capacity);
//embed the header with version information //embed the header with version information
emitByte(&collation, &capacity, &count, TOY_VERSION_MAJOR); emitByte(&collation, &capacity, &count, TOY_VERSION_MAJOR);
@@ -142,7 +149,7 @@ char* collateCompiler(Compiler* compiler, int* size) {
if (strlen(TOY_VERSION_BUILD) + count + 1 > capacity) { if (strlen(TOY_VERSION_BUILD) + count + 1 > capacity) {
int oldCapacity = capacity; int oldCapacity = capacity;
capacity = strlen(TOY_VERSION_BUILD) + count + 1; //full header size capacity = strlen(TOY_VERSION_BUILD) + count + 1; //full header size
collation = GROW_ARRAY(char, collation, oldCapacity, capacity); collation = GROW_ARRAY(unsigned char, collation, oldCapacity, capacity);
} }
memcpy(&collation[count], TOY_VERSION_BUILD, strlen(TOY_VERSION_BUILD)); memcpy(&collation[count], TOY_VERSION_BUILD, strlen(TOY_VERSION_BUILD));
@@ -208,7 +215,7 @@ char* collateCompiler(Compiler* compiler, int* size) {
emitByte(&collation, &capacity, &count, OP_EOF); //terminate bytecode emitByte(&collation, &capacity, &count, OP_EOF); //terminate bytecode
//finalize //finalize
SHRINK_ARRAY(char, collation, capacity, count); SHRINK_ARRAY(unsigned char, collation, capacity, count);
*size = count; *size = count;

View File

@@ -18,4 +18,4 @@ void writeCompiler(Compiler* compiler, Node* node);
void freeCompiler(Compiler* compiler); void freeCompiler(Compiler* compiler);
//embed the header with version information, data section, code section, etc. //embed the header with version information, data section, code section, etc.
char* collateCompiler(Compiler* compiler, int* size); unsigned char* collateCompiler(Compiler* compiler, int* size);

View File

@@ -70,9 +70,9 @@ static float readFloat(unsigned char* tb, int* count) {
} }
static char* readString(unsigned char* tb, int* count) { static char* readString(unsigned char* tb, int* count) {
char* ret = tb + *count; unsigned char* ret = tb + *count;
*count += strlen(ret) + 1; //+1 for null character *count += strlen((char*)ret) + 1; //+1 for null character
return ret; return (char*)ret;
} }
static void consumeByte(unsigned char byte, unsigned char* tb, int* count) { static void consumeByte(unsigned char byte, unsigned char* tb, int* count) {
@@ -194,6 +194,9 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) {
pushLiteralArray(&interpreter->stack, TO_INTEGER_LITERAL( AS_INTEGER(lhs) % AS_INTEGER(rhs) )); pushLiteralArray(&interpreter->stack, TO_INTEGER_LITERAL( AS_INTEGER(lhs) % AS_INTEGER(rhs) ));
return true; return true;
default:
printf("[internal] bad opcode argument passed to execArithmetic()");
return false;
} }
} }
@@ -220,6 +223,10 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) {
case OP_DIVISION: case OP_DIVISION:
pushLiteralArray(&interpreter->stack, TO_FLOAT_LITERAL( AS_FLOAT(lhs) / AS_FLOAT(rhs) )); pushLiteralArray(&interpreter->stack, TO_FLOAT_LITERAL( AS_FLOAT(lhs) / AS_FLOAT(rhs) ));
return true; return true;
default:
printf("[internal] bad opcode argument passed to execArithmetic()");
return false;
} }
} }

View File

@@ -32,7 +32,7 @@ KeywordType keywordTypes[] = {
{TOKEN_OF, "of"}, {TOKEN_OF, "of"},
{TOKEN_PRINT, "print"}, {TOKEN_PRINT, "print"},
{TOKEN_RETURN, "return"}, {TOKEN_RETURN, "return"},
{TOKEN_USING, "using"}, {TOKEN_TYPE, "type"},
{TOKEN_VAR, "var"}, {TOKEN_VAR, "var"},
{TOKEN_WHILE, "while"}, {TOKEN_WHILE, "while"},
@@ -41,8 +41,8 @@ KeywordType keywordTypes[] = {
{TOKEN_LITERAL_FALSE, "false"}, {TOKEN_LITERAL_FALSE, "false"},
//meta tokens //meta tokens
{TOKEN_PASS, "pass"}, {TOKEN_PASS, NULL},
{TOKEN_ERROR, "error"}, {TOKEN_ERROR, NULL},
{TOKEN_EOF, NULL}, {TOKEN_EOF, NULL},
}; };

View File

@@ -1,7 +1,7 @@
CC=gcc CC=gcc
IDIR =. IDIR =.
CFLAGS=$(addprefix -I,$(IDIR)) -g # -Wall -W -pedantic CFLAGS=$(addprefix -I,$(IDIR)) -g # -Wall -W -pedantic -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable
LIBS= LIBS=
ODIR=obj ODIR=obj

View File

@@ -11,6 +11,10 @@ void freeNode(Node* node) {
} }
switch(node->type) { switch(node->type) {
case NODE_ERROR:
//NO-OP
break;
case NODE_LITERAL: case NODE_LITERAL:
freeLiteral(node->atomic.literal); freeLiteral(node->atomic.literal);
break; break;
@@ -71,6 +75,10 @@ void emitNodeGrouping(Node** nodeHandle) {
void printNode(Node* node) { void printNode(Node* node) {
switch(node->type) { switch(node->type) {
case NODE_ERROR:
printf("error");
break;
case NODE_LITERAL: case NODE_LITERAL:
printf("literal:"); printf("literal:");
printLiteral(node->atomic.literal); printLiteral(node->atomic.literal);

View File

@@ -424,6 +424,10 @@ static bool calcStaticBinaryArithmetic(Node** nodeHandle) {
case OP_MODULO: case OP_MODULO:
result = TO_INTEGER_LITERAL( AS_INTEGER(lhs) % AS_INTEGER(rhs) ); result = TO_INTEGER_LITERAL( AS_INTEGER(lhs) % AS_INTEGER(rhs) );
break; break;
default:
printf("[internal] bad opcode argument passed to calcStaticBinaryArithmetic()");
return false;
} }
} }
@@ -450,6 +454,10 @@ static bool calcStaticBinaryArithmetic(Node** nodeHandle) {
case OP_DIVISION: case OP_DIVISION:
result = TO_FLOAT_LITERAL( AS_FLOAT(lhs) / AS_FLOAT(rhs) ); result = TO_FLOAT_LITERAL( AS_FLOAT(lhs) / AS_FLOAT(rhs) );
break; break;
default:
printf("[internal] bad opcode argument passed to calcStaticBinaryArithmetic()");
return false;
} }
} }

View File

@@ -73,7 +73,7 @@ void runString(char* source) {
//get the bytecode dump //get the bytecode dump
int size = 0; int size = 0;
char* tb = collateCompiler(&compiler, &size); unsigned char* tb = collateCompiler(&compiler, &size);
//cleanup //cleanup
freeCompiler(&compiler); freeCompiler(&compiler);
@@ -131,7 +131,7 @@ void repl() {
if (!error) { if (!error) {
//get the bytecode dump //get the bytecode dump
int size = 0; int size = 0;
char* tb = collateCompiler(&compiler, &size); unsigned char* tb = collateCompiler(&compiler, &size);
// for (int i = 0; i < size; i++) { // for (int i = 0; i < size; i++) {
// printf("%d ", tb[i]); // printf("%d ", tb[i]);

View File

@@ -30,7 +30,7 @@ typedef enum TokenType {
TOKEN_OF, TOKEN_OF,
TOKEN_PRINT, TOKEN_PRINT,
TOKEN_RETURN, TOKEN_RETURN,
TOKEN_USING, TOKEN_TYPE,
TOKEN_VAR, TOKEN_VAR,
TOKEN_WHILE, TOKEN_WHILE,