deal with eval errors

This commit is contained in:
Joeri Exelmans 2025-05-27 10:56:14 +02:00
parent 32bdc23ea7
commit 4a4cee6ee9
5 changed files with 31 additions and 3 deletions

View file

@ -8,6 +8,8 @@ import { biggerExample, emptySet, factorial, higherOrder, higherOrder2Params, in
import './App.css';
import { evalExpr } from '../../eval/eval';
import { Value } from '../other/Value';
import { Type } from '../other/Type';
const examples: [string, ExprBlockState][] = [
@ -182,7 +184,13 @@ export function App() {
}}
typeInfo={typeInfo}
/>
={evalExpr(currentState, extendedEnv)}
=
<Value dynamic={{
i: evalExpr(currentState, extendedEnv),
t: typeInfo.type,
}}/>
::
<Type type={typeInfo.type}/>
</GlobalContext>
</main>

View file

@ -1,8 +1,13 @@
import { getHumanReadableName, getSymbol, symbolDict, symbolDictIterator, symbolFunction, symbolList, symbolProduct, symbolSet, symbolSetIterator, symbolSum } from "dope2";
import "./Type.css";
import { ValueUnknown } from "./Value";
export function Type({type}) {
if (type === undefined) {
// Types are just values, and sometimes, when using a Type as a Value, the Type is unknown (e.g., because of an eval error when computing the type)
return <ValueUnknown/>;
}
const symbol = getSymbol(type);
switch (symbol) {
case symbolFunction:

View file

@ -16,4 +16,8 @@
margin-right: 2px;
padding-left: 2px;
padding-right: 2px;
}
.valueUnknown {
color: darkgrey;
}

View file

@ -1,4 +1,4 @@
import {getType, getInst, getSymbol, Double, Int, symbolFunction, symbolProduct, symbolSum, symbolDict, symbolSet, symbolList, eqType, match, getLeft, getRight, dict, Bool, set, Unit, symbolType, symbolUUID, getHumanReadableName, Ordering} from "dope2";
import {getType, getInst, getSymbol, Double, Int, symbolFunction, symbolProduct, symbolSum, symbolDict, symbolSet, symbolList, eqType, match, getLeft, getRight, dict, Bool, set, Unit, symbolType, symbolUUID, getHumanReadableName, Ordering, isTypeVar} from "dope2";
import "./Value.css";
import { Type } from "./Type";
@ -6,6 +6,11 @@ import { Type } from "./Type";
export function Value({dynamic}) {
const type = getType(dynamic);
const inst = getInst(dynamic);
if (inst === undefined) {
// Sometimes we only know the type of a value, not the value itself.
// For instance, if there is an evaluation error somewhere.
return <ValueUnknown/>;
}
if (eqType(type)(Double)) {
return <ValueDouble val={inst}/>;
}
@ -87,4 +92,7 @@ function ValueUUID({val}) {
}
function ValueOrdering({val}) {
return <span className="valueOrdering">{{[-1]: "LessThan", [0]: "Equal", [1]: "GreaterThan" }[val]}</span>
}
export function ValueUnknown() {
return <span className="valueUnknown">unknown</span>
}

View file

@ -49,7 +49,10 @@ export function evalCall(s: CallBlockState, env: DynamicEnvironment): EvalResult
const fn = evalExpr(s.fn, env);
const input = evalExpr(s.input, env);
if (fn !== undefined && input !== undefined) {
return fn(input);
try {
return fn(input);
}
catch {}
}
}