Run clang-format on project
This commit is contained in:
parent
3eea369072
commit
e0661f9f8f
@ -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");
|
||||
|
@ -16,7 +16,7 @@ std::shared_ptr<ASTNode> 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<ExprNode> Parser::factor()
|
||||
if (token.tk_type == TT_Number)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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> 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<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);
|
||||
}
|
||||
return left;
|
||||
@ -67,14 +67,14 @@ Result<ExprNode> Parser::term()
|
||||
Result<ExprNode> Parser::expr()
|
||||
{
|
||||
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)
|
||||
{
|
||||
Token& op = *current_token;
|
||||
advance();
|
||||
Result<ExprNode> right = term();
|
||||
if(right.is_error()) return right;
|
||||
left = Ok<ExprNode>(new SumNode(left.get(), right.get(), op.tk_type == TT_Plus ? '+' : '-'),&op);
|
||||
if (right.is_error()) return right;
|
||||
left = Ok<ExprNode>(new SumNode(left.get(), right.get(), op.tk_type == TT_Plus ? '+' : '-'), &op);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
46
src/Result.h
46
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<typename T>
|
||||
class Result
|
||||
template<typename T> class Result
|
||||
{
|
||||
public:
|
||||
public:
|
||||
Result() = default;
|
||||
bool is_error() const { return m_is_error; }
|
||||
void ethrow() { Error::throw_error(m_token->loc,m_token->line(),m_error); }
|
||||
bool is_error() const
|
||||
{
|
||||
return m_is_error;
|
||||
}
|
||||
void ethrow()
|
||||
{
|
||||
Error::throw_error(m_token->loc, m_token->line(), m_error);
|
||||
}
|
||||
std::shared_ptr<T> get()
|
||||
{
|
||||
return m_result;
|
||||
}
|
||||
protected:
|
||||
|
||||
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) {}
|
||||
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>
|
||||
class Ok final : public Result<T>
|
||||
template<typename T> class Ok final : public Result<T>
|
||||
{
|
||||
public:
|
||||
Ok(T* result, Token* token) : Result<T>(result,token) {}
|
||||
public:
|
||||
Ok(T* result, Token* token) : Result<T>(result, token)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class Err final : public Result<T>
|
||||
template<typename T> class Err final : public Result<T>
|
||||
{
|
||||
public:
|
||||
Err(const std::string& error, Token* token) : Result<T>(std::move(error),token) {}
|
||||
public:
|
||||
Err(const std::string& error, Token* token) : Result<T>(std::move(error), token)
|
||||
{
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user