Skip to content

Commit

Permalink
use format lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Mivik committed Aug 24, 2021
1 parent 282f359 commit 5e246a9
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 93 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "external/fmt"]
path = external/fmt
url = https://github.com/fmtlib/fmt.git
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ cmake_minimum_required(VERSION 3.16)
project(rin)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

enable_testing()

add_subdirectory(external/fmt)
include_directories(external/fmt/include)

find_package(LLVM REQUIRED CONFIG)

add_definitions(${LLVM_DEFINITIONS})
Expand Down
1 change: 1 addition & 0 deletions external/fmt
Submodule fmt added at 34caec
12 changes: 3 additions & 9 deletions include/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace rin {

class ParseException;

class ASTNode {
protected:
explicit ASTNode(const SourceRange &range): range(range) {}
Expand Down Expand Up @@ -96,15 +98,7 @@ class VarDeclNode : public ASTNode {
Ptr<ASTNode> type_node,
Ptr<ASTNode> value_node,
Type var_type
): ASTNode(range), name(std::move(name)),
type_node(std::move(type_node)),
value_node(std::move(value_node)),
var_type(var_type) {
if (!(this->type_node || this->value_node))
throw CodegenException("A variable should have either a default value or a type annotation");
if ((var_type == Type::CONST || var_type == Type::VAL) && !this->value_node)
throw CodegenException("The default value of const variable should be given");
}
);

[[nodiscard]] const std::string &get_name() const { return name; }
[[nodiscard]] const ASTNode *get_value_node() const { return value_node.get(); }
Expand Down
13 changes: 11 additions & 2 deletions include/codegen.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#pragma once

#include "fmt/format.h"

#include <llvm/IR/IRBuilder.h>

#include "context.h"
Expand All @@ -14,10 +16,12 @@ class Function;
class CodegenException : public std::exception {
public:
[[nodiscard]] const char *what() const noexcept override { return msg.data(); }

explicit CodegenException(std::string msg): msg(std::move(msg)) {}
private:
explicit CodegenException(std::string msg): msg(std::move(msg)) {}

std::string msg;

friend class Codegen;
};

class Codegen {
Expand All @@ -32,6 +36,11 @@ class Codegen {
Function::Static *get_function() const { return layers.back().function; }
llvm::IRBuilder<> *get_builder() const { return layers.back().builder.get(); }

template<class...Args>
[[noreturn]] void error(const char *pattern, Args&&...args) const {
throw CodegenException(fmt::format(pattern, std::forward<Args>(args)...));
}

void declare_value(const std::string &name, const Value &value) {
value_map.set(name, value);
}
Expand Down
21 changes: 14 additions & 7 deletions include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,21 @@ class Parser {
private:
Ptr<ASTNode> take_prim_inner();

template<class...Args>
[[noreturn]] void error(const char *pattern, Args &&...args) const {
throw ParseException(fmt::format(pattern, std::forward<Args>(args)...));
}

void expect_end_of_stmt() {
// TODO error message
expect(lexer.take(), TokenKind::Semicolon);
}
Token expect(const Token &&token, TokenKind kind) const {
if (token.kind != kind) // TODO unlikely
throw ParseException(
"Expected " + token_kind::name(kind) + ", "
+ "got " + token.info(get_buffer())
error(
"Expected {}, got {}",
token_kind::name(kind),
token.info(get_buffer())
);
return token;
}
Expand All @@ -61,10 +67,11 @@ class Parser {
auto token = lexer.take();
if (token.kind == delimiter) continue;
if (token.kind == end) break;
throw ParseException(
"Expect " + token_kind::name(delimiter) +
" or " + token_kind::name(end) +
", got " + token.info(get_buffer())
error(
"Expected {} or {}, got {}",
token_kind::name(delimiter),
token_kind::name(end),
token.info(get_buffer())
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ aux_source_directory(. RIN_MAIN_SOURCE_FILES)

add_library(rin SHARED ${RIN_MAIN_SOURCE_FILES})

target_link_libraries(rin LLVM-12)
target_link_libraries(rin fmt::fmt LLVM-12)
Loading

0 comments on commit 5e246a9

Please sign in to comment.