added parsing of if statements
This commit is contained in:
23
src/ast/expressions/if_then_else.cpp
Normal file
23
src/ast/expressions/if_then_else.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
#include "if_then_else.hpp"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace ast {
|
||||
if_then_else::if_then_else(token::token token)
|
||||
: token(std::move(token)),
|
||||
consequence(nullptr),
|
||||
alternative(nullptr) {};
|
||||
|
||||
std::string if_then_else::token_literal() const {
|
||||
return token.literal;
|
||||
};
|
||||
|
||||
std::string if_then_else::str() const {
|
||||
std::stringstream ss;
|
||||
ss << "if " << condition->str() << consequence->str();
|
||||
if (alternative != nullptr)
|
||||
ss << "else" << alternative->str();
|
||||
return ss.str();
|
||||
};
|
||||
} // namespace ast
|
19
src/ast/expressions/if_then_else.hpp
Normal file
19
src/ast/expressions/if_then_else.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "ast/ast.hpp"
|
||||
#include "ast/statements/block.hpp"
|
||||
#include "token/token.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace ast {
|
||||
struct if_then_else : expression {
|
||||
if_then_else(token::token);
|
||||
token::token token;
|
||||
ast::expression* condition;
|
||||
ast::block_stmt *consequence, *alternative;
|
||||
|
||||
std::string token_literal() const override;
|
||||
std::string str() const override;
|
||||
};
|
||||
} // namespace ast
|
25
src/ast/statements/block.cpp
Normal file
25
src/ast/statements/block.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include "block.hpp"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace ast {
|
||||
block_stmt::block_stmt(token::token token): token(std::move(token)) {}
|
||||
|
||||
std::string block_stmt::token_literal() const {
|
||||
return token.literal;
|
||||
}
|
||||
|
||||
block_stmt::~block_stmt() {
|
||||
for (const auto& stmt : statements)
|
||||
delete stmt;
|
||||
};
|
||||
|
||||
std::string block_stmt::str() const {
|
||||
std::stringstream ss;
|
||||
ss << "{";
|
||||
for (const auto& stmt : statements)
|
||||
ss << stmt->str();
|
||||
ss << "}";
|
||||
return ss.str();
|
||||
};
|
||||
} // namespace ast
|
20
src/ast/statements/block.hpp
Normal file
20
src/ast/statements/block.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "ast/ast.hpp"
|
||||
#include "token/token.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace ast {
|
||||
struct block_stmt : statement {
|
||||
block_stmt(token::token token);
|
||||
|
||||
token::token token;
|
||||
std::vector<ast::statement*> statements;
|
||||
|
||||
std::string token_literal() const override;
|
||||
std::string str() const override;
|
||||
|
||||
~block_stmt();
|
||||
};
|
||||
} // namespace ast
|
Reference in New Issue
Block a user