Merge pull request #45 from ValKmjolnir/develop

📝 add tutorial about new operators
This commit is contained in:
ValK 2024-06-07 00:21:42 +08:00 committed by GitHub
commit 0ffa62e5cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 205 additions and 159 deletions

View File

@ -164,6 +164,27 @@ a &= 0xca;
a |= 0xba; a |= 0xba;
``` ```
Operator `??` is used to check left hand side value is `nil` or not, if not,
return the right hand side value:
```javascript
print(nil??"should get this string");
```
The example above will print `should get this string`.
Operator `?.` is used to get hash member if left hand side value is not `nil`.
And if left hand side value is not `nil` and not `hash`,
will cause error and exit.
```javascript
var a = nil;
print(a?.try_get); # nil
var a = {try_get: "congrats!"};
print(a?.try_get); # "congrats!"
```
## Definition ## Definition
As follows. As follows.

View File

@ -159,6 +159,25 @@ a &= 0xca;
a |= 0xba; a |= 0xba;
``` ```
`??` 运算符用于检查左侧值是否为 `nil`,如果不是则返回右侧的值:
```javascript
print(nil??"should get this string");
```
上面的例子会输出 `should get this string`
`?.` 运算符用于先检查左侧不为 `nil`,如果左侧不是 `nil` 则尝试获取 hash 的字段。
当然如果左侧此时也不是 `hash` 类型,则报错退出。
```javascript
var a = nil;
print(a?.try_get); # nil
var a = {try_get: "congrats!"};
print(a?.try_get); # "congrats!"
```
## 定义变量 ## 定义变量
如下所示。 如下所示。

View File

