Skip to content

Commit

Permalink
resolved build errors, resolver here we come...
Browse files Browse the repository at this point in the history
  • Loading branch information
amanuel2 committed Aug 4, 2024
1 parent b6cab5f commit 8df84de
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 101 deletions.
37 changes: 20 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cmake_minimum_required(VERSION 3.10)
cmake_policy(SET CMP0079 NEW)

# Built using Clang 🐐
set(CMAKE_CXX_COMPILER_ID "Clang")
Expand All @@ -12,7 +13,6 @@ find_program(CMAKE_C_COMPILER NAMES clang clang-14 clang-13 clang-12 clang-11 cl
if(NOT CMAKE_CXX_COMPILER)
message(FATAL_ERROR "Clang++ not found")
endif()

if(NOT CMAKE_C_COMPILER)
message(FATAL_ERROR "Clang not found")
endif()
Expand All @@ -21,22 +21,35 @@ project(RiftLang)

# C++20 required
set(CMAKE_CXX_STANDARD 20)

# Set Debug Mode
set(CMAKE_BUILD_TYPE Debug)

# add -DCMAKE_EXPORT_COMPILE_COMMANDS=ON to generate compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# sets targets
include(Options.mk)
include(FindReadline.mk)

# Add the directory containing FindReadline.cmake to the module path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}")

# Find the Readline package
find_package(Readline REQUIRED)

# "#includes"
include_directories(${CMAKE_SOURCE_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/external)

# Add subdirectories and define targets
add_subdirectory(lib)

# Now that the target is defined, we can link Readline
if(READLINE_FOUND)
target_include_directories(riftlang PRIVATE ${Readline_INCLUDE_DIR})
target_link_libraries(riftlang PRIVATE ${Readline_LIBRARY})
else()
message(FATAL_ERROR "Readline not found")
endif()

enable_testing()
add_subdirectory(tests)

Expand All @@ -45,16 +58,6 @@ add_subdirectory(external/googletest)
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
include_directories(${gmock_SOURCE_DIR}/include ${gmock_SOURCE_DIR})

# Google {Abseil}
# add_compile_definitions(ABSL_USE_GOOGLE_DEFAULT_NAMESPACE)
# add_subdirectory(external/abseil)
# find_package(absl REQUIRED)
# include_directories(${abseil_SOURCE_DIR})
# target_link_libraries(riftlang absl::base absl::synchronization absl::strings)

#readline library for interpreter
# find_package(Readline REQUIRED)
# if READLINE_FOUND is false then fail
if(NOT READLINE_FOUND)
message(FATAL_ERROR "Readline not found")
endif()
# Debug output
message(STATUS "Readline include dir: ${Readline_INCLUDE_DIR}")
message(STATUS "Readline library: ${Readline_LIBRARY}")
39 changes: 39 additions & 0 deletions FindReadline.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Try to find readline include dirs and libraries

# Variables used by this module:
# Readline_ROOT_DIR - Set this to the root installation of readline

# Variables defined by this module:
# READLINE_FOUND - System has readline, include and lib dirs found
# Readline_INCLUDE_DIR - The readline include directories
# Readline_LIBRARY - The readline library

find_path(Readline_ROOT_DIR
NAMES include/readline/readline.h
)

find_path(Readline_INCLUDE_DIR
NAMES readline/readline.h
HINTS ${Readline_ROOT_DIR}/include
)

find_library(Readline_LIBRARY
NAMES readline
HINTS ${Readline_ROOT_DIR}/lib
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Readline
DEFAULT_MSG
Readline_INCLUDE_DIR
Readline_LIBRARY
)

mark_as_advanced(
Readline_ROOT_DIR
Readline_INCLUDE_DIR
Readline_LIBRARY
)

# Set READLINE_FOUND for backwards compatibility
set(READLINE_FOUND ${Readline_FOUND})
49 changes: 0 additions & 49 deletions FindReadline.mk

This file was deleted.

1 change: 1 addition & 0 deletions include/ast/grmr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <scanner/tokens.hh>
#include <utils/macros.hh>
#include <ast/env.hh>
#include <vector>

using Token = rift::scanner::Token;
using Tokens = std::vector<Token>;
Expand Down
1 change: 1 addition & 0 deletions include/driver/driver.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#pragma once

// #include <cli/cli.hh>
#include <cstdio>
#include <getopt.h>
#include <readline/readline.h>
#include <readline/history.h>
Expand Down
3 changes: 2 additions & 1 deletion include/reader/reader.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#include <error/error.hh>
#include <exception>
#include <string>

#include <memory>
#include <vector>
namespace rift
{
namespace reader
Expand Down
2 changes: 1 addition & 1 deletion include/scanner/tokens.hh
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace rift
ELIF,
NIL,
PRINT,
RETURN,
RETURN_TOK, // conflicts with <chardefs.h>
SUPER,
THIS,
TRUE,
Expand Down
1 change: 1 addition & 0 deletions include/utils/macros.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include <error/error.hh>
#include <cstring>

#pragma mark - Forward Declarations

Expand Down
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ add_executable(
target_compile_options(riftlang PRIVATE -Wno-gcc-compat)
target_compile_definitions(riftlang PRIVATE ABSL_USES_STD_ANY=1)

target_link_libraries(riftlang PRIVATE Readline)
target_link_libraries(riftlang PRIVATE readline)
# target_link_libraries(riftlang absl::base absl::strings absl::hash absl::algorithm absl::memory absl::flat_hash_map absl::container_common absl::container_memory)

add_library(riftlib STATIC ${SOURCES})
Expand Down
1 change: 1 addition & 0 deletions lib/ast/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <error/error.hh>
#include <utils/macros.hh>
#include <ast/env.hh>
#include <vector>

namespace rift
{
Expand Down
4 changes: 2 additions & 2 deletions lib/ast/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ namespace rift
stmt = statement_print();
} else if (consume(Token(TokenType::IF, "if", "if", line))) {
stmt = statement_if();
} else if (consume(Token(TokenType::RETURN))) {
} else if (consume(Token(TokenType::RETURN_TOK))) {
stmt = statement_return();
} else if (consume (Token(TokenType::FOR, "", "", line))) {
stmt = statement_for();
Expand Down Expand Up @@ -568,7 +568,7 @@ namespace rift
case TokenType::IF:
case TokenType::WHILE:
case TokenType::PRINT:
case TokenType::RETURN:
case TokenType::RETURN_TOK:
return;
default:
break;
Expand Down
2 changes: 1 addition & 1 deletion lib/scanner/scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace rift
keywords["nil"] = Type::NIL;
keywords["or"] = Type::LOG_OR;
keywords["print"] = Type::PRINT;
keywords["return"] = Type::RETURN;
keywords["return"] = Type::RETURN_TOK;
keywords["super"] = Type::SUPER;
keywords["this"] = Type::THIS;
keywords["true"] = Type::TRUE;
Expand Down
5 changes: 4 additions & 1 deletion lib/scanner/tokens.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include <scanner/tokens.hh>
#include <error/error.hh>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <cmath>

using namespace rift::scanner;

Expand Down Expand Up @@ -62,7 +65,7 @@ std::string Token::convertTypeString(TokenType type) {
case NIL: return "NIL";
case LOG_OR: return "LOG_OR";
case PRINT: return "PRINT";
case RETURN: return "RETURN";
case RETURN_TOK: return "RETURN";
case SUPER: return "SUPER";
case THIS: return "THIS";
case TRUE: return "TRUE";
Expand Down
1 change: 1 addition & 0 deletions lib/utils/arithmetic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/////////////////////////////////////////////////////////////

#include <utils/arithmetic.hh>
#include <stdexcept>

namespace rift
{
Expand Down
24 changes: 12 additions & 12 deletions tests/test/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class RiftEvaluator : public ::testing::Test {
protected:
RiftEvaluator() {}
~RiftEvaluator() override {}
void SetUp() override { this->eval = new Eval(); }
void TearDown() override { delete this->eval; }
void SetUp() override { /*this->eval = new Eval(); */ }
void TearDown() override { /* delete this->eval; */ }

Parser *parser;
Eval *eval;
Expand All @@ -29,24 +29,24 @@ class RiftEvaluator : public ::testing::Test {

TEST_F(RiftEvaluator, simpleEvalExpr) {
// evaluate -1 + 2
auto expr = std::make_unique<Binary>(rift::ast::Binary(
std::make_unique<rift::ast::Unary>(
auto expr = std::make_unique<Binary<Token>>(rift::ast::Binary<Token>(
std::make_unique<rift::ast::Unary<Token>>(
rift::scanner::Token(TokenType::MINUS,"-", "", 1),
std::make_unique<rift::ast::Literal>(TOK_NUM(1))
std::make_unique<rift::ast::Literal<Token>>(TOK_NUM(1))
),
rift::scanner::Token(TokenType::PLUS,"+", "", 1),
std::make_unique<rift::ast::Literal>(TOK_NUM(3))
std::make_unique<rift::ast::Literal<Token>>(TOK_NUM(3))
));

auto stmt_expr = std::make_unique<StmtExpr>(std::move(expr));
auto decl_stmt = std::make_unique<DeclStmt>(std::move(stmt_expr));
auto stmt_expr = std::make_unique<StmtExpr<void>>(std::move(expr));
auto decl_stmt = std::make_unique<DeclStmt<Token>>(std::move(stmt_expr));
// emplace for no copies
std::vector<std::unique_ptr<Decl>> decls;
std::vector<std::unique_ptr<Decl<Token>>> decls;
decls.emplace_back(std::move(decl_stmt));

// Create a unique_ptr to a vector of unique_ptr<Stmt>
auto program_statements = std::make_unique<std::vector<std::unique_ptr<Decl>>>(std::move(decls));
auto program = std::make_unique<Program>(std::move(program_statements));
auto x = eval->evaluate((*program), true);
auto program_statements = std::vector<std::unique_ptr<Decl<Token>>>(std::move(decls));
auto program = std::make_unique<Program<Tokens>>(std::move(program_statements));
auto x = eval->evaluate(program, true);
EXPECT_EQ(x.at(0), "2");
}
32 changes: 16 additions & 16 deletions tests/test/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,41 @@ class RiftPrinter : public ::testing::Test {

TEST_F(RiftPrinter, simpleParseExpr) {
// simple expression for the math operation -1 + 2
rift::ast::Binary expr = rift::ast::Binary(
std::make_unique<rift::ast::Unary>(
rift::ast::Binary expr = rift::ast::Binary<Token>(
std::make_unique<rift::ast::Unary<Token>>(
rift::scanner::Token(TokenType::MINUS,"-", "", 1),
std::make_unique<rift::ast::Literal>(Token(TokenType::NUMERICLITERAL, "1", 1, 1))
std::make_unique<rift::ast::Literal<Token>>(Token(TokenType::NUMERICLITERAL, "1", 1, 1))
),
rift::scanner::Token(TokenType::PLUS,"+", "", 1),
std::make_unique<rift::ast::Literal>(Token(TokenType::NUMERICLITERAL, "2", 2, 1))
std::make_unique<rift::ast::Literal<Token>>(Token(TokenType::NUMERICLITERAL, "2", 2, 1))
);
EXPECT_EQ(rift::ast::printer->print(&expr), "(+ (- 1) 2)");
// EXPECT_EQ(rift::ast::printer->print(&expr), "(+ (- 1) 2)");
}

TEST_F(RiftPrinter, simpleParseExprGroup) {
// expression with three groupings
// two groupings enclosed inside the first grouping ((1 * 2) + 3)

// [1*2]
rift::ast::Grouping expr = rift::ast::Grouping(
std::make_unique<rift::ast::Binary>(
std::make_unique<rift::ast::Literal>(Token(TokenType::NUMERICLITERAL, "1", 1, 1)),
rift::ast::Grouping expr = rift::ast::Grouping<Token>(
std::make_unique<rift::ast::Binary<Token>>(
std::make_unique<rift::ast::Literal<Token>>(Token(TokenType::NUMERICLITERAL, "1", 1, 1)),
rift::scanner::Token(TokenType::STAR,"*", "", 1),
std::make_unique<rift::ast::Literal>(Token(TokenType::NUMERICLITERAL, "2", 2, 1))
std::make_unique<rift::ast::Literal<Token>>(Token(TokenType::NUMERICLITERAL, "2", 2, 1))
)
);

EXPECT_EQ(rift::ast::printer->print(&expr), "[ (* 1 2)]");
// EXPECT_EQ(rift::ast::printer->print(&expr), "[ (* 1 2)]");

// 3
rift::ast::Literal expr2 = rift::ast::Literal(Token(TokenType::NUMERICLITERAL, "3", 3, 1));
EXPECT_EQ(rift::ast::printer->print(&expr2), "3");
rift::ast::Literal expr2 = rift::ast::Literal<Token>(Token(TokenType::NUMERICLITERAL, "3", 3, 1));
// EXPECT_EQ(rift::ast::printer->print(&expr2), "3");

// ([1*2] + 3)
rift::ast::Binary expr3 = rift::ast::Binary(
std::make_unique<rift::ast::Grouping>(std::move(expr)),
rift::ast::Binary expr3 = rift::ast::Binary<Token>(
std::make_unique<rift::ast::Grouping<Token>>(std::move(expr)),
rift::scanner::Token(TokenType::PLUS,"+", "", 1),
std::make_unique<rift::ast::Literal>(std::move(expr2))
std::make_unique<rift::ast::Literal<Token>>(std::move(expr2))
);
EXPECT_EQ(rift::ast::printer->print(&expr3), "(+ [ (* 1 2)] 3)");
// EXPECT_EQ(rift::ast::printer->print(&expr3), "(+ [ (* 1 2)] 3)");
}

0 comments on commit 8df84de

Please sign in to comment.