very basic parser of let statements
This commit is contained in:
9
src/ast/ast.cpp
Normal file
9
src/ast/ast.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "ast.hpp"
|
||||
|
||||
namespace ast {
|
||||
std::string program ::token_literal() const {
|
||||
if (statements.size() > 0)
|
||||
return statements[0]->token_literal();
|
||||
return "";
|
||||
}
|
||||
} // namespace ast
|
29
src/ast/ast.hpp
Normal file
29
src/ast/ast.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace ast {
|
||||
struct node {
|
||||
virtual std::string token_literal() const = 0;
|
||||
virtual ~node() = default;
|
||||
};
|
||||
|
||||
struct statement : node {
|
||||
virtual std::string token_literal() const override = 0;
|
||||
};
|
||||
|
||||
struct expression : node {
|
||||
virtual std::string token_literal() const override = 0;
|
||||
};
|
||||
|
||||
struct program : public node {
|
||||
std::vector<statement*> statements;
|
||||
std::string token_literal() const override;
|
||||
|
||||
~program() {
|
||||
for (const auto& ref : statements)
|
||||
delete ref;
|
||||
};
|
||||
};
|
||||
} // namespace ast
|
11
src/ast/expressions/identifier.cpp
Normal file
11
src/ast/expressions/identifier.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "identifier.hpp"
|
||||
|
||||
namespace ast {
|
||||
identifier::identifier(token::token token, std::string value)
|
||||
: token(std::move(token)),
|
||||
value(std::move(value)) {}
|
||||
|
||||
std::string identifier::token_literal() const {
|
||||
return token.literal;
|
||||
}
|
||||
} // namespace ast
|
16
src/ast/expressions/identifier.hpp
Normal file
16
src/ast/expressions/identifier.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "ast/ast.hpp"
|
||||
#include "token/token.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace ast {
|
||||
struct identifier : expression {
|
||||
identifier(token::token token, std::string value);
|
||||
token::token token;
|
||||
std::string value;
|
||||
|
||||
std::string token_literal() const override;
|
||||
};
|
||||
} // namespace ast
|
17
src/ast/statements/let.cpp
Normal file
17
src/ast/statements/let.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "let.hpp"
|
||||
|
||||
namespace ast {
|
||||
let::let(token::token token)
|
||||
: token(std::move(token)),
|
||||
name(nullptr),
|
||||
value(nullptr) {}
|
||||
|
||||
std::string let::token_literal() const {
|
||||
return token.literal;
|
||||
}
|
||||
|
||||
let::~let() {
|
||||
delete name;
|
||||
delete value;
|
||||
};
|
||||
} // namespace ast
|
19
src/ast/statements/let.hpp
Normal file
19
src/ast/statements/let.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "ast/ast.hpp"
|
||||
#include "ast/expressions/identifier.hpp"
|
||||
#include "token/token.hpp"
|
||||
|
||||
namespace ast {
|
||||
struct let : statement {
|
||||
let(token::token token);
|
||||
|
||||
token::token token;
|
||||
identifier* name;
|
||||
expression* value;
|
||||
|
||||
std::string token_literal() const override;
|
||||
|
||||
~let();
|
||||
};
|
||||
} // namespace ast
|
Reference in New Issue
Block a user