From e0661f9f8f6b68632b9427f8345b7999293a0bd7 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 3 Aug 2022 14:56:15 +0000 Subject: [PATCH] Run clang-format on project --- src/Lexer.cpp | 2 +- src/Parser.cpp | 20 +++++++-------- src/Result.h | 66 +++++++++++++++++++++++++++++--------------------- 3 files changed, 50 insertions(+), 38 deletions(-) diff --git a/src/Lexer.cpp b/src/Lexer.cpp index 817d39a..1bb23b3 100644 --- a/src/Lexer.cpp +++ b/src/Lexer.cpp @@ -171,7 +171,7 @@ TokenStream Lexer::lex(const std::string& text) result.push_back(Token::make_with_line({TT_Exclamation, loc}, current_line_text)); break; case '\377': - result.push_back(Token(TT_EOF,loc)); + result.push_back(Token(TT_EOF, loc)); return result; default: Error::throw_error(loc, current_line_text, "unknown character"); diff --git a/src/Parser.cpp b/src/Parser.cpp index dcefa17..d15f482 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -16,7 +16,7 @@ std::shared_ptr Parser::parse() { advance(); auto result = expr(); - if(result.is_error()) result.ethrow(); + if (result.is_error()) result.ethrow(); return result.get(); } @@ -37,28 +37,28 @@ Result Parser::factor() if (token.tk_type == TT_Number) { advance(); - return Ok(new IntegerNode(token.int_value),&token); + return Ok(new IntegerNode(token.int_value), &token); } if (token.tk_type == TT_Float) { advance(); - return Ok(new FloatNode(token.float_value),&token); + return Ok(new FloatNode(token.float_value), &token); } - - return Err("expected a number",&token); + + return Err("expected a number", &token); } Result Parser::term() { Result left = factor(); - if(left.is_error()) return left; + if (left.is_error()) return left; while (current_token->tk_type == TT_Mul || current_token->tk_type == TT_Div) { Token& op = *current_token; advance(); Result right = factor(); - if(right.is_error()) return right; + if (right.is_error()) return right; left = Ok(new MulNode(left.get(), right.get(), op.tk_type == TT_Mul ? '*' : '/'), &op); } return left; @@ -67,14 +67,14 @@ Result Parser::term() Result Parser::expr() { Result left = term(); - if(left.is_error()) return left; + if (left.is_error()) return left; while (current_token->tk_type == TT_Plus || current_token->tk_type == TT_Minus) { Token& op = *current_token; advance(); Result right = term(); - if(right.is_error()) return right; - left = Ok(new SumNode(left.get(), right.get(), op.tk_type == TT_Plus ? '+' : '-'),&op); + if (right.is_error()) return right; + left = Ok(new SumNode(left.get(), right.get(), op.tk_type == TT_Plus ? '+' : '-'), &op); } return left; } diff --git a/src/Result.h b/src/Result.h index 2c3aea6..c61f7f8 100644 --- a/src/Result.h +++ b/src/Result.h @@ -1,40 +1,52 @@ #pragma once -#include "Token.h" #include "Error.h" +#include "Token.h" // Kinda copying Rust here, but it's a great idea. -template -class Result +template class Result { -public: - Result() = default; - bool is_error() const { return m_is_error; } - void ethrow() { Error::throw_error(m_token->loc,m_token->line(),m_error); } - std::shared_ptr get() - { - return m_result; - } -protected: - Token* m_token; - std::shared_ptr m_result; - bool m_is_error; - std::string m_error; - - Result(T* result, Token* token) : m_result(result), m_token(token), m_is_error(false) {} - Result(const std::string& error, Token* token) : m_error(std::move(error)), m_token(token), m_is_error(true) {} + public: + Result() = default; + bool is_error() const + { + return m_is_error; + } + void ethrow() + { + Error::throw_error(m_token->loc, m_token->line(), m_error); + } + std::shared_ptr get() + { + return m_result; + } + + protected: + Token* m_token; + std::shared_ptr m_result; + bool m_is_error; + std::string m_error; + + Result(T* result, Token* token) : m_result(result), m_token(token), m_is_error(false) + { + } + Result(const std::string& error, Token* token) : m_error(std::move(error)), m_token(token), m_is_error(true) + { + } }; -template -class Ok final : public Result +template class Ok final : public Result { -public: - Ok(T* result, Token* token) : Result(result,token) {} + public: + Ok(T* result, Token* token) : Result(result, token) + { + } }; -template -class Err final : public Result +template class Err final : public Result { -public: - Err(const std::string& error, Token* token) : Result(std::move(error),token) {} + public: + Err(const std::string& error, Token* token) : Result(std::move(error), token) + { + } }; \ No newline at end of file