Skip to content

Commit

Permalink
✨ add new module import syntax rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ValKmjolnir committed Oct 30, 2023
1 parent c35ad2e commit 88e4b86
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/ast_dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@

namespace nasal {

bool ast_dumper::visit_use_stmt(use_stmt* node) {
dump_indent();
std::cout << "use" << format_location(node->get_location());
push_indent();
for(auto i : node->get_path()) {
if (i==node->get_path().back()) {
set_last();
}
i->accept(this);
}
pop_indent();
return true;
}

bool ast_dumper::visit_null_expr(null_expr* node) {
dump_indent();
std::cout << "null" << format_location(node->get_location());
Expand Down
1 change: 1 addition & 0 deletions src/ast_dumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ast_dumper:public ast_visitor {
}

public:
bool visit_use_stmt(use_stmt*) override;
bool visit_null_expr(null_expr*) override;
bool visit_nil_expr(nil_expr*) override;
bool visit_number_literal(number_literal*) override;
Expand Down
7 changes: 7 additions & 0 deletions src/ast_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ bool ast_visitor::visit_expr(expr* node) {
return true;
}

bool ast_visitor::visit_use_stmt(use_stmt* node) {
for(auto i : node->get_path()) {
i->accept(this);
}
return true;
}

bool ast_visitor::visit_call(call* node) {
node->accept(this);
return true;
Expand Down
1 change: 1 addition & 0 deletions src/ast_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace nasal {
class ast_visitor {
public:
virtual bool visit_expr(expr*);
virtual bool visit_use_stmt(use_stmt*);
virtual bool visit_call(call*);
virtual bool visit_null_expr(null_expr*);
virtual bool visit_nil_expr(nil_expr*);
Expand Down
10 changes: 10 additions & 0 deletions src/nasal_ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ void expr::accept(ast_visitor* visitor) {
visitor->visit_expr(this);
}

use_stmt::~use_stmt() {
for(auto i : path) {
delete i;
}
}

void use_stmt::accept(ast_visitor* visitor) {
visitor->visit_use_stmt(this);
}

void call::accept(ast_visitor* visitor) {
visitor->visit_call(this);
}
Expand Down
15 changes: 15 additions & 0 deletions src/nasal_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace nasal {

enum class expr_type:u32 {
ast_null = 0, // null node
ast_use, // use statement
ast_block, // code block
ast_nil, // nil keyword
ast_num, // number, basic value type
Expand Down Expand Up @@ -46,6 +47,7 @@ enum class expr_type:u32 {
};

class ast_visitor;
class identifier;
class hash_pair;
class parameter;
class slice_vector;
Expand Down Expand Up @@ -77,6 +79,19 @@ class expr {
virtual void accept(ast_visitor*);
};

class use_stmt: public expr {
private:
std::vector<identifier*> path;

public:
use_stmt(const span& location):
expr(location, expr_type::ast_use) {}
~use_stmt() override;
void accept(ast_visitor*) override;
void add_path(identifier* node) {path.push_back(node);}
const auto& get_path() const {return path;}
};

class call: public expr {
public:
call(const span& location, expr_type node_type):
Expand Down
1 change: 1 addition & 0 deletions src/nasal_codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,7 @@ void codegen::repl_mode_info_output_gen(expr* node) {
void codegen::block_gen(code_block* node) {
for(auto tmp : node->get_expressions()) {
switch(tmp->get_type()) {
case expr_type::ast_use: break;
case expr_type::ast_null: break;
case expr_type::ast_id:
if (need_repl_output && local.empty()) {
Expand Down
2 changes: 2 additions & 0 deletions src/nasal_lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum class tok:u32 {
id, // identifier
tktrue, // keyword true
tkfalse, // keyword false
use, // keyword use
rfor, // loop keyword for
forindex, // loop keyword forindex
foreach, // loop keyword foreach
Expand Down Expand Up @@ -103,6 +104,7 @@ class lexer {
std::vector<token> toks;

const std::unordered_map<std::string, tok> typetbl {
{"use" ,tok::use },
{"true" ,tok::tktrue },
{"false" ,tok::tkfalse },
{"for" ,tok::rfor },
Expand Down
13 changes: 13 additions & 0 deletions src/nasal_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ void parse::update_location(expr* node) {
node->update_location(toks[ptr-1].loc);
}

use_stmt* parse::use_stmt_gen() {
auto node = new use_stmt(toks[ptr].loc);
match(tok::use);
node->add_path(id());
while(lookahead(tok::dot)) {
match(tok::dot);
node->add_path(id());
}
update_location(node);
return node;
}

null_expr* parse::null() {
return new null_expr(toks[ptr].loc);
}
Expand Down Expand Up @@ -355,6 +367,7 @@ expr* parse::expression() {
die(thisspan, "must use return in functions");
}
switch(type) {
case tok::use: return use_stmt_gen();
case tok::tknil:
case tok::num:
case tok::str:
Expand Down
2 changes: 2 additions & 0 deletions src/nasal_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class parse {

private:
const std::unordered_map<tok, std::string> tokname {
{tok::use ,"use" },
{tok::rfor ,"for" },
{tok::forindex,"forindex"},
{tok::foreach ,"foreach" },
Expand Down Expand Up @@ -92,6 +93,7 @@ class parse {
void update_location(expr*);

private:
use_stmt* use_stmt_gen();
null_expr* null();
nil_expr* nil();
number_literal* num();
Expand Down

0 comments on commit 88e4b86

Please sign in to comment.