Skip to content

Commit

Permalink
Return all values if runtime error
Browse files Browse the repository at this point in the history
  • Loading branch information
OAGr committed Dec 3, 2023
1 parent 19cd514 commit accd314
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
2 changes: 2 additions & 0 deletions packages/components/src/lib/hooks/useSquiggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type SquiggleOutput = {
exports: SqDict;
result: SqValue;
bindings: SqDict;
error: any;

Check failure on line 41 in packages/components/src/lib/hooks/useSquiggle.ts

View workflow job for this annotation

GitHub Actions / Build, test, lint

Unexpected any. Specify a different type
},
SqError
>;
Expand Down Expand Up @@ -101,6 +102,7 @@ export function useSquiggle(args: SquiggleArgs): UseSquiggleOutput {
project.setContinues(sourceId, continues);
await project.run(sourceId);
const output = project.getOutput(sourceId);
console.log("output", output);

Check warning on line 105 in packages/components/src/lib/hooks/useSquiggle.ts

View workflow job for this annotation

GitHub Actions / Build, test, lint

Unexpected console statement
const executionTime = Date.now() - startTime;

setSquiggleOutput({
Expand Down
13 changes: 9 additions & 4 deletions packages/squiggle-lang/src/public/SqProject/ProjectItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type RunOutput = {
result: Value;
bindings: Bindings;
exports: Bindings;
error: IRuntimeError | undefined;
};

export type Import =
Expand Down Expand Up @@ -220,10 +221,13 @@ export class ProjectItem {
throw new Error("Expected Program expression");
}

const [result, contextAfterEvaluation] = evaluate(expression.value, {
...context,
evaluate: asyncEvaluate,
});
const [result, contextAfterEvaluation, error] = evaluate(
expression.value,
{
...context,
evaluate: asyncEvaluate,
}
);

const bindings = contextAfterEvaluation.stack.asBindings();
const exportNames = new Set(expression.value.value.exports);
Expand All @@ -232,6 +236,7 @@ export class ProjectItem {
result,
bindings,
exports,
error,
});
} catch (e: unknown) {
this.failRun(new SqRuntimeError(IRuntimeError.fromException(e)));
Expand Down
7 changes: 6 additions & 1 deletion packages/squiggle-lang/src/public/SqProject/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,12 @@ export class SqProject {
)
);

return Result.Ok({ result, bindings, exports });
return Result.Ok({
result,
bindings,
exports,
error: internalOutputR.value.error,
});
}

getResult(sourceId: string): Result.result<SqValue, SqError> {
Expand Down
1 change: 1 addition & 0 deletions packages/squiggle-lang/src/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type SqOutput = {
result: SqValue;
bindings: SqDict;
exports: SqDict;
error: any;
};

export type SqOutputResult = result<SqOutput, SqError>;
35 changes: 20 additions & 15 deletions packages/squiggle-lang/src/reducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ import { UserDefinedLambdaParameter, UserDefinedLambda } from "./lambda.js";
export type ReducerFn = (
expression: Expression,
context: Context.ReducerContext
) => [Value, Context.ReducerContext];
) => [Value, Context.ReducerContext, any | undefined];

type SubReducerFn<T extends Expression["type"] = Expression["type"]> = (
expressionValue: Extract<Expression, { type: T }>["value"],
context: Context.ReducerContext,
ast: ASTNode
) => [Value, Context.ReducerContext];
) => [Value, Context.ReducerContext, any | undefined];

function throwFrom(
error: ErrorMessage,
Expand Down Expand Up @@ -104,7 +104,7 @@ const evaluateBlock: SubReducerFn<"Block"> = (statements, context) => {
currentContext
);
}
return [currentValue, context]; // throw away block's context
return [currentValue, context, undefined]; // throw away block's context
};

const evaluateProgram: SubReducerFn<"Program"> = (expressionValue, context) => {
Expand All @@ -113,12 +113,16 @@ const evaluateProgram: SubReducerFn<"Program"> = (expressionValue, context) => {
let currentValue: Value = vVoid();

for (const statement of expressionValue.statements) {
[currentValue, currentContext] = context.evaluate(
statement,
currentContext
);
try {
[currentValue, currentContext] = context.evaluate(
statement,
currentContext
);
} catch (e) {
return [currentValue, currentContext, e];
}
}
return [currentValue, currentContext];
return [currentValue, currentContext, undefined];
};

const evaluateArray: SubReducerFn<"Array"> = (expressionValue, context) => {
Expand All @@ -127,7 +131,7 @@ const evaluateArray: SubReducerFn<"Array"> = (expressionValue, context) => {
return value;
});
const value = vArray(values);
return [value, context];
return [value, context, undefined];
};

const evaluateDict: SubReducerFn<"Dict"> = (expressionValue, context, ast) => {
Expand All @@ -148,7 +152,7 @@ const evaluateDict: SubReducerFn<"Dict"> = (expressionValue, context, ast) => {
})
)
);
return [value, context];
return [value, context, undefined];
};

const evaluateAssign: SubReducerFn<"Assign"> = (expressionValue, context) => {
Expand All @@ -163,6 +167,7 @@ const evaluateAssign: SubReducerFn<"Assign"> = (expressionValue, context) => {
evaluate: context.evaluate,
inFunction: context.inFunction,
},
undefined,
];
};

Expand All @@ -171,11 +176,11 @@ const evaluateResolvedSymbol: SubReducerFn<"ResolvedSymbol"> = (
context
) => {
const value = context.stack.get(expressionValue.offset);
return [value, context];
return [value, context, undefined];
};

const evaluateValue: SubReducerFn<"Value"> = (expressionValue, context) => {
return [expressionValue, context];
return [expressionValue, context, undefined];
};

const evaluateTernary: SubReducerFn<"Ternary"> = (
Expand All @@ -199,7 +204,7 @@ const evaluateTernary: SubReducerFn<"Ternary"> = (
predicateResult.value ? expressionValue.ifTrue : expressionValue.ifFalse,
context
);
return [value, context];
return [value, context, undefined];
};

const evaluateLambda: SubReducerFn<"Lambda"> = (
Expand Down Expand Up @@ -246,7 +251,7 @@ const evaluateLambda: SubReducerFn<"Lambda"> = (
ast.location
)
);
return [value, context];
return [value, context, undefined];
};

const evaluateCall: SubReducerFn<"Call"> = (expressionValue, context, ast) => {
Expand All @@ -262,7 +267,7 @@ const evaluateCall: SubReducerFn<"Call"> = (expressionValue, context, ast) => {
context,
ast // we pass the ast of a current expression here, to put it on frameStack and in the resulting value
);
return [result, context];
return [result, context, undefined];
}
default:
return throwFrom(new RENotAFunction(lambda.toString()), context, ast);
Expand Down

0 comments on commit accd314

Please sign in to comment.