Skip to content

Commit

Permalink
address all remaining compiler warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Feb 15, 2024
1 parent 57adb88 commit 961a5d9
Show file tree
Hide file tree
Showing 21 changed files with 235 additions and 243 deletions.
2 changes: 1 addition & 1 deletion .clangd
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
CompileFlags:
Add: [-std=c11, -Wall, -Wextra, -Wpedantic ]
Add: [-std=c11, -Wall, -Wextra, -Wpedantic, -Wmissing-braces ]
2 changes: 1 addition & 1 deletion src/builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
struct object get_builtin(const char *name);
struct object get_builtin_by_index(const uint8_t index);

void define_builtins(struct symbol_table *t);
void define_builtins(struct symbol_table *t);
113 changes: 59 additions & 54 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "opcode.h"
#include "parser.h"
#include "symbol_table.h"
#include "vm.h"
#include "builtins.h"

enum {
Expand Down Expand Up @@ -272,74 +271,80 @@ void compiler_change_jump_placeholders(struct compiler* c, uint32_t pos_start, u
}
}

static int
compile_expression(struct compiler *c, const struct expression *expr) {
int err;
switch (expr->type) {
case EXPR_INFIX: {
err = compile_expression(c, expr->infix.left);
if (err) return err;
static int compile_infix_expression(struct compiler *c, const struct expression *expr) {
int err = compile_expression(c, expr->infix.left);
if (err) return err;

err = compile_expression(c, expr->infix.right);
if (err) return err;
err = compile_expression(c, expr->infix.right);
if (err) return err;

switch (expr->infix.operator) {
case OP_ADD:
compiler_emit(c, OPCODE_ADD);
break;
switch (expr->infix.operator) {
case OP_ADD:
compiler_emit(c, OPCODE_ADD);
break;

case OP_SUBTRACT:
compiler_emit(c, OPCODE_SUBTRACT);
break;
case OP_SUBTRACT:
compiler_emit(c, OPCODE_SUBTRACT);
break;

case OP_MULTIPLY:
compiler_emit(c, OPCODE_MULTIPLY);
break;
case OP_MULTIPLY:
compiler_emit(c, OPCODE_MULTIPLY);
break;

case OP_DIVIDE:
compiler_emit(c, OPCODE_DIVIDE);
break;
case OP_DIVIDE:
compiler_emit(c, OPCODE_DIVIDE);
break;

case OP_MODULO:
compiler_emit(c, OPCODE_MODULO);
break;
case OP_MODULO:
compiler_emit(c, OPCODE_MODULO);
break;

case OP_GTE:
compiler_emit(c, OPCODE_GREATER_THAN_OR_EQUALS);
break;
case OP_GTE:
compiler_emit(c, OPCODE_GREATER_THAN_OR_EQUALS);
break;

case OP_GT:
compiler_emit(c, OPCODE_GREATER_THAN);
break;
case OP_GT:
compiler_emit(c, OPCODE_GREATER_THAN);
break;

case OP_EQ:
compiler_emit(c, OPCODE_EQUAL);
break;
case OP_EQ:
compiler_emit(c, OPCODE_EQUAL);
break;

case OP_NOT_EQ:
compiler_emit(c, OPCODE_NOT_EQUAL);
break;
case OP_NOT_EQ:
compiler_emit(c, OPCODE_NOT_EQUAL);
break;

case OP_LT:
compiler_emit(c, OPCODE_LESS_THAN);
break;
case OP_LT:
compiler_emit(c, OPCODE_LESS_THAN);
break;

case OP_LTE:
compiler_emit(c, OPCODE_LESS_THAN_OR_EQUALS);
break;
case OP_LTE:
compiler_emit(c, OPCODE_LESS_THAN_OR_EQUALS);
break;

case OP_AND:
compiler_emit(c, OPCODE_AND);
break;
case OP_AND:
compiler_emit(c, OPCODE_AND);
break;

case OP_OR:
compiler_emit(c, OPCODE_OR);
break;
case OP_OR:
compiler_emit(c, OPCODE_OR);
break;

default:
return COMPILE_ERR_UNKNOWN_OPERATOR;
break;
}
default:
return COMPILE_ERR_UNKNOWN_OPERATOR;
break;
}

return 0;
}

static int
compile_expression(struct compiler *c, const struct expression *expr) {
int err;
switch (expr->type) {
case EXPR_INFIX: {
return compile_infix_expression(c, expr);
}
break;

Expand Down
4 changes: 2 additions & 2 deletions src/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct compiler {
struct compiler_scope scopes[64];
};

struct compiler *compiler_new();
struct compiler *compiler_new(void);
struct compiler *compiler_new_with_state(struct symbol_table *t, struct object_list *constants);
void compiler_free(struct compiler *c);
int compile_program(struct compiler *compiler, const struct program *program);
Expand All @@ -32,4 +32,4 @@ const char *compiler_error_str(const int err);
uint32_t compiler_emit(struct compiler *c, const enum opcode opcode, ...);
void compiler_enter_scope(struct compiler *c);
struct instruction *compiler_leave_scope(struct compiler *c);
struct compiler_scope compiler_current_scope(struct compiler *c);
struct compiler_scope compiler_current_scope(struct compiler *c);
2 changes: 1 addition & 1 deletion src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ gc(struct vm* restrict vm)
printf("Heap size (after): %d\n", vm->heap->size);
printf("GARBAGE COLLECTION DONE\n");
#endif
}
}
4 changes: 1 addition & 3 deletions src/lexer.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "lexer.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

Expand Down Expand Up @@ -310,4 +308,4 @@ struct lexer new_lexer(const char *input) {
.cur_lineno = 1,
.cur_linepos = 0,
};
}
}
1 change: 0 additions & 1 deletion src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "util.h"
#include "opcode.h"
#include "object.h"
#include "parser.h"

const char *object_type_to_str(enum object_type t)
{
Expand Down
16 changes: 8 additions & 8 deletions src/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

enum object_type
{
OBJ_NULL,
OBJ_BOOL,
OBJ_INT,
OBJ_BUILTIN,
OBJ_ERROR,
OBJ_STRING,
OBJ_ARRAY,
OBJ_COMPILED_FUNCTION,
OBJ_NULL, // 0b000
OBJ_BOOL, // 0b001
OBJ_INT, // 0b010
OBJ_BUILTIN, // 0b011
OBJ_ERROR, // 0b100
OBJ_STRING, // 0b101
OBJ_ARRAY, // 0b110
OBJ_COMPILED_FUNCTION, // 0b111
};

struct function {
Expand Down
3 changes: 1 addition & 2 deletions src/opcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include "util.h"
#include "opcode.h"

const struct definition definitions[] = {
static const struct definition definitions[] = {
{ "OpConstant", 1, {2} },
{ "OpPop", 0, {0} },
{ "OpAdd", 0, {0} },
Expand Down
2 changes: 0 additions & 2 deletions src/parser.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#include <assert.h>
#include <ctype.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "object.h"
#include "parser.h"
#include "lexer.h"
#include "util.h"
Expand Down
1 change: 1 addition & 0 deletions src/pepper.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ static
int repl(void) {
print_version();
printf("Press CTRL+c to exit\n\n");

struct program *program;
struct symbol_table *symbol_table = symbol_table_new();
struct object_list *constants = make_object_list(64);
Expand Down
4 changes: 2 additions & 2 deletions src/sizes.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "object.h"


int main() {
int main(void) {
printf("sizeof(char): %ld\n", sizeof(char));
printf("sizeof(enum): %ld\n", sizeof(enum { A, B}));
printf("sizeof(char*): %ld\n", sizeof(char*));
Expand All @@ -31,4 +31,4 @@ int main() {
t.value.b = malloc(sizeof(int));
printf("%p\n", t.value.b);
printf("%p\n", t.value);
}
}
15 changes: 8 additions & 7 deletions src/symbol_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

#define hash(v) (v[0] - 'a')

static struct hashmap *hashmap_new();
static struct hashmap *hashmap_new(void);
static void hashmap_insert(struct hashmap *hm, const char *key, void *item);
static void *hashmap_get(struct hashmap *hm, const char *key);
static void hashmap_free(struct hashmap *hm);

static struct hashmap *
hashmap_new() {
hashmap_new(void) {
struct hashmap *hm = malloc(sizeof *hm);
if (!hm) err(EXIT_FAILURE, "out of memory");

for (int8_t i=0; i < 26; i++) {
for (unsigned i=0; i < 26u; i++) {
hm->table[i] = NULL;
}

Expand Down Expand Up @@ -51,8 +51,9 @@ hashmap_insert(struct hashmap *hm, const char *key, void *item) {

static void *
hashmap_get(struct hashmap *hm, const char *key) {
const int8_t pos = hash(key);
unsigned pos = hash(key);
struct hashmap_node *node = hm->table[pos];

while (node) {
if (strcmp(node->key, key) == 0) {
return node->value;
Expand All @@ -67,7 +68,7 @@ void hashmap_free(struct hashmap *hm) {
struct hashmap_node *node;
struct hashmap_node *next;

for (int8_t i=0; i < 26; i++) {
for (unsigned i=0; i < 26u; i++) {
node = hm->table[i];
while (node) {
next = node->next;
Expand All @@ -80,7 +81,7 @@ void hashmap_free(struct hashmap *hm) {
free(hm);
}

struct symbol_table *symbol_table_new() {
struct symbol_table *symbol_table_new(void) {
struct symbol_table *t = malloc(sizeof *t);
if (!t) err(EXIT_FAILURE, "out of memory");
t->size = 0;
Expand Down Expand Up @@ -146,4 +147,4 @@ struct symbol *symbol_table_resolve(struct symbol_table *t, const char *name) {
void symbol_table_free(struct symbol_table *t) {
hashmap_free(t->store);
free(t);
}
}
2 changes: 1 addition & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stdarg.h>

__attribute__((noreturn))
static inline void err(const int status, const char *format, ...) {
static inline void err(__attribute__((unused)) int status, const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
Expand Down
Loading

0 comments on commit 961a5d9

Please sign in to comment.