Skip to content
This repository has been archived by the owner on Nov 24, 2024. It is now read-only.

Commit

Permalink
improved makefile and added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
herEmanuel committed Nov 5, 2021
1 parent 1e80e4c commit 53c9790
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 72 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ Peregrine/test
*.so
*.o
temp
__pycache__
/Peregrine/peregrine
/objs
*.elf
doctest.h
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CC := g++
CFLAGS := -Wall

OUTPUT := peregrine.elf

SRC := $(shell find -name '*.cpp' ! -path './tests/*')
OBJS := $(subst .cpp,.o,$(subst Peregrine,objs,$(SRC)))

.PHONY: clean all dirs

all: $(OUTPUT)
./$(OUTPUT)

$(OUTPUT): $(OBJS)
$(CC) -o $(OUTPUT) $(OBJS)

objs/%.o: Peregrine/%.cpp | dirs
$(CC) -c $< -o $@

dirs:
mkdir -p $(shell dirname $(OBJS))

clean:
rm -rf ./objs
rm -f $(OUTPUT)
31 changes: 0 additions & 31 deletions Peregrine/Makefile

This file was deleted.

1 change: 0 additions & 1 deletion Peregrine/build.sh

This file was deleted.

14 changes: 0 additions & 14 deletions Peregrine/codegen/codegen_test.cpp

This file was deleted.

11 changes: 0 additions & 11 deletions Peregrine/lexer/lexer_test.cpp

This file was deleted.

14 changes: 14 additions & 0 deletions Peregrine/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>
#include <vector>
#include "lexer/lexer.hpp"
#include "lexer/tokens.hpp"

int main() {
std::vector<Token> res = lexer("\"Hello, world!\"", "");

for (auto& tok : res) {
std::cout << "Statement: " << tok.statement << " | Keyword: " << tok.keyword << " | Type: " << tok.tk_type << "\n";
}

return 0;
}
14 changes: 0 additions & 14 deletions Peregrine/parser/parser_test.cpp

This file was deleted.

29 changes: 29 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CC := g++

TEST_OUTPUT := compiler_test.elf

DOCTEST := compiler/doctest.h

TEST_SRC := $(shell find -name '*.cpp')
TEST_OBJS := $(subst .cpp,.o,$(TEST_SRC))

OBJS := $(shell find .. -path '../objs/*.o' ! -name 'main.o')

.PHONY: clean all

all: $(TEST_OUTPUT)
./$(TEST_OUTPUT)

$(TEST_OUTPUT): $(DOCTEST) $(TEST_OBJS) $(OBJS)
$(CC) -o $(TEST_OUTPUT) $(TEST_OBJS) $(OBJS)

$(DOCTEST):
wget https://raw.githubusercontent.com/onqtam/doctest/master/doctest/doctest.h
mv doctest.h compiler/doctest.h

%.o: %.cpp
$(CC) -I ../Peregrine -c $< -o $@

clean:
rm -f *.o
rm -f $(TEST_OUTPUT)
57 changes: 57 additions & 0 deletions tests/compiler/lexer_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "doctest.h"

#include <vector>
#include <lexer/lexer.hpp>
#include <lexer/tokens.hpp>

TEST_CASE("Tokenize basic primitives") {
std::vector<Token> res;

res = lexer("69420", "");
CHECK(res[0].tk_type == tk_integer);
CHECK(res[0].keyword == "69420");

res = lexer("5.32006", "");
CHECK(res[0].tk_type == tk_decimal);
CHECK(res[0].keyword == "5.32006");

res = lexer("\"A blazing fast language\"", "");
CHECK(res[0].tk_type == tk_string);
CHECK(res[0].keyword == std::string("A blazing fast language"));

res = lexer("True", "");
CHECK(res[0].tk_type == tk_true);
}

TEST_CASE("Tokenize variable declarations") {
std::vector<Token> res = lexer("int test = 23", "");

REQUIRE(res.size() == 5);

CHECK(res[0].tk_type == tk_identifier);
CHECK(res[1].tk_type == tk_identifier);
CHECK(res[2].tk_type == tk_assign);
CHECK(res[3].tk_type == tk_integer);
CHECK(res[4].tk_type == tk_eof);
}

TEST_CASE("Tokenize if-else statements") {
std::vector<Token> res = lexer("if test:\n print(\"true!\")\nelse:\n print(\"false!\")", "");

REQUIRE(res.size() == 18);

SUBCASE("Emit ident and dedent tokens") {
CHECK(res[3].tk_type == tk_ident);
CHECK(res[8].tk_type == tk_dedent);
CHECK(res[11].tk_type == tk_ident);
CHECK(res[16].tk_type == tk_dedent);
}

SUBCASE("Emit if and else tokens") {
CHECK(res[0].tk_type == tk_if);
CHECK(res[2].tk_type == tk_colon);

CHECK(res[9].tk_type == tk_else);
CHECK(res[10].tk_type == tk_colon);
}
}
2 changes: 2 additions & 0 deletions tests/compiler/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"

0 comments on commit 53c9790

Please sign in to comment.