Commit 26e25b34 authored by Sarah Spall's avatar Sarah Spall Committed by Vikram Narayanan

visitor to write header files from idl, untested

parent ec790197
#include "../include/gen_visitor.h"
HeaderVisitor::HeaderVisitor(FILE* out)
{
this->out_file_ = out;
}
void HeaderVisitor::visit(File* f)
{
// where to print verbatim?
// print an ifndef?
// need to go through all typedef and print out
// a c typedef in file scope
for(std::vector<Rpc*>::iterator it = f->rpc_defs_->begin(); it != rpc_defs.end(); it++)
{
Rpc* r = (Rpc*) *it;
r->accept(this);
fprintf(this->out_f_, "\n\n");
}
}
void HeaderVisitor::visit(Rpc* r)
{
Type* rt = r->ret_type;
rt->accept(this); // this seems unnecessary in this case
char* name = r->name_;
fprintf(this->out_f_, " %s ", name);
for(std::vector<Parameter*>::iterator it = r->params->begin(); it != r->params->end(); it++)
{
Paramter* p = (Parameter*) *it;
p->accept(this);
if((it+1) != r->params->end())
fprintf(this->out_f_, ", ");
}
fprintf(this->out_f_, ");");
}
void HeaderVisitor::visit(Parameter* p)
{
p->type_->accept(this);
// print p->name_
fprintf(this->out_f_, "%s", p->name_);
}
void HeaderVisitor::visit(ProjectionType* p)
{
fprintf(this->out_f_, "%s", p->real_type_);
}
void HeaderVisitor::visit(ProjectionField* pf)
{
// nothing to do here
}
void HeaderVisitor::visit(IntegerType* it)
{
if(it->unsigned_)
fprintf(this->out_f_, "unsigned ");
switch (it->type_) {
case kChar:
fprintf(this->out_f_, "char");
break;
case kShort:
fprintf(this->out_f_, "short");
break;
case kInt:
fprintf(this->out_f_, "int");
break;
case kLong:
fprintf(this->out_f_, "long");
break;
case kLongLong:
fprintf(this->out_f_, "long long");
break;
case kCapability:
fprintf(this->out_f_, "capability_t");
break;
default:
//error
}
}
void HeaderVisitor::visit(PointerType* pt)
{
pt->type_->accept(this);
fprintf(this->out_f_, "* ");
}
void HeaderVisitor::visit(Typedef* td)
{
// print out their type so they arent confused
// print td->alias;
fprintf(this->out_f_, "%s", td->alias);
}
#include "../include/gen_visitor.h"
void SourceVisitor::visit(File* f)
{
}
void SourceVisitor::visit(Rpc* r)
{
}
void SourceVisitor::visit(Parameter* p)
{
}
void SourceVisitor::visit(ProjectionType *p)
{
}
void SourceVisitor::visit(ProjectionField *pf)
{
}
void SourceVisitor::visit(IntegerType *it)
{
}
void SourceVisitor::visit(PointerType *pt)
{
}
void SourceVisitor::visit(Typedef* td)
{
}
#ifndef GEN_VISITOR
#define GEN_VISITOR
#include "visitor.h"
class GenVisitor : public ASTVisitor
{
FILE* out_f_;
public:
virtual void visit(File *f) =0;
virtual void visit(Rpc *rpc) =0;
virtual void visit(Parameter * p) =0;
virtual void visit(ProjectionType * p) =0;
virtual void visit(ProjectionField * pf) =0;
virtual void visit(IntegerType * it) =0;
virtual void visit(PointerType * it) =0;
virtual void visit(Typedef* td) =0;
};
class HeaderVisitor : public GenVisitor
{
//FILE* out_f_;
public:
HeaderVisitor(FILE* f);
virtual void visit(File *f);
virtual void visit(Rpc *rpc);
virtual void visit(Parameter * p);
virtual void visit(ProjectionType * p);
virtual void visit(ProjectionField * pf);
virtual void visit(IntegerType * it);
virtual void visit(PointerType * it);
virtual void visit(Typedef* td);
};
class SourceVisitor : public GenVisitor
{
// FILE* out_f_;
public:
SourceVisitor(FILE* f);
virtual void visit(File *f);
virtual void visit(Rpc *rpc);
virtual void visit(Parameter * p);
virtual void visit(ProjectionType * p);
virtual void visit(ProjectionField * pf);
virtual void visit(IntegerType * it);
virtual void visit(PointerType * it);
virtual void visit(Typedef* td);
};
#endif
......@@ -82,22 +82,6 @@ class ProjectionType : public Type // complex type
virtual void marshal();
};
class Typedef : public Declaration
{
char* real_type_; // how should this be represented?
Type* marshal_type_; // should this be a supported type?
char* name_;
public:
Typedef(char* real_type, Type* marshal_type, char* typedef_name) {
this->real_type_ = real_type;
this->marshal_type_ = marshal_type;
this->name_ = typedef_name; }
char* name() { return name_; }
DefinitionType get_definition_type(){ return kTypedef; }
void accept(ASTVisitor *worker) { worker->visit(this); }
};
class Parameter : public Base
{
Type* type_;
......@@ -112,9 +96,9 @@ class Parameter : public Base
class Rpc : public Base
{
Type* return_type_;
Type* ret_type_;
char* name_;
std::vector<Parameter* >* parameters_;
std::vector<Parameter* >* params_;
public:
Rpc(Type* return_type, char* name, std::vector<Parameter* >* parameters) {
......@@ -181,18 +165,17 @@ class Message : public Definition
class File : public Scope
{
char* verbatim_;
std::vector<Rpc *>* rpc_definitions_;
std::vector<Message *>* message_definitions_;
std::map<char *, Definition *>* symbol_table_;
// believe it is only necessary to store projections in "env" since functions can't be referenced in other functions
Root_scope * root_scope_;
std::vector<Rpc *>* rpc_defs_;
std::vector<Message *>* message_defs_;
public:
File(char* verbatim, std::vector<Rpc* >* rpc_definitions, std::vector<Message* >* message_definitions,
std::map<char* , Definition* >* symbol_table) {
File(char* verbatim, Root_scope* rs, std::vector<Rpc* >* rpc_definitions, std::vector<Message* >* message_definitions) {
this->verbatim_ = verbatim;
this->root_scope_ = rs;
this->rpc_definitions_ = rpc_definitions;
this->message_definitions_ = message_definitions;
this->symbol_table_ = symbol_table; }
}
void accept(ASTVisitor *worker) { worker->visit(this); }
};
......
......@@ -95,10 +95,12 @@ File = Spacing v:Verbatim? t:Things* <eof> {{ std::vector<Definition *>* defs =
}
}
value = new Scope((char*) v.getValue(), rpcs, messages, env); }}
value = new File((char*) v.getValue(), Root_scope::instance(), rpcs, messages); }}
Typedef = "typedef" Spacing "<" Spacing rt:RealType Spacing "," Spacing mt:Type Spacing ">" Spacing id:Identifier Spacing {{
value = new Typedef((char*) rt.getValue(), (Type*) mt.getValue(), (char*) id.getValue());}}
Typedef * val = new Typedef((char*) rt.getValue(), (Type*) mt.getValue(), (char*) id.getValue());
insertType((char*)id.getValue(),val);
}}
Verbatim = "verbatim" Spacing "[" contents:VerbatimContent "]" {{ std::ostringstream total;
for(Value::iterator it = contents.getValues().begin(); it != contents.getValues().end(); it ++)
......@@ -303,7 +305,7 @@ Line_comment = Line_comm_start Line_comm_rest
Line_comm_end = "\n"
signed_type = "int" {{ value = getType("int");}}
git stsigned_type = "int" {{ value = getType("int");}}
| "char" {{ value = getType("char"); }}
| "capability" {{ value = getType("capability"); }}
| "short" {{ value = getType("short");}}
......
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