From 64d17de6934907346602bc5f63c1579668a9f2d8 Mon Sep 17 00:00:00 2001 From: bog Date: Thu, 14 Sep 2023 14:34:51 +0200 Subject: [PATCH] ADD: syntaxic sugar for module import. --- doc/grammar.bnf | 1 + lib/std/io.hpp | 24 ++++++++++++++++++++++++ lib/std/std.cpp | 8 ++++++++ meson.build | 10 ++++++++++ src/Loc.hpp | 2 +- src/Parser.cpp | 14 +++++++++++--- tests/Parser.cpp | 6 ++++++ 7 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 lib/std/io.hpp create mode 100644 lib/std/std.cpp diff --git a/doc/grammar.bnf b/doc/grammar.bnf index 857a478..37230e8 100644 --- a/doc/grammar.bnf +++ b/doc/grammar.bnf @@ -24,4 +24,5 @@ BLOCK ::= opar colon EXPR* cpar ARRAY ::= osquare EXPR* csquare IMPORT ::= opar import string cpar SHORT_IMPORT ::= opar decl import ident string cpar +SHORTER_IMPORT ::= opar decl import ident cpar NS ::= ident ns ident diff --git a/lib/std/io.hpp b/lib/std/io.hpp new file mode 100644 index 0000000..1bd258c --- /dev/null +++ b/lib/std/io.hpp @@ -0,0 +1,24 @@ +#include "src/Loader.hpp" +#include "src/Module.hpp" + +extern "C" void lib_io(grino::Loader& loader) +{ + auto mod = loader.add_module("io"); + + mod->loader()->add_native("print", [](auto args){ + for (auto arg: args) + { + std::cout << arg->string(); + } + return grino::Value::make_nil(grino::Loc {"io"}); + }); + + mod->loader()->add_native("println", [](auto args){ + for (auto arg: args) + { + std::cout << arg->string(); + } + std::cout << std::endl; + return grino::Value::make_nil(grino::Loc {"io"}); + }); +} diff --git a/lib/std/std.cpp b/lib/std/std.cpp new file mode 100644 index 0000000..079738f --- /dev/null +++ b/lib/std/std.cpp @@ -0,0 +1,8 @@ +#include "src/Loader.hpp" + +#include "io.hpp" + +extern "C" void lib(grino::Loader& loader) +{ + lib_io(loader); +} diff --git a/meson.build b/meson.build index a38a3dc..20a4586 100644 --- a/meson.build +++ b/meson.build @@ -77,6 +77,16 @@ shared_library('grino_core', install: true, install_dir: grino_libdir) +shared_library('grino_std', + sources: [ + 'lib/std/std.cpp' + ], + dependencies: [ + grino_dep + ], + install: true, + install_dir: grino_libdir) + executable('grino', sources: [ 'src/main.cpp' diff --git a/src/Loc.hpp b/src/Loc.hpp index 863bdee..e0d9d87 100644 --- a/src/Loc.hpp +++ b/src/Loc.hpp @@ -8,7 +8,7 @@ namespace grino class Loc { public: - explicit Loc(std::filesystem::path path, int line); + explicit Loc(std::filesystem::path path, int line=0); virtual ~Loc(); std::filesystem::path path() const { return m_path; } diff --git a/src/Parser.cpp b/src/Parser.cpp index 5370476..1d1d897 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -118,7 +118,6 @@ namespace grino std::shared_ptr Parser::parse_expr() { - if (type_is({NODE_IDENT, NODE_NS})) { return parse_ns(); @@ -335,7 +334,17 @@ namespace grino consume(NODE_DECL); consume(NODE_IMPORT); auto ident = consume(NODE_IDENT); - auto val = consume(NODE_STRING); + std::shared_ptr val; + + if (type_is(NODE_STRING)) + { + val = consume(NODE_STRING); + } + else + { + val = make_node(NODE_STRING, "'" + ident->repr() + "'"); + } + consume(NODE_CPAR); auto node = make_node(NODE_VARDECL); @@ -347,5 +356,4 @@ namespace grino return node; } - } diff --git a/tests/Parser.cpp b/tests/Parser.cpp index 1e79f90..4e48495 100644 --- a/tests/Parser.cpp +++ b/tests/Parser.cpp @@ -116,4 +116,10 @@ TEST_CASE_METHOD(ParserTest, "Parser_import") test_parse("MODULE(VARDECL(IDENT[bim],IMPORT(STRING['hello'])))", "($ @bim 'hello')"); + + test_parse("MODULE(VARDECL(IDENT[bim],IMPORT(STRING['bim'])))", + "($ @ bim )"); + + test_parse("MODULE(VARDECL(IDENT[hello_world],IMPORT(STRING['hello_world'])))", + " ( $ @ hello_world )"); }