ADD: syntaxic sugar for module import.
parent
c94bf181fd
commit
64d17de693
|
@ -24,4 +24,5 @@ BLOCK ::= opar colon EXPR* cpar
|
||||||
ARRAY ::= osquare EXPR* csquare
|
ARRAY ::= osquare EXPR* csquare
|
||||||
IMPORT ::= opar import string cpar
|
IMPORT ::= opar import string cpar
|
||||||
SHORT_IMPORT ::= opar decl import ident string cpar
|
SHORT_IMPORT ::= opar decl import ident string cpar
|
||||||
|
SHORTER_IMPORT ::= opar decl import ident cpar
|
||||||
NS ::= ident ns ident
|
NS ::= ident ns ident
|
||||||
|
|
|
@ -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"});
|
||||||
|
});
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
#include "src/Loader.hpp"
|
||||||
|
|
||||||
|
#include "io.hpp"
|
||||||
|
|
||||||
|
extern "C" void lib(grino::Loader& loader)
|
||||||
|
{
|
||||||
|
lib_io(loader);
|
||||||
|
}
|
10
meson.build
10
meson.build
|
@ -77,6 +77,16 @@ shared_library('grino_core',
|
||||||
install: true,
|
install: true,
|
||||||
install_dir: grino_libdir)
|
install_dir: grino_libdir)
|
||||||
|
|
||||||
|
shared_library('grino_std',
|
||||||
|
sources: [
|
||||||
|
'lib/std/std.cpp'
|
||||||
|
],
|
||||||
|
dependencies: [
|
||||||
|
grino_dep
|
||||||
|
],
|
||||||
|
install: true,
|
||||||
|
install_dir: grino_libdir)
|
||||||
|
|
||||||
executable('grino',
|
executable('grino',
|
||||||
sources: [
|
sources: [
|
||||||
'src/main.cpp'
|
'src/main.cpp'
|
||||||
|
|
|
@ -8,7 +8,7 @@ namespace grino
|
||||||
class Loc
|
class Loc
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit Loc(std::filesystem::path path, int line);
|
explicit Loc(std::filesystem::path path, int line=0);
|
||||||
virtual ~Loc();
|
virtual ~Loc();
|
||||||
|
|
||||||
std::filesystem::path path() const { return m_path; }
|
std::filesystem::path path() const { return m_path; }
|
||||||
|
|
|
@ -118,7 +118,6 @@ namespace grino
|
||||||
|
|
||||||
std::shared_ptr<Node> Parser::parse_expr()
|
std::shared_ptr<Node> Parser::parse_expr()
|
||||||
{
|
{
|
||||||
|
|
||||||
if (type_is({NODE_IDENT, NODE_NS}))
|
if (type_is({NODE_IDENT, NODE_NS}))
|
||||||
{
|
{
|
||||||
return parse_ns();
|
return parse_ns();
|
||||||
|
@ -335,7 +334,17 @@ namespace grino
|
||||||
consume(NODE_DECL);
|
consume(NODE_DECL);
|
||||||
consume(NODE_IMPORT);
|
consume(NODE_IMPORT);
|
||||||
auto ident = consume(NODE_IDENT);
|
auto ident = consume(NODE_IDENT);
|
||||||
auto val = consume(NODE_STRING);
|
std::shared_ptr<Node> val;
|
||||||
|
|
||||||
|
if (type_is(NODE_STRING))
|
||||||
|
{
|
||||||
|
val = consume(NODE_STRING);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
val = make_node(NODE_STRING, "'" + ident->repr() + "'");
|
||||||
|
}
|
||||||
|
|
||||||
consume(NODE_CPAR);
|
consume(NODE_CPAR);
|
||||||
|
|
||||||
auto node = make_node(NODE_VARDECL);
|
auto node = make_node(NODE_VARDECL);
|
||||||
|
@ -347,5 +356,4 @@ namespace grino
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,4 +116,10 @@ TEST_CASE_METHOD(ParserTest, "Parser_import")
|
||||||
|
|
||||||
test_parse("MODULE(VARDECL(IDENT[bim],IMPORT(STRING['hello'])))",
|
test_parse("MODULE(VARDECL(IDENT[bim],IMPORT(STRING['hello'])))",
|
||||||
"($ @bim '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 )");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue