Skip to content

Instantly share code, notes, and snippets.

@codebrainz
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebrainz/9da3e3e7f1a6fe993837 to your computer and use it in GitHub Desktop.
Save codebrainz/9da3e3e7f1a6fe993837 to your computer and use it in GitHub Desktop.
Sample C++ TreeGen output
//
// test.ast - Sample AST description for treegen
//
target CPlusPlus {
// In string literals,
// $$ is target/variable (for externs)
// $_ is "this" or "self", etc. (for nodes and externs)
// $@ is the type (for nodes and externs)
header_only: true;
strong_ptr: "$@*";
weak_ptr: "$@*";
//allocator: "new $@";
//deleter: "delete($$)";
list_type: "std::vector<$@>";
use_line_directives: true;
use_accessors: true;
namespace: "Tree";
epilog: "data/header.txt";
prolog: "data/footer.txt";
includes: [ "<vector>", "<iostream>" ];
// stuffed at end of class decl
class_extra: [ "void codegen(std::ostream& cio);" ];
//extern D {
// type: "D*";
// construct: "$$ = new D($_);";
// destruct: "delete $$;";
//}
}
visitor AstVisitor {
visit_method: "visit";
accept_method: "accept";
preorder: true;
inorder: true;
postorder: true;
visit_children: true;
}
abstract node A {
int f1;
string f2;
A(f1);
}
node B : A {
C f3;
B(f3);
}
node C : B {
float f4;
C(f4);
}
// This file is auto-generated, do not edit.
#ifndef TESTCODE_H
# define TESTCODE_H 1
# include <string>
# line 24 "tests/test.ast"
# include <vector>
# line 9 "misc/testcode.h"
# line 24 "tests/test.ast"
# include <iostream>
# line 12 "misc/testcode.h"
# line 20 "tests/test.ast"
namespace Tree {
# line 16 "misc/testcode.h"
struct A;
struct B;
struct C;
# line 36 "tests/test.ast"
struct AstVisitor {
# line 24 "misc/testcode.h"
void visit(A& node) {}
void visit(B& node) {}
void visit(C& node) {}
};
# line 45 "tests/test.ast"
struct A {
# line 32 "misc/testcode.h"
# line 46 "tests/test.ast"
int f1;
# line 35 "misc/testcode.h"
# line 47 "tests/test.ast"
std::string f2;
# line 38 "misc/testcode.h"
# line 48 "tests/test.ast"
A(int f1)
: f1(f1) {}
# line 42 "misc/testcode.h"
virtual ~A();
int get_f1() const;
void set_f1(int value);
std::string get_f2() const;
void set_f2(std::string value);
void accept(AstVisitor& visitor) {
visitor.visit(*this);
}
# line 27 "tests/test.ast"
void codegen(std::ostream& cio);
# line 53 "misc/testcode.h"
};
# line 51 "tests/test.ast"
struct B : public A {
# line 58 "misc/testcode.h"
# line 52 "tests/test.ast"
C* f3;
# line 61 "misc/testcode.h"
# line 53 "tests/test.ast"
B(int f1, C* f3)
: A(f1),
f3(f3) {}
# line 66 "misc/testcode.h"
virtual ~B();
C* get_f3() const;
void set_f3(C* value);
void accept(AstVisitor& visitor) {
visitor.visit(*this);
}
# line 27 "tests/test.ast"
void codegen(std::ostream& cio);
# line 75 "misc/testcode.h"
};
# line 56 "tests/test.ast"
struct C : public B {
# line 80 "misc/testcode.h"
# line 57 "tests/test.ast"
float f4;
# line 83 "misc/testcode.h"
# line 58 "tests/test.ast"
C(int f1, C* f3, float f4)
: B(f1, f3),
f4(f4) {}
# line 88 "misc/testcode.h"
virtual ~C();
float get_f4() const;
void set_f4(float value);
void accept(AstVisitor& visitor) {
visitor.visit(*this);
}
# line 27 "tests/test.ast"
void codegen(std::ostream& cio);
# line 97 "misc/testcode.h"
};
int A::get_f1() const {
return f1;
}
void A::set_f1(int value) {
f1 = value;
}
std::string A::get_f2() const {
return f2;
}
void A::set_f2(std::string value) {
f2 = value;
}
inline A::~A() {}
C* B::get_f3() const {
return f3;
}
void B::set_f3(C* value) {
delete f3;
f3 = value;
}
inline B::~B() {
delete f3;
}
float C::get_f4() const {
return f4;
}
void C::set_f4(float value) {
f4 = value;
}
inline C::~C() {}
}
#endif
// This file is auto-generated, do not edit.
#ifndef TESTCODE_H
# define TESTCODE_H 1
# include <string>
# include <vector>
# include <iostream>
namespace Tree {
struct A;
struct B;
struct C;
struct AstVisitor {
void visit(A& node) {}
void visit(B& node) {}
void visit(C& node) {}
};
struct A {
int f1;
std::string f2;
A(int f1)
: f1(f1) {}
virtual ~A();
int get_f1() const;
void set_f1(int value);
std::string get_f2() const;
void set_f2(std::string value);
void accept(AstVisitor& visitor) {
visitor.visit(*this);
}
void codegen(std::ostream& cio);
};
struct B : public A {
C* f3;
B(int f1, C* f3)
: A(f1),
f3(f3) {}
virtual ~B();
C* get_f3() const;
void set_f3(C* value);
void accept(AstVisitor& visitor) {
visitor.visit(*this);
}
void codegen(std::ostream& cio);
};
struct C : public B {
float f4;
C(int f1, C* f3, float f4)
: B(f1, f3),
f4(f4) {}
virtual ~C();
float get_f4() const;
void set_f4(float value);
void accept(AstVisitor& visitor) {
visitor.visit(*this);
}
void codegen(std::ostream& cio);
};
int A::get_f1() const {
return f1;
}
void A::set_f1(int value) {
f1 = value;
}
std::string A::get_f2() const {
return f2;
}
void A::set_f2(std::string value) {
f2 = value;
}
inline A::~A() {}
C* B::get_f3() const {
return f3;
}
void B::set_f3(C* value) {
delete f3;
f3 = value;
}
inline B::~B() {
delete f3;
}
float C::get_f4() const {
return f4;
}
void C::set_f4(float value) {
f4 = value;
}
inline C::~C() {}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment