Browse Source

Add booleans

master
Danila Fedorin 4 years ago
parent
commit
ede35fe3ba
  1. 2
      parser.y
  2. 13
      tree.cpp
  3. 8
      tree.hpp

2
parser.y

@ -96,7 +96,7 @@ primary_expression
: IDENTIFIER { $$ = new expr_id(*$1); delete $1; }
| FLOAT { $$ = new expr_float(std::stod(*$1)); delete $1; }
| INTEGER { $$ = new expr_int(std::stoi(*$1)); delete $1; }
| BOOLEAN { $$ = new expr_int(*$1 == "True"); delete $1; }
| BOOLEAN { $$ = new expr_bool(*$1 == "True"); delete $1; }
| LPAREN expression RPAREN { $$ = $2; }
;

13
tree.cpp

@ -16,6 +16,11 @@ std::string label<const char*>(const char* v) {
return v;
}
template <>
std::string label<bool>(bool v) {
return v ? "TRUE" : "FALSE";
}
template <>
std::string label<binop>(binop op) {
switch(op) {
@ -67,12 +72,16 @@ void expr_id::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label(std::string("identifier: ") + id, true));
}
void expr_bool::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label(std::string("Boolean: ") + label(val), true));
}
void expr_int::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label(std::string("integer: ") + label(val), true));
into.push_back(prefix + wrap_label(std::string("Integer: ") + label(val), true));
}
void expr_float::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label(std::string("float: ") + label(val), true));
into.push_back(prefix + wrap_label(std::string("Float: ") + label(val), true));
}
void expr_binop::print_dot(dot& into, std::string prefix) {

8
tree.hpp

@ -41,6 +41,14 @@ struct expr_id : expr {
virtual void print_dot(dot& into, std::string prefix);
};
struct expr_bool : expr {
bool val;
expr_bool(bool v) : val(v) {}
virtual void print_dot(dot& into, std::string prefix);
};
struct expr_int : expr {
int val;

Loading…
Cancel
Save