Skip to content

Commit

Permalink
✨ update
Browse files Browse the repository at this point in the history
  • Loading branch information
ValKmjolnir committed Jun 20, 2023
1 parent c5d6a66 commit ac49c0d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 49 deletions.
58 changes: 51 additions & 7 deletions ast/nasal_new_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ enum class expr_type:u32 {
ast_cond, // mark a sub-tree of conditional expression
ast_if, // if keyword
ast_multi_id, // multi identifiers sub-tree
ast_tuple,
ast_def, // definition
ast_multi_assign,
ast_continue, // continue keyword, only used in loop
ast_break, // break keyword, only used in loop
ast_ret // return keyword, only used in function block
Expand Down Expand Up @@ -316,6 +318,21 @@ class unary_operator:public expr {
virtual void accept(ast_visitor*) override;
};

class call_expr:public expr {
private:
expr* first;
std::vector<expr*> calls;

public:
call_expr(const span& location):
expr(location, expr_type::ast_call),
first(nullptr) {}
~call_expr();
void set_first(expr* node) {first = node;}
void add_call(expr* node) {calls.push_back(node);}
virtual void accept(ast_visitor*) override;
};

class call_hash:public expr {
private:
std::string field;
Expand Down Expand Up @@ -369,17 +386,17 @@ class slice_vector:public expr {
virtual void accept(ast_visitor*) override;
};

class definition:public expr {
class definition_expr:public expr {
private:
identifier* variable;
multi_define* variables;
expr* value;

public:
definition(const span& location):
definition_expr(const span& location):
expr(location, expr_type::ast_def),
variable(nullptr), variables(nullptr), value(nullptr) {}
~definition();
~definition_expr();
virtual void accept(ast_visitor*) override;
};

Expand All @@ -391,6 +408,33 @@ class multi_define:public expr {
multi_define(const span& location):
expr(location, expr_type::ast_multi_id) {}
~multi_define();
void add_var(expr* node) {variables.push_back(node);}
virtual void accept(ast_visitor*) override;
};

class tuple_expr:public expr {
private:
std::vector<expr*> elements;

public:
tuple_expr(const span& location):
expr(location, expr_type::ast_tuple) {}
~tuple_expr();
void add_element(expr* node) {elements.push_back(node);}
virtual void accept(ast_visitor*) override;
};

class multi_assign:public expr {
private:
tuple_expr* left;
expr* right;

public:
multi_assign(const span& location):
expr(location, expr_type::ast_multi_assign) {}
~multi_assign();
void set_left(tuple_expr* node) {left = node;}
void set_right(expr* node) {right = node;}
virtual void accept(ast_visitor*) override;
};

Expand Down Expand Up @@ -438,17 +482,17 @@ class for_expr:public expr {
class iter_expr:public expr {
private:
identifier* name;
expr* value;
expr* call;

public:
iter_expr(const span& location):
expr(location, expr_type::ast_iter),
name(nullptr), value(nullptr) {}
name(nullptr), call(nullptr) {}
~iter_expr();
void set_name(identifier* node) {name = node;}
void set_value(expr* node) {value = node;}
void set_call(expr* node) {call = node;}
identifier* get_name() {return name;}
expr* get_value() {return value;}
expr* get_call() {return call;}
virtual void accept(ast_visitor*) override;
};

Expand Down
71 changes: 38 additions & 33 deletions ast/nasal_new_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,26 @@ bool parse::check_tuple() {
return false;
}

bool parse::check_func_end(const ast& node) {
u32 type=node.type();
if (type==ast_func) {
bool parse::check_func_end(expr* node) {
auto type=node->get_type();
if (type==expr_type::ast_func) {
return true;
} else if (type==ast_num || type==ast_id || type==ast_str || type==ast_nil || type==ast_vec || type==ast_hash) {
} else if (type==expr_type::ast_num ||
type==expr_type::ast_id ||
type==expr_type::ast_str ||
type==expr_type::ast_nil ||
type==expr_type::ast_vec ||
type==expr_type::ast_hash) {
return false;
}
if (node.child().empty() || (
type!=ast_def &&
type!=ast_equal &&
type!=ast_addeq &&
type!=ast_subeq &&
type!=ast_multeq &&
type!=ast_diveq &&
type!=ast_lnkeq)) {
type!=expr_type::ast_def &&
type!=expr_type::ast_equal &&
type!=expr_type::ast_addeq &&
type!=expr_type::ast_subeq &&
type!=expr_type::ast_multeq &&
type!=expr_type::ast_diveq &&
type!=expr_type::ast_lnkeq)) {
return false;
} else {
return check_func_end(node.child().back());
Expand Down Expand Up @@ -326,7 +331,7 @@ void parse::params(function* func_node) {
expr* parse::lcurve_expr() {
if (toks[ptr+1].type==tok::var)
return definition();
return check_tuple()?multi_assgin():calc();
return check_tuple()?multi_assignment():calc();
}

expr* parse::expression() {
Expand Down Expand Up @@ -712,7 +717,7 @@ slice_vector* parse::subvec() {
}

expr* parse::definition() {
ast node(toks[ptr].loc, ast_def);
auto node = new definition_expr(toks[ptr].loc);
if (lookahead(tok::var)) {
match(tok::var);
switch(toks[ptr].type) {
Expand All @@ -733,33 +738,33 @@ expr* parse::definition() {
return node;
}

expr* parse::incurve_def() {
multi_define* parse::incurve_def() {
const auto& loc=toks[ptr].loc;
match(tok::lcurve);
match(tok::var);
ast node=multi_id();
auto node = multi_id();
update_location(node);
match(tok::rcurve);
node.set_begin(loc.begin_line, loc.begin_column);
node->set_begin(loc.begin_line, loc.begin_column);
return node;
}

expr* parse::outcurve_def() {
multi_define* parse::outcurve_def() {
const auto& loc=toks[ptr].loc;
match(tok::lcurve);
ast node=multi_id();
auto node = multi_id();
update_location(node);
match(tok::rcurve);
node.set_begin(loc.begin_line, loc.begin_column);
node->set_begin(loc.begin_line, loc.begin_column);
return node;
}

expr* parse::multi_id() {
ast node(toks[ptr].loc, ast_multi_id);
auto node = new multi_define(toks[ptr].loc);
while(!lookahead(tok::eof)) {
// only identifier is allowed here
// but we check it at codegen stage
node.add(calc());
node->add_var(calc());
if (lookahead(tok::comma)) {
match(tok::comma);
} else if (lookahead(tok::id)) { // first set of identifier
Expand All @@ -772,18 +777,18 @@ expr* parse::multi_id() {
return node;
}

expr* parse::multi_scalar() {
tuple_expr* parse::multi_scalar() {
// if check_call_memory is true,we will check if value called here can reach a memory space
const tok panic[]={
tok::id,tok::str,tok::num,tok::tktrue,
tok::tkfalse,tok::opnot,tok::sub,tok::tknil,
tok::func,tok::var,tok::lcurve,tok::floater,
tok::lbrace,tok::lbracket,tok::null
};
ast node(toks[ptr].loc, ast_tuple);
auto node = new tuple_expr(toks[ptr].loc);
match(tok::lcurve);
while(!lookahead(tok::rcurve)) {
node.add(calc());
node->add_element(calc());
if (lookahead(tok::comma)) {
match(tok::comma);
} else if (lookahead(tok::eof)) {
Expand All @@ -797,18 +802,18 @@ expr* parse::multi_scalar() {
return node;
}

expr* parse::multi_assgin() {
ast node(toks[ptr].loc, ast_multi_assign);
node.add(multi_scalar());
multi_assign* parse::multi_assignment() {
auto node = new multi_assign(toks[ptr].loc);
node->set_left(multi_scalar());
match(tok::eq);
if (lookahead(tok::eof)) {
die(thisspan, "expected value list");
return node;
}
if (lookahead(tok::lcurve)) {
node.add(check_tuple()?multi_scalar():calc());
node->set_right(check_tuple()?multi_scalar():calc());
} else {
node.add(calc());
node->set_right(calc());
}
update_location(node);
return node;
Expand Down Expand Up @@ -917,14 +922,14 @@ iter_expr* parse::iter_gen() {
auto node = new iter_expr(toks[ptr].loc);
if (lookahead(tok::var)) {
match(tok::var);
node.set_type(ast_iter);
node->set_name(id());
} else {
node.set_type(ast_call);
node.add(id());
auto tmp = new call_expr(toks[ptr].loc);
tmp->set_first(id());
while(is_call(toks[ptr].type)) {
node.add(call_scalar());
tmp->add_call(call_scalar());
}
node->set_call(tmp);
}
update_location(node);
return node;
Expand Down
12 changes: 6 additions & 6 deletions ast/nasal_new_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class parse {
bool is_call(tok);
bool check_comma(const tok*);
bool check_tuple();
bool check_func_end(const ast&);
bool check_func_end(expr*);
bool check_special_call();
bool need_semi_check(expr*);
void update_location(expr*);
Expand Down Expand Up @@ -121,11 +121,11 @@ class parse {
call_function* callf();
slice_vector* subvec();
expr* definition();
expr* incurve_def();
expr* outcurve_def();
expr* multi_id();
expr* multi_scalar();
expr* multi_assgin();
multi_define* incurve_def();
multi_define* outcurve_def();
multi_define* multi_id();
tuple_expr* multi_scalar();
multi_assign* multi_assignment();
expr* loop();
while_expr* while_loop();
for_expr* for_loop();
Expand Down
2 changes: 2 additions & 0 deletions ast/new_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "nasal_dbg.h"

#include <unordered_map>
#include <thread>

const u32 VM_AST =0x01;
const u32 VM_CODE =0x02;
Expand Down Expand Up @@ -55,6 +56,7 @@ std::ostream& logo(std::ostream& out) {
<<" \\_\\ \\/ \\__,_|___/\\__,_|_|\n"
<<"ver : "<<__nasver<<" ("<<__DATE__<<" "<<__TIME__<<")\n"
<<"std : c++ "<<__cplusplus<<"\n"
<<"core : "<<std::thread::hardware_concurrency()<<" cores\n"
<<"repo : https://github.com/ValKmjolnir/Nasal-Interpreter\n"
<<"repo : https://gitee.com/valkmjolnir/Nasal-Interpreter\n"
<<"wiki : https://wiki.flightgear.org/Nasal_scripting_language\n"
Expand Down
6 changes: 3 additions & 3 deletions nasal_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class parse {
ast outcurve_def();
ast multi_id();
ast multi_scalar();
ast multi_assgin();
ast multi_assign();
ast loop();
ast while_loop();
ast for_loop();
Expand Down Expand Up @@ -459,7 +459,7 @@ ast parse::params() {
ast parse::lcurve_expr() {
if (toks[ptr+1].type==tok::var)
return definition();
return check_tuple()?multi_assgin():calc();
return check_tuple()?multi_assign():calc();
}

ast parse::expr() {
Expand Down Expand Up @@ -917,7 +917,7 @@ ast parse::multi_scalar() {
return node;
}

ast parse::multi_assgin() {
ast parse::multi_assign() {
ast node(toks[ptr].loc, ast_multi_assign);
node.add(multi_scalar());
match(tok::eq);
Expand Down

0 comments on commit ac49c0d

Please sign in to comment.