Skip to content

Commit 48be56e

Browse files
committed
adding tests
1 parent d75a60d commit 48be56e

File tree

5 files changed

+163
-97
lines changed

5 files changed

+163
-97
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
*.asm
2-
*.bin
2+
*.bin
3+
bin/*
4+
obj/*

makefile

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#The target binary
2-
BINARY = bin/cc
31

42
#The target library
53
LIB_DIR = lib
@@ -13,11 +11,17 @@ INC_DIR = include
1311
# The directory for object files
1412
OBJ_DIR = obj
1513

14+
# The directory for storing binary executables
15+
BIN_DIR = bin
16+
17+
#The target binary
18+
BINARY = $(BIN_DIR)/cc
19+
1620
# The C compiler to use
1721
CC = gcc
1822

1923
# The C flags to use
20-
CFLAGS = -I$(INC_DIR)
24+
CFLAGS = -I$(INC_DIR)
2125

2226
LOCAL_LIBS = $(wildcard $(LIB_DIR)/*/*.a)
2327

@@ -30,11 +34,15 @@ SOURCES = $(wildcard $(SRC_DIR)/*.c)
3034
OBJECTS = $(SOURCES:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
3135

3236
# The default target
33-
all: libs $(BINARY)
37+
all: dirs libs $(BINARY)
3438

3539
libs:
3640
for i in $(LOCAL_LIBS); do make -C $$(dirname $$i) all; done
3741

42+
dirs:
43+
mkdir -p $(OBJ_DIR)
44+
mkdir -p $(BIN_DIR)
45+
3846
# Link the objects into the binary
3947
$(BINARY): $(OBJECTS) $(LIBRARY)
4048
$(CC) $(CFLAGS) $(LDFLAGS) -o $(BINARY) $(OBJECTS) $(LIBS)
@@ -43,6 +51,11 @@ $(BINARY): $(OBJECTS) $(LIBRARY)
4351
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
4452
$(CC) $(CFLAGS) -c -o $@ $<
4553

54+
test:
55+
$(CC) $(CFLAGS) -o $(BIN_DIR)/test tests/main.c $(SRC_DIR)/{parse.c,parseutils.c,tokenize.c,hashtable.c,pattern.c} $(LIBS)
56+
$(BIN_DIR)/test
57+
58+
4659
# Clean up
4760
clean:
4861
rm -f $(OBJ_DIR)/*.o

src/parse.c

+24-21
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <stdlib.h>
55
#include <stdlib.h>
66
#include <stdio.h>
7-
#include "limits.h"
7+
#include "limits.h"
88

99

1010

@@ -31,7 +31,7 @@ struct AST* parse_op(int* tokens, int* index) {
3131
int len = 0;
3232
while (!t_isop(tokens[*index + len])) {
3333

34-
// skip any
34+
// skip any
3535

3636
len++;
3737
}
@@ -74,7 +74,7 @@ struct AST* parse_op(int* tokens, int* index) {
7474
break;
7575
case T_OR:
7676
ast->op->tag = AST_OR;
77-
break;
77+
break;
7878
case T_NOT:
7979
ast->op->tag = AST_NOT;
8080
break;
@@ -117,13 +117,13 @@ struct AST* parse_arg(int t) {
117117
}
118118

119119
struct AST* parse_specifier(int t) {
120-
120+
121121
dbg(2, "Parsing specifier %d", t);
122-
122+
123123
struct AST* ast = malloc(sizeof(struct AST));
124124
ast->type = AST_SPEC;
125125
ast->spec = malloc(sizeof(struct ASTspec));
126-
126+
127127
switch (t) {
128128
case T_T8:
129129
ast->spec->tag = AST_8;
@@ -139,14 +139,14 @@ struct AST* parse_specifier(int t) {
139139
exit(1);
140140
break;
141141
}
142-
142+
143143
return ast;
144144
}
145145

146146
struct AST* parse_loop(int* tokens, int* index) {
147147

148148
dbg(2, "Parsing loop from %d", tokens[*index]);
149-
149+
150150
// Syntaxic definition
151151
/*
152152
loop -> while ( condition ) block
@@ -459,7 +459,7 @@ struct AST* parse_declare(int* tokens, int* index) {
459459
capacity *= 2;
460460
ast->stmt->args = realloc(ast->stmt->args, capacity * sizeof(struct AST*));
461461
}
462-
// first we should have a type indicator
462+
// first we should have a type indicator
463463
if (!t_istype(tokens[*index])) {
464464
msg(ERROR,"Expected type in declare argument\n");
465465
exit(1);
@@ -473,8 +473,8 @@ struct AST* parse_declare(int* tokens, int* index) {
473473

474474
if (tokens[*index] == T_COMMA) {
475475
(*index)++;
476-
}
477-
476+
}
477+
478478
}
479479

480480
(*index)++;
@@ -494,21 +494,21 @@ struct AST* parse_block(int* tokens, int* index) {
494494
stmts -> stmt stmts | stmt
495495
- <stmt> <stmts> | <stmt>
496496
*/
497-
497+
498498
struct AST* ast = malloc(sizeof(struct AST));
499499
ast->type = AST_BLOCK;
500500
ast->block = malloc(sizeof(struct ASTblock));
501501
ast->block->size = 0;
502502
int capacity = 10;
503503
ast->block->childs = malloc(capacity * sizeof(struct AST));
504-
504+
505505
if (tokens[*index] != T_LBRACE) {
506506
printf("Expected '{' in block\n");
507507
exit(1);
508508
}
509-
509+
510510
(*index)++;
511-
511+
512512
while (tokens[*index] != T_RBRACE) {
513513
if (ast->block->size >= capacity) {
514514
capacity *= 2;
@@ -517,9 +517,9 @@ struct AST* parse_block(int* tokens, int* index) {
517517
ast->block->childs[ast->block->size] = parse(tokens, index,0);
518518
ast->block->size++;
519519
}
520-
520+
521521
(*index)++;
522-
522+
523523
return ast;
524524
}
525525

@@ -558,10 +558,12 @@ struct AST* parse_opblock(int* tokens, int* index) {
558558

559559
struct AST* parse(int* tokens, int* index,int len) {
560560

561+
msg(INFO,"Parsing %d tokens...", len);
562+
561563
static int rec = 0;
562564
rec++;
563565
if (rec > 10) {
564-
msg(ERROR, "Recursion limit reached\n");
566+
msg(ERROR, "Recursion limit reached");
565567
exit(1);
566568
}
567569

@@ -570,19 +572,20 @@ struct AST* parse(int* tokens, int* index,int len) {
570572
}
571573

572574
// The parser will be a recursive descent parser
573-
575+
574576
// The parser need to identify structures in the tokenized code
575577
// the parser will return an AST (Abstract Syntax Tree) element.
576578

577579
// in order to identify structure we will use a pattern table
578-
580+
579581
// create a parser variable (function pointer) to the parse function
580582
// the parser will return an AST element
581583

582584
// Assuming match_pattern returns a pair where value is a function pointer
583585
pattern* pattern = match_pattern(tokens + *index, len);
584586
if (pattern == NULL) {
585587
// Handle error: no matching pattern found
588+
msg(ERROR,"No matching pattern found\n");
586589
return NULL;
587590
}
588591

@@ -678,4 +681,4 @@ void visualize_ast(struct AST ast) {
678681
printf("Unknown AST type\n");
679682
break;
680683
}
681-
}
684+
}

0 commit comments

Comments
 (0)