Run clang-format on project

This commit is contained in:
apio 2022-08-03 14:56:15 +00:00
parent 3eea369072
commit e0661f9f8f
3 changed files with 50 additions and 38 deletions

View File

@ -171,7 +171,7 @@ TokenStream Lexer::lex(const std::string& text)
result.push_back(Token::make_with_line({TT_Exclamation, loc}, current_line_text)); result.push_back(Token::make_with_line({TT_Exclamation, loc}, current_line_text));
break; break;
case '\377': case '\377':
result.push_back(Token(TT_EOF,loc)); result.push_back(Token(TT_EOF, loc));
return result; return result;
default: default:
Error::throw_error(loc, current_line_text, "unknown character"); Error::throw_error(loc, current_line_text, "unknown character");

View File

@ -16,7 +16,7 @@ std::shared_ptr<ASTNode> Parser::parse()
{ {
advance(); advance();
auto result = expr(); auto result = expr();
if(result.is_error()) result.ethrow(); if (result.is_error()) result.ethrow();
return result.get(); return result.get();
} }
@ -37,28 +37,28 @@ Result<ExprNode> Parser::factor()
if (token.tk_type == TT_Number) if (token.tk_type == TT_Number)
{ {
advance(); advance();
return Ok<ExprNode>(new IntegerNode(token.int_value),&token); return Ok<ExprNode>(new IntegerNode(token.int_value), &token);
} }
if (token.tk_type == TT_Float) if (token.tk_type == TT_Float)
{ {
advance(); advance();
return Ok<ExprNode>(new FloatNode(token.float_value),&token); return Ok<ExprNode>(new FloatNode(token.float_value), &token);
} }
return Err<ExprNode>("expected a number",&token); return Err<ExprNode>("expected a number", &token);
} }
Result<ExprNode> Parser::term() Result<ExprNode> Parser::term()
{ {
Result<ExprNode> left = factor(); Result<ExprNode> 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) while (current_token->tk_type == TT_Mul || current_token->tk_type == TT_Div)
{ {
Token& op = *current_token; Token& op = *current_token;
advance(); advance();
Result<ExprNode> right = factor(); Result<ExprNode> right = factor();
if(right.is_error()) return right; if (right.is_error()) return right;
left = Ok<ExprNode>(new MulNode(left.get(), right.get(), op.tk_type == TT_Mul ? '*' : '/'), &op); left = Ok<ExprNode>(new MulNode(left.get(), right.get(), op.tk_type == TT_Mul ? '*' : '/'), &op);
} }
return left; return left;
@ -67,14 +67,14 @@ Result<ExprNode> Parser::term()
Result<ExprNode> Parser::expr() Result<ExprNode> Parser::expr()
{ {
Result<ExprNode> left = term(); Result<ExprNode> 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) while (current_token->tk_type == TT_Plus || current_token->tk_type == TT_Minus)
{ {
Token& op = *current_token; Token& op = *current_token;
advance(); advance();
Result<ExprNode> right = term(); Result<ExprNode> right = term();
if(right.is_error()) return right; if (right.is_error()) return right;
left = Ok<ExprNode>(new SumNode(left.get(), right.get(), op.tk_type == TT_Plus ? '+' : '-'),&op); left = Ok<ExprNode>(new SumNode(left.get(), right.get(), op.tk_type == TT_Plus ? '+' : '-'), &op);
} }
return left; return left;
} }

View File

@ -1,40 +1,52 @@
#pragma once #pragma once
#include "Token.h"
#include "Error.h" #include "Error.h"
#include "Token.h"
// Kinda copying Rust here, but it's a great idea. // Kinda copying Rust here, but it's a great idea.
template<typename T> template<typename T> class Result
class Result
{ {
public: public:
Result() = default; Result() = default;
bool is_error() const { return m_is_error; } bool is_error() const
void ethrow() { Error::throw_error(m_token->loc,m_token->line(),m_error); } {
std::shared_ptr<T> get() return m_is_error;
{ }
return m_result; void ethrow()
} {
protected: Error::throw_error(m_token->loc, m_token->line(), m_error);
Token* m_token; }
std::shared_ptr<T> m_result; std::shared_ptr<T> get()
bool m_is_error; {
std::string m_error; return m_result;
}
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) {} protected:
Token* m_token;
std::shared_ptr<T> 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<typename T> template<typename T> class Ok final : public Result<T>
class Ok final : public Result<T>
{ {
public: public:
Ok(T* result, Token* token) : Result<T>(result,token) {} Ok(T* result, Token* token) : Result<T>(result, token)
{
}
}; };
template<typename T> template<typename T> class Err final : public Result<T>
class Err final : public Result<T>
{ {
public: public:
Err(const std::string& error, Token* token) : Result<T>(std::move(error),token) {} Err(const std::string& error, Token* token) : Result<T>(std::move(error), token)
{
}
}; };