Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bit operations tokenization is implemented #14

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ project(etude)

include(FetchContent)

function(fetch_package pakcage_name git_link git_tag)
find_package("${pakcage_name}" QUIET)

if (NOT "${${pakcage_name}_FOUND}")

FetchContent_Declare("${pakcage_name}"
GIT_REPOSITORY "${git_link}"
GIT_TAG "${git_tag}"
)

FetchContent_MakeAvailable("${pakcage_name}")

endif (NOT "${${pakcage_name}_FOUND}")
endfunction()

# --------------------------------------------------------------------

set(CMAKE_CXX_STANDARD 20)
Expand All @@ -15,20 +30,7 @@ add_compile_options(-Wall -Wextra -g -Og -fsanitize=undefined)

# --------------------------------------------------------------------

find_package(fmt QUIET)

if (NOT fmt_FOUND)

FetchContent_Declare(fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG master
)

FetchContent_MakeAvailable(fmt)

endif (NOT fmt_FOUND)

# find_package(Catch2 2 REQUIRED)
fetch_package(fmt https://github.com/fmtlib/fmt.git master)

# --------------------------------------------------------------------

Expand Down
4 changes: 3 additions & 1 deletion src/lex/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ std::optional<TokenType> Lexer::MatchOperator() {
scanner_.MoveRight();
return TokenType::ARROW_CAST;
} else {
std::abort();
return TokenType::TILDE;
}

case '+':
Expand Down Expand Up @@ -180,6 +180,8 @@ std::optional<TokenType> Lexer::MatchOperator() {
return TokenType::ADDR;
case '|':
return TokenType::BIT_OR;
case '^':
return TokenType::BIT_XOR;
case '(':
return TokenType::LEFT_PAREN;
case ')':
Expand Down
2 changes: 2 additions & 0 deletions src/lex/token_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ namespace lex {
code(IF) \
code(MATCH) \
code(BIT_OR) \
code(BIT_XOR) \
code(TILDE) \
code(THEN) \
code(ELSE) \
code(COLON) \
Expand Down
4 changes: 3 additions & 1 deletion src/lex/token_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ enum class TokenType {
STRUCT,
SUM,
UNION,

OF,
FOR,
IMPL,
Expand All @@ -84,6 +84,8 @@ enum class TokenType {
IF,
MATCH,
BIT_OR,
BIT_XOR,
TILDE,
THEN,
ELSE,

Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
fetch_package(Catch2 https://github.com/catchorg/Catch2.git devel)

message(STATUS "Generating tests")

get_filename_component(TESTS_PATH "." ABSOLUTE)
Expand Down
13 changes: 13 additions & 0 deletions tests/lex/cases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ TEST_CASE("Statement", "[lex]") {

//////////////////////////////////////////////////////////////////////

TEST_CASE("Bit operations", "[lex]") {
std::stringstream source("~ ^ & |");
lex::Lexer l{source};

CHECK(l.Matches(lex::TokenType::TILDE));
CHECK(l.Matches(lex::TokenType::BIT_XOR));
CHECK(l.Matches(lex::TokenType::ADDR));
CHECK(l.Matches(lex::TokenType::BIT_OR));
CHECK(l.Matches(lex::TokenType::TOKEN_EOF));
}

//////////////////////////////////////////////////////////////////////

TEST_CASE("String literal", "[lex]") {
std::stringstream source("\"Hello world\"");
lex::Lexer l{source};
Expand Down