Skip to content

Commit

Permalink
Added lambda at r0 level. Fixed functions to properly uniquify
Browse files Browse the repository at this point in the history
  • Loading branch information
chutasano committed Apr 21, 2018
1 parent dd12989 commit dc8d564
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
main
.*.swp
woof
compile_commands.json

1 change: 0 additions & 1 deletion bugs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ IMPORTANT: need to actually do garbage collection (all implementation left in
lib.c AFAIK. Hopefully no weirdness)

Rootstack is not properly accounted for across function calls. Need to fix

48 changes: 42 additions & 6 deletions rep/r0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void P::deep_delete()
void P::uniquify()
{
gensym("", true);
for (F f : funcs)
for (F &f : funcs)
{
f.uniquify();
}
Expand Down Expand Up @@ -205,6 +205,11 @@ F F::clone() const
void F::uniquify()
{
unordered_map<string, string> varmap;
for (Var &v : args)
{
varmap[v.name] = gensym(v.name);
v.name = varmap[v.name];
}
this->e->uniquify(varmap);
}

Expand Down Expand Up @@ -235,7 +240,7 @@ c0::F F::flatten() const
{
unordered_map<string, int> vars;
vector<string> args_names;
for (Var v : args)
for (const Var &v : args)
{
vars[v.name] = v.t;
args_names.push_back(v.name);
Expand All @@ -255,7 +260,7 @@ void F::generate_fun_type(unordered_map<string, int> &vars)
vector<int> ftype;
if (t < TFUN)
{
for (Var v : args)
for (const Var &v : args)
{
if (v.t == TUNKNOWN)
{
Expand All @@ -275,7 +280,7 @@ void F::generate_fun_type(unordered_map<string, int> &vars)

void F::type_check(unordered_map<string, int> vars)
{
for (Var v : args)
for (const Var &v : args)
{
vars[v.name] = v.t;
}
Expand Down Expand Up @@ -502,15 +507,19 @@ Var* Var::clone() const

void Var::uniquify(unordered_map<string, string> m)
{
const auto &it = m.find(this->name); // FIXME const iterator will be the "right"
// thing to do instead of auto
const auto &it = m.find(this->name);
if (it != m.end())
{
#ifdef DEBUG
cout << "Uniquify var: changing " << this->name << " to " << it->second << endl;
#endif
this->name = it->second;
}
else
{
cerr << "Uniquify var: var ref DNE?\n";
exit(1);
}
}

c0::Arg* Var::to_c0(unordered_map<string, int> &vars, vector<c0::AS*> &stmts) const
Expand Down Expand Up @@ -859,6 +868,33 @@ c0::Arg* VectorSet::to_c0(unordered_map<string, int> &vars, vector<c0::AS*> &stm
return new c0::Var(s);
}

void Lambda::uniquify(unordered_map<string, string> m)
{
// manually uniquify function args
for (auto &s : args)
{
// overwrite if exists
m[s] = gensym(s);
s = m[s];
}
body->uniquify(m);
}

int Lambda::t_check(unordered_map<string, int> vmap)
{
if (t == TUNKNOWN)
{
}
return t;
}

c0::Arg* Lambda::to_c0(unordered_map<string, int> &vars, vector<c0::AS*> &stmts) const
{
string s = gensym("r0VecSet");
vars[s] = t;
return new c0::Var(s);
}

list<E*> Sugar::get_childs()
{
cerr << "Call desugar before using any r0->c0 functionality\n";
Expand Down
14 changes: 14 additions & 0 deletions rep/r0.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,20 @@ namespace r0
E* desugar() { vec = vec->desugar(); asg = asg->desugar(); return this; };
};

struct Lambda : E
{
Lambda(std::vector<std::string> args, E* body) : args(args), body(body) { }
std::vector<std::string> args;
E* body;
std::list<E*> get_childs() { return { body }; }
void uniquify(std::unordered_map<std::string, std::string> m);
int t_check(std::unordered_map<std::string, int>);
c0::Arg* to_c0(std::unordered_map<std::string, int> &vars, std::vector<c0::AS*> &stmts) const;
Lambda* clone() const { return new Lambda(args, body->clone()); }
void deep_delete() { body->deep_delete(); delete body; }
E* desugar() { body = body->desugar(); return this; };
};


/********* Below are syntactic sugars *********/
struct Sugar : E
Expand Down

0 comments on commit dc8d564

Please sign in to comment.