Merge pull request #855 from quantified-uncertainty/reducer-parsermodules

Reducer: Simplified modules
This commit is contained in:
Ozzie Gooen 2022-07-18 09:13:57 -07:00 committed by GitHub
commit 9fb33776c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 84 additions and 201 deletions

View File

@ -2,15 +2,15 @@
module ErrorValue = Reducer_ErrorValue module ErrorValue = Reducer_ErrorValue
module ExternalExpressionValue = ReducerInterface.ExternalExpressionValue module ExternalExpressionValue = ReducerInterface.ExternalExpressionValue
module InternalExpressionValue = ReducerInterface.InternalExpressionValue module InternalExpressionValue = ReducerInterface.InternalExpressionValue
module Module = Reducer_Module module Bindings = Reducer_Bindings
let removeDefaultsInternal = (iev: InternalExpressionValue.t) => { let removeDefaultsInternal = (iev: InternalExpressionValue.t) => {
switch iev { switch iev {
| InternalExpressionValue.IEvModule(nameSpace) => | InternalExpressionValue.IEvBindings(nameSpace) =>
Module.removeOther( Bindings.removeOther(
nameSpace, nameSpace,
ReducerInterface.StdLib.internalStdLib, ReducerInterface.StdLib.internalStdLib,
)->InternalExpressionValue.IEvModule )->InternalExpressionValue.IEvBindings
| value => value | value => value
} }
} }

View File

@ -1,119 +0,0 @@
module InternalExpressionValue = ReducerInterface_InternalExpressionValue
module ExpressionT = Reducer_Expression_T
module Module = Reducer_Module
module Bindings = Reducer_Module
module ErrorValue = Reducer_ErrorValue
open Jest
open Expect
// ----------------------
// --- Start of Module File
// ----------------------
module FooImplementation = {
// As this is a Rescript module, functions can use other functions in this module
// and in other stdLib modules implemented this way.
// Embedding function definitions in to switch statements is a bad practice
// - to reduce line count or to
let fooNumber = 0.0
let fooString = "Foo String"
let fooBool = true
let makeFoo = (a: string, b: string, _environment): string => `I am ${a}-foo and I am ${b}-foo`
let makeBar = (a: float, b: float, _environment): string =>
`I am ${a->Js.Float.toString}-bar and I am ${b->Js.Float.toString}-bar`
// You can also define functions that has their internal errors
let makeReturningError = (_a: float, _b: float, _environment): result<float, ErrorValue.t> =>
if false {
0.->Ok
} else {
ErrorValue.RETodo("test error")->Error
}
}
// There is a potential for type modules to define lift functions
// for their own type to get rid of switch statements.
module FooFFI = {
let makeFoo: ExpressionT.optionFfiFn = (args: array<InternalExpressionValue.t>, environment) => {
switch args {
| [IEvString(a), IEvString(b)] => FooImplementation.makeFoo(a, b, environment)->IEvString->Some
| _ => None
}
}
let makeBar: ExpressionT.optionFfiFn = (args: array<InternalExpressionValue.t>, environment) =>
switch args {
| [IEvNumber(a), IEvNumber(b)] => FooImplementation.makeBar(a, b, environment)->IEvString->Some
| _ => None
}
let makeReturningError: ExpressionT.optionFfiFnReturningResult = (
args: array<InternalExpressionValue.t>,
environment,
) =>
switch args {
| [IEvNumber(a), IEvNumber(b)] =>
FooImplementation.makeReturningError(a, b, environment)
->Belt.Result.map(v => v->InternalExpressionValue.IEvNumber)
->Some
| _ => None
}
}
let fooModule: Module.t =
Module.emptyStdLib
->Module.defineNumber("fooNumber", FooImplementation.fooNumber)
->Module.defineString("fooString", FooImplementation.fooString)
->Module.defineBool("fooBool", FooImplementation.fooBool)
->Module.defineFunction("makeFoo", FooFFI.makeFoo)
->Module.defineFunction("makeBar", FooFFI.makeBar)
->Module.defineFunctionReturningResult("makeReturningError", FooFFI.makeReturningError)
let makeBindings = (prevBindings: Bindings.t): Bindings.t =>
prevBindings->Module.defineModule("Foo", fooModule)
// ----------------------
// --- End of Module File
// ----------------------
let stdLibWithFoo = Bindings.emptyBindings->makeBindings
let evalWithFoo = sourceCode =>
Reducer_Expression.parse(sourceCode)->Belt.Result.flatMap(expr =>
Reducer_Expression.reduceExpression(
expr,
stdLibWithFoo,
InternalExpressionValue.defaultEnvironment,
)
)
let evalToStringResultWithFoo = sourceCode =>
evalWithFoo(sourceCode)->InternalExpressionValue.toStringResult
describe("Module", () => {
test("fooNumber", () => {
let result = evalToStringResultWithFoo("Foo.fooNumber")
expect(result)->toEqual("Ok(0)")
})
test("fooString", () => {
let result = evalToStringResultWithFoo("Foo.fooString")
expect(result)->toEqual("Ok('Foo String')")
})
test("fooBool", () => {
let result = evalToStringResultWithFoo("Foo.fooBool")
expect(result)->toEqual("Ok(true)")
})
test("fooBool", () => {
let result = evalToStringResultWithFoo("Foo.fooBool")
expect(result)->toEqual("Ok(true)")
})
test("makeFoo", () => {
let result = evalToStringResultWithFoo("Foo.makeFoo('a', 'b')")
expect(result)->toEqual("Ok('I am a-foo and I am b-foo')")
})
test("makeFoo wrong arguments", () => {
let result = evalToStringResultWithFoo("Foo.makeFoo(1, 2)")
// Notice the error with types
expect(result)->toEqual("Error(Function not found: makeFoo(Number,Number))")
})
test("makeBar", () => {
let result = evalToStringResultWithFoo("Foo.makeBar(1, 2)")
expect(result)->toEqual("Ok('I am 1-bar and I am 2-bar')")
})
})

