time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

SqLambda.js (2413B)


      1 import { IRuntimeError } from "../../errors/IError.js";
      2 import { getStdLib } from "../../library/index.js";
      3 import { createContext } from "../../reducer/context.js";
      4 import * as Result from "../../utility/result.js";
      5 import { SqOtherError, SqRuntimeError } from "../SqError.js";
      6 import { wrapDomain } from "./SqDomain.js";
      7 import { wrapValue } from "./index.js";
      8 function lambdaToSqLambdaSignatures(lambda) {
      9     switch (lambda.type) {
     10         case "UserDefinedLambda":
     11             return [
     12                 lambda.parameters.map((param) => {
     13                     return {
     14                         name: param.name,
     15                         domain: param.domain ? wrapDomain(param.domain.value) : undefined,
     16                     };
     17                 }),
     18             ];
     19         case "BuiltinLambda":
     20             return lambda.signatures().map((def) => def.map((p, index) => ({
     21                 name: index.toString(),
     22                 domain: undefined,
     23                 typeName: p.getName(),
     24             })));
     25     }
     26 }
     27 export class SqLambda {
     28     constructor(_value, context) {
     29         this._value = _value;
     30         this.context = context;
     31     }
     32     static createFromStdlibName(name) {
     33         const value = getStdLib().get(name);
     34         if (!value) {
     35             throw new Error(`Name ${name} not found in stdlib`);
     36         }
     37         if (value.type !== "Lambda") {
     38             throw new Error(`Stdlib value ${name} is not a function`);
     39         }
     40         return new SqLambda(value.value);
     41     }
     42     parameterCounts() {
     43         return this._value.parameterCounts();
     44     }
     45     signatures() {
     46         return lambdaToSqLambdaSignatures(this._value);
     47     }
     48     call(args, env) {
     49         if (!env) {
     50             if (!this.context) {
     51                 return Result.Err(new SqOtherError("Programmatically constructed lambda call requires env argument"));
     52             }
     53             env = this.context.project.getEnvironment();
     54         }
     55         const rawArgs = args.map((arg) => arg._value);
     56         try {
     57             const value = this._value.call(rawArgs, createContext(env));
     58             return Result.Ok(wrapValue(value));
     59         }
     60         catch (e) {
     61             return Result.Err(new SqRuntimeError(IRuntimeError.fromException(e)));
     62         }
     63     }
     64     toString() {
     65         return this._value.toString();
     66     }
     67     parameterString() {
     68         return this._value.parameterString();
     69     }
     70 }
     71 //# sourceMappingURL=SqLambda.js.map