Skip to content
Snippets Groups Projects
Commit e9e28298 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add ofstream support within the language

On the way rewrite OStream handling: `<<` and `>>` are now treated as
standard binary operators.

This change required the introduction of "global" variable created by
module loading since now: cout, cerr and clog are no more keywords but
variables of type `ostream`.
parent 579eb523
No related branches found
No related tags found
1 merge request!111Add ofstream support within the language
Showing
with 186 additions and 77 deletions
...@@ -126,11 +126,7 @@ struct BREAK : seq < break_kw, ignored > {}; ...@@ -126,11 +126,7 @@ struct BREAK : seq < break_kw, ignored > {};
struct continue_kw : TAO_PEGTL_KEYWORD("continue") {}; struct continue_kw : TAO_PEGTL_KEYWORD("continue") {};
struct CONTINUE : seq < continue_kw, ignored > {}; struct CONTINUE : seq < continue_kw, ignored > {};
struct cout_kw : TAO_PEGTL_KEYWORD("cout") {}; struct keyword : sor < basic_type, import_kw, true_kw, false_kw, let_kw, do_kw, while_kw, for_kw, if_kw, else_kw, and_kw, or_kw, xor_kw, not_kw, break_kw, continue_kw> {};
struct cerr_kw : TAO_PEGTL_KEYWORD("cerr") {};
struct clog_kw : TAO_PEGTL_KEYWORD("clog") {};
struct keyword : sor < basic_type, import_kw, true_kw, false_kw, let_kw, do_kw, while_kw, for_kw, if_kw, else_kw, and_kw, or_kw, xor_kw, not_kw, break_kw, continue_kw, cout_kw, cerr_kw, clog_kw > {};
struct identifier_minus_keyword : minus< identifier, keyword > {}; struct identifier_minus_keyword : minus< identifier, keyword > {};
...@@ -218,7 +214,9 @@ struct product : list_must< unary_expression, sor< multiply_op, divide_op > > {} ...@@ -218,7 +214,9 @@ struct product : list_must< unary_expression, sor< multiply_op, divide_op > > {}
struct sum : list_must< product, sor< plus_op, minus_op > > {}; struct sum : list_must< product, sor< plus_op, minus_op > > {};
struct compare : list_must<sum, sor< lesser_or_eq_op, greater_or_eq_op, lesser_op, greater_op > >{}; struct shift : list_must<sum, sor< shift_left_op, shift_right_op > >{};
struct compare : list_must<shift, sor< lesser_or_eq_op, greater_or_eq_op, lesser_op, greater_op > >{};
struct equality : list_must< compare, sor< eqeq_op, not_eq_op > >{}; struct equality : list_must< compare, sor< eqeq_op, not_eq_op > >{};
...@@ -289,15 +287,11 @@ struct for_statement_block; ...@@ -289,15 +287,11 @@ struct for_statement_block;
struct for_statement : if_must< FOR, open_parent, for_init, SEMICOL, for_test, SEMICOL, for_post, close_parent, for_statement_block >{}; struct for_statement : if_must< FOR, open_parent, for_init, SEMICOL, for_test, SEMICOL, for_post, close_parent, for_statement_block >{};
struct ostream_object : seq< sor< cout_kw, cerr_kw, clog_kw >, ignored >{};
struct ostream_statement : seq< ostream_object, star< if_must< shift_left_op, expression, ignored > > >{};
struct instruction struct instruction
: sor<if_statement, : sor<if_statement,
if_must<do_while_statement, semicol>, if_must<do_while_statement, semicol>,
while_statement, while_statement,
for_statement, for_statement,
if_must< ostream_statement, semicol >,
if_must< BREAK, semicol >, if_must< BREAK, semicol >,
if_must< CONTINUE, semicol >, if_must< CONTINUE, semicol >,
block, block,
......
...@@ -214,20 +214,6 @@ struct ASTBuilder::simplify_for_post : TAO_PEGTL_NAMESPACE::parse_tree::apply<AS ...@@ -214,20 +214,6 @@ struct ASTBuilder::simplify_for_post : TAO_PEGTL_NAMESPACE::parse_tree::apply<AS
} }
}; };
struct ASTBuilder::simplify_stream_statement
: TAO_PEGTL_NAMESPACE::parse_tree::apply<ASTBuilder::simplify_stream_statement>
{
template <typename... States>
static void
transform(std::unique_ptr<ASTNode>& n, States&&...)
{
for (size_t i = 1; i < n->children.size(); ++i) {
n->children[0]->children.emplace_back(std::move(n->children[i]));
}
n = std::move(n->children[0]);
}
};
template <typename Rule> template <typename Rule>
using selector = TAO_PEGTL_NAMESPACE::parse_tree::selector< using selector = TAO_PEGTL_NAMESPACE::parse_tree::selector<
Rule, Rule,
...@@ -248,9 +234,6 @@ using selector = TAO_PEGTL_NAMESPACE::parse_tree::selector< ...@@ -248,9 +234,6 @@ using selector = TAO_PEGTL_NAMESPACE::parse_tree::selector<
language::vector_type, language::vector_type,
language::matrix_type, language::matrix_type,
language::string_type, language::string_type,
language::cout_kw,
language::cerr_kw,
language::clog_kw,
language::var_declaration, language::var_declaration,
language::fct_declaration, language::fct_declaration,
language::type_mapping, language::type_mapping,
...@@ -269,6 +252,7 @@ using selector = TAO_PEGTL_NAMESPACE::parse_tree::selector< ...@@ -269,6 +252,7 @@ using selector = TAO_PEGTL_NAMESPACE::parse_tree::selector<
language::equality, language::equality,
language::compare, language::compare,
language::sum, language::sum,
language::shift,
language::product, language::product,
language::affectation, language::affectation,
language::expression>, language::expression>,
...@@ -282,6 +266,8 @@ using selector = TAO_PEGTL_NAMESPACE::parse_tree::selector< ...@@ -282,6 +266,8 @@ using selector = TAO_PEGTL_NAMESPACE::parse_tree::selector<
language::name_subscript_expression>, language::name_subscript_expression>,
TAO_PEGTL_NAMESPACE::parse_tree::remove_content::on<language::plus_op, TAO_PEGTL_NAMESPACE::parse_tree::remove_content::on<language::plus_op,
language::minus_op, language::minus_op,
language::shift_left_op,
language::shift_right_op,
language::multiply_op, language::multiply_op,
language::divide_op, language::divide_op,
language::lesser_op, language::lesser_op,
...@@ -308,8 +294,7 @@ using selector = TAO_PEGTL_NAMESPACE::parse_tree::selector< ...@@ -308,8 +294,7 @@ using selector = TAO_PEGTL_NAMESPACE::parse_tree::selector<
ASTBuilder::simplify_statement_block::on<language::statement_block>, ASTBuilder::simplify_statement_block::on<language::statement_block>,
ASTBuilder::simplify_for_init::on<language::for_init>, ASTBuilder::simplify_for_init::on<language::for_init>,
ASTBuilder::simplify_for_test::on<language::for_test>, ASTBuilder::simplify_for_test::on<language::for_test>,
ASTBuilder::simplify_for_post::on<language::for_post>, ASTBuilder::simplify_for_post::on<language::for_post>>;
ASTBuilder::simplify_stream_statement::on<language::ostream_statement>>;
template <typename InputT> template <typename InputT>
std::unique_ptr<ASTNode> std::unique_ptr<ASTNode>
......
...@@ -22,6 +22,11 @@ ASTNodeBinaryOperatorExpressionBuilder::ASTNodeBinaryOperatorExpressionBuilder(A ...@@ -22,6 +22,11 @@ ASTNodeBinaryOperatorExpressionBuilder::ASTNodeBinaryOperatorExpressionBuilder(A
} else if (n.is_type<language::minus_op>()) { } else if (n.is_type<language::minus_op>()) {
return binaryOperatorMangler<language::minus_op>(lhs_data_type, rhs_data_type); return binaryOperatorMangler<language::minus_op>(lhs_data_type, rhs_data_type);
} else if (n.is_type<language::shift_left_op>()) {
return binaryOperatorMangler<language::shift_left_op>(lhs_data_type, rhs_data_type);
} else if (n.is_type<language::shift_right_op>()) {
return binaryOperatorMangler<language::shift_right_op>(lhs_data_type, rhs_data_type);
} else if (n.is_type<language::or_op>()) { } else if (n.is_type<language::or_op>()) {
return binaryOperatorMangler<language::or_op>(lhs_data_type, rhs_data_type); return binaryOperatorMangler<language::or_op>(lhs_data_type, rhs_data_type);
} else if (n.is_type<language::and_op>()) { } else if (n.is_type<language::and_op>()) {
......
...@@ -161,8 +161,6 @@ ASTNodeDataTypeBuilder::_buildNodeDataTypes(ASTNode& n) const ...@@ -161,8 +161,6 @@ ASTNodeDataTypeBuilder::_buildNodeDataTypes(ASTNode& n) const
} else if (n.is_type<language::literal>()) { } else if (n.is_type<language::literal>()) {
n.m_data_type = ASTNodeDataType::build<ASTNodeDataType::string_t>(); n.m_data_type = ASTNodeDataType::build<ASTNodeDataType::string_t>();
} else if (n.is_type<language::cout_kw>() or n.is_type<language::cerr_kw>() or n.is_type<language::clog_kw>()) {
n.m_data_type = ASTNodeDataType::build<ASTNodeDataType::void_t>();
} else if (n.is_type<language::var_declaration>()) { } else if (n.is_type<language::var_declaration>()) {
auto& name_node = *(n.children[0]); auto& name_node = *(n.children[0]);
auto& type_node = *(n.children[1]); auto& type_node = *(n.children[1]);
...@@ -460,6 +458,7 @@ ASTNodeDataTypeBuilder::_buildNodeDataTypes(ASTNode& n) const ...@@ -460,6 +458,7 @@ ASTNodeDataTypeBuilder::_buildNodeDataTypes(ASTNode& n) const
} }
} else if (n.is_type<language::plus_op>() or n.is_type<language::minus_op>() or } else if (n.is_type<language::plus_op>() or n.is_type<language::minus_op>() or
n.is_type<language::multiply_op>() or n.is_type<language::divide_op>() or n.is_type<language::multiply_op>() or n.is_type<language::divide_op>() or
n.is_type<language::shift_left_op>() or n.is_type<language::shift_right_op>() or
n.is_type<language::lesser_op>() or n.is_type<language::lesser_or_eq_op>() or n.is_type<language::lesser_op>() or n.is_type<language::lesser_or_eq_op>() or
n.is_type<language::greater_op>() or n.is_type<language::greater_or_eq_op>() or n.is_type<language::greater_op>() or n.is_type<language::greater_or_eq_op>() or
n.is_type<language::eqeq_op>() or n.is_type<language::not_eq_op>() or n.is_type<language::and_op>() or n.is_type<language::eqeq_op>() or n.is_type<language::not_eq_op>() or n.is_type<language::and_op>() or
...@@ -482,6 +481,12 @@ ASTNodeDataTypeBuilder::_buildNodeDataTypes(ASTNode& n) const ...@@ -482,6 +481,12 @@ ASTNodeDataTypeBuilder::_buildNodeDataTypes(ASTNode& n) const
return operator_repository.getBinaryOperatorValueType( return operator_repository.getBinaryOperatorValueType(
binaryOperatorMangler<language::divide_op>(type_0, type_1)); binaryOperatorMangler<language::divide_op>(type_0, type_1));
} else if (n.is_type<language::shift_left_op>()) {
return operator_repository.getBinaryOperatorValueType(
binaryOperatorMangler<language::shift_left_op>(type_0, type_1));
} else if (n.is_type<language::shift_right_op>()) {
return operator_repository.getBinaryOperatorValueType(
binaryOperatorMangler<language::shift_right_op>(type_0, type_1));
} else if (n.is_type<language::lesser_op>()) { } else if (n.is_type<language::lesser_op>()) {
return operator_repository.getBinaryOperatorValueType( return operator_repository.getBinaryOperatorValueType(
binaryOperatorMangler<language::lesser_op>(type_0, type_1)); binaryOperatorMangler<language::lesser_op>(type_0, type_1));
......
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
#include <language/node_processor/IfProcessor.hpp> #include <language/node_processor/IfProcessor.hpp>
#include <language/node_processor/LocalNameProcessor.hpp> #include <language/node_processor/LocalNameProcessor.hpp>
#include <language/node_processor/NameProcessor.hpp> #include <language/node_processor/NameProcessor.hpp>
#include <language/node_processor/OStreamProcessor.hpp>
#include <language/node_processor/TupleToTinyVectorProcessor.hpp> #include <language/node_processor/TupleToTinyVectorProcessor.hpp>
#include <language/node_processor/TupleToVectorProcessor.hpp> #include <language/node_processor/TupleToVectorProcessor.hpp>
#include <language/node_processor/ValueProcessor.hpp> #include <language/node_processor/ValueProcessor.hpp>
...@@ -105,17 +104,12 @@ ASTNodeExpressionBuilder::_buildExpression(ASTNode& n) ...@@ -105,17 +104,12 @@ ASTNodeExpressionBuilder::_buildExpression(ASTNode& n)
} else if (n.is_type<language::multiply_op>() or n.is_type<language::divide_op>() or n.is_type<language::plus_op>() or } else if (n.is_type<language::multiply_op>() or n.is_type<language::divide_op>() or n.is_type<language::plus_op>() or
n.is_type<language::minus_op>() or n.is_type<language::or_op>() or n.is_type<language::and_op>() or n.is_type<language::minus_op>() or n.is_type<language::or_op>() or n.is_type<language::and_op>() or
n.is_type<language::xor_op>() or n.is_type<language::greater_op>() or n.is_type<language::xor_op>() or n.is_type<language::greater_op>() or
n.is_type<language::shift_left_op>() or n.is_type<language::shift_right_op>() or
n.is_type<language::greater_or_eq_op>() or n.is_type<language::lesser_op>() or n.is_type<language::greater_or_eq_op>() or n.is_type<language::lesser_op>() or
n.is_type<language::lesser_or_eq_op>() or n.is_type<language::eqeq_op>() or n.is_type<language::lesser_or_eq_op>() or n.is_type<language::eqeq_op>() or
n.is_type<language::not_eq_op>()) { n.is_type<language::not_eq_op>()) {
ASTNodeBinaryOperatorExpressionBuilder{n}; ASTNodeBinaryOperatorExpressionBuilder{n};
} else if (n.is_type<language::cout_kw>()) {
n.m_node_processor = std::make_unique<OStreamProcessor>(n, std::cout);
} else if (n.is_type<language::cerr_kw>()) {
n.m_node_processor = std::make_unique<OStreamProcessor>(n, std::cerr);
} else if (n.is_type<language::clog_kw>()) {
n.m_node_processor = std::make_unique<OStreamProcessor>(n, std::clog);
} else if (n.is_type<language::if_statement>()) { } else if (n.is_type<language::if_statement>()) {
n.m_node_processor = std::make_unique<IfProcessor>(n); n.m_node_processor = std::make_unique<IfProcessor>(n);
} else if (n.is_type<language::statement_block>()) { } else if (n.is_type<language::statement_block>()) {
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <language/utils/BuiltinFunctionEmbedder.hpp> #include <language/utils/BuiltinFunctionEmbedder.hpp>
#include <language/utils/TypeDescriptor.hpp> #include <language/utils/TypeDescriptor.hpp>
#include <language/utils/ValueDescriptor.hpp>
#include <utils/Exceptions.hpp> #include <utils/Exceptions.hpp>
#include <memory> #include <memory>
...@@ -44,6 +45,17 @@ BuiltinModule::_addBuiltinFunction(const std::string& name, ...@@ -44,6 +45,17 @@ BuiltinModule::_addBuiltinFunction(const std::string& name,
} }
} }
void
BuiltinModule::_addNameValue(const std::string& name, const ASTNodeDataType& type, const DataVariant& data)
{
std::shared_ptr value_descriptor = std::make_shared<ValueDescriptor>(type, data);
auto [i_type, success] = m_name_value_map.insert(std::make_pair(name, value_descriptor));
if (not success) {
throw NormalError("variable '" + name + "' cannot be added!\n");
}
}
void void
BuiltinModule::_addTypeDescriptor(const ASTNodeDataType& ast_node_data_type) BuiltinModule::_addTypeDescriptor(const ASTNodeDataType& ast_node_data_type)
{ {
......
...@@ -6,18 +6,22 @@ ...@@ -6,18 +6,22 @@
class IBuiltinFunctionEmbedder; class IBuiltinFunctionEmbedder;
class TypeDescriptor; class TypeDescriptor;
class ValueDescriptor;
class BuiltinModule : public IModule class BuiltinModule : public IModule
{ {
protected: protected:
NameBuiltinFunctionMap m_name_builtin_function_map; NameBuiltinFunctionMap m_name_builtin_function_map;
NameTypeMap m_name_type_map; NameTypeMap m_name_type_map;
NameValueMap m_name_value_map;
void _addBuiltinFunction(const std::string& name, void _addBuiltinFunction(const std::string& name,
std::shared_ptr<IBuiltinFunctionEmbedder> builtin_function_embedder); std::shared_ptr<IBuiltinFunctionEmbedder> builtin_function_embedder);
void _addTypeDescriptor(const ASTNodeDataType& type); void _addTypeDescriptor(const ASTNodeDataType& type);
void _addNameValue(const std::string& name, const ASTNodeDataType& type, const DataVariant& data);
const bool m_is_mandatory; const bool m_is_mandatory;
public: public:
...@@ -39,6 +43,12 @@ class BuiltinModule : public IModule ...@@ -39,6 +43,12 @@ class BuiltinModule : public IModule
return m_name_type_map; return m_name_type_map;
} }
const NameValueMap&
getNameValueMap() const final
{
return m_name_value_map;
}
BuiltinModule(bool is_mandatory = false); BuiltinModule(bool is_mandatory = false);
~BuiltinModule() = default; ~BuiltinModule() = default;
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#include <language/utils/IncDecOperatorRegisterForN.hpp> #include <language/utils/IncDecOperatorRegisterForN.hpp>
#include <language/utils/IncDecOperatorRegisterForR.hpp> #include <language/utils/IncDecOperatorRegisterForR.hpp>
#include <language/utils/IncDecOperatorRegisterForZ.hpp> #include <language/utils/IncDecOperatorRegisterForZ.hpp>
#include <language/utils/OFStream.hpp>
#include <language/utils/OStream.hpp>
#include <language/utils/UnaryOperatorRegisterForB.hpp> #include <language/utils/UnaryOperatorRegisterForB.hpp>
#include <language/utils/UnaryOperatorRegisterForN.hpp> #include <language/utils/UnaryOperatorRegisterForN.hpp>
#include <language/utils/UnaryOperatorRegisterForR.hpp> #include <language/utils/UnaryOperatorRegisterForR.hpp>
...@@ -83,6 +85,25 @@ CoreModule::CoreModule() : BuiltinModule(true) ...@@ -83,6 +85,25 @@ CoreModule::CoreModule() : BuiltinModule(true)
[]() { RandomEngine::instance().resetRandomSeed(); } []() { RandomEngine::instance().resetRandomSeed(); }
)); ));
this->_addTypeDescriptor(ast_node_data_type_from<std::shared_ptr<const OStream>>);
this
->_addBuiltinFunction("ofstream",
std::make_shared<BuiltinFunctionEmbedder<std::shared_ptr<const OStream>(const std::string&)>>(
[](const std::string& filename) { return std::make_shared<const OFStream>(filename); }
));
this->_addNameValue("cout", ast_node_data_type_from<std::shared_ptr<const OStream>>,
EmbeddedData{std::make_shared<DataHandler<const OStream>>(std::make_shared<OStream>(std::cout))});
this->_addNameValue("cerr", ast_node_data_type_from<std::shared_ptr<const OStream>>,
EmbeddedData{std::make_shared<DataHandler<const OStream>>(std::make_shared<OStream>(std::cerr))});
this->_addNameValue("clog", ast_node_data_type_from<std::shared_ptr<const OStream>>,
EmbeddedData{std::make_shared<DataHandler<const OStream>>(std::make_shared<OStream>(std::clog))});
} }
void void
......
#ifndef IMODULE_HPP #ifndef IMODULE_HPP
#define IMODULE_HPP #define IMODULE_HPP
#include <language/utils/DataVariant.hpp>
#include <memory> #include <memory>
#include <string> #include <string>
#include <string_view> #include <string_view>
...@@ -8,12 +10,14 @@ ...@@ -8,12 +10,14 @@
class IBuiltinFunctionEmbedder; class IBuiltinFunctionEmbedder;
class TypeDescriptor; class TypeDescriptor;
class ValueDescriptor;
class IModule class IModule
{ {
public: public:
using NameBuiltinFunctionMap = std::unordered_map<std::string, std::shared_ptr<IBuiltinFunctionEmbedder>>; using NameBuiltinFunctionMap = std::unordered_map<std::string, std::shared_ptr<IBuiltinFunctionEmbedder>>;
using NameTypeMap = std::unordered_map<std::string, std::shared_ptr<TypeDescriptor>>; using NameTypeMap = std::unordered_map<std::string, std::shared_ptr<TypeDescriptor>>;
using NameValueMap = std::unordered_map<std::string, std::shared_ptr<ValueDescriptor>>;
IModule() = default; IModule() = default;
IModule(IModule&&) = default; IModule(IModule&&) = default;
...@@ -25,6 +29,8 @@ class IModule ...@@ -25,6 +29,8 @@ class IModule
virtual const NameTypeMap& getNameTypeMap() const = 0; virtual const NameTypeMap& getNameTypeMap() const = 0;
virtual const NameValueMap& getNameValueMap() const = 0;
virtual void registerOperators() const = 0; virtual void registerOperators() const = 0;
virtual std::string_view name() const = 0; virtual std::string_view name() const = 0;
......
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
#include <language/utils/ParseError.hpp> #include <language/utils/ParseError.hpp>
#include <language/utils/SymbolTable.hpp> #include <language/utils/SymbolTable.hpp>
#include <language/utils/TypeDescriptor.hpp> #include <language/utils/TypeDescriptor.hpp>
#include <language/utils/ValueDescriptor.hpp>
#include <utils/PugsAssert.hpp> #include <utils/PugsAssert.hpp>
#include <algorithm> #include <algorithm>
...@@ -85,6 +87,28 @@ ModuleRepository::_populateEmbedderTableT(const ASTNode& module_node, ...@@ -85,6 +87,28 @@ ModuleRepository::_populateEmbedderTableT(const ASTNode& module_node,
} }
} }
void
ModuleRepository::_populateSymbolTable(const ASTNode& module_node,
const std::string& module_name,
const IModule::NameValueMap& name_value_descriptor_map,
SymbolTable& symbol_table)
{
for (auto [symbol_name, value_descriptor] : name_value_descriptor_map) {
auto [i_symbol, success] = symbol_table.add(symbol_name, module_node.begin());
if (not success) {
std::ostringstream error_message;
error_message << "importing module '" << module_name << "', cannot add symbol '" << symbol_name
<< "', it is already defined!";
throw ParseError(error_message.str(), module_node.begin());
}
i_symbol->attributes().setDataType(value_descriptor->type());
i_symbol->attributes().setIsInitialized();
i_symbol->attributes().value() = value_descriptor->value();
}
}
void void
ModuleRepository::populateSymbolTable(const ASTNode& module_name_node, SymbolTable& symbol_table) ModuleRepository::populateSymbolTable(const ASTNode& module_name_node, SymbolTable& symbol_table)
{ {
...@@ -110,6 +134,8 @@ ModuleRepository::populateSymbolTable(const ASTNode& module_name_node, SymbolTab ...@@ -110,6 +134,8 @@ ModuleRepository::populateSymbolTable(const ASTNode& module_name_node, SymbolTab
ASTNodeDataType::build<ASTNodeDataType::type_name_id_t>(), symbol_table, ASTNodeDataType::build<ASTNodeDataType::type_name_id_t>(), symbol_table,
symbol_table.typeEmbedderTable()); symbol_table.typeEmbedderTable());
this->_populateSymbolTable(module_name_node, module_name, populating_module.getNameValueMap(), symbol_table);
for (auto [symbol_name, embedded] : populating_module.getNameTypeMap()) { for (auto [symbol_name, embedded] : populating_module.getNameTypeMap()) {
BasicAffectationRegisterFor<EmbeddedData>(ASTNodeDataType::build<ASTNodeDataType::type_id_t>(symbol_name)); BasicAffectationRegisterFor<EmbeddedData>(ASTNodeDataType::build<ASTNodeDataType::type_id_t>(symbol_name));
} }
...@@ -132,6 +158,12 @@ ModuleRepository::populateMandatorySymbolTable(const ASTNode& root_node, SymbolT ...@@ -132,6 +158,12 @@ ModuleRepository::populateMandatorySymbolTable(const ASTNode& root_node, SymbolT
ASTNodeDataType::build<ASTNodeDataType::type_name_id_t>(), symbol_table, ASTNodeDataType::build<ASTNodeDataType::type_name_id_t>(), symbol_table,
symbol_table.typeEmbedderTable()); symbol_table.typeEmbedderTable());
this->_populateSymbolTable(root_node, module_name, i_module->getNameValueMap(), symbol_table);
for (auto [symbol_name, embedded] : i_module->getNameTypeMap()) {
BasicAffectationRegisterFor<EmbeddedData>(ASTNodeDataType::build<ASTNodeDataType::type_id_t>(symbol_name));
}
i_module->registerOperators(); i_module->registerOperators();
} }
} }
......
...@@ -26,6 +26,11 @@ class ModuleRepository ...@@ -26,6 +26,11 @@ class ModuleRepository
SymbolTable& symbol_table, SymbolTable& symbol_table,
EmbedderTableT& embedder_table); EmbedderTableT& embedder_table);
void _populateSymbolTable(const ASTNode& module_node,
const std::string& module_name,
const IModule::NameValueMap& name_value_map,
SymbolTable& symbol_table);
public: public:
void populateSymbolTable(const ASTNode& module_name_node, SymbolTable& symbol_table); void populateSymbolTable(const ASTNode& module_name_node, SymbolTable& symbol_table);
void populateMandatorySymbolTable(const ASTNode& root_node, SymbolTable& symbol_table); void populateMandatorySymbolTable(const ASTNode& root_node, SymbolTable& symbol_table);
......
...@@ -82,6 +82,34 @@ struct BinOp<language::lesser_op> ...@@ -82,6 +82,34 @@ struct BinOp<language::lesser_op>
} }
}; };
class OStream;
template <>
struct BinOp<language::shift_left_op>
{
template <typename A, typename B>
PUGS_INLINE auto
eval(const A& a, const B& b) -> decltype(a << b)
{
if constexpr (std::is_same_v<A, std::shared_ptr<const OStream>> and std::is_same_v<B, bool>) {
return a << std::boolalpha << b;
} else {
return a << b;
}
}
};
template <>
struct BinOp<language::shift_right_op>
{
template <typename A, typename B>
PUGS_INLINE auto
eval(const A& a, const B& b) -> decltype(a >> b)
{
return a >> b;
}
};
template <> template <>
struct BinOp<language::lesser_or_eq_op> struct BinOp<language::lesser_or_eq_op>
{ {
...@@ -286,7 +314,8 @@ struct BinaryExpressionProcessor<BinaryOpT, std::shared_ptr<ValueT>, std::shared ...@@ -286,7 +314,8 @@ struct BinaryExpressionProcessor<BinaryOpT, std::shared_ptr<ValueT>, std::shared
PUGS_INLINE DataVariant PUGS_INLINE DataVariant
_eval(const DataVariant& a, const DataVariant& b) _eval(const DataVariant& a, const DataVariant& b)
{ {
if constexpr ((std::is_arithmetic_v<B_DataT>) or (is_tiny_matrix_v<B_DataT>) or (is_tiny_vector_v<B_DataT>)) { if constexpr ((std::is_arithmetic_v<B_DataT>) or (is_tiny_matrix_v<B_DataT>) or (is_tiny_vector_v<B_DataT>) or
(std::is_same_v<std::string, B_DataT>)) {
const auto& embedded_a = std::get<EmbeddedData>(a); const auto& embedded_a = std::get<EmbeddedData>(a);
const auto& b_value = std::get<B_DataT>(b); const auto& b_value = std::get<B_DataT>(b);
......
#ifndef OSTREAM_PROCESSOR_HPP
#define OSTREAM_PROCESSOR_HPP
#include <language/ast/ASTNode.hpp>
#include <language/node_processor/INodeProcessor.hpp>
#include <language/utils/ParseError.hpp>
class OStreamProcessor final : public INodeProcessor
{
private:
ASTNode& m_node;
std::ostream& m_os;
public:
DataVariant
execute(ExecutionPolicy& exec_policy)
{
for (size_t i = 0; i < m_node.children.size(); ++i) {
m_os << m_node.children[i]->execute(exec_policy);
}
return {};
}
OStreamProcessor(ASTNode& node, std::ostream& os) : m_node{node}, m_os(os)
{
for (auto& child : m_node.children) {
if ((child->m_data_type == ASTNodeDataType::type_name_id_t) or
(child->m_data_type == ASTNodeDataType::function_t) or
(child->m_data_type == ASTNodeDataType::builtin_function_t)) {
throw ParseError("invalid argument, cannot print a '" + dataTypeName(child->m_data_type) + "'",
std::vector{child->begin()});
}
}
}
};
#endif // OSTREAM_PROCESSOR_HPP
...@@ -13,6 +13,9 @@ struct divide_op; ...@@ -13,6 +13,9 @@ struct divide_op;
struct plus_op; struct plus_op;
struct minus_op; struct minus_op;
struct shift_left_op;
struct shift_right_op;
struct or_op; struct or_op;
struct and_op; struct and_op;
struct xor_op; struct xor_op;
...@@ -56,6 +59,10 @@ binaryOperatorMangler(const ASTNodeDataType& lhs_data_type, const ASTNodeDataTyp ...@@ -56,6 +59,10 @@ binaryOperatorMangler(const ASTNodeDataType& lhs_data_type, const ASTNodeDataTyp
return "=="; return "==";
} else if constexpr (std::is_same_v<BinaryOperatorT, language::not_eq_op>) { } else if constexpr (std::is_same_v<BinaryOperatorT, language::not_eq_op>) {
return "!="; return "!=";
} else if constexpr (std::is_same_v<BinaryOperatorT, language::shift_left_op>) {
return "<<";
} else if constexpr (std::is_same_v<BinaryOperatorT, language::shift_right_op>) {
return ">>";
} else { } else {
static_assert(std::is_same_v<language::multiply_op, BinaryOperatorT>, "undefined binary operator"); static_assert(std::is_same_v<language::multiply_op, BinaryOperatorT>, "undefined binary operator");
} }
......
#include <language/utils/BinaryOperatorRegisterForB.hpp> #include <language/utils/BinaryOperatorRegisterForB.hpp>
#include <language/utils/BasicBinaryOperatorRegisterComparisonOf.hpp> #include <language/utils/BasicBinaryOperatorRegisterComparisonOf.hpp>
#include <language/utils/DataHandler.hpp>
#include <language/utils/OStream.hpp>
void
BinaryOperatorRegisterForB::_register_ostream()
{
OperatorRepository& repository = OperatorRepository::instance();
repository.addBinaryOperator<language::shift_left_op>(
std::make_shared<BinaryOperatorProcessorBuilder<language::shift_left_op, std::shared_ptr<const OStream>,
std::shared_ptr<const OStream>, bool>>());
}
void void
BinaryOperatorRegisterForB::_register_comparisons() BinaryOperatorRegisterForB::_register_comparisons()
...@@ -61,6 +73,7 @@ BinaryOperatorRegisterForB::_register_divide() ...@@ -61,6 +73,7 @@ BinaryOperatorRegisterForB::_register_divide()
BinaryOperatorRegisterForB::BinaryOperatorRegisterForB() BinaryOperatorRegisterForB::BinaryOperatorRegisterForB()
{ {
this->_register_ostream();
this->_register_comparisons(); this->_register_comparisons();
this->_register_logical_operators(); this->_register_logical_operators();
this->_register_plus(); this->_register_plus();
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
class BinaryOperatorRegisterForB class BinaryOperatorRegisterForB
{ {
private: private:
void _register_ostream();
void _register_comparisons(); void _register_comparisons();
void _register_logical_operators(); void _register_logical_operators();
void _register_plus(); void _register_plus();
......
#include <language/utils/BinaryOperatorRegisterForN.hpp> #include <language/utils/BinaryOperatorRegisterForN.hpp>
#include <language/utils/BasicBinaryOperatorRegisterComparisonOf.hpp> #include <language/utils/BasicBinaryOperatorRegisterComparisonOf.hpp>
#include <language/utils/DataHandler.hpp>
#include <language/utils/OStream.hpp>
void
BinaryOperatorRegisterForN::_register_ostream()
{
OperatorRepository& repository = OperatorRepository::instance();
repository.addBinaryOperator<language::shift_left_op>(
std::make_shared<BinaryOperatorProcessorBuilder<language::shift_left_op, std::shared_ptr<const OStream>,
std::shared_ptr<const OStream>, uint64_t>>());
}
void void
BinaryOperatorRegisterForN::_register_comparisons() BinaryOperatorRegisterForN::_register_comparisons()
...@@ -42,6 +54,7 @@ BinaryOperatorRegisterForN::_register_minus() ...@@ -42,6 +54,7 @@ BinaryOperatorRegisterForN::_register_minus()
BinaryOperatorRegisterForN::BinaryOperatorRegisterForN() BinaryOperatorRegisterForN::BinaryOperatorRegisterForN()
{ {
this->_register_ostream();
this->_register_comparisons(); this->_register_comparisons();
this->_register_arithmetic<language::plus_op>(); this->_register_arithmetic<language::plus_op>();
this->_register_minus(); this->_register_minus();
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
class BinaryOperatorRegisterForN class BinaryOperatorRegisterForN
{ {
private: private:
void _register_ostream();
template <typename OperatorT> template <typename OperatorT>
void _register_arithmetic(); void _register_arithmetic();
void _register_comparisons(); void _register_comparisons();
......
#include <language/utils/BinaryOperatorRegisterForR.hpp> #include <language/utils/BinaryOperatorRegisterForR.hpp>
#include <language/utils/BasicBinaryOperatorRegisterComparisonOf.hpp> #include <language/utils/BasicBinaryOperatorRegisterComparisonOf.hpp>
#include <language/utils/DataHandler.hpp>
#include <language/utils/OStream.hpp>
void
BinaryOperatorRegisterForR::_register_ostream()
{
OperatorRepository& repository = OperatorRepository::instance();
repository.addBinaryOperator<language::shift_left_op>(
std::make_shared<BinaryOperatorProcessorBuilder<language::shift_left_op, std::shared_ptr<const OStream>,
std::shared_ptr<const OStream>, double>>());
}
void void
BinaryOperatorRegisterForR::_register_comparisons() BinaryOperatorRegisterForR::_register_comparisons()
...@@ -44,6 +56,7 @@ BinaryOperatorRegisterForR::_register_arithmetic() ...@@ -44,6 +56,7 @@ BinaryOperatorRegisterForR::_register_arithmetic()
BinaryOperatorRegisterForR::BinaryOperatorRegisterForR() BinaryOperatorRegisterForR::BinaryOperatorRegisterForR()
{ {
this->_register_ostream();
this->_register_comparisons(); this->_register_comparisons();
this->_register_arithmetic<language::plus_op>(); this->_register_arithmetic<language::plus_op>();
this->_register_arithmetic<language::minus_op>(); this->_register_arithmetic<language::minus_op>();
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
class BinaryOperatorRegisterForR class BinaryOperatorRegisterForR
{ {
private: private:
void _register_ostream();
template <typename OperatorT> template <typename OperatorT>
void _register_arithmetic(); void _register_arithmetic();
void _register_comparisons(); void _register_comparisons();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment