Commit a5ae3ec4 authored by Alastair Robertson's avatar Alastair Robertson

Add print_ast()

parent 0ac2f44a
#include "ast.h"
namespace ebpf {
namespace bpftrace {
namespace ast {
void Integer::print_ast(std::ostream &out, unsigned int depth) const
{
out << "int: " << n << std::endl;
}
void Identifier::print_ast(std::ostream &out, unsigned int depth) const
{
out << "ident: " << ident << std::endl;
}
void Statement::print_ast(std::ostream &out, unsigned int depth) const
{
out << "stmt" << std::endl;
out << std::string(depth, ' ');
expr->print_ast(out, depth+1);
}
void Probe::print_ast(std::ostream &out, unsigned int depth) const
{
out << "Probe" << std::endl;
for (Statement *stmt : *stmts) {
out << std::string(depth, ' ');
stmt->print_ast(out, depth+1);
}
}
void Program::print_ast(std::ostream &out, unsigned int depth) const
{
out << "Program" << std::endl;
++depth;
for (Probe *probe : *probes) {
out << std::string(depth, ' ');
probe->print_ast(out, depth+1);
}
}
} // namespace ast
} // namespace bpftrace
} // namespace ebpf
......@@ -2,6 +2,7 @@
#include <string>
#include <vector>
#include <ostream>
namespace ebpf {
namespace bpftrace {
......@@ -10,6 +11,7 @@ namespace ast {
class Node {
public:
virtual ~Node() { }
virtual void print_ast(std::ostream &out, unsigned int depth = 0) const = 0;
};
class Expression : public Node {
......@@ -18,34 +20,30 @@ class Expression : public Node {
class Integer : public Expression {
public:
explicit Integer(int n) : n(n) { }
void print_ast(std::ostream &out, unsigned int depth = 0) const override;
int n;
};
class Identifier : public Expression {
public:
explicit Identifier(std::string &ident) : ident(ident) { }
void print_ast(std::ostream &out, unsigned int depth = 0) const override;
std::string ident;
};
class Statement : public Node {
public:
explicit Statement(Expression *expr) : expr(expr) { }
void print_ast(std::ostream &out, unsigned int depth = 0) const override;
Expression *expr;
};
using StatementList = std::vector<Statement *>;
class PreProcessor : public Node {
public:
explicit PreProcessor(const std::string &line) : line(line) { }
std::string line;
};
using PreProcessorList = std::vector<PreProcessor *>;
class Probe : public Node {
public:
Probe(std::string type, std::string attach_point, StatementList *stmts)
: type(type), attach_point(attach_point), stmts(stmts) { }
void print_ast(std::ostream &out, unsigned int depth = 0) const override;
std::string type;
std::string attach_point;
......@@ -55,9 +53,8 @@ using ProbeList = std::vector<Probe *>;
class Program : public Node {
public:
Program(PreProcessorList *preprocs, ProbeList *probes)
: preprocs(preprocs), probes(probes) { }
PreProcessorList *preprocs;
explicit Program(ProbeList *probes) : probes(probes) { }
void print_ast(std::ostream &out, unsigned int depth = 0) const override;
ProbeList *probes;
};
......
......@@ -25,7 +25,6 @@ using namespace ebpf::bpftrace;
[\n\r]+ { loc.lines(yyleng); loc.step(); }
"//".*$ // Comments
^"#".*$ { return Parser::make_PREPROCESSOR(yytext, loc); }
[_a-zA-Z][_a-zA-Z0-9]* { return Parser::make_IDENT(yytext, loc); }
[0-9]+ { return Parser::make_INT(strtoul(yytext, NULL, 0), loc); }
0[xX][0-9a-fA-F]+ { return Parser::make_INT(strtoul(yytext, NULL, 0), loc); }
......
......@@ -12,8 +12,6 @@ int main(int argc, char *argv[])
result = driver.parse(argv[1]);
}
for (auto &preproc : *(driver.root_->preprocs)) {
std::cout << preproc->line << std::endl;
}
driver.root_->print_ast(std::cout);
return result;
}
......@@ -44,11 +44,9 @@ void yyerror(ebpf::bpftrace::Driver &driver, const char *s);
RBRACE "}"
;
%token <std::string> PREPROCESSOR "preprocessor"
%token <std::string> IDENT "identifier"
%token <int> INT "integer"
%type <ast::PreProcessorList *> preprocs
%type <ast::ProbeList *> probes
%type <ast::StatementList *> block stmts
%type <ast::Probe *> probe
......@@ -61,17 +59,11 @@ void yyerror(ebpf::bpftrace::Driver &driver, const char *s);
%%
program : preprocs probes { driver.root_ = new ast::Program($1, $2); }
| probes
program : probes { driver.root_ = new ast::Program($1); }
;
preprocs : preprocs PREPROCESSOR { $$ = $1; $1->push_back(new ast::PreProcessor($2)); }
| PREPROCESSOR { $$ = new ast::PreProcessorList;
$$->push_back(new ast::PreProcessor($1)); }
probes : probes probe { $$ = $1; $1->push_back($2); }
| probe { $$ = new ast::ProbeList;
$$->push_back($1); }
| probe { $$ = new ast::ProbeList; $$->push_back($1); }
;
probe : IDENT ":" IDENT block { $$ = new ast::Probe($1, $3, $4); }
......
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