@ -133,7 +133,13 @@ bool ast_dumper::visit_code_block(code_block* node) {
bool ast_dumper::visit_parameter(parameter* node) { bool ast_dumper::visit_parameter(parameter* node) {
dump_indent(); dump_indent();
std::cout << "parameter " << node->get_parameter_name(); std::cout << "parameter ";
switch(node->get_parameter_type()) {
case parameter::kind::normal_parameter: std::cout << "[normal]"; break;
case parameter::kind::dynamic_parameter: std::cout << "[dynamic]"; break;
case parameter::kind::default_parameter: std::cout << "[default]"; break;
}
std::cout << " " << node->get_parameter_name();
std::cout << format_location(node); std::cout << format_location(node);
if (node->get_default_value()) { if (node->get_default_value()) {
push_indent(); push_indent();
@ -169,23 +175,23 @@ bool ast_dumper::visit_binary_operator(binary_operator* node) {
dump_indent(); dump_indent();
std::cout << "binary_operator \""; std::cout << "binary_operator \"";
switch(node->get_operator_type()) { switch(node->get_operator_type()) {
case binary_operator::binary_type::add: std::cout << "+"; break; case binary_operator::kind::add: std::cout << "+"; break;
case binary_operator::binary_type::sub: std::cout << "-"; break; case binary_operator::kind::sub: std::cout << "-"; break;
case binary_operator::binary_type::mult: std::cout << "*"; break; case binary_operator::kind::mult: std::cout << "*"; break;
case binary_operator::binary_type::div: std::cout << "/"; break; case binary_operator::kind::div: std::cout << "/"; break;
case binary_operator::binary_type::concat: std::cout << "~"; break; case binary_operator::kind::concat: std::cout << "~"; break;
case binary_operator::binary_type::bitwise_and: std::cout << "&"; break; case binary_operator::kind::bitwise_and: std::cout << "&"; break;
case binary_operator::binary_type::bitwise_or: std::cout << "|"; break; case binary_operator::kind::bitwise_or: std::cout << "|"; break;
case binary_operator::binary_type::bitwise_xor: std::cout << "^"; break; case binary_operator::kind::bitwise_xor: std::cout << "^"; break;
case binary_operator::binary_type::cmpeq: std::cout << "=="; break; case binary_operator::kind::cmpeq: std::cout << "=="; break;
case binary_operator::binary_type::cmpneq: std::cout << "!="; break; case binary_operator::kind::cmpneq: std::cout << "!="; break;
case binary_operator::binary_type::grt: std::cout << ">"; break; case binary_operator::kind::grt: std::cout << ">"; break;
case binary_operator::binary_type::geq: std::cout << ">="; break; case binary_operator::kind::geq: std::cout << ">="; break;
case binary_operator::binary_type::less: std::cout << "<"; break; case binary_operator::kind::less: std::cout << "<"; break;
case binary_operator::binary_type::leq: std::cout << "<="; break; case binary_operator::kind::leq: std::cout << "<="; break;
case binary_operator::binary_type::condition_and: std::cout << "and"; break; case binary_operator::kind::condition_and: std::cout << "and"; break;
case binary_operator::binary_type::condition_or: std::cout << "or"; break; case binary_operator::kind::condition_or: std::cout << "or"; break;
case binary_operator::binary_type::null_chain: std::cout << "??"; break; case binary_operator::kind::null_chain: std::cout << "??"; break;
} }
std::cout << "\"" << format_location(node); std::cout << "\"" << format_location(node);
push_indent(); push_indent();
@ -204,9 +210,9 @@ bool ast_dumper::visit_unary_operator(unary_operator* node) {
dump_indent(); dump_indent();
std::cout << "unary_operator \""; std::cout << "unary_operator \"";
switch(node->get_operator_type()) { switch(node->get_operator_type()) {
case unary_operator::unary_type::negative: std::cout << "-"; break; case unary_operator::kind::negative: std::cout << "-"; break;
case unary_operator::unary_type::logical_not: std::cout << "!"; break; case unary_operator::kind::logical_not: std::cout << "!"; break;
case unary_operator::unary_type::bitwise_not: std::cout << "~"; break; case unary_operator::kind::bitwise_not: std::cout << "~"; break;
} }
std::cout << "\"" << format_location(node); std::cout << "\"" << format_location(node);
push_indent(); push_indent();
@ -320,15 +326,15 @@ bool ast_dumper::visit_assignment_expr(assignment_expr* node) {
dump_indent(); dump_indent();
std::cout << "assignment "; std::cout << "assignment ";
switch(node->get_assignment_type()) { switch(node->get_assignment_type()) {
case assignment_expr::assign_type::add_equal: std::cout << "+="; break; case assignment_expr::kind::add_equal: std::cout << "+="; break;
case assignment_expr::assign_type::sub_equal: std::cout << "-="; break; case assignment_expr::kind::sub_equal: std::cout << "-="; break;
case assignment_expr::assign_type::mult_equal: std::cout << "*="; break; case assignment_expr::kind::mult_equal: std::cout << "*="; break;
case assignment_expr::assign_type::div_equal: std::cout << "/="; break; case assignment_expr::kind::div_equal: std::cout << "/="; break;
case assignment_expr::assign_type::concat_equal: std::cout << "~="; break; case assignment_expr::kind::concat_equal: std::cout << "~="; break;
case assignment_expr::assign_type::equal: std::cout << "="; break; case assignment_expr::kind::equal: std::cout << "="; break;
case assignment_expr::assign_type::bitwise_and_equal: std::cout << "&="; break; case assignment_expr::kind::bitwise_and_equal: std::cout << "&="; break;
case assignment_expr::assign_type::bitwise_or_equal: std::cout << "|="; break; case assignment_expr::kind::bitwise_or_equal: std::cout << "|="; break;
case assignment_expr::assign_type::bitwise_xor_equal: std::cout << "^="; break; case assignment_expr::kind::bitwise_xor_equal: std::cout << "^="; break;
} }
std::cout << format_location(node); std::cout << format_location(node);
push_indent(); push_indent();
@ -428,7 +434,7 @@ bool ast_dumper::visit_iter_expr(iter_expr* node) {
bool ast_dumper::visit_forei_expr(forei_expr* node) { bool ast_dumper::visit_forei_expr(forei_expr* node) {
dump_indent(); dump_indent();
if (node->get_loop_type()==forei_expr::forei_loop_type::foreach) { if (node->get_loop_type()==forei_expr::kind::foreach) {
std::cout << "foreach"; std::cout << "foreach";
} else { } else {
std::cout << "forindex"; std::cout << "forindex";

View File

@ -240,14 +240,14 @@ public:
class parameter: public expr { class parameter: public expr {
public: public:
enum class param_type { enum class kind {
normal_parameter, normal_parameter,
default_parameter, default_parameter,
dynamic_parameter dynamic_parameter
}; };
private: private:
param_type type; kind type;
std::string name; std::string name;
expr* default_value; expr* default_value;
@ -256,12 +256,12 @@ public:
expr(location, expr_type::ast_param), expr(location, expr_type::ast_param),
name(""), default_value(nullptr) {} name(""), default_value(nullptr) {}
~parameter() override; ~parameter() override;
void set_parameter_type(param_type pt) {type = pt;} void set_parameter_type(kind pt) {type = pt;}
void set_parameter_name(const std::string& pname) {name = pname;} void set_parameter_name(const std::string& pname) {name = pname;}
void set_default_value(expr* node) {default_value = node;} void set_default_value(expr* node) {default_value = node;}
param_type get_parameter_type() {return type;} auto get_parameter_type() {return type;}
const std::string& get_parameter_name() const {return name;} const auto& get_parameter_name() const {return name;}
expr* get_default_value() {return default_value;} auto get_default_value() {return default_value;}
void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
@ -287,7 +287,7 @@ public:
class binary_operator: public expr { class binary_operator: public expr {
public: public:
enum class binary_type { enum class kind {
add, add,
sub, sub,
mult, mult,
@ -308,7 +308,7 @@ public:
}; };
private: private:
binary_type type; kind type;
expr* left; expr* left;
expr* right; expr* right;
number_literal* optimized_const_number; number_literal* optimized_const_number;
@ -321,29 +321,29 @@ public:
optimized_const_number(nullptr), optimized_const_number(nullptr),
optimized_const_string(nullptr) {} optimized_const_string(nullptr) {}
~binary_operator() override; ~binary_operator() override;
void set_operator_type(binary_type operator_type) {type = operator_type;} void set_operator_type(kind operator_type) {type = operator_type;}
void set_left(expr* node) {left = node;} void set_left(expr* node) {left = node;}
void set_right(expr* node) {right = node;} void set_right(expr* node) {right = node;}
void set_optimized_number(number_literal* node) {optimized_const_number = node;} void set_optimized_number(number_literal* node) {optimized_const_number = node;}
void set_optimized_string(string_literal* node) {optimized_const_string = node;} void set_optimized_string(string_literal* node) {optimized_const_string = node;}
binary_type get_operator_type() const {return type;} auto get_operator_type() const {return type;}
expr* get_left() {return left;} auto get_left() {return left;}
expr* get_right() {return right;} auto get_right() {return right;}
number_literal* get_optimized_number() {return optimized_const_number;} auto get_optimized_number() {return optimized_const_number;}
string_literal* get_optimized_string() {return optimized_const_string;} auto get_optimized_string() {return optimized_const_string;}
void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class unary_operator: public expr { class unary_operator: public expr {
public: public:
enum class unary_type { enum class kind {
negative, negative,
logical_not, logical_not,
bitwise_not bitwise_not
}; };
private: private:
unary_type type; kind type;
expr* value; expr* value;
number_literal* optimized_number; number_literal* optimized_number;
@ -352,12 +352,12 @@ public:
expr(location, expr_type::ast_unary), expr(location, expr_type::ast_unary),
value(nullptr), optimized_number(nullptr) {} value(nullptr), optimized_number(nullptr) {}
~unary_operator() override; ~unary_operator() override;
void set_operator_type(unary_type operator_type) {type = operator_type;} void set_operator_type(kind operator_type) {type = operator_type;}
void set_value(expr* node) {value = node;} void set_value(expr* node) {value = node;}
void set_optimized_number(number_literal* node) {optimized_number = node;} void set_optimized_number(number_literal* node) {optimized_number = node;}
unary_type get_operator_type() const {return type;} auto get_operator_type() const {return type;}
expr* get_value() {return value;} auto get_value() {return value;}
number_literal* get_optimized_number() {return optimized_number;} auto get_optimized_number() {return optimized_number;}
void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
@ -473,7 +473,7 @@ public:
class assignment_expr: public expr { class assignment_expr: public expr {
public: public:
enum class assign_type { enum class kind {
equal, equal,
add_equal, add_equal,
sub_equal, sub_equal,
@ -486,7 +486,7 @@ public:
}; };
private: private:
assign_type type; kind type;
expr* left; expr* left;
expr* right; expr* right;
@ -495,12 +495,12 @@ public:
expr(location, expr_type::ast_assign), expr(location, expr_type::ast_assign),
left(nullptr), right(nullptr) {} left(nullptr), right(nullptr) {}
~assignment_expr() override; ~assignment_expr() override;
void set_assignment_type(assign_type operator_type) {type = operator_type;} void set_assignment_type(kind operator_type) {type = operator_type;}
void set_left(expr* node) {left = node;} void set_left(expr* node) {left = node;}
void set_right(expr* node) {right = node;} void set_right(expr* node) {right = node;}
assign_type get_assignment_type() const {return type;} auto get_assignment_type() const {return type;}
expr* get_left() {return left;} auto get_left() {return left;}
expr* get_right() {return right;} auto get_right() {return right;}
void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
@ -611,13 +611,13 @@ public:
class forei_expr: public expr { class forei_expr: public expr {
public: public:
enum class forei_loop_type { enum class kind {
foreach, foreach,
forindex forindex
}; };
private: private:
forei_loop_type type; kind type;
iter_expr* iterator; iter_expr* iterator;
expr* vector_node; expr* vector_node;
code_block* block; code_block* block;
@ -625,17 +625,17 @@ private:
public: public:
forei_expr(const span& location): forei_expr(const span& location):
expr(location, expr_type::ast_forei), expr(location, expr_type::ast_forei),
type(forei_loop_type::foreach), iterator(nullptr), type(kind::foreach), iterator(nullptr),
vector_node(nullptr), block(nullptr) {} vector_node(nullptr), block(nullptr) {}
~forei_expr() override; ~forei_expr() override;
void set_loop_type(forei_loop_type ft) {type = ft;} void set_loop_type(kind ft) {type = ft;}
void set_iterator(iter_expr* node) {iterator = node;} void set_iterator(iter_expr* node) {iterator = node;}
void set_value(expr* node) {vector_node = node;} void set_value(expr* node) {vector_node = node;}
void set_code_block(code_block* node) {block = node;} void set_code_block(code_block* node) {block = node;}
forei_loop_type get_loop_type() const {return type;} auto get_loop_type() const {return type;}
iter_expr* get_iterator() {return iterator;} auto get_iterator() {return iterator;}
expr* get_value() {return vector_node;} auto get_value() {return vector_node;}
code_block* get_code_block() {return block;} auto get_code_block() {return block;}
void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };

View File

@ -209,16 +209,16 @@ void codegen::func_gen(function* node) {
std::unordered_map<std::string, bool> argname; std::unordered_map<std::string, bool> argname;
for(auto tmp : node->get_parameter_list()) { for(auto tmp : node->get_parameter_list()) {
if (tmp->get_parameter_type()== if (tmp->get_parameter_type()==
parameter::param_type::default_parameter) { parameter::kind::default_parameter) {
checked_default = true; checked_default = true;
} else if (tmp->get_parameter_type()== } else if (tmp->get_parameter_type()==
parameter::param_type::dynamic_parameter) { parameter::kind::dynamic_parameter) {
checked_dynamic = true; checked_dynamic = true;
} }
// check default parameter and dynamic parameter // check default parameter and dynamic parameter
if (checked_default && if (checked_default &&
tmp->get_parameter_type()!= tmp->get_parameter_type()!=
parameter::param_type::default_parameter) { parameter::kind::default_parameter) {
die("must use default parameter here", die("must use default parameter here",
tmp->get_location() tmp->get_location()
); );
@ -262,14 +262,14 @@ void codegen::func_gen(function* node) {
} }
regist_string(name); regist_string(name);
switch(tmp->get_parameter_type()) { switch(tmp->get_parameter_type()) {
case parameter::param_type::normal_parameter: case parameter::kind::normal_parameter:
emit(op_para, const_string_map.at(name), tmp->get_location()); emit(op_para, const_string_map.at(name), tmp->get_location());
break; break;
case parameter::param_type::default_parameter: case parameter::kind::default_parameter:
calc_gen(tmp->get_default_value()); calc_gen(tmp->get_default_value());
emit(op_deft, const_string_map.at(name), tmp->get_location()); emit(op_deft, const_string_map.at(name), tmp->get_location());
break; break;
case parameter::param_type::dynamic_parameter: case parameter::kind::dynamic_parameter:
emit(op_dyn, const_string_map.at(name), tmp->get_location()); emit(op_dyn, const_string_map.at(name), tmp->get_location());
break; break;
} }
@ -603,12 +603,12 @@ void codegen::definition_gen(definition_expr* node) {
void codegen::assignment_expression(assignment_expr* node) { void codegen::assignment_expression(assignment_expr* node) {
switch(node->get_assignment_type()) { switch(node->get_assignment_type()) {
case assignment_expr::assign_type::equal: case assignment_expr::kind::equal:
calc_gen(node->get_right()); calc_gen(node->get_right());
mcall(node->get_left()); mcall(node->get_left());
emit(op_meq, 0, node->get_location()); emit(op_meq, 0, node->get_location());
break; break;
case assignment_expr::assign_type::add_equal: case assignment_expr::kind::add_equal:
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
calc_gen(node->get_right()); calc_gen(node->get_right());
} }
@ -622,7 +622,7 @@ void codegen::assignment_expression(assignment_expr* node) {
emit(op_addeqc, const_number_map[num], node->get_location()); emit(op_addeqc, const_number_map[num], node->get_location());
} }
break; break;
case assignment_expr::assign_type::sub_equal: case assignment_expr::kind::sub_equal:
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
calc_gen(node->get_right()); calc_gen(node->get_right());
} }
@ -636,7 +636,7 @@ void codegen::assignment_expression(assignment_expr* node) {
emit(op_subeqc, const_number_map[num], node->get_location()); emit(op_subeqc, const_number_map[num], node->get_location());
} }
break; break;
case assignment_expr::assign_type::mult_equal: case assignment_expr::kind::mult_equal:
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
calc_gen(node->get_right()); calc_gen(node->get_right());
} }
@ -650,7 +650,7 @@ void codegen::assignment_expression(assignment_expr* node) {
emit(op_muleqc, const_number_map[num], node->get_location()); emit(op_muleqc, const_number_map[num], node->get_location());
} }
break; break;
case assignment_expr::assign_type::div_equal: case assignment_expr::kind::div_equal:
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
calc_gen(node->get_right()); calc_gen(node->get_right());
} }
@ -664,7 +664,7 @@ void codegen::assignment_expression(assignment_expr* node) {
emit(op_diveqc, const_number_map[num], node->get_location()); emit(op_diveqc, const_number_map[num], node->get_location());
} }
break; break;
case assignment_expr::assign_type::concat_equal: case assignment_expr::kind::concat_equal:
if (node->get_right()->get_type()!=expr_type::ast_str) { if (node->get_right()->get_type()!=expr_type::ast_str) {
calc_gen(node->get_right()); calc_gen(node->get_right());
} }
@ -678,17 +678,17 @@ void codegen::assignment_expression(assignment_expr* node) {
emit(op_lnkeqc, const_string_map[str], node->get_location()); emit(op_lnkeqc, const_string_map[str], node->get_location());
} }
break; break;
case assignment_expr::assign_type::bitwise_and_equal: case assignment_expr::kind::bitwise_and_equal:
calc_gen(node->get_right()); calc_gen(node->get_right());
mcall(node->get_left()); mcall(node->get_left());
emit(op_btandeq, 0, node->get_location()); emit(op_btandeq, 0, node->get_location());
break; break;
case assignment_expr::assign_type::bitwise_or_equal: case assignment_expr::kind::bitwise_or_equal:
calc_gen(node->get_right()); calc_gen(node->get_right());
mcall(node->get_left()); mcall(node->get_left());
emit(op_btoreq, 0, node->get_location()); emit(op_btoreq, 0, node->get_location());
break; break;
case assignment_expr::assign_type::bitwise_xor_equal: case assignment_expr::kind::bitwise_xor_equal:
calc_gen(node->get_right()); calc_gen(node->get_right());
mcall(node->get_left()); mcall(node->get_left());
emit(op_btxoreq, 0, node->get_location()); emit(op_btxoreq, 0, node->get_location());
@ -740,17 +740,17 @@ void codegen::replace_left_assignment_with_load(const span& location) {
void codegen::assignment_statement(assignment_expr* node) { void codegen::assignment_statement(assignment_expr* node) {
switch(node->get_assignment_type()) { switch(node->get_assignment_type()) {
case assignment_expr::assign_type::equal: case assignment_expr::kind::equal:
gen_assignment_equal_statement(node); gen_assignment_equal_statement(node);
break; break;
case assignment_expr::assign_type::add_equal: case assignment_expr::kind::add_equal:
case assignment_expr::assign_type::sub_equal: case assignment_expr::kind::sub_equal:
case assignment_expr::assign_type::mult_equal: case assignment_expr::kind::mult_equal:
case assignment_expr::assign_type::div_equal: case assignment_expr::kind::div_equal:
case assignment_expr::assign_type::concat_equal: case assignment_expr::kind::concat_equal:
case assignment_expr::assign_type::bitwise_and_equal: case assignment_expr::kind::bitwise_and_equal:
case assignment_expr::assign_type::bitwise_or_equal: case assignment_expr::kind::bitwise_or_equal:
case assignment_expr::assign_type::bitwise_xor_equal: case assignment_expr::kind::bitwise_xor_equal:
calc_gen(node); calc_gen(node);
if (op_addeq<=code.back().op && code.back().op<=op_btxoreq) { if (op_addeq<=code.back().op && code.back().op<=op_btxoreq) {
code.back().num = 1; code.back().num = 1;
@ -920,7 +920,7 @@ void codegen::forei_gen(forei_expr* node) {
calc_gen(node->get_value()); calc_gen(node->get_value());
emit(op_cnt, 0, node->get_value()->get_location()); emit(op_cnt, 0, node->get_value()->get_location());
usize loop_begin = code.size(); usize loop_begin = code.size();
if (node->get_loop_type()==forei_expr::forei_loop_type::forindex) { if (node->get_loop_type()==forei_expr::kind::forindex) {
emit(op_findex, 0, node->get_location()); emit(op_findex, 0, node->get_location());
} else { } else {
emit(op_feach, 0, node->get_location()); emit(op_feach, 0, node->get_location());
@ -1032,11 +1032,11 @@ void codegen::unary_gen(unary_operator* node) {
calc_gen(node->get_value()); calc_gen(node->get_value());
switch(node->get_operator_type()) { switch(node->get_operator_type()) {
case unary_operator::unary_type::negative: case unary_operator::kind::negative:
emit(op_usub, 0, node->get_location()); break; emit(op_usub, 0, node->get_location()); break;
case unary_operator::unary_type::logical_not: case unary_operator::kind::logical_not:
emit(op_lnot, 0, node->get_location()); break; emit(op_lnot, 0, node->get_location()); break;
case unary_operator::unary_type::bitwise_not: case unary_operator::kind::bitwise_not:
emit(op_bnot, 0, node->get_location()); break; emit(op_bnot, 0, node->get_location()); break;
} }
} }
@ -1053,43 +1053,43 @@ void codegen::binary_gen(binary_operator* node) {
} }
switch(node->get_operator_type()) { switch(node->get_operator_type()) {
case binary_operator::binary_type::condition_or: or_gen(node); return; case binary_operator::kind::condition_or: or_gen(node); return;
case binary_operator::binary_type::condition_and: and_gen(node); return; case binary_operator::kind::condition_and: and_gen(node); return;
default: break; default: break;
} }
switch(node->get_operator_type()) { switch(node->get_operator_type()) {
case binary_operator::binary_type::cmpeq: case binary_operator::kind::cmpeq:
calc_gen(node->get_left()); calc_gen(node->get_left());
calc_gen(node->get_right()); calc_gen(node->get_right());
emit(op_eq, 0, node->get_location()); emit(op_eq, 0, node->get_location());
return; return;
case binary_operator::binary_type::cmpneq: case binary_operator::kind::cmpneq:
calc_gen(node->get_left()); calc_gen(node->get_left());
calc_gen(node->get_right()); calc_gen(node->get_right());
emit(op_neq, 0, node->get_location()); emit(op_neq, 0, node->get_location());
return; return;
case binary_operator::binary_type::bitwise_or: case binary_operator::kind::bitwise_or:
calc_gen(node->get_left()); calc_gen(node->get_left());
calc_gen(node->get_right()); calc_gen(node->get_right());
emit(op_btor, 0, node->get_location()); emit(op_btor, 0, node->get_location());
return; return;
case binary_operator::binary_type::bitwise_xor: case binary_operator::kind::bitwise_xor:
calc_gen(node->get_left()); calc_gen(node->get_left());
calc_gen(node->get_right()); calc_gen(node->get_right());
emit(op_btxor, 0, node->get_location()); emit(op_btxor, 0, node->get_location());
return; return;
case binary_operator::binary_type::bitwise_and: case binary_operator::kind::bitwise_and:
calc_gen(node->get_left()); calc_gen(node->get_left());
calc_gen(node->get_right()); calc_gen(node->get_right());
emit(op_btand, 0, node->get_location()); emit(op_btand, 0, node->get_location());
return; return;
case binary_operator::binary_type::null_chain: case binary_operator::kind::null_chain:
null_chain_gen(node); null_chain_gen(node);
return; return;
default: break; default: break;
} }
switch(node->get_operator_type()) { switch(node->get_operator_type()) {
case binary_operator::binary_type::add: case binary_operator::kind::add:
calc_gen(node->get_left()); calc_gen(node->get_left());
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
calc_gen(node->get_right()); calc_gen(node->get_right());
@ -1101,7 +1101,7 @@ void codegen::binary_gen(binary_operator* node) {
emit(op_addc, const_number_map.at(num), node->get_location()); emit(op_addc, const_number_map.at(num), node->get_location());
} }
return; return;
case binary_operator::binary_type::sub: case binary_operator::kind::sub:
calc_gen(node->get_left()); calc_gen(node->get_left());
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
calc_gen(node->get_right()); calc_gen(node->get_right());
@ -1113,7 +1113,7 @@ void codegen::binary_gen(binary_operator* node) {
emit(op_subc, const_number_map.at(num), node->get_location()); emit(op_subc, const_number_map.at(num), node->get_location());
} }
return; return;
case binary_operator::binary_type::mult: case binary_operator::kind::mult:
calc_gen(node->get_left()); calc_gen(node->get_left());
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
calc_gen(node->get_right()); calc_gen(node->get_right());
@ -1125,7 +1125,7 @@ void codegen::binary_gen(binary_operator* node) {
emit(op_mulc, const_number_map.at(num), node->get_location()); emit(op_mulc, const_number_map.at(num), node->get_location());
} }
return; return;
case binary_operator::binary_type::div: case binary_operator::kind::div:
calc_gen(node->get_left()); calc_gen(node->get_left());
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
calc_gen(node->get_right()); calc_gen(node->get_right());
@ -1137,7 +1137,7 @@ void codegen::binary_gen(binary_operator* node) {
emit(op_divc, const_number_map.at(num), node->get_location()); emit(op_divc, const_number_map.at(num), node->get_location());
} }
return; return;
case binary_operator::binary_type::concat: case binary_operator::kind::concat:
calc_gen(node->get_left()); calc_gen(node->get_left());
if (node->get_right()->get_type()!=expr_type::ast_str) { if (node->get_right()->get_type()!=expr_type::ast_str) {
calc_gen(node->get_right()); calc_gen(node->get_right());
@ -1149,7 +1149,7 @@ void codegen::binary_gen(binary_operator* node) {
emit(op_lnkc, const_string_map.at(str), node->get_location()); emit(op_lnkc, const_string_map.at(str), node->get_location());
} }
break; break;
case binary_operator::binary_type::less: case binary_operator::kind::less:
calc_gen(node->get_left()); calc_gen(node->get_left());
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
calc_gen(node->get_right()); calc_gen(node->get_right());
@ -1161,7 +1161,7 @@ void codegen::binary_gen(binary_operator* node) {
emit(op_lessc, const_number_map.at(num), node->get_location()); emit(op_lessc, const_number_map.at(num), node->get_location());
} }
return; return;
case binary_operator::binary_type::leq: case binary_operator::kind::leq:
calc_gen(node->get_left()); calc_gen(node->get_left());
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
calc_gen(node->get_right()); calc_gen(node->get_right());
@ -1173,7 +1173,7 @@ void codegen::binary_gen(binary_operator* node) {
emit(op_leqc, const_number_map.at(num), node->get_location()); emit(op_leqc, const_number_map.at(num), node->get_location());
} }
return; return;
case binary_operator::binary_type::grt: case binary_operator::kind::grt:
calc_gen(node->get_left()); calc_gen(node->get_left());
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
calc_gen(node->get_right()); calc_gen(node->get_right());
@ -1185,7 +1185,7 @@ void codegen::binary_gen(binary_operator* node) {
emit(op_grtc, const_number_map.at(num), node->get_location()); emit(op_grtc, const_number_map.at(num), node->get_location());
} }
return; return;
case binary_operator::binary_type::geq: case binary_operator::kind::geq:
calc_gen(node->get_left()); calc_gen(node->get_left());
if (node->get_right()->get_type()!=expr_type::ast_num) { if (node->get_right()->get_type()!=expr_type::ast_num) {
calc_gen(node->get_right()); calc_gen(node->get_right());

View File

@ -347,13 +347,13 @@ void parse::params(function* func_node) {
match(tok::tk_id); match(tok::tk_id);
if (lookahead(tok::tk_eq)) { if (lookahead(tok::tk_eq)) {
match(tok::tk_eq); match(tok::tk_eq);
param->set_parameter_type(parameter::param_type::default_parameter); param->set_parameter_type(parameter::kind::default_parameter);
param->set_default_value(calc()); param->set_default_value(calc());
} else if (lookahead(tok::tk_ellipsis)) { } else if (lookahead(tok::tk_ellipsis)) {
match(tok::tk_ellipsis); match(tok::tk_ellipsis);
param->set_parameter_type(parameter::param_type::dynamic_parameter); param->set_parameter_type(parameter::kind::dynamic_parameter);
} else { } else {
param->set_parameter_type(parameter::param_type::normal_parameter); param->set_parameter_type(parameter::kind::normal_parameter);
} }
update_location(param); update_location(param);
func_node->add_parameter(param); func_node->add_parameter(param);
@ -462,12 +462,12 @@ expr* parse::calc() {
} else if (tok::tk_eq<=toks[ptr].type && toks[ptr].type<=tok::tk_lnkeq) { } else if (tok::tk_eq<=toks[ptr].type && toks[ptr].type<=tok::tk_lnkeq) {
auto tmp = new assignment_expr(toks[ptr].loc); auto tmp = new assignment_expr(toks[ptr].loc);
switch(toks[ptr].type) { switch(toks[ptr].type) {
case tok::tk_eq: tmp->set_assignment_type(assignment_expr::assign_type::equal); break; case tok::tk_eq: tmp->set_assignment_type(assignment_expr::kind::equal); break;
case tok::tk_addeq: tmp->set_assignment_type(assignment_expr::assign_type::add_equal); break; case tok::tk_addeq: tmp->set_assignment_type(assignment_expr::kind::add_equal); break;
case tok::tk_subeq: tmp->set_assignment_type(assignment_expr::assign_type::sub_equal); break; case tok::tk_subeq: tmp->set_assignment_type(assignment_expr::kind::sub_equal); break;
case tok::tk_multeq: tmp->set_assignment_type(assignment_expr::assign_type::mult_equal); break; case tok::tk_multeq: tmp->set_assignment_type(assignment_expr::kind::mult_equal); break;
case tok::tk_diveq: tmp->set_assignment_type(assignment_expr::assign_type::div_equal); break; case tok::tk_diveq: tmp->set_assignment_type(assignment_expr::kind::div_equal); break;
case tok::tk_lnkeq: tmp->set_assignment_type(assignment_expr::assign_type::concat_equal); break; case tok::tk_lnkeq: tmp->set_assignment_type(assignment_expr::kind::concat_equal); break;
default: break; default: break;
} }
tmp->set_left(node); tmp->set_left(node);
@ -479,9 +479,9 @@ expr* parse::calc() {
toks[ptr].type==tok::tk_btxoreq) { toks[ptr].type==tok::tk_btxoreq) {
auto tmp = new assignment_expr(toks[ptr].loc); auto tmp = new assignment_expr(toks[ptr].loc);
switch(toks[ptr].type) { switch(toks[ptr].type) {
case tok::tk_btandeq: tmp->set_assignment_type(assignment_expr::assign_type::bitwise_and_equal); break; case tok::tk_btandeq: tmp->set_assignment_type(assignment_expr::kind::bitwise_and_equal); break;
case tok::tk_btoreq: tmp->set_assignment_type(assignment_expr::assign_type::bitwise_or_equal); break; case tok::tk_btoreq: tmp->set_assignment_type(assignment_expr::kind::bitwise_or_equal); break;
case tok::tk_btxoreq: tmp->set_assignment_type(assignment_expr::assign_type::bitwise_xor_equal); break; case tok::tk_btxoreq: tmp->set_assignment_type(assignment_expr::kind::bitwise_xor_equal); break;
default: break; default: break;
} }
tmp->set_left(node); tmp->set_left(node);
@ -497,7 +497,7 @@ expr* parse::bitwise_or() {
auto node = bitwise_xor(); auto node = bitwise_xor();
while(lookahead(tok::tk_btor)) { while(lookahead(tok::tk_btor)) {
auto tmp = new binary_operator(toks[ptr].loc); auto tmp = new binary_operator(toks[ptr].loc);
tmp->set_operator_type(binary_operator::binary_type::bitwise_or); tmp->set_operator_type(binary_operator::kind::bitwise_or);
tmp->set_left(node); tmp->set_left(node);
match(tok::tk_btor); match(tok::tk_btor);
tmp->set_right(bitwise_xor()); tmp->set_right(bitwise_xor());
@ -512,7 +512,7 @@ expr* parse::bitwise_xor() {
auto node = bitwise_and(); auto node = bitwise_and();
while(lookahead(tok::tk_btxor)) { while(lookahead(tok::tk_btxor)) {
auto tmp = new binary_operator(toks[ptr].loc); auto tmp = new binary_operator(toks[ptr].loc);
tmp->set_operator_type(binary_operator::binary_type::bitwise_xor); tmp->set_operator_type(binary_operator::kind::bitwise_xor);
tmp->set_left(node); tmp->set_left(node);
match(tok::tk_btxor); match(tok::tk_btxor);
tmp->set_right(bitwise_and()); tmp->set_right(bitwise_and());
@ -527,7 +527,7 @@ expr* parse::bitwise_and() {
auto node = or_expr(); auto node = or_expr();
while(lookahead(tok::tk_btand)) { while(lookahead(tok::tk_btand)) {
auto tmp = new binary_operator(toks[ptr].loc); auto tmp = new binary_operator(toks[ptr].loc);
tmp->set_operator_type(binary_operator::binary_type::bitwise_and); tmp->set_operator_type(binary_operator::kind::bitwise_and);
tmp->set_left(node); tmp->set_left(node);
match(tok::tk_btand); match(tok::tk_btand);
tmp->set_right(or_expr()); tmp->set_right(or_expr());
@ -542,7 +542,7 @@ expr* parse::or_expr() {
auto node = and_expr(); auto node = and_expr();
while(lookahead(tok::tk_or)) { while(lookahead(tok::tk_or)) {
auto tmp = new binary_operator(toks[ptr].loc); auto tmp = new binary_operator(toks[ptr].loc);
tmp->set_operator_type(binary_operator::binary_type::condition_or); tmp->set_operator_type(binary_operator::kind::condition_or);
tmp->set_left(node); tmp->set_left(node);
match(tok::tk_or); match(tok::tk_or);
tmp->set_right(and_expr()); tmp->set_right(and_expr());
@ -557,7 +557,7 @@ expr* parse::and_expr() {
auto node = cmp_expr(); auto node = cmp_expr();
while(lookahead(tok::tk_and)) { while(lookahead(tok::tk_and)) {
auto tmp = new binary_operator(toks[ptr].loc); auto tmp = new binary_operator(toks[ptr].loc);
tmp->set_operator_type(binary_operator::binary_type::condition_and); tmp->set_operator_type(binary_operator::kind::condition_and);
tmp->set_left(node); tmp->set_left(node);
match(tok::tk_and); match(tok::tk_and);
tmp->set_right(cmp_expr()); tmp->set_right(cmp_expr());
@ -573,12 +573,12 @@ expr* parse::cmp_expr() {
while(tok::tk_cmpeq<=toks[ptr].type && toks[ptr].type<=tok::tk_geq) { while(tok::tk_cmpeq<=toks[ptr].type && toks[ptr].type<=tok::tk_geq) {
auto tmp = new binary_operator(toks[ptr].loc); auto tmp = new binary_operator(toks[ptr].loc);
switch(toks[ptr].type) { switch(toks[ptr].type) {
case tok::tk_cmpeq: tmp->set_operator_type(binary_operator::binary_type::cmpeq); break; case tok::tk_cmpeq: tmp->set_operator_type(binary_operator::kind::cmpeq); break;
case tok::tk_neq: tmp->set_operator_type(binary_operator::binary_type::cmpneq); break; case tok::tk_neq: tmp->set_operator_type(binary_operator::kind::cmpneq); break;
case tok::tk_less: tmp->set_operator_type(binary_operator::binary_type::less); break; case tok::tk_less: tmp->set_operator_type(binary_operator::kind::less); break;
case tok::tk_leq: tmp->set_operator_type(binary_operator::binary_type::leq); break; case tok::tk_leq: tmp->set_operator_type(binary_operator::kind::leq); break;
case tok::tk_grt: tmp->set_operator_type(binary_operator::binary_type::grt); break; case tok::tk_grt: tmp->set_operator_type(binary_operator::kind::grt); break;
case tok::tk_geq: tmp->set_operator_type(binary_operator::binary_type::geq); break; case tok::tk_geq: tmp->set_operator_type(binary_operator::kind::geq); break;
default: break; default: break;
} }
tmp->set_left(node); tmp->set_left(node);
@ -595,7 +595,7 @@ expr* parse::null_chain_expr() {
auto node = additive_expr(); auto node = additive_expr();
while(lookahead(tok::tk_quesques)) { while(lookahead(tok::tk_quesques)) {
auto tmp = new binary_operator(toks[ptr].loc); auto tmp = new binary_operator(toks[ptr].loc);
tmp->set_operator_type(binary_operator::binary_type::null_chain); tmp->set_operator_type(binary_operator::kind::null_chain);
tmp->set_left(node); tmp->set_left(node);
match(tok::tk_quesques); match(tok::tk_quesques);
tmp->set_right(additive_expr()); tmp->set_right(additive_expr());
@ -613,9 +613,9 @@ expr* parse::additive_expr() {
lookahead(tok::tk_floater)) { lookahead(tok::tk_floater)) {
auto tmp = new binary_operator(toks[ptr].loc); auto tmp = new binary_operator(toks[ptr].loc);
switch(toks[ptr].type) { switch(toks[ptr].type) {
case tok::tk_add: tmp->set_operator_type(binary_operator::binary_type::add); break; case tok::tk_add: tmp->set_operator_type(binary_operator::kind::add); break;
case tok::tk_sub: tmp->set_operator_type(binary_operator::binary_type::sub); break; case tok::tk_sub: tmp->set_operator_type(binary_operator::kind::sub); break;
case tok::tk_floater: tmp->set_operator_type(binary_operator::binary_type::concat); break; case tok::tk_floater: tmp->set_operator_type(binary_operator::kind::concat); break;
default: break; default: break;
} }
tmp->set_left(node); tmp->set_left(node);
@ -635,9 +635,9 @@ expr* parse::multive_expr() {
while(lookahead(tok::tk_mult) || lookahead(tok::tk_div)) { while(lookahead(tok::tk_mult) || lookahead(tok::tk_div)) {
auto tmp = new binary_operator(toks[ptr].loc); auto tmp = new binary_operator(toks[ptr].loc);
if (lookahead(tok::tk_mult)) { if (lookahead(tok::tk_mult)) {
tmp->set_operator_type(binary_operator::binary_type::mult); tmp->set_operator_type(binary_operator::kind::mult);
} else { } else {
tmp->set_operator_type(binary_operator::binary_type::div); tmp->set_operator_type(binary_operator::kind::div);
} }
tmp->set_left(node); tmp->set_left(node);
match(toks[ptr].type); match(toks[ptr].type);
@ -657,15 +657,15 @@ unary_operator* parse::unary() {
auto node = new unary_operator(toks[ptr].loc); auto node = new unary_operator(toks[ptr].loc);
switch(toks[ptr].type) { switch(toks[ptr].type) {
case tok::tk_sub: case tok::tk_sub:
node->set_operator_type(unary_operator::unary_type::negative); node->set_operator_type(unary_operator::kind::negative);
match(tok::tk_sub); match(tok::tk_sub);
break; break;
case tok::tk_not: case tok::tk_not:
node->set_operator_type(unary_operator::unary_type::logical_not); node->set_operator_type(unary_operator::kind::logical_not);
match(tok::tk_not); match(tok::tk_not);
break; break;
case tok::tk_floater: case tok::tk_floater:
node->set_operator_type(unary_operator::unary_type::bitwise_not); node->set_operator_type(unary_operator::kind::bitwise_not);
match(tok::tk_floater); match(tok::tk_floater);
break; break;
default: break; default: break;
@ -1018,11 +1018,11 @@ forei_expr* parse::forei_loop() {
auto node = new forei_expr(toks[ptr].loc); auto node = new forei_expr(toks[ptr].loc);
switch(toks[ptr].type) { switch(toks[ptr].type) {
case tok::tk_forindex: case tok::tk_forindex:
node->set_loop_type(forei_expr::forei_loop_type::forindex); node->set_loop_type(forei_expr::kind::forindex);
match(tok::tk_forindex); match(tok::tk_forindex);
break; break;
case tok::tk_foreach: case tok::tk_foreach:
node->set_loop_type(forei_expr::forei_loop_type::foreach); node->set_loop_type(forei_expr::kind::foreach);
match(tok::tk_foreach); match(tok::tk_foreach);
break; break;
default: break; default: break;

View File

@ -6,7 +6,7 @@ void optimizer::const_string(
binary_operator* node, binary_operator* node,
string_literal* left_node, string_literal* left_node,
string_literal* right_node) { string_literal* right_node) {
if (node->get_operator_type()!=binary_operator::binary_type::concat) { if (node->get_operator_type()!=binary_operator::kind::concat) {
return; return;
} }
const auto& left = left_node->get_content(); const auto& left = left_node->get_content();
@ -24,19 +24,19 @@ void optimizer::const_number(
const auto right = right_node->get_number(); const auto right = right_node->get_number();
f64 res; f64 res;
switch(node->get_operator_type()) { switch(node->get_operator_type()) {
case binary_operator::binary_type::add: res = left+right; break; case binary_operator::kind::add: res = left+right; break;
case binary_operator::binary_type::sub: res = left-right; break; case binary_operator::kind::sub: res = left-right; break;
case binary_operator::binary_type::mult: res = left*right; break; case binary_operator::kind::mult: res = left*right; break;
case binary_operator::binary_type::div: res = left/right; break; case binary_operator::kind::div: res = left/right; break;
case binary_operator::binary_type::less: res = left<right; break; case binary_operator::kind::less: res = left<right; break;
case binary_operator::binary_type::leq: res = left<=right; break; case binary_operator::kind::leq: res = left<=right; break;
case binary_operator::binary_type::grt: res = left>right; break; case binary_operator::kind::grt: res = left>right; break;
case binary_operator::binary_type::geq: res = left>=right; break; case binary_operator::kind::geq: res = left>=right; break;
case binary_operator::binary_type::bitwise_or: case binary_operator::kind::bitwise_or:
res = static_cast<i32>(left)|static_cast<i32>(right); break; res = static_cast<i32>(left)|static_cast<i32>(right); break;
case binary_operator::binary_type::bitwise_xor: case binary_operator::kind::bitwise_xor:
res = static_cast<i32>(left)^static_cast<i32>(right); break; res = static_cast<i32>(left)^static_cast<i32>(right); break;
case binary_operator::binary_type::bitwise_and: case binary_operator::kind::bitwise_and:
res = static_cast<i32>(left)&static_cast<i32>(right); break; res = static_cast<i32>(left)&static_cast<i32>(right); break;
default: return; default: return;
} }
@ -53,11 +53,11 @@ void optimizer::const_number(
number_literal* value_node) { number_literal* value_node) {
auto res = value_node->get_number(); auto res = value_node->get_number();
switch(node->get_operator_type()) { switch(node->get_operator_type()) {
case unary_operator::unary_type::negative: case unary_operator::kind::negative:
res = -res; break; res = -res; break;
case unary_operator::unary_type::bitwise_not: case unary_operator::kind::bitwise_not:
res = ~static_cast<i32>(res); break; res = ~static_cast<i32>(res); break;
case unary_operator::unary_type::logical_not: case unary_operator::kind::logical_not:
res = !res; break; res = !res; break;
} }
if (std::isinf(res) || std::isnan(res)) { if (std::isinf(res) || std::isnan(res)) {