Skip to content

Commit

Permalink
function support on r0
Browse files Browse the repository at this point in the history
  • Loading branch information
chutasano committed Apr 1, 2018
1 parent 80185a1 commit 2032537
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 29 deletions.
3 changes: 3 additions & 0 deletions bugs.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Need to properly destruct x0s (try a maybe delete implementation for I(Src)*(Dst)*

IMPORTANT: need to actually do garbage collection (all implementation left in
lib.c AFAIK. Hopefully no weirdness)

2 changes: 1 addition & 1 deletion interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ mv interp(const P &p)
{
vecs.clear();
map<string, mv> map;
return eval(p.e, map);
return eval(p.funcs.at(0).e, map);
}

static bool veceq(mv actual, vec_t expect[], int where)
Expand Down
115 changes: 103 additions & 12 deletions rep/r0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ string gensym(string sym, bool reset = false)
return sym + "_" + to_string(id);
}

P::P(E* ee, int heap_s) : e(ee)
P::P(std::vector<F> fs, string to_run, int heap_s) : funcs(fs), to_run(to_run)
{
if (heap_s%8 != 0 || heap_s < 0)
{
Expand All @@ -64,26 +64,119 @@ P::P(E* ee, int heap_s) : e(ee)
}
}

P::P(E* ee, int heap_s)
{
funcs.push_back(F("main", { }, TUNKNOWN, ee));
to_run = "main";
if (heap_s%8 != 0 || heap_s < 0)
{
cerr << "ERROR: invalid heap_size: " << heap_s << endl;
cerr << "using default heapsize of 2048\n";
heap_size = 2048;
}
else
{
heap_size = heap_s;
}
}

P::P(const P &obj)
{
this->e = obj.e->clone();
for (const F &f : obj.funcs)
{
this->funcs.push_back(f.clone());
}
this->to_run = obj.to_run;
this->heap_size = obj.heap_size;
}

void P::deep_delete()
{
this->e->deep_delete();
delete this->e;
for (F f : funcs)
{
f.deep_delete();
}
}

void P::uniquify()
{
gensym("", true); // resets gensym count
gensym("", true);
for (F f : funcs)
{
f.uniquify();
}
}

bool P::is_unique() const
{
for (const F &f : funcs)
{
if (!f.is_unique())
{
return false;
}
}
return true;
}

c0::P P::flatten() const
{
// TODO
vector<c0::P> cs;
for (const F &f : funcs)
{
//cs.push_back(f.flatten());
if (f.name == to_run)
{
return f.flatten();
}
}
exit(0);
}

void P::type_check()
{
vec_type.clear();
vec_type[TVEC] = { };
for (F &f : funcs)
{
f.type_check();
if (f.name == to_run)
{
t = f.t;
}
}
}

void P::desugar()
{
for (F &f : funcs)
{
f.desugar();
}
}

F::F(const F &obj)
{
name = obj.name;
args = obj.args;
t = obj.t;
e = obj.e;
}

F F::clone() const
{
E* e_copied = e->clone();
return F(name, args, t, e_copied);
}

void F::uniquify()
{
unordered_map<string, string> varmap;
this->e->uniquify(varmap);
}

bool P::is_unique() const
bool F::is_unique() const
{
list<string> varnames;
queue<E*> nodes;
Expand All @@ -106,7 +199,7 @@ bool P::is_unique() const
return varnames.size() == uniquenames.size();
}

c0::P P::flatten() const
c0::P F::flatten() const
{
unordered_map<string, int> vars;
vector<c0::AS*> stmts;
Expand All @@ -116,18 +209,16 @@ c0::P P::flatten() const
cerr << "Type check failed";
exit(1);
}
return c0::P(vars, stmts, a, t, heap_size);
return c0::P(vars, stmts, a, t, 2048);
}

void P::type_check()
void F::type_check()
{
vec_type.clear();
vec_type[TVEC] = { };
unordered_map<string, int> vars;
t = e->t_check(vars);
}

void P::desugar()
void F::desugar()
{
e = e->desugar();
}
Expand Down
51 changes: 35 additions & 16 deletions rep/r0.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,6 @@ namespace r0
int t;
};

struct P
{
P(E* ee, int heap_s);
P(const P &obj);
E* e;
int heap_size;
int t;
void deep_delete();
bool is_unique() const;
void uniquify();
void type_check();
c0::P flatten() const;
void desugar();
};

struct Num : E
{
Num(int64_t v) { value = v; }
Expand Down Expand Up @@ -131,7 +116,7 @@ namespace r0
GlobalVar* clone() const;
void deep_delete() { }
E* desugar() { return this; }
};
};

struct Call : E
{
Expand Down Expand Up @@ -244,5 +229,39 @@ namespace r0
void deep_delete();
E* desugar();
};

struct F
{
F(std::string name, std::vector<Var> args, int t, E* e) : name(name), args(args), t(t), e(e) { }
F(const F &obj);
std::string name;
std::vector<Var> args;
int t;
E* e;
F clone() const;
void deep_delete() { this->e->deep_delete(); delete this->e; }
bool is_unique() const;
void uniquify();
void type_check();
c0::P flatten() const;
void desugar();
};

struct P
{
P(std::vector<F> fs, std::string to_run, int heap_s);
P(E* ee, int heap_s);
P(const P &obj);
std::vector<F> funcs;
int heap_size;
std::string to_run;
int t;
void deep_delete();
bool is_unique() const;
void uniquify();
void type_check();
c0::P flatten() const;
void desugar();
};
}

0 comments on commit 2032537

Please sign in to comment.