Commit 3b7f2791 authored by Sarah Spall's avatar Sarah Spall Committed by Vikram Narayanan

modified ast structures, and changed to match google c++ style conventions, by...

modified ast structures, and changed to match google c++ style conventions, by necessity changed peg file as well
parent 08bb2d4f
#include "lcd_ast.h"
Module::Module(char * n, std::vector<Rpc *>* rs, std::vector<Message *>* ms,
std::map<char *, Projection *>* e, std::map<char *, Module *>* mods)
PrimitiveType::PrimitiveType(TypeModifier type_mod, PrimType primitive)
{
this->name = n;
this->rpcs = rs;
this->messages = ms;
this->env = e;
this->modules = mods;
this->type_modifier_ = type_mod;
this->primitive_ = primitive;
}
Module::Module(std::vector<char *>* in, std::vector<Rpc *>* rs, std::vector<Message *>* ms,
std::map<char *, Projection *>* e, std::map<char *, Module *>* mods)
ProjectionType::ProjectionType(char * type_name, bool pointer)
{
this->includes = in;
this->rpcs = rs;
this->messages = ms;
this->env = e;
this->modules = mods;
this->type_name_ = type_name;
this->pointer_ = pointer;
}
char * Module::get_name()
Scope::Scope(std::vector<char* >* includes, std::vector<Rpc* >* rpc_definitions, std::vector<Message* >* message_definitions,
std::map<char* , Projection* >* projection_definitions)
{
return this->name;
this->includes_ = includes;
this->rpc_definitions_ = rpc_definitions;
this->message_definitions_ = message_definitions;
this->projection_definitions_ = projection_definitions;
}
Rpc::Rpc(Type* r, char * n, std::vector<Parameter *>* param)
Rpc::Rpc(Type* return_type, char * name, std::vector<Parameter* >* parameters)
{
this->ret = r;
this->name = n;
this->parameters = param;
this->return_type_ = return_type;
this->name_ = name;
this->parameters_ = parameters;
}
Projection::Projection(char * name, Type* real, std::vector<ProjField *>* f)
Projection::Projection(char * name, char* true_type, std::vector<ProjectionField* >* fields)
{
this->name = name;
this->underlying = real;
this->fields = f;
this->name_ = name;
this->true_type_ = true_type;
this->fields_ = fields;
}
char * Projection::get_name()
ProjectionField::ProjectionField(bool in, bool out, bool alloc, bool bind, Type* field_type, char* field_name)
{
return this->name;
this->in_ = in;
this->out_ = out;
this->alloc_ = alloc;
this->bind_ = bind;
this->field_type_ = field_type;
this->field_name_ = field_name;
}
ProjField::ProjField(int s, Type* t, char * name)
Parameter::Parameter(Type* type, char* name)
{
this->specifications = s;
this->t = t;
this->name = name;
this->type_ = type;
this->name_ = name;
}
/* o is option such as "struct" or "projection
t is type such as "int"
m is module, "" if it belongs to top level
p is * etc */
Type::Type(char * o, char * t, char * m, char * p)
MessageField::MessageField(Type* field_type, char * field_name)
{
this->option = o;
this->type_ = t;
this->module = m;
this->pointer = p;
this->field_type_ = field_type;
this->field_name_ = field_name;
}
Parameter::Parameter(Type* t, char * n)
Message::Message(char * name, std::vector<MessageField* >* fields)
{
this->type = t;
this->name = n;
}
bool Parameter::isProjection()
{
}
Message::Message(char * n, std::vector<Capability *>* cap)
{
this->name = n;
this->caps = cap;
}
/* Capability constructor */
Capability::Capability(char * n)
{
this->name = n;
}
/* get name of capability */
char * Capability::get_name()
{
return this->name;
this->name_ = name;
this->fields_ = fields;
}
......
......@@ -4,122 +4,134 @@
#include <vector>
#include <map>
#define IN 0x1
#define OUT 0x2
#define BIND 0x4
#define ALLOC 0x8
enum definition_type {rpc, module, message, projection};
enum DefinitionType {kRpc = 1, kModule, kMessage, kProjection};
enum PrimType { kChar = 1, kShort, kInt, kLong, kLongLong, kCapability};
enum TypeModifier {kUnsigned = 1, kNone};
class Definition
{
public:
virtual definition_type get_definition_type() = 0;
virtual DefinitionType get_definition_type(void) = 0;
// virtual ~Definition() {};
};
class Type
{
char * option;
char * type_;
char * module;
char * pointer;
public:
virtual ~Type() {}
};
class PrimitiveType : public Type
{
TypeModifier type_modifier_;
PrimType primitive_;
public:
PrimitiveType(TypeModifier type_mod, PrimType primitive);
TypeModifier type_modifier() {return type_modifier_; }
PrimType primitive() {return primitive_; }
char * GetFullName();
// maybe for consistency this shouldnt have getfullname
};
class ProjectionType : public Type // complex type
{
char* type_name_;
bool pointer_;
public:
Type(char * o, char * t, char * m, char * p);
~Type();
ProjectionType(char * type_name, bool pointer);
char * type_name() {return type_name_; }
bool pointer() { return pointer_; }
// get full name function? class isn't aware of parent need to pass env
};
class Parameter
{
Type* type;
char* name;
Type* type_;
char* name_;
public:
Parameter(Type* t,char * n);
Parameter(Type* type, char* name);
~Parameter();
bool isProjection();
};
class Rpc : public Definition
{
Type* ret;
char * name;
std::vector<Parameter *>* parameters;
Type* return_type_;
char* name_;
std::vector<Parameter* >* parameters_;
public:
Rpc(Type* r, char * n, std::vector<Parameter *>* param);
~Rpc();
definition_type get_definition_type(){return rpc;}
Rpc(Type* return_type, char* name, std::vector<Parameter* >* parameters);
DefinitionType get_definition_type(){ return kRpc; }
};
class Capability // capability? for message
class ProjectionField
{
char * name;
bool in_;
bool out_;
bool alloc_;
bool bind_;
Type* field_type_;
char* field_name_;
public:
Capability(char * n);
~Capability();
char * get_name();
ProjectionField(bool in, bool out, bool alloc, bool bind, Type* field_type, char* field_name);
~ProjectionField();
bool in() { return in_; }
bool out() { return out_; }
bool alloc() { return alloc_; }
bool bind() { return bind_; }
};
class ProjField
class Projection : public Definition
{
int specifications; // set bits for in out bind alloc
Type* t;
char * name;
char* name_;
char* true_type_; // struct isn't allowed anywhere but here so just use char *
std::vector<ProjectionField* >* fields_;
public:
ProjField(int s, Type* t, char * name);
~ProjField();
Projection(char* name, char* true_type, std::vector<ProjectionField* >* fields);
char* name() const { return name_; }
DefinitionType get_definition_type(){return kProjection;}
};
class Projection : public Definition
class MessageField
{
char * name;
Type* underlying;
std::vector<ProjField *>* fields;
Type* field_type_;
char* field_name_;
public:
Projection(char * name, Type* real, std::vector<ProjField *>* f);
~Projection();
definition_type get_definition_type(){return projection;}
char * get_name();
MessageField(Type* field_type, char* field_name);
};
class Message : public Definition
{
char * name;
std::vector<Capability *>* caps;
char * name_;
std::vector<MessageField* >* fields_;
public:
Message(char * n, std::vector<Capability *>* e);
~Message();
std::vector<Capability *>* get_capabilities();
definition_type get_definition_type() {return message; }
Message(char * name, std::vector<MessageField* >* fields);
DefinitionType get_definition_type() { return kMessage; }
};
class Module : public Definition
class Scope
{
char * name; // just leave "" if top level
std::vector<char *> * includes; // only top level can have includes
std::vector<Rpc *> * rpcs;
std::vector<Message *> * messages;
std::map<char *, Projection *> *env;
std::map<char *, Module *> *modules;
std::vector<char* >* includes_;
std::vector<Rpc *>* rpc_definitions_;
std::vector<Message *>* message_definitions_;
std::map<char *, Projection *>* projection_definitions_;
// believe it is only necessary to store projections in "env" since functions can't be referenced in other functions
public:
Module(char * n, std::vector<Rpc *>* rs, std::vector<Message *>* ms,
std::map<char *, Projection *>* e, std::map<char *, Module *>* mods);
// for top level
Module(std::vector<char *>* in, std::vector<Rpc *>* rs, std::vector<Message *>* ms,
std::map<char *, Projection *>* e, std::map<char *, Module *>* mods);
~Module();
char * get_name();
definition_type get_definition_type() {return module; }
Scope(std::vector<char* >* includes, std::vector<Rpc* >* rpc_definitions, std::vector<Message* >* message_definitions,
std::map<char* , Projection* >* projection_definitions);
};
......
......@@ -14,7 +14,7 @@ int main(int argc, char ** argv)
try
{
char * file = argv[1];
Module * tree = (Module *) Parser::parse(std::string(file));
Scope * tree = (Scope *) Parser::parse(std::string(file));
printf("we have a tree\n");
}
catch (const Parser::ParseException e)
......
......@@ -32,7 +32,6 @@ File = Spacing i:Include* t:Things* <eof> {{ std::vector<Definition *>* defs =
std::vector<Rpc *>* rpcs = new std::vector<Rpc *>;
std::vector<Message *>* messages = new std::vector<Message *>;
std::map<char *, Projection *> *env = new std::map<char *, Projection *>;
std::map<char *, Module *> *modules = new std::map<char *, Module *>;
for(Value::iterator it = t.getValues().begin(); it != t.getValues().end(); it ++)
{
const Value & v = *it;
......@@ -44,28 +43,22 @@ File = Spacing i:Include* t:Things* <eof> {{ std::vector<Definition *>* defs =
}
switch (ds->get_definition_type())
{
case rpc:
case kRpc:
{
Rpc * rp = reinterpret_cast<Rpc *>(ds);
rpcs->push_back(rp);
}
break;
case module:
{
Module * mod = reinterpret_cast<Module *>(ds);
modules->insert(std::pair<char *, Module *>(mod->get_name(), mod));
}
break;
case message:
case kMessage:
{
Message * mess = reinterpret_cast<Message *>(ds);
messages->push_back(mess);
}
break;
case projection:
case kProjection:
{
Projection * proj = reinterpret_cast<Projection *>(ds);
env->insert(std::pair<char *, Projection *>(proj->get_name(), proj));
Projection* proj = reinterpret_cast<Projection *>(ds);
env->insert(std::pair<char *, Projection *>(proj->name(), proj));
}
break;
default:
......@@ -82,7 +75,7 @@ File = Spacing i:Include* t:Things* <eof> {{ std::vector<Definition *>* defs =
char * c = (char *) v.getValue();
includes->push_back(c);
}
value = new Module(includes, rpcs, messages, env, modules); }}
value = new Scope(includes, rpcs, messages, env); }}
String = <ascii 34> l:any_letter+ <ascii 34> {{
std::ostringstream total;
......@@ -120,59 +113,7 @@ String = <ascii 34> l:any_letter+ <ascii 34> {{
any_letter = [_.,/?<>'; =:%`!@#$^&*()-+{}|\\ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789]
Module = "module" Space+ id:Identifier OPENC Spacing t:Things* CLOSEC Spacing {{
std::vector<Definition *>* defs = new std::vector<Definition *>;
std::vector<Rpc *>* rpcs = new std::vector<Rpc *>;
std::vector<Message *>* messages = new std::vector<Message *>;
std::map<char *, Projection *> *env = new std::map<char *, Projection *>;
std::map<char *, Module *> *modules = new std::map<char *, Module *>;
for(Value::iterator it = t.getValues().begin(); it != t.getValues().end(); it ++)
{
const Value & v = *it;
Definition * ds = (Definition *) v.getValue();
if(!ds)
{
printf("error null pointer\n");
exit(0);
}
switch (ds->get_definition_type())
{
case rpc:
{
Rpc * rp = reinterpret_cast<Rpc *>(ds);
rpcs->push_back(rp);
}
break;
case module:
{
Module * mod = reinterpret_cast<Module *>(ds);
modules->insert(std::pair<char *, Module *>(mod->get_name(), mod));
}
break;
case message:
{
Message * mess = reinterpret_cast<Message *>(ds);
messages->push_back(mess);
}
break;
case projection:
{
Projection * proj = reinterpret_cast<Projection *>(ds);
env->insert(std::pair<char *, Projection *>(proj->get_name(), proj));
}
break;
default:
{
printf("fatal error\n");
}
}
}
value = new Module((char *) id.getValue(), rpcs, messages, env, modules); }}
Things = Module | Rpc | Message | Projection
Things = Rpc | Message | Projection
Include = "#include" Space+ "<" Spacing f:Identifier Spacing ">" Spacing {{
std::ostringstream total;
......@@ -194,78 +135,101 @@ LastParam = t:Type_form id:Identifier Spacing {{
value = new Parameter((Type *) t.getValue(), (char *) id.getValue()); }}
Parameters = f:FirstParam* l:LastParam {{
std::vector<Parameter *>* params = new std::vector<Parameter *>;
std::vector<Parameter* >* params = new std::vector<Parameter* >;
for(Value::iterator it = f.getValues().begin(); it != f.getValues().end(); it ++)
{
{
const Value & v = *it;
Parameter * p = (Parameter *) v.getValue();
Parameter* p = (Parameter* ) v.getValue();
params->push_back(p);
}
params->push_back((Parameter *) l.getValue());
params->push_back((Parameter* ) l.getValue());
value = params; }}
Rpc = "rpc" Space+ t:Type_form id:Identifier Spacing OPEN p:Parameters CLOSE SEMI Spacing {{
value = new Rpc((Type *) t.getValue(), (char *) id.getValue(), (std::vector<Parameter *> *) p.getValue()); }}
Message = "message" Space+ id:Identifier Spacing OPENC Spacing c:Cap* CLOSEC Spacing {{ std::vector<Capability *>* caps = new std::vector<Capability *>;
for(Value::iterator it = c.getValues().begin(); it != c.getValues().end(); it ++)
Message = "message" Space+ id:Identifier Spacing OPENC Spacing m:Message_field* CLOSEC Spacing {{ std::vector<MessageField* >* fields = new std::vector<MessageField* >;
for(Value::iterator it = m.getValues().begin(); it != m.getValues().end(); it ++)
{
const Value & v = *it;
Capability* cap = (Capability *) v.getValue();
caps->push_back(cap);
MessageField* mess_field = (MessageField *) v.getValue();
fields->push_back(mess_field);
}
value = new Message((char *) id.getValue(), caps); }}
value = new Message((char *) id.getValue(), fields); }}
Cap = "capability" Space+ id:Identifier Spacing SEMI Spacing {{ value = new Capability((char *) id.getValue()); }}
Message_field = t:Type_form Space+ id:Identifier Spacing SEMI Spacing {{
value = new MessageField((Type* ) t.getValue(), (char* ) id.getValue()); }}
Projection = "projection" Space+ id:Identifier "<" Spacing t:Proj_type ">" Spacing OPENC Spacing p:ProjField+ CLOSEC Spacing {{ std::vector<ProjField *>* p_fields = new std::vector<ProjField *>;
Projection = "projection" Space+ id:Identifier "<" Spacing t:Proj_type ">" Spacing OPENC Spacing p:ProjField+ CLOSEC Spacing {{ std::vector<ProjectionField *>* p_fields = new std::vector<ProjectionField *>;
for(Value::iterator it = p.getValues().begin(); it != p.getValues().end(); it ++)
{
const Value & v = *it;
ProjField * p = (ProjField *) v.getValue();
ProjectionField * p = (ProjectionField *) v.getValue();
p_fields->push_back(p);
}
value = new Projection((char *) id.getValue(), (Type *) t.getValue(), p_fields); }}
value = new Projection((char* ) id.getValue(), (char* ) t.getValue(), p_fields); }}
## what is the point of having a projection without at least one line
Proj_type = "struct" Space+ id:Identifier Spacing
Proj_type = "struct" Space+ id:Identifier Spacing {{
std::ostringstream total;
total << "struct " << (char *) id.getValue();
std::string str = total.str();
value = &str[0]; }}
Keyword = "out" {{ int * x = (int *) malloc(sizeof(int));
*x = OUT;
*x = 1;
value = x; }}
| "in" {{ int * x = (int *) malloc(sizeof(int));
*x = IN;
*x = 2;
value = x; }}
| "bind" {{ int * x = (int *) malloc(sizeof(int));
*x = BIND;
*x = 3;
value = x; }}
| "alloc" {{ int * x = (int *) malloc(sizeof(int));
*x = ALLOC;
*x = 4;
value = x; }}
Start = k:Keyword Spacing COMMA Spacing {{ value = k; }}
Specification = s:Start* l:Last {{ std::vector<int *>* specs = new std::vector<int*>;
int res = 0;
Specification = s:Start* l:Last {{
std::vector<bool> specs (4, false);
for(Value::iterator it = s.getValues().begin(); it != s.getValues().end(); it ++)
{
const Value & v = *it;
int *x = (int*) v.getValue();
int* x = (int*) v.getValue();
if(!x)
{
printf("error x is null\n");
exit(0);
}
res = res & *x;
specs[*x -1] = true;
}
int * y = (int *) l.getValue();
int * ret = (int *) malloc(sizeof(int));
*ret = res & *y;
value = ret; }}
if(!y) { printf("error y is null\n"); exit(0); }
specs[*y-1] = true;
value = &specs; }}
ProjField = OPENS Spacing s:Specification CLOSES Spacing t:Type_form id:Identifier SEMI Spacing {{ int * spec = (int *)s.getValue();
value = new ProjField(*spec, (Type *) t.getValue(), (char *) id.getValue()); }}
bool in = false; bool out = false; bool bind = false; bool alloc = false;
std::vector<bool>* specs = (std::vector<bool>*) s.getValue();
int i = 0;
for(std::vector<bool>::iterator it = specs->begin(); it != specs->end(); it ++)
{
if(*it)
{
if(i == 0)
out = true;
else if(i == 1)
in = true;
else if(i == 2)
bind = true;
else
alloc = true;
}
}
value = new ProjectionField(in, out, alloc, bind, (Type *) t.getValue(), (char *) id.getValue()); }}
Last = k:Keyword Spacing {{ value = k; }}
......@@ -293,16 +257,39 @@ Line_comment = Line_comm_start Line_comm_rest
Line_comm_end = "\n"
Type = "int" | "char" | "capability" | "short" | "long" Space+ "long" | "long"
| "unsigned" Space+ "short" | "unsigned" Space+ "int"
| "unsigned" Space+ "long" Space+ "long"
| "unsigned" Space+ "long"
Simple_Type = "int" {{ int* ret = (int*) malloc(sizeof(int)); *ret = kInt; value = ret; }}
| "char" {{ int* ret = (int*) malloc(sizeof(int)); *ret = kChar; value = ret; }}
| "capability" {{ int* ret = (int*) malloc(sizeof(int)); *ret = kCapability; value = ret; }}
| "short" {{ int* ret = (int*) malloc(sizeof(int)); *ret = kShort; value = ret; }}
| "long" Space+ "long" {{ int* ret = (int*) malloc(sizeof(int)); *ret = kLongLong; value = ret; }}
| "long" {{ int* ret = (int*) malloc(sizeof(int)); *ret = kLong; value = ret; }}
Modifier = "unsigned" {{
int* ret = (int*) malloc(sizeof(int));
*ret = kUnsigned;
value = ret; }}
Type_w_modifier = m:Modifier Space+ s:Simple_Type {{
int* val = (int*) m.getValue();
int* sval = (int*) s.getValue();
value = new PrimitiveType((TypeModifier) *val,
(PrimType) *sval); }}
Type = Type_w_modifier
| s:Simple_Type {{
int* sval = (int*) s.getValue();
value = new PrimitiveType(kNone, (PrimType) *sval); }}
## just ignore the modules ones, not supporting right now
Type_form = "projection" Space+ m:Identifier "::" name:Identifier Spacing "*" Spacing
| "projection" Space+ m:Identifier "::" name:Identifier Space+
| "projection" Space+ name:Identifier Spacing "*" Spacing
| "projection" Space+ name:Identifier Space+
| t:Type Space+
| "projection" Space+ name:Identifier Spacing "*" Spacing {{
value = new ProjectionType((char *)name.getValue(), 1); }}
| "projection" Space+ name:Identifier Space+ {{
value = new ProjectionType((char *)name.getValue(), 0); }}
| t:Type Space+ {{
value = (PrimitiveType *) t.getValue(); }}
Spacing = Space*
......
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