Skip to content
Browse files

Added decent user interface. Fixed int to pointer conversion (did not

compile on 64bit).
  • Loading branch information...
1 parent dfdf4dd commit 5848db90eceb7f918c3c7e43832767cd57cc7755 @franapoli committed
Showing with 108 additions and 48 deletions.
  1. +6 −6 Edgeset.cpp
  2. +3 −3 Edgeset.h
  3. +3 −3 Environment.h
  4. +9 −8 Frame.cpp
  5. +9 −5 Graph.cpp
  6. +3 −3 Linkable.cpp
  7. +1 −1 NameSpace.cpp
  8. +1 −1 NameSpace.h
  9. +3 −3 SymbolTable.h
  10. +70 −15 main.cpp
View
12 Edgeset.cpp
@@ -19,7 +19,7 @@ Edgeset::Edgeset(Nodeset & ns1, Nodeset & ns2) {
for (i = ns1.begin(); i != ns1.end(); i++)
for (j = ns2.begin(); j != ns2.end(); j++)
- AddEdge(i->getId(), j->getId());
+ addEdge(i->getId(), j->getId());
}
Edgeset::~Edgeset() {
@@ -33,27 +33,27 @@ Edgeset::~Edgeset() {
return *e;
}*/
-Edge &Edgeset::AddEdge(unsigned int id1, unsigned int id2) {
+Edge &Edgeset::addEdge(unsigned int id1, unsigned int id2) {
Edge *e = new Edge(id1, id2);
if (!has(id1, id2))
push_back(*e);
return *e;
}
-Edge &Edgeset::AddEdge(unsigned int id1, unsigned int id2, unsigned id) {
+Edge &Edgeset::addEdge(unsigned int id1, unsigned int id2, unsigned id) {
Edge *e = new Edge(id1, id2, id);
if (!has(id1, id2))
push_back(*e);
return *e;
}
-void Edgeset::AddEdges(Edgeset & es, bool copyid) {
+void Edgeset::addEdges(Edgeset & es, bool copyid) {
Edgeset::iterator i;
for (i = es.begin(); i != es.end(); i++)
if (copyid)
- AddEdge(i->getSrcNodeId(), i->getDstNodeId(), i->getId());
+ addEdge(i->getSrcNodeId(), i->getDstNodeId(), i->getId());
else
- AddEdge(i->getSrcNodeId(), i->getDstNodeId());
+ addEdge(i->getSrcNodeId(), i->getDstNodeId());
}
/*Nodeset & Edgeset::GetOutNodes(Node & node)
View
6 Edgeset.h
@@ -22,9 +22,9 @@ class Edgeset: public vector<Edge> {
virtual ~Edgeset();
void AddEdge(Edge &);
// Edge & AddEdge(Node &, Node &);
- Edge & AddEdge(unsigned int id1, unsigned int id2);
- Edge & AddEdge(unsigned int id1, unsigned int id2, unsigned id);
- void AddEdges(Edgeset &, bool copyid = false);
+ Edge & addEdge(unsigned int id1, unsigned int id2);
+ Edge & addEdge(unsigned int id1, unsigned int id2, unsigned id);
+ void addEdges(Edgeset &, bool copyid = false);
//Nodeset &GetInNodes(Node &);
//Nodeset &GetOutNodes(Node &);
std::ostream & operator>>(std::ostream &str);
View
6 Environment.h
@@ -25,14 +25,14 @@ class Environment {
}
Linkable *getSymbol(std::string symbol) {
- return _namespaces.find("global")->second->GetObject(symbol);
+ return _namespaces.find("global")->second->getObject(symbol);
}
Linkable *getSymbol(std::string space, std::string symbol) {
- Linkable *l = _namespaces.find(space)->second->GetObject(symbol);
+ Linkable *l = _namespaces.find(space)->second->getObject(symbol);
if (l)
return l;
else
- return _namespaces.find("global")->second->GetObject(symbol);
+ return _namespaces.find("global")->second->getObject(symbol);
}
private:
View
17 Frame.cpp
@@ -16,6 +16,7 @@
#include "Nodeset.h"
#include "Environment.h"
#include <typeinfo>
+#include <stdint.h>
using namespace std;
@@ -50,7 +51,7 @@ Linkable* Frame::doAction(ACTION_TYPE type, Linkable * p1, Linkable * p2,
static unsigned int groupid = 0;
if (suspend_actions) {
- if ((int) p1 == 0 && type == ACT_FUNCTDEF) {
+ if ((uintptr_t) p1 == 0 && type == ACT_FUNCTDEF) {
suspend_actions = false;
if (FRAME_DEBUG)
cout << "Re-enabling actions" << endl;
@@ -66,7 +67,7 @@ Linkable* Frame::doAction(ACTION_TYPE type, Linkable * p1, Linkable * p2,
return (Linkable *) attr;
case ACT_FUNCTDEF:
- if ((int) p1 == 1) {
+ if ((uintptr_t) p1 == 1) {
suspend_actions = true;
if (FRAME_DEBUG)
cout << "Suspending actions" << endl;
@@ -97,7 +98,7 @@ Linkable* Frame::doAction(ACTION_TYPE type, Linkable * p1, Linkable * p2,
case ACT_GRAPHNAME:
name = (const char *) p1;
symboltable.addNameSpace(name, *new NameSpace);
- if ((int) p2)
+ if ((uintptr_t) p2)
attribs.push_back(
pair<string, string>(((pair<string, string>*) p2)->first,
((pair<string, string>*) p2)->second));
@@ -167,13 +168,13 @@ Linkable* Frame::doAction(ACTION_TYPE type, Linkable * p1, Linkable * p2,
attribs.clear();
}
- if ((int) p4 == 1) {
+ if ((uintptr_t) p4 == 1) {
if (!realins)
realins = new Nodeset;
realins->addNode(*(Node *) l);
inconnectors.push_back((const char *) p2);
}
- if ((int) p4 == 2) {
+ if ((uintptr_t) p4 == 2) {
if (!realouts)
realouts = new Nodeset;
realouts->addNode(*(Node *) l);
@@ -242,7 +243,7 @@ Linkable* Frame::doAction(ACTION_TYPE type, Linkable * p1, Linkable * p2,
gtemp->addNodes(arrowgraph->getNodes());
gtemp->addEdges(arrowgraph->getEdges(), true);
- if ((int) p3 != 2) { // hashing
+ if ((uintptr_t) p3 != 2) { // hashing
Nodeset &temp = p2->getOutputs();
Nodeset::iterator it;
@@ -293,7 +294,7 @@ Linkable* Frame::doAction(ACTION_TYPE type, Linkable * p1, Linkable * p2,
gtemp->setInputs(p2->getInputs());
}
- if ((int) p4 == ACT_FLAG_HASH) {
+ if ((uintptr_t) p4 == ACT_FLAG_HASH) {
Nodeset &temp = p2->getInputs();
Nodeset::iterator it;
@@ -355,7 +356,7 @@ Linkable* Frame::doAction(ACTION_TYPE type, Linkable * p1, Linkable * p2,
gtemp->setInputs(p2->getInputs());
}
- if ((int) p4 == ACT_FLAG_HASH) {
+ if ((uintptr_t) p4 == ACT_FLAG_HASH) {
Nodeset &temp = p2->getOutputs();
Nodeset::iterator it;
View
14 Graph.cpp
@@ -20,7 +20,7 @@ Graph::~Graph() {
}
Graph::Graph(Nodeset &n, Edgeset&e) {
- _E.AddEdges(e);
+ _E.addEdges(e);
_V.addNodes(n);
}
@@ -33,6 +33,10 @@ void Graph::destroy(void) {
void Graph::toDot(std::string fname) {
FILE *fid;
fid = fopen((string(fname)).c_str(), "w");
+ if (!fid) {
+ fprintf(stderr, "%s", "Error opening output file.\n");
+ throw 1;
+ }
string attr_string = "";
std::map<std::string, std::string>::iterator mapit;
std::map<std::string, std::string> attribs;
@@ -81,7 +85,7 @@ void Graph::toDot(std::string fname) {
minedge->getDstNodeId(), minedge->getId());
}
- fprintf(fid, "}");
+ fprintf(fid, "}\n");
fclose(fid);
}
@@ -246,9 +250,9 @@ Edgeset &Graph::addEdges(Edgeset &e, bool copyid) {
n2 = &_V.findNode(i->getDstNodeId());
if (copyid)
newedges.push_back(
- _E.AddEdge(n1->getId(), n2->getId(), i->getId()));
+ _E.addEdge(n1->getId(), n2->getId(), i->getId()));
else
- newedges.push_back(_E.AddEdge(n1->getId(), n2->getId()));
+ newedges.push_back(_E.addEdge(n1->getId(), n2->getId()));
n1->_issink = false;
n2->_issource = false;
@@ -316,7 +320,7 @@ Edgeset& Graph::getEdgesRooted(Linkable& n) {
if (!j->isVisited())
stack.addNode(*j);
edgeid = findEdge(i.getId(), j->getId())->getId();
- es.AddEdge(i.getId(), j->getId(), edgeid);
+ es.addEdge(i.getId(), j->getId(), edgeid);
}
}
View
6 Linkable.cpp
@@ -111,12 +111,12 @@ Edgeset & link(Linkable & l1, Linkable & l2) {
for (dbiter = dbo.begin(); dbiter != dbo.end(); dbiter++) {
found = dbi.find(dbiter->first);
if (found != dbi.end())
- E->AddEdges(*new Edgeset(dbiter->second, found->second));
+ E->addEdges(*new Edgeset(dbiter->second, found->second));
}
}
else
- E->AddEdges(*new Edgeset(l1.getOutputs(), l2.getInputs()));
+ E->addEdges(*new Edgeset(l1.getOutputs(), l2.getInputs()));
return *E;
}
@@ -125,7 +125,7 @@ Edgeset& ulink(Linkable & l1, Linkable & l2) {
Edgeset& e1 = (*new Edgeset(l1.getOutputs(), l2.getInputs()));
Edgeset& e2 = (*new Edgeset(l2.getOutputs(), l1.getInputs()));
- e1.AddEdges(e2);
+ e1.addEdges(e2);
e2.~Edgeset();
return e1;
View
2 NameSpace.cpp
@@ -17,7 +17,7 @@ NameSpace::~NameSpace() {
// TODO Auto-generated destructor stub
}
-Linkable *NameSpace::GetObject(std::string s) {
+Linkable *NameSpace::getObject(std::string s) {
std::map<std::string, Linkable *>::iterator i;
i = _symvalues.find(s);
if (i == _symvalues.end())
View
2 NameSpace.h
@@ -26,7 +26,7 @@ class NameSpace {
bool HasSymbol(std::string s) {
return !(_symvalues.find(s) == _symvalues.end());
}
- Linkable *GetObject(std::string s);
+ Linkable *getObject(std::string s);
std::ostream& operator>>(std::ostream &);
std::string getName() const {
return _name;
View
6 SymbolTable.h
@@ -25,14 +25,14 @@ class SymbolTable {
}
Linkable *getSymbol(std::string symbol) {
- return _namespaces.find("global")->second->GetObject(symbol);
+ return _namespaces.find("global")->second->getObject(symbol);
}
Linkable *getSymbol(std::string space, std::string symbol) {
- Linkable *l = _namespaces.find(space)->second->GetObject(symbol);
+ Linkable *l = _namespaces.find(space)->second->getObject(symbol);
if (l)
return l;
else
- return _namespaces.find("global")->second->GetObject(symbol);
+ return _namespaces.find("global")->second->getObject(symbol);
}
private:
View
85 main.cpp
@@ -3,7 +3,7 @@
#include "Frame.h"
#include "CallStack.h"
#include "Function.h"
-#include "lex.yy.hh"
+#include "lglc.yy.hh"
#include "AdjMatrix.h"
//#define DEBUG
@@ -18,25 +18,83 @@ CallStack cs;
const char *protname;
AdjMatrix *adjmat;
-int main(int ARGC, char *ARGV[]) {
- Graph *res;
- f->setEnv(symboltable);
+const char *helptext =
+ "\
+Usage: lglc [options] file\n\
+Options:\n\
+ -o: output file name\n";
- yydebug = 0;
+void say(const char *string, bool iserr = true) {
+ char outs[100] = "\0";
+ strcat(outs, "lglc: ");
+ if (iserr)
+ strcat(outs, "error: ");
+ strcat(outs, string);
+ if (iserr)
+ fprintf(stderr, "%s", outs);
+ else
+ fprintf(stdout, "%s", outs);
+}
+
+int main(int argc, char *argv[]) {
+ char ofile[100] = "\0";
+ int c;
+ char text[100];
+
+ opterr = 0;
+
+ while ((c = getopt(argc, argv, "hvo:")) != -1)
+ switch (c) {
+ case 'v':
+ sprintf(text, "%s Version %d.%d\n", "Leaf Graph Language Compiler",
+ 0, 1);
+ say(text, false);
+ break;
+ case 'h':
+ fprintf(stdout, "%s", helptext);
+ return 0;
+ break;
+ case 'o':
+ sprintf(ofile, "%s", optarg);
+ break;
+ case '?':
+ if (optopt == 'o') {
+ sprintf(text, "option -%c requires an argument.\n", optopt);
+ say(text, false);
+ return 0;
+ } else if (isprint(optopt)) {
+ sprintf(text, "unknown option `-%c'.\n", optopt);
+ say(text);
+ } else {
+ sprintf(text, "unknown option character `\\x%x'.\n", optopt);
+ say(text);
+ }
+ return 1;
+ }
- if (ARGC < 2) {
- fprintf(stderr, "No protocol to parse\n");
+ if (argc - optind > 1) {
+ say("too many arguments \n");
+ return 1;
+ } else if (argc - optind < 1) {
+ say("no input files \n");
return 1;
}
- fid = fopen(ARGV[1], "r");
- protname = ARGV[1];
-
+ if (ofile[0] == '\0') {
+ strcat(ofile, argv[optind]);
+ strcat(ofile, ".dot");
+ }
+ fid = fopen(argv[optind], "r");
if (!fid) {
- fprintf(stderr, "Error opening protocol file: %s\n", ARGV[1]);
+ sprintf(text, "Error opening file: %s\n", argv[optind]);
+ say(text);
return 1;
}
+ protname = argv[optind];
+ Graph *res;
+ f->setEnv(symboltable);
+ yydebug = 0;
f->setStream(fid);
cs.push(*f);
@@ -46,10 +104,7 @@ int main(int ARGC, char *ARGV[]) {
return (1);
}
- res->toDot("out.dot");
- //adjmat = &res->Visit();
- //adjmat->toDot(res->GetNodes(), "out.dot");
- //fprintf(stdout, "Protocol correctly parsed.\n");
+ res->toDot(ofile);
return 0;
}

0 comments on commit 5848db9

Please sign in to comment.
Something went wrong with that request. Please try again.