From 20325375ec40d99acd1162312a5d8a1f9d6f204a Mon Sep 17 00:00:00 2001 From: Chuta Sano Date: Sun, 1 Apr 2018 15:27:21 -0400 Subject: [PATCH] function support on r0 --- bugs.txt | 3 ++ interp.cpp | 2 +- rep/r0.cpp | 115 +++++++++++++++++++++++++++++++++++++++++++++++------ rep/r0.h | 51 ++++++++++++++++-------- 4 files changed, 142 insertions(+), 29 deletions(-) diff --git a/bugs.txt b/bugs.txt index b0361be..b3680af 100644 --- a/bugs.txt +++ b/bugs.txt @@ -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) + diff --git a/interp.cpp b/interp.cpp index 337285c..affe6ac 100644 --- a/interp.cpp +++ b/interp.cpp @@ -158,7 +158,7 @@ mv interp(const P &p) { vecs.clear(); map map; - return eval(p.e, map); + return eval(p.funcs.at(0).e, map); } static bool veceq(mv actual, vec_t expect[], int where) diff --git a/rep/r0.cpp b/rep/r0.cpp index 940b80a..d5d6640 100644 --- a/rep/r0.cpp +++ b/rep/r0.cpp @@ -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 fs, string to_run, int heap_s) : funcs(fs), to_run(to_run) { if (heap_s%8 != 0 || heap_s < 0) { @@ -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 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 varmap; this->e->uniquify(varmap); } -bool P::is_unique() const +bool F::is_unique() const { list varnames; queue nodes; @@ -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 vars; vector stmts; @@ -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 vars; t = e->t_check(vars); } -void P::desugar() +void F::desugar() { e = e->desugar(); } diff --git a/rep/r0.h b/rep/r0.h index f47e5fe..3532ea8 100644 --- a/rep/r0.h +++ b/rep/r0.h @@ -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; } @@ -131,7 +116,7 @@ namespace r0 GlobalVar* clone() const; void deep_delete() { } E* desugar() { return this; } - }; + }; struct Call : E { @@ -244,5 +229,39 @@ namespace r0 void deep_delete(); E* desugar(); }; + + struct F + { + F(std::string name, std::vector args, int t, E* e) : name(name), args(args), t(t), e(e) { } + F(const F &obj); + std::string name; + std::vector 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 fs, std::string to_run, int heap_s); + P(E* ee, int heap_s); + P(const P &obj); + std::vector 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(); + }; }