Skip to content

Commit

Permalink
Merge pull request #3 from Glyphack/add-tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
Glyphack authored Oct 13, 2024
2 parents 6d221d4 + ae57ecc commit eadf897
Show file tree
Hide file tree
Showing 20 changed files with 1,214 additions and 39 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Check

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Set up GCC
uses: egor-tensin/setup-gcc@v1
with:
version: latest

- name: Run tests
run: python scripts/test.py
54 changes: 54 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/temp_snapshots

# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
4 changes: 2 additions & 2 deletions codegen/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ context new_context()
return ctx;
}

char *asprintf(char *fmt, ...)
char *cc_asprintf(char *fmt, ...)
{
char *txt = (char *)malloc(128);
va_list args;
Expand Down Expand Up @@ -89,4 +89,4 @@ symbol *new_symbol(context *ctx, char *name, int sz)
symbol *new_temp_symbol(context *ctx, int sz)
{
return new_symbol(ctx, "", sz);
}
}
4 changes: 2 additions & 2 deletions codegen/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ typedef struct
int offset;
} symbol;

char *asprintf(char *fmt, ...);
char *cc_asprintf(char *fmt, ...);
symbol *find_symbol(context *tab, char *name);
symbol *new_symbol(context *ctx, char *name, int sz);
symbol *new_temp_symbol(context *ctx, int sz);
char *new_label(context *ctx);
#endif
#endif
9 changes: 9 additions & 0 deletions examples/inp4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct MyStruct
{
int c;
};

int main() {
struct MyStruct m;
return 0;
}
19 changes: 11 additions & 8 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ char *read_source_file(FILE *fp);
(p) = NULL; \
} while (0)

int main(void)
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return 1;
}

int ret = 0;
char *content = NULL;
const char *filename = argv[1];

FILE *fp = fopen("./examples/inp.c", "rb");
if (!fp)
{
perror("failed to read source file");
ret = 1;
goto defer_exit;
FILE *fp = fopen(filename, "rb");
if (fp == NULL) {
fprintf(stderr, "Error opening file: %s\n", filename);
return 1;
}

content = read_source_file(fp);

if (!content)
Expand Down
4 changes: 2 additions & 2 deletions parser/expr/assign.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ char *assign_apply(parser_node *node, context *ctx)
symbol *sym = find_symbol(ctx, assign->identity);

add_text(ctx, "mov rax, %s", val);
char *res = asprintf("[rsp+%u]", sym->offset);
char *res = cc_asprintf("[rsp+%u]", sym->offset);
add_text(ctx, "mov %s, rax", res);

return res;
Expand Down Expand Up @@ -76,4 +76,4 @@ parser_node *parse_assign(typed_token **tkns_ptr)
}

return NULL;
}
}
6 changes: 3 additions & 3 deletions parser/expr/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ char *binary_op_apply(parser_node *node, context *ctx)
symbol *tmp = new_temp_symbol(ctx, 8);

add_text(ctx, "mov [rsp + %u], rax", tmp->offset);
return asprintf("[rsp + %u]", tmp->offset);
return cc_asprintf("[rsp + %u]", tmp->offset);
}

void cond_debug(int depth, parser_node *node)
Expand Down Expand Up @@ -142,7 +142,7 @@ char *cond_apply(parser_node *node, context *ctx)
add_text(ctx, "%s:", l2);
symbol *sym = new_temp_symbol(ctx, 8);
add_text(ctx, "mov [rsp+%u], rax", sym->offset);
return asprintf("[rsp+%u]", sym->offset);
return cc_asprintf("[rsp+%u]", sym->offset);
}

void cast_debug(int depth, parser_node *node)
Expand Down Expand Up @@ -364,4 +364,4 @@ parser_node *parse_expr_prec(typed_token **tkns_ptr, parser_node *lhs, int min_p
}

return NULL;
}
}
6 changes: 3 additions & 3 deletions parser/expr/func_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ char *func_call_apply(parser_node *node, context *ctx)

symbol *tmp = new_temp_symbol(ctx, 8);

char *regname = asprintf("[rsp + %u]", tmp->offset);
char *regname = cc_asprintf("[rsp + %u]", tmp->offset);

add_text(ctx, "mov rax, %s", regval);
add_text(ctx, "mov %s, rax", regname);
Expand Down Expand Up @@ -66,7 +66,7 @@ char *func_call_apply(parser_node *node, context *ctx)
symbol *tmp = new_temp_symbol(ctx, 8);
add_text(ctx, "mov [rsp + %u], rax", tmp->offset);

return asprintf("[rsp + %u]", tmp->offset);
return cc_asprintf("[rsp + %u]", tmp->offset);
}

parser_node *parse_func_call(typed_token **tkns_ptr)
Expand Down Expand Up @@ -128,4 +128,4 @@ parser_node *parse_func_call(typed_token **tkns_ptr)
}

