Skip to content

Commit

Permalink
Supported vector output on interp
Browse files Browse the repository at this point in the history
  • Loading branch information
chutasano committed Mar 11, 2018
1 parent 1f45784 commit 593f32c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
44 changes: 43 additions & 1 deletion interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <typeinfo>

#include "rep/r0.h"
#include "rep/type.h"
#include "test.h"

using namespace std;
using namespace r0;
Expand Down Expand Up @@ -154,6 +156,7 @@ mv eval(const E* e, map<string, mv> vmap)

mv interp(const P &p)
{
vecs.clear();
map<string, mv> map;
return eval(p.e, map);
}
Expand All @@ -163,11 +166,50 @@ bool test_interp(const P &p, int expect)
mv actual = interp(p);
if (actual.is_vector)
{
cerr << "ERROR: vector as output not yet implemented\n";
cerr << "ERROR: got vector when expecting non-vector";
return false;
}
else
{
return expect == actual.val;
}
}

static bool veceq(mv actual, vec_t expect[], int where)
{
if (actual.is_vector && expect[where].t == TVEC)
{
bool status = true;
for (int i = 0; i < expect[where].val; i++)
{
vector<mv> cur = vecs.at(actual.val);
if (cur.at(i).is_vector)
{
status &= veceq(cur.at(i), expect, where+i+1);
}
else
{
status &= cur.at(i).val == expect[where+i+1].val;
}
}
return status;
}
else
{
cerr << "ERROR: got a vector, expected non-vector";
return false;
}
}

bool test_interp(const P &p, vec_t expect[])
{
mv actual = interp(p);
if (!actual.is_vector)
{
return test_interp(p, expect[0].val);
}
else
{
return veceq(actual, expect, 0);
}
}
3 changes: 2 additions & 1 deletion interp.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "rep/r0.h"
#include "test.h"

bool test_interp(const r0::P &p, int expect);
bool test_interp(const r0::P &p, vec_t expect[]);
25 changes: 19 additions & 6 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@

using namespace std;

function<bool(r0::P, int)> testfunc;
function<bool(r0::P, vec_t[])> testfunc;

static bool both(const r0::P &p, int expect)
static bool both(const r0::P &p, vec_t expect[])
{
bool woof = test_interp(p, expect);
bool meow = test_compile(p, expect);
bool meow = test_compile(p, expect[0].val);
if (!woof)
{
cerr << "Interpreter failed\n";
Expand Down Expand Up @@ -111,7 +111,7 @@ static void ts(string name)

static int fails = 0;

static void t(r0::E* e, int expect)
static void t(r0::E* e, vec_t expect[])
{
r0::P p(e);
p.desugar();
Expand All @@ -121,11 +121,17 @@ static void t(r0::E* e, int expect)
}
else
{
cout << " Test failed!!!!!!!!!!!!!!!!1\n";
cout << " Test failed!!!!!!!!!!!!\n";
fails++;
}
}

static void t(r0::E* e, int expect)
{
vec_t woof[] = { vec_t(TNUM, expect) };
t(e, woof);
}

// test for uniqueness and uniquify
static void tu(r0::E* e, bool unique)
{
Expand Down Expand Up @@ -408,7 +414,14 @@ void test_all()
UPTR(r0::Begin, b, {v1,v2,v3,v4,v5,v6,v7,v8, n10 });
t(b, 10);
UPTR(r0::Begin, vs, {v1,v2,v3,v4,v5,v6,v7,v8});
t(vs, 1); //TODO
vec_t vs_expect[] =
{
vec_t(TVEC, 3),
vec_t(TNUM, 10),
vec_t(TNUM, -1),
vec_t(TBOOL, TB_FALSE)
};
t(vs, vs_expect);
}

cout << "Total tests failed: " << fails << endl;
Expand Down
11 changes: 11 additions & 0 deletions test.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
#pragma once

#include "rep/type.h"

struct vec_t
{
vec_t(type t, int64_t val) : t(t), val(val) { }
type t;
// val -> raw value for num/bool/void
// size for vector
int64_t val;
};

void test_all();

0 comments on commit 593f32c

Please sign in to comment.