Commit 8822dc45 authored by Alastair Robertson's avatar Alastair Robertson

Add unary operators and parentheses

parent 4aeeaff0
...@@ -85,6 +85,26 @@ void Binop::print_ast(std::ostream &out, unsigned int depth) const ...@@ -85,6 +85,26 @@ void Binop::print_ast(std::ostream &out, unsigned int depth) const
right->print_ast(out, depth+1); right->print_ast(out, depth+1);
} }
void Unop::print_ast(std::ostream &out, unsigned int depth) const
{
std::string indent(depth, ' ');
std::string opstr;
switch (op) {
case ebpf::bpftrace::Parser::token::LNOT:
opstr = "!";
break;
case ebpf::bpftrace::Parser::token::BNOT:
opstr = "~";
break;
default:
opstr = "???";
break;
}
out << indent << opstr << std::endl;
expr->print_ast(out, depth+1);
}
void ExprStatement::print_ast(std::ostream &out, unsigned int depth) const void ExprStatement::print_ast(std::ostream &out, unsigned int depth) const
{ {
std::string indent(depth, ' '); std::string indent(depth, ' ');
......
...@@ -42,6 +42,14 @@ public: ...@@ -42,6 +42,14 @@ public:
int op; int op;
}; };
class Unop : public Expression {
public:
Unop(int op, Expression *expr) : expr(expr), op(op) { }
void print_ast(std::ostream &out, unsigned int depth = 0) const override;
Expression *expr;
int op;
};
class Statement : public Node { class Statement : public Node {
}; };
using StatementList = std::vector<Statement *>; using StatementList = std::vector<Statement *>;
......
...@@ -34,6 +34,8 @@ using namespace ebpf::bpftrace; ...@@ -34,6 +34,8 @@ using namespace ebpf::bpftrace;
"}" { return Parser::make_RBRACE(loc); } "}" { return Parser::make_RBRACE(loc); }
"[" { return Parser::make_LBRACKET(loc); } "[" { return Parser::make_LBRACKET(loc); }
"]" { return Parser::make_RBRACKET(loc); } "]" { return Parser::make_RBRACKET(loc); }
"(" { return Parser::make_LPAREN(loc); }
")" { return Parser::make_RPAREN(loc); }
\//[ \t\n\r]*[\/\{] { return Parser::make_ENDPRED(loc); } // If "/" is followed by "/" or "{", choose ENDPRED, otherwise DIV \//[ \t\n\r]*[\/\{] { return Parser::make_ENDPRED(loc); } // If "/" is followed by "/" or "{", choose ENDPRED, otherwise DIV
"," { return Parser::make_COMMA(loc); } "," { return Parser::make_COMMA(loc); }
"=" { return Parser::make_ASSIGN(loc); } "=" { return Parser::make_ASSIGN(loc); }
......
...@@ -43,6 +43,8 @@ void yyerror(ebpf::bpftrace::Driver &driver, const char *s); ...@@ -43,6 +43,8 @@ void yyerror(ebpf::bpftrace::Driver &driver, const char *s);
RBRACE "}" RBRACE "}"
LBRACKET "[" LBRACKET "["
RBRACKET "]" RBRACKET "]"
LPAREN "("
RPAREN ")"
ENDPRED "end predicate" ENDPRED "end predicate"
COMMA "," COMMA ","
ASSIGN "=" ASSIGN "="
...@@ -119,6 +121,7 @@ stmt : expr { $$ = new ast::ExprStatement($1); } ...@@ -119,6 +121,7 @@ stmt : expr { $$ = new ast::ExprStatement($1); }
expr : INT { $$ = new ast::Integer($1); } expr : INT { $$ = new ast::Integer($1); }
| var { $$ = $1; } | var { $$ = $1; }
| "(" expr ")" { $$ = $2; }
| expr EQ expr { $$ = new ast::Binop($1, token::EQ, $3); } | expr EQ expr { $$ = new ast::Binop($1, token::EQ, $3); }
| expr NE expr { $$ = new ast::Binop($1, token::NE, $3); } | expr NE expr { $$ = new ast::Binop($1, token::NE, $3); }
| expr LE expr { $$ = new ast::Binop($1, token::LE, $3); } | expr LE expr { $$ = new ast::Binop($1, token::LE, $3); }
...@@ -135,6 +138,8 @@ expr : INT { $$ = new ast::Integer($1); } ...@@ -135,6 +138,8 @@ expr : INT { $$ = new ast::Integer($1); }
| expr BAND expr { $$ = new ast::Binop($1, token::BAND, $3); } | expr BAND expr { $$ = new ast::Binop($1, token::BAND, $3); }
| expr BOR expr { $$ = new ast::Binop($1, token::BOR, $3); } | expr BOR expr { $$ = new ast::Binop($1, token::BOR, $3); }
| expr BXOR expr { $$ = new ast::Binop($1, token::BXOR, $3); } | expr BXOR expr { $$ = new ast::Binop($1, token::BXOR, $3); }
| LNOT expr { $$ = new ast::Unop(token::LNOT, $2); }
| BNOT expr { $$ = new ast::Unop(token::BNOT, $2); }
; ;
var : IDENT { $$ = new ast::Variable($1); } var : IDENT { $$ = new ast::Variable($1); }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment