environment access

This commit is contained in:
Umur Ozkul 2022-09-11 16:48:14 +02:00
parent 4dc7619e5a
commit 1e05507ffe
4 changed files with 83 additions and 3 deletions

View File

@ -0,0 +1,17 @@
open Jest
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}}`,
(),
)
testToExpression(
"withEnvironmentSampleCount(100, environment.sampleCount)",
"{(:$_endOfOuterBlock_$ () (:$$_withEnvironmentSampleCount_$$ 100 (:$_atIndex_$ (:$$_environment_$$) 'sampleCount')))}",
~v="100",
(),
)
})

View File

@ -152,6 +152,41 @@ let dispatchMacroCall = (
)
}
let doEnvironment = (accessors: ProjectAccessorsT.t) => {
let environment = accessors.environment
let environmentPairs = [
("sampleCount", environment.sampleCount->Js.Int.toFloat->InternalExpressionValue.IEvNumber),
(
"xyPointLength",
environment.xyPointLength->Js.Int.toFloat->InternalExpressionValue.IEvNumber,
),
]
let environmentMap = Belt.Map.String.fromArray(environmentPairs)
ExpressionWithContext.noContext(ExpressionBuilder.eRecord(environmentMap))->Ok
}
let doWithEnvironmentSampleCount = (
sampleCountExpr: expression,
expr: expression,
bindings: ExpressionT.bindings,
accessors: ProjectAccessorsT.t,
) => {
let blockSampleCount = ExpressionBuilder.eBlock(list{sampleCountExpr})
let rSampleCount = reduceExpression(blockSampleCount, bindings, accessors)
rSampleCount->Result.flatMap(sampleCountValue =>
switch sampleCountValue {
| InternalExpressionValue.IEvNumber(sampleCount) => {
let newEnvironment = {...accessors.environment, sampleCount: Js.Math.floor(sampleCount)}
let newAccessors = {...accessors, environment: newEnvironment}
reduceExpression(expr, bindings, newAccessors)->Belt.Result.map(value =>
value->ExpressionT.EValue->ExpressionWithContext.noContext
)
}
| _ => REExpectedType("Number", "")->Error
}
)
}
let expandExpressionList = (aList, bindings: ExpressionT.bindings, accessors): result<
expressionWithContext,
errorValue,
@ -185,6 +220,13 @@ let dispatchMacroCall = (
doLambdaDefinition(bindings, parameters, lambdaDefinition)
| list{ExpressionT.EValue(IEvCall("$$_ternary_$$")), condition, ifTrue, ifFalse} =>
doTernary(condition, ifTrue, ifFalse, bindings, accessors)
| list{ExpressionT.EValue(IEvCall("$$_environment_$$"))} => doEnvironment(accessors)
| list{
ExpressionT.EValue(IEvCall("$$_withEnvironmentSampleCount_$$")),
expr,
sampleCountExpr,
} =>
doWithEnvironmentSampleCount(sampleCountExpr, expr, bindings, accessors)
| _ => ExpressionWithContext.noContext(ExpressionT.EList(aList))->Ok
}

View File

@ -22,7 +22,7 @@ let rec reduceExpressionInProject = (
continuation: T.bindings,
accessors: ProjectAccessorsT.t,
): result<InternalExpressionValue.t, 'e> => {
// Js.log(`reduce: ${T.toString(expression)} bindings: ${bindings->Bindings.toString}`)
// `reduce: ${T.toString(expression)}`->Js.log
switch expression {
| T.EValue(value) => value->Ok
| T.EList(list) =>

View File

@ -17,14 +17,35 @@ let rec fromNode = (node: Parse.node): expression => {
ExpressionBuilder.eFunction("$$_lambda_$$", list{args, body})
}
// Convert function calls to macro calls
let caseCallIdentifier = (nodeCallIdentifier: Parse.nodeCallIdentifier): expression => {
let callIdentifier = nodeCallIdentifier["value"]
// `callIdentifier ${callIdentifier}`->Js.log
let callValue = switch callIdentifier {
| "withEnvironmentSampleCount" => "$$_withEnvironmentSampleCount_$$"
| callName => callName
}
ExpressionBuilder.eCall(callValue)
}
// Convert identifiers to macro calls
let caseIdentifier = (nodeIdentifier: Parse.nodeIdentifier): expression => {
let identifier = nodeIdentifier["value"]
// `caseIdentifier ${identifier}`->Js.log
switch identifier {
| "environment" => "$$_environment_$$"->ExpressionBuilder.eFunction(list{})
| symbol => symbol->ExpressionBuilder.eSymbol
}
}
switch Parse.castNodeType(node) {
| PgNodeBlock(nodeBlock) => caseBlock(nodeBlock)
| PgNodeBoolean(nodeBoolean) => ExpressionBuilder.eBool(nodeBoolean["value"])
| PgNodeCallIdentifier(nodeCallIdentifier) => ExpressionBuilder.eCall(nodeCallIdentifier["value"])
| PgNodeCallIdentifier(nodeCallIdentifier) => caseCallIdentifier(nodeCallIdentifier)
| PgNodeExpression(nodeExpression) =>
ExpressionT.EList(nodeExpression["nodes"]->Js.Array2.map(fromNode)->Belt.List.fromArray)
| PgNodeFloat(nodeFloat) => ExpressionBuilder.eNumber(nodeFloat["value"])
| PgNodeIdentifier(nodeIdentifier) => ExpressionBuilder.eSymbol(nodeIdentifier["value"])
| PgNodeIdentifier(nodeIdentifier) => caseIdentifier(nodeIdentifier)
| PgNodeInteger(nodeInteger) => ExpressionBuilder.eNumber(Belt.Int.toFloat(nodeInteger["value"]))
| PgNodeKeyValue(nodeKeyValue) =>
ExpressionT.EList(list{fromNode(nodeKeyValue["key"]), fromNode(nodeKeyValue["value"])})