greybel-interpreter 3.4.1 → 3.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,6 @@
1
1
  import { ASTEvaluationExpression } from 'miniscript-core';
2
2
  import { OperationContext } from '../context';
3
3
  import { CustomValue } from '../types/base';
4
- import { CustomBoolean } from '../types/boolean';
5
4
  import { CustomList } from '../types/list';
6
5
  import { CustomNumber } from '../types/number';
7
6
  import { CustomString } from '../types/string';
@@ -40,7 +39,7 @@ export declare class Evaluate extends Operation {
40
39
  constructor(item: ASTEvaluationExpression, target?: string);
41
40
  build(visit: CPSVisit): Promise<Operation>;
42
41
  resolveBinaryExpression(ctx: OperationContext, expr: Evaluate): Promise<CustomValue>;
43
- resolveLogicalExpression(ctx: OperationContext, expr: Evaluate): Promise<CustomValue | CustomBoolean>;
42
+ resolveLogicalExpression(ctx: OperationContext, expr: Evaluate): Promise<CustomValue | CustomNumber>;
44
43
  resolve(ctx: OperationContext, op: Operation): Promise<CustomValue>;
45
44
  handle(ctx: OperationContext): Promise<CustomValue>;
46
45
  }
@@ -22,9 +22,24 @@ const number_1 = require("../types/number");
22
22
  const string_1 = require("../types/string");
23
23
  const deep_equal_1 = require("../utils/deep-equal");
24
24
  const operation_1 = require("./operation");
25
+ const absClamp01 = (d) => {
26
+ if (d < 0)
27
+ d = -d;
28
+ if (d > 1)
29
+ return 1;
30
+ return d;
31
+ };
25
32
  exports.GenericProcessorHandler = {
26
- [miniscript_core_1.Operator.And]: (a, b) => new boolean_1.CustomBoolean(a.toTruthy() && b.toTruthy()),
27
- [miniscript_core_1.Operator.Or]: (a, b) => new boolean_1.CustomBoolean(a.toTruthy() || b.toTruthy())
33
+ [miniscript_core_1.Operator.And]: (a, b) => {
34
+ const left = Number(a instanceof number_1.CustomNumber ? a.toNumber() : a.toTruthy());
35
+ const right = Number(b instanceof number_1.CustomNumber ? b.toNumber() : b.toTruthy());
36
+ return new number_1.CustomNumber(absClamp01(left * right));
37
+ },
38
+ [miniscript_core_1.Operator.Or]: (a, b) => {
39
+ const left = Number(a instanceof number_1.CustomNumber ? a.toNumber() : a.toTruthy());
40
+ const right = Number(b instanceof number_1.CustomNumber ? b.toNumber() : b.toTruthy());
41
+ return new number_1.CustomNumber(absClamp01(left + right - left * right));
42
+ }
28
43
  };
29
44
  exports.NumberProcessorHandler = {
30
45
  [miniscript_core_1.Operator.Plus]: (a, b) => new number_1.CustomNumber(a.toNumber() + b.toNumber()),
@@ -317,6 +332,9 @@ class Evaluate extends operation_1.Operation {
317
332
  return new boolean_1.CustomBoolean(false);
318
333
  }
319
334
  else if (expr.op === miniscript_core_1.Operator.Or && left.toTruthy()) {
335
+ if (left instanceof number_1.CustomNumber) {
336
+ return new number_1.CustomNumber(absClamp01(left.value));
337
+ }
320
338
  return new boolean_1.CustomBoolean(true);
321
339
  }
322
340
  const right = yield this.resolve(ctx, expr.right);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "3.4.1",
3
+ "version": "3.4.3",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",
@@ -49,7 +49,7 @@
49
49
  },
50
50
  "dependencies": {
51
51
  "lru-cache": "^10.0.1",
52
- "greybel-core": "~1.4.1"
52
+ "greybel-core": "~1.4.2"
53
53
  },
54
54
  "keywords": [
55
55
  "miniscript"