ADD: syntaxic sugar for module import.

main
bog 2023-09-14 14:34:51 +02:00
parent c94bf181fd
commit 64d17de693
7 changed files with 61 additions and 4 deletions

View File

@ -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

24
lib/std/io.hpp Normal file
View File

@ -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"});
});
}

8
lib/std/std.cpp Normal file
View File

@ -0,0 +1,8 @@
#include "src/Loader.hpp"
#include "io.hpp"
extern "C" void lib(grino::Loader& loader)
{
lib_io(loader);
}

View File

@ -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'

View File

@ -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; }

View File

@ -118,7 +118,6 @@ namespace grino
std::shared_ptr<Node> 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<Node> 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;
}
}

View File

@ -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 )");
}