From 88cd2b7c5470a46967044ecdfa9c00b2e4260e3c Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Sat, 13 Jan 2024 16:15:04 -0600 Subject: [PATCH 1/2] Danger.location() function --- packages/squiggle-lang/src/fr/danger.ts | 44 ++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/squiggle-lang/src/fr/danger.ts b/packages/squiggle-lang/src/fr/danger.ts index 7b77b71563..a36f9a4dee 100644 --- a/packages/squiggle-lang/src/fr/danger.ts +++ b/packages/squiggle-lang/src/fr/danger.ts @@ -17,6 +17,7 @@ import { makeDefinition } from "../library/registry/fnDefinition.js"; import { frAny, frArray, + frDict, frDist, frDistPointset, frLambda, @@ -566,12 +567,51 @@ Note: The Poisson distribution is a discrete distribution. When representing thi ], }), ]; +const otherLibrary: FRFunction[] = [ + maker.make({ + name: "location", + displaySection: "Other", + description: + "Returns a dictionary describing the location in source code where this function was called.", + definitions: [ + makeDefinition( + [], + frDict( + ["source", frString], + [ + "start", + frDict( + ["line", frNumber], + ["column", frNumber], + ["offset", frNumber] + ), + ], + [ + "end", + frDict( + ["line", frNumber], + ["column", frNumber], + ["offset", frNumber] + ), + ] + ), + (_, { frameStack }) => { + const location = frameStack.getTopFrame()?.location; + if (!location) { + throw new REOther("Location is missing in call stack"); + } + return location; + } + ), + ], + }), +]; export const library = [ // Combinatorics ...combinatoricsLibrary, - // // Integration + // Integration ...integrationLibrary, // Diminishing marginal return functions @@ -579,4 +619,6 @@ export const library = [ // previously called `scaleLog`/`scaleExp`/... ...mapYLibrary, + + ...otherLibrary, ]; From a11260a55ed218a0b84de54b30a5880b71b51cc0 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Sun, 14 Jan 2024 19:19:34 -0600 Subject: [PATCH 2/2] location() function --- .changeset/pink-humans-know.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/pink-humans-know.md diff --git a/.changeset/pink-humans-know.md b/.changeset/pink-humans-know.md new file mode 100644 index 0000000000..08187d1a14 --- /dev/null +++ b/.changeset/pink-humans-know.md @@ -0,0 +1,5 @@ +--- +"@quri/squiggle-lang": patch +--- + +Added `Danger.location()` function