return NULL;
}
}
8 changes: 4 additions & 4 deletions parser/expr/literal.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ char *literal_apply(parser_node *node, context *ctx)
node_literal *lit = (node_literal *)node->data;
if (lit->type == TKN_LIT_STR)
{
char *varname = asprintf("__temp_str_%u", ctx->data.count);
char *varname = cc_asprintf("__temp_str_%u", ctx->data.count);
add_data(ctx, "%s db `%s`, 0", varname, escape(lit->value));
return varname;
}
if (lit->type == TKN_LIT_INT)
{
return asprintf("%u", *((int *)lit->value));
return cc_asprintf("%u", *((int *)lit->value));
}
if (lit->type == TKN_LIT_CHAR)
{
return asprintf("%u", (int)(*((char *)lit->value)));
return cc_asprintf("%u", (int)(*((char *)lit->value)));
}
return lit->value;
}
Expand Down Expand Up @@ -105,4 +105,4 @@ parser_node *parse_literal(typed_token **tkns_ptr)
}

return NULL;
}
}
4 changes: 2 additions & 2 deletions parser/expr/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ char *var_apply(parser_node *node, context *ctx)
}
else
{
return asprintf("[rsp + %u]", sym->offset);
return cc_asprintf("[rsp + %u]", sym->offset);
}
return NULL;
}
Expand All @@ -48,4 +48,4 @@ parser_node *parse_var(typed_token **tkns_ptr)
return node;
}
return NULL;
}
}
50 changes: 41 additions & 9 deletions parser/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,64 @@ void type_debug(int depth, parser_node *node)
for (int i = 0; i < tp->dim; i++) {
printf("[%u]", tp->dims[i]);
}
printf(")\n");
printf(")");
if (tp->is_struct == 1) {
printf("struct");
}
printf("\n");
}

parser_node *parse_type(typed_token **tkns_ptr)
{
typed_token *tkn = *tkns_ptr;
parser_node *node = NULL;
if (tkn->type_id == TKN_INT || tkn->type_id == TKN_VOID || tkn->type_id == TKN_CHAR)
{
typed_token *ret_type_tkn = tkn;
tkn = tkn->next;
node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_type));
node->debug = type_debug;
node_type *par = (node_type *)node->data;
par->name = ret_type_tkn->data;
} else if (tkn->type_id == TKN_LIT_STRUCT)
{
tkn = tkn->next;
typed_token *struct_name_tkn = tkn;
// TODO: maybe error?
if (struct_name_tkn == NULL) {
return NULL;
}
node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_type));
node->debug = type_debug;
node_type *par = (node_type *)node->data;
if (tkn->next == NULL) {
// After name there must be either { or the variable name
return NULL;
}
// This is a struct definition not variable
if (tkn->next->type_id == TKN_L_BRACE) {
return NULL;
}
par->name = malloc(128);
par->is_struct = 1;
strcpy(par->name, struct_name_tkn->data);
tkn = struct_name_tkn->next;
}

if (node != NULL) {
int num_pointing = 0;
while (tkn->type_id == TKN_STAR)
{
tkn = tkn->next;
num_pointing++;
}
*tkns_ptr = tkn;
parser_node *node = (parser_node *)malloc(sizeof(parser_node));
node->data = (void *)malloc(sizeof(node_type));
node->debug = type_debug;
node_type *par = (node_type *)node->data;
par->name = ret_type_tkn->data;
par->num_pointing = num_pointing;
par->dim = 0;
par->dims = NULL;
return node;
*tkns_ptr = tkn;
}
return NULL;
}
return node;
}
3 changes: 2 additions & 1 deletion parser/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ typedef struct
{
char *name;
int num_pointing;
int is_struct;
int dim;
int *dims;
} node_type;

#endif
#endif
4 changes: 3 additions & 1 deletion parser/var_decl.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ parser_node *parse_var_decl(typed_token **tkns_ptr)
decl->type = tp;
decl->identity = malloc(128);
strcpy(decl->identity, name_tkn->data);
printf("name: %s\n", decl->identity);
node->debug(1,node);
decl->value = val_expr;

return node;
}
}

return NULL;
}
}
4 changes: 2 additions & 2 deletions preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ typed_token *preprocess(typed_token *tkns)
tkn = tkn->next;
if (tkn->type_id == TKN_ID)
{
char *name = asprintf("%s", (char *)tkn->data);
char *name = cc_asprintf("%s", (char *)tkn->data);
tkn = tkn->next;
define *def = (define *)malloc(sizeof(define));
def->id = name;
Expand Down Expand Up @@ -98,4 +98,4 @@ typed_token *preprocess(typed_token *tkns)
tkn = tkn->next;
}
return first;
}
}
Loading

0 comments on commit eadf897

Please sign in to comment.