Added lexer, implemented tests build system

This commit is contained in:
2024-08-11 20:15:09 +10:00
parent 82f63013d8
commit a912b6a29c
10 changed files with 632 additions and 14 deletions

View File

47
tests/cases/test_lexer.c Normal file
View File

@@ -0,0 +1,47 @@
#include "toy_lexer.h"
#include "toy_console_colors.h"
#include <stdio.h>
#include <string.h>
int main() {
{
//source code sample to operate on
char* source = "print null;";
//test the lexer
Toy_Lexer lexer;
Toy_bindLexer(&lexer, source);
//get each token
Toy_Token print = Toy_private_scanLexer(&lexer);
Toy_Token null = Toy_private_scanLexer(&lexer);
Toy_Token semi = Toy_private_scanLexer(&lexer);
Toy_Token eof = Toy_private_scanLexer(&lexer);
//test each token is correct
if (strncmp(print.lexeme, "print", print.length)) {
fprintf(stderr, TOY_CC_ERROR "ERROR: print lexeme is wrong: %s" TOY_CC_RESET, print.lexeme);
return -1;
}
if (strncmp(null.lexeme, "null", null.length)) {
fprintf(stderr, TOY_CC_ERROR "ERROR: null lexeme is wrong: %s" TOY_CC_RESET, null.lexeme);
return -1;
}
if (strncmp(semi.lexeme, ";", semi.length)) {
fprintf(stderr, TOY_CC_ERROR "ERROR: semicolon lexeme is wrong: %s" TOY_CC_RESET, semi.lexeme);
return -1;
}
if (eof.type != TOY_TOKEN_EOF) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to find EOF token" TOY_CC_RESET);
return -1;
}
}
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
return 0;
}

74
tests/makefile Normal file
View File

@@ -0,0 +1,74 @@
#compiler settings
CFLAGS=-g -Wall -Werror -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable
LIBS=-lm
#directories
TEST_SOURCEDIR=../$(TOY_SOURCEDIR)
TEST_CASESDIR=cases
TEST_OUTDIR=out
TEST_OBJDIR=obj
#file names
TEST_SOURCEFILES=$(wildcard $(TEST_SOURCEDIR)/*.c)
TEST_CASESFILES=$(wildcard $(TEST_CASESDIR)/*.c)
#build the object files, compile the test cases, and run
all: clean build-source build-cases $(addprefix $(TEST_OUTDIR)/,$(notdir $(TEST_CASESFILES:%.c=%.exe)))
#targets for each set of source files
.PHONY: build-source
build-source: $(TEST_OUTDIR) $(TEST_OBJDIR) $(addprefix $(TEST_OBJDIR)/,$(notdir $(TEST_SOURCEFILES:.c=.o)))
.PHONY: build-cases
build-cases: $(TEST_OUTDIR) $(TEST_OBJDIR) $(addprefix $(TEST_OBJDIR)/,$(notdir $(TEST_CASESFILES:.c=.o)))
#util targets
$(TEST_OUTDIR):
mkdir $(TEST_OUTDIR)
$(TEST_OBJDIR):
mkdir $(TEST_OBJDIR)
#compilation step
$(TEST_OBJDIR)/%.o: $(TEST_SOURCEDIR)/%.c
$(CC) -c -o $@ $< $(addprefix -I,$(TEST_SOURCEDIR)) $(CFLAGS) -fdata-sections -ffunction-sections
$(TEST_OBJDIR)/%.o: $(TEST_CASESDIR)/%.c
$(CC) -c -o $@ $< $(addprefix -I,$(TEST_SOURCEDIR) $(TEST_CASESDIR)) $(CFLAGS) -fdata-sections -ffunction-sections
#final linking step (with extra flags to strip dead code)
$(TEST_OUTDIR)/%.exe: $(TEST_OBJDIR)/%.o
@$(CC) -o $@ $(shell find $(TEST_OBJDIR) -name '*.o') $(CFLAGS) $(LIBS) -Wl,--gc-sections
$@
#util commands
.PHONY: clean
clean:
ifeq ($(shell uname),Linux)
find . -type f -name '*.o' -delete
find . -type f -name '*.a' -delete
find . -type f -name '*.exe' -delete
find . -type f -name '*.dll' -delete
find . -type f -name '*.lib' -delete
find . -type f -name '*.so' -delete
find . -type f -name '*.dylib' -delete
find . -type d -name 'out' -delete
find . -type d -name 'obj' -delete
else ifeq ($(OS),Windows_NT)
del *.o *.a *.exe *.dll *.lib *.so *.dylib
del /s out
del /s obj
else ifeq ($(shell uname),Darwin)
find . -type f -name '*.o' -delete
find . -type f -name '*.a' -delete
find . -type f -name '*.exe' -delete
find . -type f -name '*.dll' -delete
find . -type f -name '*.lib' -delete
find . -type f -name '*.so' -delete
find . -type f -name '*.dylib' -delete
find . -type d -name 'out' -delete
find . -type d -name 'obj' -delete
else
@echo "Deletion failed - what platform is this?"
endif