Skip to content

Commit

Permalink
AST refacroting: Green refactoring preparing for a more general visit…
Browse files Browse the repository at this point in the history
…or system: Made accept() a macro and use a common supertype BaseVisitable.
  • Loading branch information
kintel committed Jun 13, 2016
1 parent fb18ec9 commit 7800683
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 80 deletions.
15 changes: 15 additions & 0 deletions src/BaseVisitable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include "visitor.h"

class BaseVisitable
{
public:
virtual ~BaseVisitable() {}
virtual Response accept(class State&, Visitor&) const = 0;
};

#define VISITABLE() \
virtual Response accept(class State &state, Visitor &visitor) const { \
return visitor.visit(state, *this); \
}
4 changes: 1 addition & 3 deletions src/cgaladvnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ enum cgaladv_type_e {
class CgaladvNode : public AbstractNode
{
public:
VISITABLE();
CgaladvNode(const ModuleInstantiation *mi, cgaladv_type_e type) : AbstractNode(mi), type(type) {
convexity = 1;
}
virtual ~CgaladvNode() { }
virtual Response accept(class State &state, Visitor &visitor) const {
return visitor.visit(state, *this);
}
virtual std::string toString() const;
virtual std::string name() const;

Expand Down
4 changes: 1 addition & 3 deletions src/colornode.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
class ColorNode : public AbstractNode
{
public:
VISITABLE();
ColorNode(const ModuleInstantiation *mi) : AbstractNode(mi) { }
virtual Response accept(class State &state, Visitor &visitor) const {
return visitor.visit(state, *this);
}
virtual std::string toString() const;
virtual std::string name() const;

Expand Down
4 changes: 1 addition & 3 deletions src/csgops.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
class CsgOpNode : public AbstractNode
{
public:
VISITABLE();
OpenSCADOperator type;
CsgOpNode(const ModuleInstantiation *mi, OpenSCADOperator type) : AbstractNode(mi), type(type) { }
virtual Response accept(class State &state, Visitor &visitor) const {
return visitor.visit(state, *this);
}
virtual std::string toString() const;
virtual std::string name() const;
};
4 changes: 1 addition & 3 deletions src/importnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ enum import_type_e {
class ImportNode : public LeafNode
{
public:
VISITABLE();
ImportNode(const ModuleInstantiation *mi, import_type_e type) : LeafNode(mi), type(type) { }
virtual Response accept(class State &state, Visitor &visitor) const {
return visitor.visit(state, *this);
}
virtual std::string toString() const;
virtual std::string name() const;

Expand Down
4 changes: 1 addition & 3 deletions src/linearextrudenode.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
class LinearExtrudeNode : public AbstractPolyNode
{
public:
VISITABLE();
LinearExtrudeNode(const ModuleInstantiation *mi) : AbstractPolyNode(mi) {
convexity = slices = 0;
fn = fs = fa = height = twist = 0;
origin_x = origin_y = 0;
scale_x = scale_y = 1;
center = has_twist = false;
}
virtual Response accept(class State &state, Visitor &visitor) const {
return visitor.visit(state, *this);
}
virtual std::string toString() const;
virtual std::string name() const { return "linear_extrude"; }

Expand Down
31 changes: 0 additions & 31 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,6 @@ AbstractNode::~AbstractNode()
std::for_each(this->children.begin(), this->children.end(), del_fun<AbstractNode>());
}

Response AbstractNode::accept(class State &state, Visitor &visitor) const
{
return visitor.visit(state, *this);
}

Response AbstractIntersectionNode::accept(class State &state, Visitor &visitor) const
{
return visitor.visit(state, *this);
}

Response AbstractPolyNode::accept(class State &state, Visitor &visitor) const
{
return visitor.visit(state, *this);
}

Response GroupNode::accept(class State &state, Visitor &visitor) const
{
return visitor.visit(state, *this);
}

Response RootNode::accept(class State &state, Visitor &visitor) const
{
return visitor.visit(state, *this);
}


Response LeafNode::accept(class State &state, Visitor &visitor) const
{
return visitor.visit(state, *this);
}

std::string AbstractNode::toString() const
{
return this->name() + "()";
Expand Down
19 changes: 10 additions & 9 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

#include <vector>
#include <string>
#include "traverser.h"
#include "BaseVisitable.h"

extern int progress_report_count;
extern void (*progress_report_f)(const class AbstractNode*, void*, int);
extern void *progress_report_vp;

void progress_report_prep(AbstractNode *root, void (*f)(const class AbstractNode *node, void *vp, int mark), void *vp);
void progress_report_prep(class AbstractNode *root, void (*f)(const class AbstractNode *node, void *vp, int mark), void *vp);
void progress_report_fin();

/*!
Expand All @@ -18,17 +18,17 @@ void progress_report_fin();
scratch for each compile.
*/
class AbstractNode
class AbstractNode : public BaseVisitable
{
// FIXME: the idx_counter/idx is mostly (only?) for debugging.
// We can hash on pointer value or smth. else.
// -> remove and
// use smth. else to display node identifier in CSG tree output?
static size_t idx_counter; // Node instantiation index
public:
VISITABLE();
AbstractNode(const class ModuleInstantiation *mi);
virtual ~AbstractNode();
virtual Response accept(class State &state, class Visitor &visitor) const;
virtual std::string toString() const;
/*! The 'OpenSCAD name' of this node, defaults to classname, but can be
overloaded to provide specialization for e.g. CSG nodes, primitive nodes etc.
Expand Down Expand Up @@ -58,19 +58,19 @@ class AbstractNode
class AbstractIntersectionNode : public AbstractNode
{
public:
VISITABLE();
AbstractIntersectionNode(const ModuleInstantiation *mi) : AbstractNode(mi) { };
virtual ~AbstractIntersectionNode() { };
virtual Response accept(class State &state, class Visitor &visitor) const;
virtual std::string toString() const;
virtual std::string name() const;
};

class AbstractPolyNode : public AbstractNode
{
public:
VISITABLE();
AbstractPolyNode(const ModuleInstantiation *mi) : AbstractNode(mi) { };
virtual ~AbstractPolyNode() { };
virtual Response accept(class State &state, class Visitor &visitor) const;

enum render_mode_e {
RENDER_CGAL,
Expand All @@ -85,9 +85,9 @@ class AbstractPolyNode : public AbstractNode
class GroupNode : public AbstractNode
{
public:
VISITABLE();
GroupNode(const class ModuleInstantiation *mi) : AbstractNode(mi) { }
virtual ~GroupNode() { }
virtual Response accept(class State &state, class Visitor &visitor) const;
virtual std::string name() const;
};

Expand All @@ -97,18 +97,19 @@ class GroupNode : public AbstractNode
class RootNode : public GroupNode
{
public:
VISITABLE();

RootNode(const class ModuleInstantiation *mi) : GroupNode(mi) { }
virtual ~RootNode() { }
virtual Response accept(class State &state, class Visitor &visitor) const;
virtual std::string name() const;
};

class LeafNode : public AbstractPolyNode
{
public:
VISITABLE();
LeafNode(const ModuleInstantiation *mi) : AbstractPolyNode(mi) { };
virtual ~LeafNode() { };
virtual Response accept(class State &state, class Visitor &visitor) const;
virtual const class Geometry *createGeometry() const = 0;
};

Expand Down
4 changes: 1 addition & 3 deletions src/offsetnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
class OffsetNode : public AbstractPolyNode
{
public:
VISITABLE();
OffsetNode(const ModuleInstantiation *mi) : AbstractPolyNode(mi), fn(0), fs(0), fa(0), delta(1), miter_limit(1000000.0), join_type(ClipperLib::jtRound) { }
virtual Response accept(class State &state, Visitor &visitor) const {
return visitor.visit(state, *this);
}
virtual std::string toString() const;
virtual std::string name() const { return "offset"; }

Expand Down
4 changes: 1 addition & 3 deletions src/primitives.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ class PrimitiveModule : public AbstractModule
class PrimitiveNode : public LeafNode
{
public:
VISITABLE();
PrimitiveNode(const ModuleInstantiation *mi, primitive_type_e type) : LeafNode(mi), type(type) { }
virtual Response accept(class State &state, Visitor &visitor) const {
return visitor.visit(state, *this);
}
virtual std::string toString() const;
virtual std::string name() const {
switch (this->type) {
Expand Down
4 changes: 1 addition & 3 deletions src/projectionnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
class ProjectionNode : public AbstractPolyNode
{
public:
VISITABLE();
ProjectionNode(const ModuleInstantiation *mi) : AbstractPolyNode(mi) {
cut_mode = false;
}
virtual Response accept(class State &state, Visitor &visitor) const {
return visitor.visit(state, *this);
}
virtual std::string toString() const;
virtual std::string name() const { return "projection"; }

Expand Down
4 changes: 1 addition & 3 deletions src/rendernode.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
class RenderNode : public AbstractNode
{
public:
VISITABLE();
RenderNode(const ModuleInstantiation *mi) : AbstractNode(mi), convexity(1) { }
virtual Response accept(class State &state, Visitor &visitor) const {
return visitor.visit(state, *this);
}
virtual std::string toString() const;
virtual std::string name() const { return "render"; }

Expand Down
4 changes: 1 addition & 3 deletions src/rotateextrudenode.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
class RotateExtrudeNode : public AbstractPolyNode
{
public:
VISITABLE();
RotateExtrudeNode(const ModuleInstantiation *mi) : AbstractPolyNode(mi) {
convexity = 0;
fn = fs = fa = 0;
origin_x = origin_y = scale = 0;
angle = 360;
}
virtual Response accept(class State &state, Visitor &visitor) const {
return visitor.visit(state, *this);
}
virtual std::string toString() const;
virtual std::string name() const { return "rotate_extrude"; }

Expand Down
4 changes: 1 addition & 3 deletions src/surface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ typedef std::unordered_map<std::pair<int,int>, double, boost::hash<std::pair<int
class SurfaceNode : public LeafNode
{
public:
VISITABLE();
SurfaceNode(const ModuleInstantiation *mi) : LeafNode(mi) { }
virtual Response accept(class State &state, Visitor &visitor) const {
return visitor.visit(state, *this);
}
virtual std::string toString() const;
virtual std::string name() const { return "surface"; }

Expand Down
5 changes: 1 addition & 4 deletions src/textnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ class TextModule;
class TextNode : public AbstractPolyNode
{
public:
VISITABLE();
TextNode(const ModuleInstantiation *mi) : AbstractPolyNode(mi) {}

virtual Response accept(class State &state, Visitor &visitor) const {
return visitor.visit(state, *this);
}

virtual std::string toString() const;
virtual std::string name() const { return "text"; }

Expand Down
4 changes: 1 addition & 3 deletions src/transformnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
class TransformNode : public AbstractNode
{
public:
VISITABLE();
TransformNode(const ModuleInstantiation *mi) : AbstractNode(mi) { }
virtual Response accept(class State &state, Visitor &visitor) const {
return visitor.visit(state, *this);
}
virtual std::string toString() const;
virtual std::string name() const;

Expand Down

0 comments on commit 7800683

Please sign in to comment.