diff --git a/src/AST/NumberNode.h b/src/AST/NumberNode.h index 9b420e4..d66424d 100644 --- a/src/AST/NumberNode.h +++ b/src/AST/NumberNode.h @@ -7,4 +7,39 @@ class NumberNode : public ExprNode public: NumberNode(); ~NumberNode(); -}; \ No newline at end of file + + virtual bool is_floating() + { + return false; + } + virtual bool is_integral() + { + return false; + } +}; + +template class BasicNumberNode : public NumberNode +{ + T value; + + public: + BasicNumberNode(T value) : value(value) + { + } + ~BasicNumberNode() + { + } + + constexpr bool is_floating() override + { + return std::is_floating_point::value; + } + + constexpr bool is_integral() override + { + return std::is_integral::value; + } +}; + +using IntegerNode = BasicNumberNode; +using FloatNode = BasicNumberNode; \ No newline at end of file diff --git a/src/Parser.cpp b/src/Parser.cpp index 465f10e..4e53d9f 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -10,39 +10,51 @@ Parser::~Parser() std::shared_ptr Parser::new_parser(const TokenStream& tokens) { - return std::shared_ptr( - new Parser(tokens)); // As always, not using std::make_shared 'cause constructor is private + return std::shared_ptr( + new Parser(tokens)); // As always, not using std::make_shared 'cause constructor is private } std::shared_ptr Parser::parse() { - auto result = walk_expr(); + auto result = walk_expr(); - if (result.is_error()) - { - result.ethrow(); - } + if (result.is_error()) + { + result.ethrow(); + } - return result.get(); + return result.get(); } Parser::ErrorOr Parser::walk_expr() { - return ErrorOr(new ExprNode()); // constructor does not want to accept a shared_ptr in the argument - // list, thats why im not using make_shared here + return ErrorOr(new ExprNode()); // constructor does not want to accept a shared_ptr in the argument + // list, thats why im not using make_shared here } Parser::ErrorOr Parser::walk_number() { - return ErrorOr(new NumberNode()); + Token& current_token = tokens[m_index++]; + if (current_token.tk_type == TT_Number) + { + return ErrorOr(new IntegerNode(current_token.int_value)); + } + else if (current_token.tk_type == TT_Float) + { + return ErrorOr(new FloatNode(current_token.float_value)); + } + else + { + return ErrorOr("expected a number", current_token); + } } void Parser::save_current_position() { - saved_m_index = m_index; + saved_m_index = m_index; } void Parser::restore_current_position() { - m_index = saved_m_index; + m_index = saved_m_index; }