Skip to content

Commit 752f200

Browse files
committed
inferencing on return types
1 parent c841809 commit 752f200

File tree

6 files changed

+59
-10
lines changed

6 files changed

+59
-10
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
[submodule "test-support/circuits"]
1414
path = test-support/circuits
1515
url = https://github.com/2graphic/circuits-plugin.git
16+
[submodule "test-support/bfs"]
17+
path = test-support/bfs
18+
url = https://github.com/Sheyne/sinap-bfs-plugin

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "sinap-typescript",
33
"description": "Bindings for synap-types from typescript",
4-
"version": "0.4.14",
4+
"version": "0.4.15",
55
"readme": "README.md",
66
"license": "MIT",
77
"repository": {

src/natural.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export function valueToNatural(prototypes: Map<Type.CustomObject | Type.Intersec
203203
export function naturalToValue(environment: Value.Environment, typeMap: Iterable<[Function, Type.Type]>) {
204204
const map = new Map([...typeMap].map(([a, b]) => [a.prototype, b] as [any, Type.Type]));
205205
const transformations = new Map<any, Value.Value>();
206-
return function(value: any) {
207-
return toValueInner(value, environment, map, transformations, undefined);
206+
return function(value: any, knownType?: Type.Type) {
207+
return toValueInner(value, environment, map, transformations, knownType);
208208
};
209209
}

src/program.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { naturalToValue } from "./natural";
77

88
export class TypescriptProgram implements Core.Program {
99
readonly model: Model;
10-
readonly toValue: (a: any) => Value.Value;
10+
readonly toValue: (a: any, knownType?: Type.Type) => Value.Value;
1111

1212
constructor(modelIn: Model, public plugin: TypescriptPlugin) {
1313
this.model = Model.fromSerial(modelIn.serialize(), plugin);
@@ -62,14 +62,30 @@ export class TypescriptProgram implements Core.Program {
6262
const steps: Value.CustomObject[] = [];
6363

6464
while (state instanceof this.plugin.naturalStateType) {
65-
steps.push(this.toValue(state) as Value.CustomObject);
65+
steps.push(this.toValue(state, this.plugin.types.state) as Value.CustomObject);
6666
try {
6767
state = this.plugin.implementation.step(state);
6868
} catch (err) {
6969
return { steps: steps, error: Value.makePrimitive(this.model.environment, err) };
7070
}
7171
}
72-
return { steps: steps, result: this.toValue(state) };
72+
73+
// TODO: fixup
74+
let res: Value.Value;
75+
if (this.plugin.types.result instanceof Type.Union) FoundAResult: {
76+
for (const type of this.plugin.types.result.types) {
77+
try {
78+
res = this.toValue(state, type);
79+
break FoundAResult;
80+
} catch (err) {
81+
}
82+
}
83+
throw new Error("no types worked");
84+
} else {
85+
res = this.toValue(state, this.plugin.types.result);
86+
}
87+
88+
return { steps: steps, result: res };
7389
}
7490

7591
validate() {

src/test-plugins.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as path from "path";
44
import { expect } from "chai";
55
import { Type, Value } from "sinap-types";
66
import { TypescriptPlugin } from "./plugin";
7+
import { imap } from "sinap-types/lib/util";
78

89
function verifyReserial(m1: Model, p: Plugin) {
910
const s1 = m1.serialize();
@@ -16,6 +17,12 @@ function verifyReserial(m1: Model, p: Plugin) {
1617
expect(s3).to.deep.equal(s2);
1718
}
1819

20+
function node(label: string, model: Model) {
21+
const node = model.makeNode();
22+
node.set("label", new Value.Primitive(new Type.Primitive("string"), model.environment, label));
23+
return node;
24+
}
25+
1926
describe("Actual Plugins", () => {
2027
const loader = new TypescriptPluginLoader();
2128
async function loadPlugin(...p: string[]) {
@@ -32,11 +39,9 @@ describe("Actual Plugins", () => {
3239
const plugin = await loadPlugin("test-support", "turing-machine");
3340

3441
const model = new Model(plugin);
35-
const q1 = model.makeNode();
36-
q1.set("label", new Value.Primitive(new Type.Primitive("string"), model.environment, "q1"));
42+
const q1 = node("q1", model);
3743
q1.set("isStartState", new Value.Primitive(new Type.Primitive("boolean"), model.environment, true));
38-
const q2 = model.makeNode();
39-
q2.set("label", new Value.Primitive(new Type.Primitive("string"), model.environment, "q2"));
44+
const q2 = node("q1", model);
4045
q2.set("isAcceptState", new Value.Primitive(new Type.Primitive("boolean"), model.environment, true));
4146

4247
const edge = model.makeEdge(undefined, q1, q2);
@@ -105,6 +110,30 @@ describe("Actual Plugins", () => {
105110
const prog = plugin.makeProgram(model);
106111
prog.validate();
107112

113+
verifyReserial(model, plugin);
114+
});
115+
it("loads BFS", async () => {
116+
const plugin = await loadPlugin("test-support", "bfs");
117+
118+
const model = new Model(plugin);
119+
const n1 = node("q1", model);
120+
model.makeEdge(undefined, n1, node("q2", model));
121+
122+
const prog = plugin.makeProgram(model);
123+
prog.validate();
124+
125+
const res = await prog.run([prog.model.environment.values.get(n1.uuid)!]);
126+
const result = res.result as Value.ArrayObject;
127+
expect(result).to.instanceof(Value.ArrayObject);
128+
129+
const values = [...imap((value) => {
130+
expect(value).to.instanceof(Value.CustomObject);
131+
const label = (value as Value.CustomObject).get("label") as Value.Primitive;
132+
expect(label).to.instanceof(Value.Primitive);
133+
return label.value;
134+
}, result)];
135+
expect(values).to.deep.equal(["q1", "q2"]);
136+
108137
verifyReserial(model, plugin);
109138
});
110139
});

test-support/bfs

Submodule bfs added at 1e956b5

0 commit comments

Comments
 (0)