diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_environment_test.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_environment_test.res index b20ce707..2467f43c 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_environment_test.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_environment_test.res @@ -3,15 +3,35 @@ open Reducer_Peggy_TestHelpers describe("Environment Accesss", () => { testToExpression( - "environment", - "{(:$_endOfOuterBlock_$ () (:$$_environment_$$))}", - ~v=`{sampleCount: ${ReducerInterface_InternalExpressionValue.defaultEnvironment.sampleCount->Js.Int.toString},xyPointLength: ${ReducerInterface_InternalExpressionValue.defaultEnvironment.xyPointLength->Js.Int.toString}}`, + "Environment.sampleCount", + "{(:$_endOfOuterBlock_$ () (:$_atIndex_$ (:$$_environment_$$) 'sampleCount'))}", + ~v=ReducerInterface_InternalExpressionValue.defaultEnvironment.sampleCount->Js.Int.toString, (), ) testToExpression( - "withEnvironmentSampleCount(100, environment.sampleCount)", + "Environment.withSampleCount(100, Environment.sampleCount)", "{(:$_endOfOuterBlock_$ () (:$$_withEnvironmentSampleCount_$$ 100 (:$_atIndex_$ (:$$_environment_$$) 'sampleCount')))}", ~v="100", (), ) + testToExpression( + "Environment.withSampleCount(100, 99)", + "{(:$_endOfOuterBlock_$ () (:$$_withEnvironmentSampleCount_$$ 100 99))}", + ~v="99", + (), + ) + + testToExpression( + "f(x) = Environment.withSampleCount(999, Environment.sampleCount+1); f(1)", + "{(:$_let_$ :f (:$$_lambda_$$ [x] {(:$$_withEnvironmentSampleCount_$$ 999 (:add (:$_atIndex_$ (:$$_environment_$$) 'sampleCount') 1))})); (:$_endOfOuterBlock_$ () (:f 1))}", + ~v="1000", + (), + ) + + testToExpression( + "f(x) = Environment.sampleCount+1; Environment.withSampleCount(999, f(1))", + "{(:$_let_$ :f (:$$_lambda_$$ [x] {(:add (:$_atIndex_$ (:$$_environment_$$) 'sampleCount') 1)})); (:$_endOfOuterBlock_$ () (:$$_withEnvironmentSampleCount_$$ 999 (:f 1)))}", + ~v="1000", + (), + ) }) diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res index db77a0a2..8cdb8104 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res @@ -169,13 +169,15 @@ let dispatchMacroCall = ( | InternalExpressionValue.IEvNumber(sampleCount) => { let newEnvironment = {...accessors.environment, sampleCount: Js.Math.floor(sampleCount)} let newAccessors = {...accessors, environment: newEnvironment} - reduceExpression(expr, bindings, newAccessors) + let exprBlock = eBlock(list{expr}) + reduceExpression(exprBlock, bindings, newAccessors) ->ExpressionT.EValue ->ExpressionWithContext.noContext } | _ => REExpectedType("Number", "")->Reducer_ErrorValue.toException } } + let expandExpressionList = ( aList, bindings: ExpressionT.bindings, @@ -213,8 +215,8 @@ let dispatchMacroCall = ( | list{ExpressionT.EValue(IEvCall("$$_environment_$$"))} => doEnvironment(accessors) | list{ ExpressionT.EValue(IEvCall("$$_withEnvironmentSampleCount_$$")), - expr, sampleCountExpr, + expr, } => doWithEnvironmentSampleCount(sampleCountExpr, expr, bindings, accessors) | _ => ExpressionWithContext.noContext(ExpressionT.EList(aList)) diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression.res index 1db8366b..0016e185 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression.res @@ -33,7 +33,11 @@ let rec fromNode = (node: Parse.node): expression => { let identifier = nodeIdentifier["value"] // `caseIdentifier ${identifier}`->Js.log switch identifier { - | "System.environment" => "$$_environment_$$"->ExpressionBuilder.eFunction(list{}) + | "Environment.sampleCount" => + "$_atIndex_$"->ExpressionBuilder.eFunction(list{ + "$$_environment_$$"->ExpressionBuilder.eFunction(list{}), + ExpressionBuilder.eString("sampleCount"), + }) | symbol => symbol->ExpressionBuilder.eSymbol } }