~groovestomp/c-parser

70a6051ad4d196a0a469a758e9c3eb49d2103267 — Aaron Oman 2 years ago ccec1b8
Various fixes

- Proper release and debug builds.
- Make tests pass.
- Fix a couple of warnings.
5 files changed, 28 insertions(+), 18 deletions(-)

M Makefile
M README.md
M src/gs.h
M src/parse_tree.c
M src/test.c
M Makefile => Makefile +12 -6
@@ 1,11 1,17 @@
.DEFAULT_GOAL := build
.PHONY: help test clean
.DEFAULT_GOAL := release
.PHONY: help test clean release debug

CC=gcc
CFLAGS=-std=c99 -pedantic-errors -fextended-identifiers -g -x c -gdwarf-2 -g3 -Wno-format-security
CFLAGS=-std=c99 -pedantic-errors -x c -Wno-format-security
RELEASE_CFLAGS=-O2
DEBUG_CFLAGS=-gdwarf-4 -g3 -fvar-tracking-assignments
EXE=cparser

build: $(OBJ)
	$(CC) $(CFLAGS) -o cparser src/main.c
debug:
	$(CC) $(CFLAGS) $(DEBUG_CFLAGS) -o $(EXE) src/main.c

release:
	$(CC) $(CFLAGS) $(RELEASE_CFLAGS) -o $(EXE) src/main.c

test:
	$(CC) $(CFLAGS) -o test src/test.c


@@ 14,4 20,4 @@ help:
	@sh ./sh/view-help README.md

clean:
	@rm -f src/*.o cparser test
	@rm -f src/*.o $(EXE) $(EXE) test

M README.md => README.md +4 -3
@@ 35,11 35,12 @@ Clone this repo and chdir to it.

## Build

Build `cparser` and `test` executables
These commands output an executable named `cparser`

    $ make build
    $ make debug
    $ make release

Other targets:

    $ make test # Run tests
    $ make test # Build test executable
    $ make help # Show this help on the CLI

M src/gs.h => src/gs.h +1 -1
@@ 927,7 927,7 @@ GSFileCopyToBuffer(char *FileName, gs_buffer *Buffer)
        if(FileSize > Remaining) return false;

        fseek(File, 0, SEEK_SET);
        fread(Buffer->Cursor, 1, FileSize, File);
        size_t BytesRead = fread(Buffer->Cursor, 1, FileSize, File);
        Buffer->Length += FileSize;
        Buffer->Cursor += FileSize;
        *(Buffer->Cursor) = '\0';

M src/parse_tree.c => src/parse_tree.c +1 -1
@@ 177,7 177,7 @@ void ParseTreePrint(parse_tree_node *Self, u32 IndentLevel, u32 IndentIncrement)
                printf("Unknown Name");
        }

        if (Self->Token.Type != Token_Unknown) printf("( %.*s )", Self->Token.TextLength, Self->Token.Text);
        if (Self->Token.Type != Token_Unknown) printf("( %.*s )", CastSizeIntTo32Bits(Self->Token.TextLength), Self->Token.Text);

        printf("\n");


M src/test.c => src/test.c +10 -7
@@ 8,23 8,25 @@
#include "parser.c"
#include "parse_tree.c"

typedef gs_bool (*parser_function)(struct tokenizer *, parse_tree_node *);
typedef bool (*parser_function)(struct tokenizer *, parse_tree_node *);

parse_tree_allocator Allocator = { .Alloc = malloc, .Free = free };

#define Accept(Function, String) \
        { \
                parse_tree_node *ParseTree = ParseTreeNew(); \
                parse_tree_node *ParseTree = ParseTreeInit(Allocator); \
                struct tokenizer Tokenizer = InitTokenizer((String)); \
                gs_bool Result = Function(&Tokenizer, ParseTree); \
                ParseTreeFree(ParseTree); \
                bool Result = Function(&Tokenizer, ParseTree); \
                ParseTreeDeinit(ParseTree); \
                GSTestAssert(Result == true, "Result should be true\n"); \
                GSTestAssert(Tokenizer.At == (String) + GSStringLength((String)), "Tokenizer advances to end of string\n"); \
        }
#define Reject(Function, String) \
        { \
                parse_tree_node *ParseTree = ParseTreeNew(); \
                parse_tree_node *ParseTree = ParseTreeInit(Allocator); \
                struct tokenizer Tokenizer = InitTokenizer((String)); \
                gs_bool Result = Function(&Tokenizer, ParseTree); \
                ParseTreeFree(ParseTree); \
                bool Result = Function(&Tokenizer, ParseTree); \
                ParseTreeDeinit(ParseTree); \
                GSTestAssert(Result != true, "Result should be false\n"); \
                GSTestAssert(Tokenizer.At == (String), "Tokenizer doesn't advance\n"); \
        }


@@ 631,5 633,6 @@ int main(int ArgCount, char **Arguments) {
        TestTranslationUnit();

        printf("All tests successful\n");

        return EXIT_SUCCESS;
}