Skip to content

Commit

Permalink
Merge pull request #1759 from ds26gte/table-annot
Browse files Browse the repository at this point in the history
Add annotation for table-from-raw-array(), checking arg is indeed a raw array of rows
  • Loading branch information
ds26gte authored Dec 6, 2024
2 parents 8d9fba0 + 4bebd23 commit c764908
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/arr/trove/tables.arr
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
provide:
* hiding (is-kv-pairs),
* hiding (is-kv-pairs, is-raw-array-of-rows),
type *
end

import global as G
include from G: raw-array-duplicate end
include lists
import table as T

type Reducer<Acc, InVal, OutVal> = {
one :: (InVal -> {Acc; OutVal}),
Expand Down Expand Up @@ -88,7 +89,11 @@ fun empty-table(col-names :: List<String>) -> Table:
end
end

fun table-from-raw-array(arr):
fun is-raw-array-of-rows(ra :: RawArray<Any>) -> Boolean:
raw-array-fold(lam(base, elt, _): base and is-row(elt) end, true, ra, 0)
end

fun table-from-raw-array(arr :: T.RawArrayOfRows) -> Table:
col-names = raw-array-get(arr, 0).get-column-names()
with-cols = empty-table(col-names)
for raw-array-fold(t from with-cols, r from arr, _ from 0):
Expand Down
2 changes: 1 addition & 1 deletion src/js/base/post-load-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ define("pyret-base/js/post-load-hooks", function() {
runtime["checkEQ"] = runtime.makeCheckType(ffi.isEqualityResult, "EqualityResult");
},
"builtin://table": function(table) {
table = table.jsmod;
table = runtime.getField(runtime.getField(table, "provide-plus-types"), "internal");
runtime["makeTable"] = table.makeTable;
runtime["makeRow"] = table.makeRow;
runtime["makeRowFromArray"] = table.makeRowFromArray;
Expand Down
4 changes: 3 additions & 1 deletion src/js/trove/reactors.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@
i += 1;
return ans;
});
return tables.makeTable(["tick", "state"], rows);
console.log('tables =', tables);
return gf(tables, "internal").makeTable(["tick", "state"], rows);
// return runtime.makeTable(["tick", "state"], rows);
}
else {
runtime.ffi.throwMessageException("Tried to get trace of a reactor that isn't tracing; try calling start-trace() first");
Expand Down
34 changes: 28 additions & 6 deletions src/js/trove/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
nativeRequires: [
"pyret-base/js/type-util"
],
provides: {},
provides: {
types: {
'RawArrayOfRows': 'tany'
}
},
theModule: function(runtime, namespace, uri, VSlib, EQlib, ffi, t) {
var get = runtime.getField;

Expand All @@ -23,6 +27,20 @@
var brandRow = runtime.namedBrander("row", ["table: row brander"]);
var annRow = runtime.makeBranderAnn(brandRow, "Row");

var ann = function(name, pred) {
return runtime.makePrimitiveAnn(name, pred);
};

function isRawArrayOfRows(raor) {
if (!Array.isArray(raor)) return false;
for (let i = 0; i < raor.length; i++) {
if (!runtime.hasBrand(raor[i], brandRow._brand)) return false;
}
return true;
}

var annRawArrayOfRows = ann("RawArrayOfRows", isRawArrayOfRows);

var rowGetValue = runtime.makeMethod1(function(self, arg) {
ffi.checkArity(2, arguments, "get-value", true);
runtime.checkArgsInternal2("tables", "get-value",
Expand Down Expand Up @@ -820,16 +838,20 @@
}));
}

return runtime.makeJSModuleReturn({
TableAnn : annTable,
RowAnn : annRow,
var internal = {
makeTable: makeTable,
makeRow: makeRow,
makeRowFromArray: makeRowFromArray,
openTable: openTable,
isTable: isTable,
isRow: isRow
},
{});
};
var types = {
Table: annTable,
Row: annRow,
RawArrayOfRows: annRawArrayOfRows
};
var values = {};
return runtime.makeModuleReturn(values, types, internal);
}
})
18 changes: 17 additions & 1 deletion tests/pyret/tests/test-tables.arr
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,24 @@ check "table-from-rows":
t4 = table-from-rows.make(raw-array-from-list(new-row-list))

nothing does-not-raise # Dummy test to avoid well-formedness errors in the previous row
end

[table-from-rows:
[list:
[raw-row: {"A"; 5}, {"B"; 7}, {"C"; 8}],
[raw-row: {"A"; 1}, {"B"; 2}, {"C"; 3}]]]
raises "RawArrayOfRows"

[table-from-rows:
1, [raw-row: {"A"; 1}, {"B"; 2}],
false, [raw-row: {"A"; 3}, {"B"; 4}],
"non-row string", [raw-row: {"A"; 5}, {"B"; 6}]]
raises "RawArrayOfRows"

[table-from-rows:
[raw-row: 5, {"B"; 7}, {"C"; 8}],
[raw-row: {"A"; 1}, {"B"; 2}, {"C"; 3}]]
raises "KeyValPair"
end

table-from-column = TS.table-from-column
table-from-columns = TS.table-from-columns
Expand Down

0 comments on commit c764908

Please sign in to comment.