Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dawa79 committed Feb 23, 2021
2 parents 937704a + 90c9865 commit b360f05
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "Build"
on: [push, pull_request]
on: [push]
jobs:
linux-clang-build:
runs-on: ${{matrix.os}}
Expand Down
11 changes: 11 additions & 0 deletions include/hobbes/db/series.H
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ namespace hobbes {

typedef uint64_t ufileref;

// A -> (A)
MonoTypePtr entuple(const MonoTypePtr&);
// A N -> carray A N
MonoTypePtr carrayty(const MonoTypePtr&, const MonoTypePtr&);
// A -> ^x.(()+(A*x@?))
MonoTypePtr storedListOf(const MonoTypePtr&);
// A N -> fseq A N
MonoTypePtr storedStreamOf(const MonoTypePtr&, size_t);
// A -- StoredAs A B --> B
MonoTypePtr storeAs(cc* c, const MonoTypePtr&);

class RawStoredSeries {
public:
RawStoredSeries(cc*, writer*, const std::string&, const MonoTypePtr&, size_t);
Expand Down
27 changes: 27 additions & 0 deletions include/hobbes/lang/tylift.H
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,33 @@ template <typename T>
template <typename T>
struct lift<T, false, typename tbool<T::is_hmeta_enum && sizeof(typename T::rep_t)!=sizeof(uint32_t)>::type> : public liftEnum<T, typename T::rep_t> { };

// lift variant records
template <typename T>
struct liftVariantRecord {
struct descF {
Variant::Members* ctors;
typedb* tenv;
descF(Variant::Members* ctors, typedb* tenv) : ctors(ctors), tenv(tenv) { }

template <typename U>
void ctor(const char* n, int id) {
this->ctors->push_back(Variant::Member(n, lift<U, true>::type(*this->tenv), id));
}
};

static MonoTypePtr type(typedb& tenv) {
Variant::Members ms;
descF f(&ms, &tenv);
T::meta(f);
return Variant::make(ms);
}
};

template <typename T>
struct lift<T, true, typename tbool<T::is_hmeta_variant>::type> : public liftVariantRecord<T> { };
template <typename T>
struct lift<T*, false, typename tbool<T::is_hmeta_variant>::type> : public liftVariantRecord<T> { };

// lift plain tuples
template <typename ... Fields>
struct liftTuple {
Expand Down
10 changes: 5 additions & 5 deletions lib/hobbes/db/series.C
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,22 @@ void StoredSeries::clear(bool signal) {
*******/

// A -> (A)
static MonoTypePtr entuple(const MonoTypePtr& ty) {
MonoTypePtr entuple(const MonoTypePtr& ty) {
Record::Members ms;
ms.push_back(Record::Member(".f0", ty));
return MonoTypePtr(Record::make(ms));
}

// A N -> carray A N
static MonoTypePtr carrayty(const MonoTypePtr& ty, const MonoTypePtr& n) {
MonoTypePtr carrayty(const MonoTypePtr& ty, const MonoTypePtr& n) {
Record::Members ms;
ms.push_back(Record::Member("avail", primty("long")));
ms.push_back(Record::Member("buffer", FixedArray::make(tvar("t"), tvar("c"))));
return tapp(primty("carray", tabs(str::strings("t","c"), Record::make(ms))), list(ty, n));
}

// A -> ^x.(()+(A*x@?))
static MonoTypePtr storedListOf(const MonoTypePtr& ty) {
MonoTypePtr storedListOf(const MonoTypePtr& ty) {
Record::Members pms;
pms.push_back(Record::Member(".f0", ty));
pms.push_back(Record::Member(".f1", fileRefTy(tvar("x"))));
Expand All @@ -182,12 +182,12 @@ static MonoTypePtr storedListOf(const MonoTypePtr& ty) {
}

// A N -> fseq A N
static MonoTypePtr storedStreamOf(const MonoTypePtr& ty, size_t n) {
MonoTypePtr storedStreamOf(const MonoTypePtr& ty, size_t n) {
return tapp(primty("fseq", tabs(str::strings("t", "c"), fileRefTy(storedListOf(fileRefTy(carrayty(tvar("t"), tvar("c"))))))), list(ty, tlong(n)));
}

// A -- StoredAs A B --> B
static MonoTypePtr storeAs(cc* c, const MonoTypePtr& ty) {
MonoTypePtr storeAs(cc* c, const MonoTypePtr& ty) {
// construct the constraint that the input type stores to some output type
MonoTypeUnifier u(c->typeEnv());
MonoTypePtr sty = freshTypeVar();
Expand Down
13 changes: 13 additions & 0 deletions test/Variants.C
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ TEST(Variants, Enums) {
EXPECT_EQ((makeStdString(c().compileFn<const array<char>*(PColor pc)>("x", "show(x)")(PColor::Red()))), "|Red|");
}

DEFINE_VARIANT(Item,
(vehicle, int),
(food, float)
);

TEST(Variants, VariantRecord) {
Item aVehicle = Item::vehicle(123);
Item aFood = Item::food(321.123);

EXPECT_EQ((c().compileFn<int(const Item*)>("x", "match x with | |vehicle=v| -> v | |food=_| -> 321")(&aVehicle)), 123);
EXPECT_EQ((c().compileFn<int(const Item*)>("x", "match x with | |vehicle=v| -> v | |food=_| -> 321")(&aFood)), 321);
}

TEST(Variants, WithFunctions) {
EXPECT_TRUE(c().compileFn<bool()>("either((\\|1=f|.f(1L,0L))(just(if (0 > 0) then llt else lgt)), false, id)")());
}
Expand Down

0 comments on commit b360f05

Please sign in to comment.