Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init branch with slight changes to the code in gapc.cc and signature.cc #219

Closed
wants to merge 16 commits into from
Closed
9 changes: 5 additions & 4 deletions src/cpp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,10 @@ void Printer::Cpp::print_subseq_typedef(const AST &ast) {
<< ", unsigned> TUSubsequence;\n\n";
}

void Printer::Cpp::enum_graph_print() {
stream << indent() << "unsigned int parentID" << endl;
}


void Printer::Cpp::header(const AST &ast) {
if (!ast.code_mode().subopt_buddy()) {
Expand Down Expand Up @@ -2482,7 +2486,6 @@ void Printer::Cpp::backtrack_footer(const AST &ast) {
print_subopt_fn(ast);
}


void Printer::Cpp::print_value_pp(const AST &ast) {
stream << indent() << "template <typename Value>";
stream << " void print_result(std::ostream &out, "
Expand All @@ -2506,9 +2509,8 @@ void Printer::Cpp::print_value_pp(const AST &ast) {
stream << indent() << "} else {" << endl;
inc_indent();
stream << indent() << "out << res << '\\n';" << endl;
dec_indent();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a general error? But if I look at the line below, we are closing the block via }. Why should we increase instead of decrease the indentation?

indent();
stream << indent() << '}' << endl;

dec_indent();
stream << indent() << '}' << endl << endl;
}
Expand All @@ -2519,7 +2521,6 @@ void Printer::Cpp::close_class() {
stream << indent() << "};" << endl << endl;
}


void Printer::Cpp::typedefs(Code::Gen &code) {
stream << "#ifndef NO_GAPC_TYPEDEFS" << endl;
stream << indent() << "namespace gapc {" << endl;
Expand Down
2 changes: 2 additions & 0 deletions src/cpp.hh
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class Cpp : public Base {

void print(const Type::Multi &expr);

void enum_graph_print();
void header(const AST &ast);
void header_footer(const AST &ast);
void footer(const AST &ast);
Expand All @@ -199,6 +200,7 @@ class Cpp : public Base {

public:
void backtrack_footer(const AST &ast);
void backtrack_tree_footer(const AST &ast);

private:
void print(const std::list<Statement::Var_Decl*> &l);
Expand Down
7 changes: 6 additions & 1 deletion src/gapc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ class Main {
driver.ast.set_outside_nt_list(&opts.outside_nt_list);
driver.parse_product(opts.product);


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert empty lines



if (driver.is_failing()) {
throw LogError("Seen parse errors.");
}
Expand Down Expand Up @@ -450,6 +453,7 @@ class Main {
*/
void back(Instance *i = 0, Instance *instance_buddy = 0) {
Instance *instance = i;

if (!i || instance_buddy) {
if (opts.backtrack || opts.subopt || opts.kbacktrack) {
instance = driver.ast.split_instance_for_backtrack(opts.instance);
Expand Down Expand Up @@ -679,6 +683,8 @@ class Main {
+ opts.plot_grammar_file + " > foo.pdf' to generate a PDF.");
}



driver.ast.set_class_name(opts.class_name);


Expand Down Expand Up @@ -741,7 +747,6 @@ class Main {
}

hh.backtrack_footer(driver.ast);

hh.close_class();
}

Expand Down
3 changes: 2 additions & 1 deletion src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,8 @@ algebra: algebra_head '{' fn_defs '}'
\begin{lstlisting}
automatic_specifier:
@enum@ |
@count@
@count@ |
@trees@
;
\end{lstlisting}
The {\tt automatic} keyword specifies the auto generation of the
Expand Down
154 changes: 154 additions & 0 deletions src/signature.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,14 @@ Algebra *Signature::generate(std::string *n, std::string *mode) {
return generate_count(n);
if (*mode == "enum")
return generate_enum(n);
if (*mode == "trees")
return generate_trees(n);
return NULL;
}




Comment on lines +158 to +159
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

struct Generate_Stmts {
virtual void apply(Fn_Def &fn) const = 0;
virtual ~Generate_Stmts() {}
Expand Down Expand Up @@ -413,6 +417,156 @@ Algebra *Signature::generate_enum(std::string *n) {
}



//------------------------------------------------------------------------------------------------------------


struct Generate_Tree_Stmts : public Generate_Stmts {
private:
// closes nodes for simple tracks
void apply(std::list<Statement::Base*> &l, Para_Decl::Simple *s,
Statement::Var_Decl *&cur) const {
Statement::Fn_Call *f = new Statement::Fn_Call(
Statement::Fn_Call::STR_APPEND);
f->add_arg(*cur);
f->add_arg(s->name());
l.push_back(f);
}

// Generating Child Nodes for grammar operations in Multitrack
void apply(std::list<Statement::Base*> &l, Para_Decl::Multi *m,
Statement::Var_Decl *&cur) const {
Statement::Fn_Call * f = new Statement::Fn_Call(
Statement::Fn_Call::STR_APPEND);
f->add_arg(*cur);
f->add_arg(new Expr::Const("child {node {"));
l.push_back(f);

const std::list<Para_Decl::Simple*> &p = m->list();
std::list<Para_Decl::Simple*>::const_iterator j = p.begin();
if (j != p.end()) {
apply(l, *j, cur);
++j;
}
for (; j != p.end(); ++j) {
f = new Statement::Fn_Call(Statement::Fn_Call::STR_APPEND);
f->add_arg(*cur);
f->add_arg(new Expr::Const(", "));
f->add_arg(new Expr::Const(2));
l.push_back(f);
apply(l, *j, cur);
}

f = new Statement::Fn_Call(Statement::Fn_Call::STR_APPEND);
f->add_arg(*cur);
f->add_arg(new Expr::Const("}}"));
f->add_arg(new Expr::Const(2));
l.push_back(f);
}


void apply(std::list<Statement::Base*> &l,
const std::list<Para_Decl::Base*> &paras,
Statement::Var_Decl *&cur) const {
std::list<Statement::Base*> apps;
unsigned int a = 0;
for (std::list<Para_Decl::Base*>::const_iterator i = paras.begin();
i != paras.end(); ++i) {
if (a > 0 && !(a % 3)) {
std::ostringstream o;
o << "ret_" << a;
Type::External *str = new Type::External("Rope");
Statement::Var_Decl *t = new Statement::Var_Decl(str, o.str());
l.push_back(t);
Statement::Fn_Call *f =
new Statement::Fn_Call(Statement::Fn_Call::STR_APPEND);
f->add_arg(*cur);
f->add_arg(*t);
cur = t;
apps.push_front(f);
}
Statement::Fn_Call *f = new Statement::Fn_Call(
Statement::Fn_Call::STR_APPEND);
f->add_arg(*cur);
Para_Decl::Multi *m = dynamic_cast<Para_Decl::Multi*>(*i);
if (m) {
f->add_arg(new Expr::Const(' '));
l.push_back(f);
apply(l, m, cur);
} else {
Para_Decl::Simple *s = dynamic_cast<Para_Decl::Simple*>(*i);
f->add_arg(new Expr::Const("child {node {"));
l.push_back(f);
assert(s);
apply(l, s, cur);
}
a++;

if (!m) {
Statement::Fn_Call *g = new Statement::Fn_Call(
Statement::Fn_Call::STR_APPEND);
g->add_arg(*cur);
g->add_arg(new Expr::Const("}}"));
l.push_back(g);
}
}

l.insert(l.end(), apps.begin(), apps.end());
}

public:
void apply(Fn_Def &fn) const {
if (fn.is_Choice_Fn()) {
Statement::Return *ret = new Statement::Return(fn.names.front());
fn.stmts.push_back(ret);
return;
}
Type::External *str = new Type::External("Rope");
Statement::Var_Decl *ret = new Statement::Var_Decl(str, "ret");
fn.stmts.push_back(ret);
Statement::Fn_Call *f = new Statement::Fn_Call(
Statement::Fn_Call::STR_APPEND);
f->add_arg(*ret);
std::string t = "child {node {" + *fn.name + "}";
f->add_arg(new Expr::Const(t));
f->add_arg(new Expr::Const(static_cast<int>(t.size())));
fn.stmts.push_back(f);

Statement::Var_Decl *cur = ret;
std::list<Statement::Base*> l;
apply(l, fn.paras, cur);
fn.stmts.insert(fn.stmts.end(), l.begin(), l.end());

if (fn.ntparas().size() > 0) {
f = new Statement::Fn_Call(Statement::Fn_Call::STR_APPEND);
f->add_arg(*ret);
f->add_arg(new Expr::Const(';'));
fn.stmts.push_back(f);
std::list<Statement::Base*> lntparas;
apply(lntparas, fn.ntparas(), ret);
fn.stmts.insert(fn.stmts.end(), lntparas.begin(), lntparas.end());
}
f = new Statement::Fn_Call(Statement::Fn_Call::STR_APPEND);
f->add_arg(*ret);
f->add_arg(new Expr::Const(' '));
fn.stmts.push_back(f);
f = new Statement::Fn_Call(Statement::Fn_Call::STR_APPEND);
f->add_arg(*ret);
f->add_arg(new Expr::Const('}'));
fn.stmts.push_back(f);
Statement::Return *r = new Statement::Return(*ret);
fn.stmts.push_back(r);
}
};


Algebra *Signature::generate_trees(std::string *n) {
return generate_algebra(n, Mode::PRETTY, new Type::External("Rope"),
Generate_Tree_Stmts());
}
//------------------------------------------------------------------------------------------------------------


struct Generate_Backtrace_Stmts : public Generate_Stmts {
Generate_Backtrace_Stmts() : value_type(0), pos_type(0) {}
Type::Base *value_type;
Expand Down
1 change: 1 addition & 0 deletions src/signature.hh
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class Signature : public Signature_Base {
private:
Algebra *generate_count(std::string *n);
Algebra *generate_enum(std::string *n);
Algebra *generate_trees(std::string *n);
Algebra *generate_algebra(
std::string *n, Mode::Type mode_type, Type::Base *answer_type,
Type::Base *alph, const Generate_Stmts &generate_stmts);
Expand Down
Loading