Finally, some real progress on the AST!!

This commit is contained in:
apio 2022-07-11 11:59:02 +02:00
parent 73f3f20a71
commit 98b14e6bf5
2 changed files with 61 additions and 14 deletions

View File

@ -7,4 +7,39 @@ class NumberNode : public ExprNode
public: public:
NumberNode(); NumberNode();
~NumberNode(); ~NumberNode();
};
virtual bool is_floating()
{
return false;
}
virtual bool is_integral()
{
return false;
}
};
template<typename T> class BasicNumberNode : public NumberNode
{
T value;
public:
BasicNumberNode(T value) : value(value)
{
}
~BasicNumberNode()
{
}
constexpr bool is_floating() override
{
return std::is_floating_point<T>::value;
}
constexpr bool is_integral() override
{
return std::is_integral<T>::value;
}
};
using IntegerNode = BasicNumberNode<int>;
using FloatNode = BasicNumberNode<float>;

View File

@ -10,39 +10,51 @@ Parser::~Parser()
std::shared_ptr<Parser> Parser::new_parser(const TokenStream& tokens) std::shared_ptr<Parser> Parser::new_parser(const TokenStream& tokens)
{ {
return std::shared_ptr<Parser>( return std::shared_ptr<Parser>(
new Parser(tokens)); // As always, not using std::make_shared 'cause constructor is private new Parser(tokens)); // As always, not using std::make_shared 'cause constructor is private
} }
std::shared_ptr<ASTNode> Parser::parse() std::shared_ptr<ASTNode> Parser::parse()
{ {
auto result = walk_expr(); auto result = walk_expr();
if (result.is_error()) if (result.is_error())
{ {
result.ethrow(); result.ethrow();
} }
return result.get(); return result.get();
} }
Parser::ErrorOr<ExprNode> Parser::walk_expr() Parser::ErrorOr<ExprNode> Parser::walk_expr()
{ {
return ErrorOr<ExprNode>(new ExprNode()); // constructor does not want to accept a shared_ptr<T> in the argument return ErrorOr<ExprNode>(new ExprNode()); // constructor does not want to accept a shared_ptr<T> in the argument
// list, thats why im not using make_shared here // list, thats why im not using make_shared here
} }
Parser::ErrorOr<NumberNode> Parser::walk_number() Parser::ErrorOr<NumberNode> Parser::walk_number()
{ {
return ErrorOr<NumberNode>(new NumberNode()); Token& current_token = tokens[m_index++];
if (current_token.tk_type == TT_Number)
{
return ErrorOr<NumberNode>(new IntegerNode(current_token.int_value));
}
else if (current_token.tk_type == TT_Float)
{
return ErrorOr<NumberNode>(new FloatNode(current_token.float_value));
}
else
{
return ErrorOr<NumberNode>("expected a number", current_token);
}
} }
void Parser::save_current_position() void Parser::save_current_position()
{ {
saved_m_index = m_index; saved_m_index = m_index;
} }
void Parser::restore_current_position() void Parser::restore_current_position()
{ {
m_index = saved_m_index; m_index = saved_m_index;
} }