View File

@ -236,7 +236,8 @@ describe("Peggy parse", () => {
testParse("1m+2cm", "{(::add (::fromUnit_m 1) (::fromUnit_cm 2))}") testParse("1m+2cm", "{(::add (::fromUnit_m 1) (::fromUnit_cm 2))}")
}) })
describe("Module", () => { describe("Module", () => {
testParse("Math.pi", "{(::$_atIndex_$ @Math 'pi')}") testParse("x", "{:x}")
testParse("Math.pi", "{:Math.pi}")
}) })
}) })

View File

@ -4,6 +4,7 @@ module ExpressionValue = ReducerInterface.InternalExpressionValue
module Parse = Reducer_Peggy_Parse module Parse = Reducer_Peggy_Parse
module Result = Belt.Result module Result = Belt.Result
module ToExpression = Reducer_Peggy_ToExpression module ToExpression = Reducer_Peggy_ToExpression
module Bindings = Reducer_Bindings
open Jest open Jest
open Expect open Expect

View File

@ -1,5 +1,9 @@
module Bindings = Reducer_Bindings
module InternalExpressionValue = ReducerInterface_InternalExpressionValue
open Jest open Jest
open Reducer_Peggy_TestHelpers open Reducer_Peggy_TestHelpers
open Expect
describe("Peggy to Expression", () => { describe("Peggy to Expression", () => {
describe("literals operators parenthesis", () => { describe("literals operators parenthesis", () => {
@ -183,6 +187,14 @@ describe("Peggy to Expression", () => {
}) })
describe("module", () => { describe("module", () => {
testToExpression("Math.pi", "{(:$_atIndex_$ :Math 'pi')}", ~v="3.141592653589793", ()) // testToExpression("Math.pi", "{:Math.pi}", ~v="3.141592653589793", ())
// Only.test("stdlibrary", () => {
// ReducerInterface_StdLib.internalStdLib
// ->IEvBindings
// ->InternalExpressionValue.toString
// ->expect
// ->toBe("")
// })
testToExpression("Math.pi", "{:Math.pi}", ~v="3.141592653589793", ())
}) })
}) })

View File

@ -8,7 +8,7 @@ module InternalExpressionValue = ReducerInterface.InternalExpressionValue
module ExpressionWithContext = Reducer_ExpressionWithContext module ExpressionWithContext = Reducer_ExpressionWithContext
module Macro = Reducer_Expression_Macro module Macro = Reducer_Expression_Macro
module T = Reducer_Expression_T module T = Reducer_Expression_T
module Module = Reducer_Module module Bindings = Reducer_Bindings
let testMacro_ = ( let testMacro_ = (
tester, tester,
@ -16,7 +16,7 @@ let testMacro_ = (
expr: T.expression, expr: T.expression,
expectedCode: string, expectedCode: string,
) => { ) => {
let bindings = Module.fromArray(bindArray) let bindings = Bindings.fromArray(bindArray)
tester(expr->T.toString, () => tester(expr->T.toString, () =>
expr expr
->Macro.expandMacroCall( ->Macro.expandMacroCall(
@ -36,7 +36,7 @@ let testMacroEval_ = (
expr: T.expression, expr: T.expression,
expectedValue: string, expectedValue: string,
) => { ) => {
let bindings = Module.fromArray(bindArray) let bindings = Bindings.fromArray(bindArray)
tester(expr->T.toString, () => tester(expr->T.toString, () =>
expr expr
->Macro.doMacroCall( ->Macro.doMacroCall(

View File

@ -75,10 +75,10 @@ let emptyBindings = emptyModule
let fromTypeScriptBindings = ReducerInterface_InternalExpressionValue.nameSpaceFromTypeScriptBindings let fromTypeScriptBindings = ReducerInterface_InternalExpressionValue.nameSpaceFromTypeScriptBindings
let toTypeScriptBindings = ReducerInterface_InternalExpressionValue.nameSpaceToTypeScriptBindings let toTypeScriptBindings = ReducerInterface_InternalExpressionValue.nameSpaceToTypeScriptBindings
let toExpressionValue = (nameSpace: t): internalExpressionValue => IEvModule(nameSpace) let toExpressionValue = (nameSpace: t): internalExpressionValue => IEvBindings(nameSpace)
let fromExpressionValue = (aValue: internalExpressionValue): t => let fromExpressionValue = (aValue: internalExpressionValue): t =>
switch aValue { switch aValue {
| IEvModule(nameSpace) => nameSpace | IEvBindings(nameSpace) => nameSpace
| _ => emptyModule | _ => emptyModule
} }

View File

@ -3,7 +3,7 @@ module ExpressionT = Reducer_Expression_T
module ExternalLibrary = ReducerInterface.ExternalLibrary module ExternalLibrary = ReducerInterface.ExternalLibrary
module Lambda = Reducer_Expression_Lambda module Lambda = Reducer_Expression_Lambda
module MathJs = Reducer_MathJs module MathJs = Reducer_MathJs
module Module = Reducer_Module module Bindings = Reducer_Bindings
module Result = Belt.Result module Result = Belt.Result
module TypeBuilder = Reducer_Type_TypeBuilder module TypeBuilder = Reducer_Type_TypeBuilder
open ReducerInterface_InternalExpressionValue open ReducerInterface_InternalExpressionValue
@ -49,9 +49,9 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce
} }
let moduleAtIndex = (nameSpace: nameSpace, sIndex) => let moduleAtIndex = (nameSpace: nameSpace, sIndex) =>
switch Module.get(nameSpace, sIndex) { switch Bindings.get(nameSpace, sIndex) {
| Some(value) => value->Ok | Some(value) => value->Ok
| None => RERecordPropertyNotFound("Module property not found", sIndex)->Error | None => RERecordPropertyNotFound("Bindings property not found", sIndex)->Error
} }
let recordAtIndex = (dict: Belt.Map.String.t<internalExpressionValue>, sIndex) => let recordAtIndex = (dict: Belt.Map.String.t<internalExpressionValue>, sIndex) =>
@ -81,19 +81,19 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce
} }
let doSetBindings = (bindings: nameSpace, symbol: string, value: internalExpressionValue) => { let doSetBindings = (bindings: nameSpace, symbol: string, value: internalExpressionValue) => {
Module.set(bindings, symbol, value)->IEvModule->Ok Bindings.set(bindings, symbol, value)->IEvBindings->Ok
} }
let doSetTypeAliasBindings = ( let doSetTypeAliasBindings = (
bindings: nameSpace, bindings: nameSpace,
symbol: string, symbol: string,
value: internalExpressionValue, value: internalExpressionValue,
) => Module.setTypeAlias(bindings, symbol, value)->IEvModule->Ok ) => Bindings.setTypeAlias(bindings, symbol, value)->IEvBindings->Ok
let doSetTypeOfBindings = (bindings: nameSpace, symbol: string, value: internalExpressionValue) => let doSetTypeOfBindings = (bindings: nameSpace, symbol: string, value: internalExpressionValue) =>
Module.setTypeOf(bindings, symbol, value)->IEvModule->Ok Bindings.setTypeOf(bindings, symbol, value)->IEvBindings->Ok
let doExportBindings = (bindings: nameSpace) => bindings->Module.toExpressionValue->Ok let doExportBindings = (bindings: nameSpace) => bindings->Bindings.toExpressionValue->Ok
let doKeepArray = (aValueArray, aLambdaValue) => { let doKeepArray = (aValueArray, aLambdaValue) => {
let rMappedList = aValueArray->Belt.Array.reduceReverse(Ok(list{}), (rAcc, elem) => let rMappedList = aValueArray->Belt.Array.reduceReverse(Ok(list{}), (rAcc, elem) =>
@ -169,16 +169,16 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce
switch call { switch call {
| ("$_atIndex_$", [IEvArray(aValueArray), IEvNumber(fIndex)]) => arrayAtIndex(aValueArray, fIndex) | ("$_atIndex_$", [IEvArray(aValueArray), IEvNumber(fIndex)]) => arrayAtIndex(aValueArray, fIndex)
| ("$_atIndex_$", [IEvModule(dict), IEvString(sIndex)]) => moduleAtIndex(dict, sIndex) | ("$_atIndex_$", [IEvBindings(dict), IEvString(sIndex)]) => moduleAtIndex(dict, sIndex)
| ("$_atIndex_$", [IEvRecord(dict), IEvString(sIndex)]) => recordAtIndex(dict, sIndex) | ("$_atIndex_$", [IEvRecord(dict), IEvString(sIndex)]) => recordAtIndex(dict, sIndex)
| ("$_constructArray_$", [IEvArray(aValueArray)]) => IEvArray(aValueArray)->Ok | ("$_constructArray_$", [IEvArray(aValueArray)]) => IEvArray(aValueArray)->Ok
| ("$_constructRecord_$", [IEvArray(arrayOfPairs)]) => constructRecord(arrayOfPairs) | ("$_constructRecord_$", [IEvArray(arrayOfPairs)]) => constructRecord(arrayOfPairs)
| ("$_exportBindings_$", [IEvModule(nameSpace)]) => doExportBindings(nameSpace) | ("$_exportBindings_$", [IEvBindings(nameSpace)]) => doExportBindings(nameSpace)
| ("$_setBindings_$", [IEvModule(nameSpace), IEvSymbol(symbol), value]) => | ("$_setBindings_$", [IEvBindings(nameSpace), IEvSymbol(symbol), value]) =>
doSetBindings(nameSpace, symbol, value) doSetBindings(nameSpace, symbol, value)
| ("$_setTypeAliasBindings_$", [IEvModule(nameSpace), IEvTypeIdentifier(symbol), value]) => | ("$_setTypeAliasBindings_$", [IEvBindings(nameSpace), IEvTypeIdentifier(symbol), value]) =>
doSetTypeAliasBindings(nameSpace, symbol, value) doSetTypeAliasBindings(nameSpace, symbol, value)
| ("$_setTypeOfBindings_$", [IEvModule(nameSpace), IEvSymbol(symbol), value]) => | ("$_setTypeOfBindings_$", [IEvBindings(nameSpace), IEvSymbol(symbol), value]) =>
doSetTypeOfBindings(nameSpace, symbol, value) doSetTypeOfBindings(nameSpace, symbol, value)
| ("$_typeModifier_memberOf_$", [IEvTypeIdentifier(typeIdentifier), IEvArray(arr)]) => | ("$_typeModifier_memberOf_$", [IEvTypeIdentifier(typeIdentifier), IEvArray(arr)]) =>
TypeBuilder.typeModifier_memberOf(IEvTypeIdentifier(typeIdentifier), IEvArray(arr)) TypeBuilder.typeModifier_memberOf(IEvTypeIdentifier(typeIdentifier), IEvArray(arr))

View File

@ -9,7 +9,7 @@ module ExpressionBuilder = Reducer_Expression_ExpressionBuilder
module ExpressionT = Reducer_Expression_T module ExpressionT = Reducer_Expression_T
module InternalExpressionValue = ReducerInterface_InternalExpressionValue module InternalExpressionValue = ReducerInterface_InternalExpressionValue
module ExpressionWithContext = Reducer_ExpressionWithContext module ExpressionWithContext = Reducer_ExpressionWithContext
module Module = Reducer_Module module Bindings = Reducer_Bindings
module Result = Belt.Result module Result = Belt.Result
open Reducer_Expression_ExpressionBuilder open Reducer_Expression_ExpressionBuilder
@ -28,7 +28,7 @@ let dispatchMacroCall = (
let rExternalBindingsValue = reduceExpression(bindingExpr, bindings, environment) let rExternalBindingsValue = reduceExpression(bindingExpr, bindings, environment)
rExternalBindingsValue->Result.flatMap(nameSpaceValue => { rExternalBindingsValue->Result.flatMap(nameSpaceValue => {
let newBindings = Module.fromExpressionValue(nameSpaceValue) let newBindings = Bindings.fromExpressionValue(nameSpaceValue)
let rNewStatement = BindingsReplacer.replaceSymbols(newBindings, statement) let rNewStatement = BindingsReplacer.replaceSymbols(newBindings, statement)
rNewStatement->Result.map(boundStatement => rNewStatement->Result.map(boundStatement =>

View File

@ -6,7 +6,7 @@ module InternalExpressionValue = ReducerInterface_InternalExpressionValue
module Lambda = Reducer_Expression_Lambda module Lambda = Reducer_Expression_Lambda
module Macro = Reducer_Expression_Macro module Macro = Reducer_Expression_Macro
module MathJs = Reducer_MathJs module MathJs = Reducer_MathJs
module Module = Reducer_Module module Bindings = Reducer_Bindings
module Result = Belt.Result module Result = Belt.Result
module T = Reducer_Expression_T module T = Reducer_Expression_T
@ -121,10 +121,10 @@ let evaluateUsingOptions = (
ReducerInterface_ExternalExpressionValue.defaultEnvironment, ReducerInterface_ExternalExpressionValue.defaultEnvironment,
) )
let mergedBindings: InternalExpressionValue.nameSpace = Module.merge( let mergedBindings: InternalExpressionValue.nameSpace = Bindings.merge(
ReducerInterface_StdLib.internalStdLib, ReducerInterface_StdLib.internalStdLib,
Belt.Option.map(externalBindings, Module.fromTypeScriptBindings)->Belt.Option.getWithDefault( Belt.Option.map(externalBindings, Bindings.fromTypeScriptBindings)->Belt.Option.getWithDefault(
Module.emptyModule, Bindings.emptyModule,
), ),
) )

View File

@ -3,7 +3,7 @@ module ErrorValue = Reducer_ErrorValue
module ExpressionT = Reducer_Expression_T module ExpressionT = Reducer_Expression_T
module InternalExpressionValue = ReducerInterface_InternalExpressionValue module InternalExpressionValue = ReducerInterface_InternalExpressionValue
module Result = Belt.Result module Result = Belt.Result
module Module = Reducer_Module module Bindings = Reducer_Bindings
type bindings = ExpressionT.bindings type bindings = ExpressionT.bindings
type context = bindings type context = bindings
@ -41,7 +41,7 @@ let toString = expressionWithContext =>
| ExpressionNoContext(expr) => ExpressionT.toString(expr) | ExpressionNoContext(expr) => ExpressionT.toString(expr)
| ExpressionWithContext(expr, context) => | ExpressionWithContext(expr, context) =>
`${ExpressionT.toString(expr)} context: ${context `${ExpressionT.toString(expr)} context: ${context
->Module.toExpressionValue ->Bindings.toExpressionValue
->InternalExpressionValue.toString}` ->InternalExpressionValue.toString}`
} }

View File

@ -2,7 +2,7 @@ module ErrorValue = Reducer_ErrorValue
module ExpressionT = Reducer_Expression_T module ExpressionT = Reducer_Expression_T
module InternalExpressionValue = ReducerInterface_InternalExpressionValue module InternalExpressionValue = ReducerInterface_InternalExpressionValue
module Result = Belt.Result module Result = Belt.Result
module Module = Reducer_Module module Bindings = Reducer_Bindings
type errorValue = Reducer_ErrorValue.errorValue type errorValue = Reducer_ErrorValue.errorValue
type expression = ExpressionT.expression type expression = ExpressionT.expression
@ -42,8 +42,8 @@ and replaceSymbolsOnExpressionList = (bindings, list) => {
} }
and replaceSymbolOnValue = (bindings, evValue: internalExpressionValue) => and replaceSymbolOnValue = (bindings, evValue: internalExpressionValue) =>
switch evValue { switch evValue {
| IEvSymbol(symbol) => Module.getWithDefault(bindings, symbol, evValue)->Ok | IEvSymbol(symbol) => Bindings.getWithDefault(bindings, symbol, evValue)->Ok
| IEvCall(symbol) => Module.getWithDefault(bindings, symbol, evValue)->checkIfCallable | IEvCall(symbol) => Bindings.getWithDefault(bindings, symbol, evValue)->checkIfCallable
| _ => evValue->Ok | _ => evValue->Ok
} }
and checkIfCallable = (evValue: internalExpressionValue) => and checkIfCallable = (evValue: internalExpressionValue) =>

View File

@ -2,7 +2,7 @@ module BBindingsReplacer = Reducer_Expression_BindingsReplacer
module BErrorValue = Reducer_ErrorValue module BErrorValue = Reducer_ErrorValue
module BExpressionT = Reducer_Expression_T module BExpressionT = Reducer_Expression_T
module BInternalExpressionValue = ReducerInterface_InternalExpressionValue module BInternalExpressionValue = ReducerInterface_InternalExpressionValue
module BModule = Reducer_Module module BBindings = Reducer_Bindings
type errorValue = BErrorValue.errorValue type errorValue = BErrorValue.errorValue
type expression = BExpressionT.expression type expression = BExpressionT.expression
@ -15,7 +15,7 @@ let eArray = anArray => anArray->BInternalExpressionValue.IEvArray->BExpressionT
let eArrayString = anArray => anArray->BInternalExpressionValue.IEvArrayString->BExpressionT.EValue let eArrayString = anArray => anArray->BInternalExpressionValue.IEvArrayString->BExpressionT.EValue
let eBindings = (anArray: array<(string, BInternalExpressionValue.t)>) => let eBindings = (anArray: array<(string, BInternalExpressionValue.t)>) =>
anArray->BModule.fromArray->BModule.toExpressionValue->BExpressionT.EValue anArray->BBindings.fromArray->BBindings.toExpressionValue->BExpressionT.EValue
let eBool = aBool => aBool->BInternalExpressionValue.IEvBool->BExpressionT.EValue let eBool = aBool => aBool->BInternalExpressionValue.IEvBool->BExpressionT.EValue
@ -35,12 +35,12 @@ let eLambda = (
BInternalExpressionValue.IEvLambda({ BInternalExpressionValue.IEvLambda({
parameters: parameters, parameters: parameters,
context: context, context: context,
body: NotFFI(expr)->BModule.castExpressionToInternalCode, body: NotFFI(expr)->BBindings.castExpressionToInternalCode,
})->BExpressionT.EValue })->BExpressionT.EValue
} }
let eLambdaFFI = (ffiFn: ffiFn) => { let eLambdaFFI = (ffiFn: ffiFn) => {
ffiFn->BModule.eLambdaFFIValue->BExpressionT.EValue ffiFn->BBindings.eLambdaFFIValue->BExpressionT.EValue
} }
let eNumber = aNumber => aNumber->BInternalExpressionValue.IEvNumber->BExpressionT.EValue let eNumber = aNumber => aNumber->BInternalExpressionValue.IEvNumber->BExpressionT.EValue
@ -57,7 +57,7 @@ let eList = (list: list<expression>): expression => list->BExpressionT.EList
let eBlock = (exprs: list<expression>): expression => eFunction("$$_block_$$", exprs) let eBlock = (exprs: list<expression>): expression => eFunction("$$_block_$$", exprs)
let eModule = (nameSpace: BInternalExpressionValue.nameSpace): expression => let eModule = (nameSpace: BInternalExpressionValue.nameSpace): expression =>
nameSpace->BInternalExpressionValue.IEvModule->BExpressionT.EValue nameSpace->BInternalExpressionValue.IEvBindings->BExpressionT.EValue
let eLetStatement = (symbol: string, valueExpression: expression): expression => let eLetStatement = (symbol: string, valueExpression: expression): expression =>
eFunction("$_let_$", list{eSymbol(symbol), valueExpression}) eFunction("$_let_$", list{eSymbol(symbol), valueExpression})

View File

@ -3,7 +3,7 @@ module ErrorValue = Reducer_ErrorValue
module ExpressionBuilder = Reducer_Expression_ExpressionBuilder module ExpressionBuilder = Reducer_Expression_ExpressionBuilder
module ExpressionT = Reducer_Expression_T module ExpressionT = Reducer_Expression_T
module ExpressionValue = ReducerInterface_InternalExpressionValue module ExpressionValue = ReducerInterface_InternalExpressionValue
module Module = Reducer_Module module Bindings = Reducer_Bindings
module Result = Belt.Result module Result = Belt.Result
type environment = ReducerInterface_InternalExpressionValue.environment type environment = ReducerInterface_InternalExpressionValue.environment
@ -50,7 +50,7 @@ let caseNotFFI = (lambdaValue: ExpressionValue.lambdaValue, expr, args, environm
let bindings = Belt.List.reduce(zippedParameterList, lambdaValue.context, ( let bindings = Belt.List.reduce(zippedParameterList, lambdaValue.context, (
acc, acc,
(variable, variableValue), (variable, variableValue),
) => acc->Module.set(variable, variableValue)) ) => acc->Bindings.set(variable, variableValue))
let newExpression = ExpressionBuilder.eBlock(list{expr}) let newExpression = ExpressionBuilder.eBlock(list{expr})
reducer(newExpression, bindings, environment) reducer(newExpression, bindings, environment)
} }

View File

@ -1,14 +0,0 @@
module ExpressionBuilder = Reducer_Expression_ExpressionBuilder
module ExpressionT = Reducer_Expression_T
type expression = ExpressionT.expression
let defaultCaseFFI = (functionName: string): expression => {
ExpressionBuilder.eLambdaFFI(Reducer_Module.functionNotFoundErrorFFIFn(functionName))
}
let addGuard = (
guard: expression,
expression: expression,
previousExpression: expression,
): expression => ExpressionBuilder.eTernary(guard, expression, previousExpression)

View File

@ -187,13 +187,16 @@ basicLiteral
/ dollarIdentifier / dollarIdentifier
dollarIdentifierWithModule 'identifier' dollarIdentifierWithModule 'identifier'
= head:moduleIdentifier = head:$moduleIdentifier
tail:('.' _nl @$moduleIdentifier)* '.' _nl tail:('.' _nl @$moduleIdentifier)* '.' _nl
final:$dollarIdentifier final:$dollarIdentifier
{ tail.push(final); {
return tail.reduce(function(result, element) { let modifiers = [...tail]
return h.makeFunctionCall(h.postOperatorToFunction['[]'], [result, h.nodeString(element)]) modifiers.unshift(head)
}, head)} modifiers.push(final)
let modifiedIdentifier = modifiers.join('.')
return h.nodeIdentifier(modifiedIdentifier)
}
identifier 'identifier' identifier 'identifier'
= ([_a-z]+[_a-z0-9]i*) {return h.nodeIdentifier(text(), location())} = ([_a-z]+[_a-z0-9]i*) {return h.nodeIdentifier(text(), location())}

View File

@ -15,7 +15,7 @@ type rec t =
| IEvDeclaration(lambdaDeclaration) | IEvDeclaration(lambdaDeclaration)
| IEvDistribution(DistributionTypes.genericDist) | IEvDistribution(DistributionTypes.genericDist)
| IEvLambda(lambdaValue) | IEvLambda(lambdaValue)
| IEvModule(nameSpace) | IEvBindings(nameSpace)
| IEvNumber(float) | IEvNumber(float)
| IEvRecord(map) | IEvRecord(map)
| IEvString(string) | IEvString(string)
@ -52,7 +52,7 @@ let rec toString = aValue =>
| IEvDeclaration(d) => Declaration.toString(d, r => toString(IEvLambda(r))) | IEvDeclaration(d) => Declaration.toString(d, r => toString(IEvLambda(r)))
| IEvDistribution(dist) => GenericDist.toString(dist) | IEvDistribution(dist) => GenericDist.toString(dist)
| IEvLambda(lambdaValue) => `lambda(${Js.Array2.toString(lambdaValue.parameters)}=>internal code)` | IEvLambda(lambdaValue) => `lambda(${Js.Array2.toString(lambdaValue.parameters)}=>internal code)`
| IEvModule(m) => `@${m->toStringNameSpace}` | IEvBindings(m) => `@${m->toStringNameSpace}`
| IEvNumber(aNumber) => Js.String.make(aNumber) | IEvNumber(aNumber) => Js.String.make(aNumber)
| IEvRecord(aMap) => aMap->toStringMap | IEvRecord(aMap) => aMap->toStringMap
| IEvString(aString) => `'${aString}'` | IEvString(aString) => `'${aString}'`
@ -84,7 +84,7 @@ let toStringWithType = aValue =>
| IEvDeclaration(_) => `Declaration::${toString(aValue)}` | IEvDeclaration(_) => `Declaration::${toString(aValue)}`
| IEvDistribution(_) => `Distribution::${toString(aValue)}` | IEvDistribution(_) => `Distribution::${toString(aValue)}`
| IEvLambda(_) => `Lambda::${toString(aValue)}` | IEvLambda(_) => `Lambda::${toString(aValue)}`
| IEvModule(_) => `Module::${toString(aValue)}` | IEvBindings(_) => `Module::${toString(aValue)}`
| IEvNumber(_) => `Number::${toString(aValue)}` | IEvNumber(_) => `Number::${toString(aValue)}`
| IEvRecord(_) => `Record::${toString(aValue)}` | IEvRecord(_) => `Record::${toString(aValue)}`
| IEvString(_) => `String::${toString(aValue)}` | IEvString(_) => `String::${toString(aValue)}`
@ -150,7 +150,7 @@ let valueToValueType = value =>
| IEvDeclaration(_) => EvtDeclaration | IEvDeclaration(_) => EvtDeclaration
| IEvDistribution(_) => EvtDistribution | IEvDistribution(_) => EvtDistribution
| IEvLambda(_) => EvtLambda | IEvLambda(_) => EvtLambda
| IEvModule(_) => EvtModule | IEvBindings(_) => EvtModule
| IEvNumber(_) => EvtNumber | IEvNumber(_) => EvtNumber
| IEvRecord(_) => EvtRecord | IEvRecord(_) => EvtRecord
| IEvString(_) => EvtString | IEvString(_) => EvtString
@ -211,7 +211,7 @@ let rec toExternal = (iev: t): ExternalExpressionValue.t => {
| IEvTimeDuration(v) => EvTimeDuration(v) | IEvTimeDuration(v) => EvTimeDuration(v)
| IEvType(v) => v->mapToExternal->EvType | IEvType(v) => v->mapToExternal->EvType
| IEvTypeIdentifier(v) => EvTypeIdentifier(v) | IEvTypeIdentifier(v) => EvTypeIdentifier(v)
| IEvModule(v) => v->nameSpaceToTypeScriptBindings->EvModule | IEvBindings(v) => v->nameSpaceToTypeScriptBindings->EvModule
} }
} }
and mapToExternal = v => and mapToExternal = v =>
@ -243,7 +243,7 @@ let rec toInternal = (ev: ExternalExpressionValue.t): t => {
} }
| EvDistribution(v) => IEvDistribution(v) | EvDistribution(v) => IEvDistribution(v)
| EvLambda(v) => IEvLambda(lambdaValueToInternal(v)) | EvLambda(v) => IEvLambda(lambdaValueToInternal(v))
| EvModule(v) => v->nameSpaceFromTypeScriptBindings->IEvModule | EvModule(v) => v->nameSpaceFromTypeScriptBindings->IEvBindings
| EvNumber(v) => IEvNumber(v) | EvNumber(v) => IEvNumber(v)
| EvRecord(v) => v->recordToInternal->IEvRecord | EvRecord(v) => v->recordToInternal->IEvRecord
| EvString(v) => IEvString(v) | EvString(v) => IEvString(v)

View File

@ -1,6 +1,6 @@
module Module = Reducer_Module module Bindings = Reducer_Bindings
let internalStdLib = Module.emptyModule->SquiggleLibrary_Math.makeBindings let internalStdLib = Bindings.emptyBindings->SquiggleLibrary_Math.makeBindings
@genType @genType
let externalStdLib = internalStdLib->Module.toTypeScriptBindings let externalStdLib = internalStdLib->Bindings.toTypeScriptBindings

View File

@ -1,17 +1,16 @@
module Bindings = Reducer_Module module Bindings = Reducer_Bindings
module Module = Reducer_Module
let availableNumbers: array<(string, float)> = [ let availableNumbers: array<(string, float)> = [
("pi", Js.Math._PI), ("Math.pi", Js.Math._PI),
("e", Js.Math._E), ("Math.e", Js.Math._E),
("ln2", Js.Math._LN2), ("Math.ln2", Js.Math._LN2),
("ln10", Js.Math._LN10), ("Math.ln10", Js.Math._LN10),
("log2e", Js.Math._LOG2E), ("Math.log2e", Js.Math._LOG2E),
("log10e", Js.Math._LOG10E), ("Math.log10e", Js.Math._LOG10E),
("sqrt2", Js.Math._SQRT2), ("Math.sqrt2", Js.Math._SQRT2),
("sqrt1_2", Js.Math._SQRT1_2), ("Math.sqrt1_2", Js.Math._SQRT1_2),
("phi", 1.618033988749895), ("Math.phi", 1.618033988749895),
("tau", 6.283185307179586), ("Math.tau", 6.283185307179586),
] ]
let mathBindings: Bindings.t = let mathBindings: Bindings.t =
@ -20,4 +19,4 @@ let mathBindings: Bindings.t =
->Bindings.fromArray ->Bindings.fromArray
let makeBindings = (previousBindings: Bindings.t): Bindings.t => let makeBindings = (previousBindings: Bindings.t): Bindings.t =>
previousBindings->Bindings.defineModule("Math", mathBindings) previousBindings->Bindings.merge(mathBindings)