Math module tests defined (failing)

rename modules

define module

Math module

test helper fixed for defaults and test defined
This commit is contained in:
Umur Ozkul 2022-06-10 14:38:31 +02:00
parent 3ca209a53d
commit 69bab17331
14 changed files with 73 additions and 25 deletions

View File

@ -0,0 +1,19 @@
module ExpressionT = Reducer_Expression_T
module ExpressionValue = ReducerInterface.ExpressionValue
module ErrorValue = Reducer_ErrorValue
module Bindings = Reducer_Category_Bindings
let removeDefaults = (ev: ExpressionT.expressionValue): ExpressionT.expressionValue =>
switch ev {
| EvRecord(extbindings) => {
let bindings: Bindings.t = Bindings.fromRecord(extbindings)
let keys = Js.Dict.keys(Reducer.defaultExternalBindings)
Belt.Map.String.keep(bindings, (key, _value) => {
let removeThis = Js.Array2.includes(keys, key)
!removeThis
})->Bindings.toExpressionValue
}
| value => value
}
let rRemoveDefaults = r => Belt.Result.map(r, ev => removeDefaults(ev))

View File

@ -23,8 +23,13 @@ let expectToExpressionToBe = (expr, answer, ~v="_", ()) => {
let a2 =
rExpr
->Result.flatMap(expr =>
Expression.reduceExpression(expr, Belt.Map.String.empty, ExpressionValue.defaultEnvironment)
Expression.reduceExpression(
expr,
ReducerInterface_DefaultExternalBindings.defaultInternalBindings,
ExpressionValue.defaultEnvironment,
)
)
->Reducer_Helpers.rRemoveDefaults
->ExpressionValue.toStringResultOkless
(a1, a2)->expect->toEqual((answer, v))
}

View File

@ -183,6 +183,6 @@ describe("Peggy to Expression", () => {
})
describe("module", () => {
testToExpression("Math.pi", "{(:$_atIndex_$ :Math 'pi')}", ())
testToExpression("Math.pi", "{(:$_atIndex_$ :Math 'pi')}", ~v="3.141592653589793", ())
})
})

View File

@ -1,6 +1,7 @@
module ExpressionT = Reducer_Expression_T
module ExpressionValue = ReducerInterface.ExpressionValue
module ErrorValue = Reducer_ErrorValue
module Bindings = Reducer_Category_Bindings
open Jest
open Expect
@ -17,7 +18,11 @@ let expectParseToBe = (expr: string, answer: string) =>
Reducer.parse(expr)->ExpressionT.toStringResult->expect->toBe(answer)
let expectEvalToBe = (expr: string, answer: string) =>
Reducer.evaluate(expr)->ExpressionValue.toStringResult->expect->toBe(answer)
Reducer.evaluate(expr)
->Reducer_Helpers.rRemoveDefaults
->ExpressionValue.toStringResult
->expect
->toBe(answer)
let expectEvalError = (expr: string) =>
Reducer.evaluate(expr)->ExpressionValue.toStringResult->expect->toMatch("Error\(")

View File

@ -0,0 +1,7 @@
open Jest
open Reducer_TestHelpers
describe("Math Library", () => {
testEvalToBe("Math.e", "")
testEvalToBe("Math.pi", "")
})

View File

@ -24,4 +24,4 @@ let foreignFunctionInterface = (
let defaultEnvironment = ExpressionValue.defaultEnvironment
let defaultExternalBindings = ExpressionValue.defaultExternalBindings
let defaultExternalBindings = ReducerInterface_DefaultExternalBindings.defaultExternalBindings

View File

@ -1,4 +1,4 @@
include Reducer_Manager_Module
include Reducer_Category_Module // Bindings inherit from Module
open ReducerInterface_ExpressionValue

View File

@ -7,18 +7,12 @@ type t = ExpressionT.bindings
let typeAliasesKey = "_typeAliases_"
let typeReferencesKey = "_typeReferences_"
let define = (container: t, identifier: string, ev: expressionValue): t =>
Belt.Map.String.set(container, identifier, ev) // TODO build lambda for polymorphic functions here
let defineNumber = (container: t, identifier: string, value: float): t =>
container->define(identifier, EvNumber(value))
let emptyModule: t = Belt.Map.String.empty
let cloneRecord = (r: record): record => r->Js.Dict.entries->Js.Dict.fromArray
let fromRecord = (r: record): t => Js.Dict.entries(r)->Belt.Map.String.fromArray
let toRecord = (container: t): record => Belt.Map.String.toArray(container)->Js.Dict.fromArray
let emptyModule: t = Belt.Map.String.empty
let toExpressionValue = (container: t): expressionValue => EvModule(toRecord(container))
let fromExpressionValue = (aValue: expressionValue): t =>
switch aValue {
@ -27,3 +21,13 @@ let fromExpressionValue = (aValue: expressionValue): t =>
}
let toString = (container: t): string => container->toRecord->EvRecord->expressionValueToString
// -- Module definition
let define = (container: t, identifier: string, ev: expressionValue): t =>
Belt.Map.String.set(container, identifier, ev) // TODO build lambda for polymorphic functions here
let defineNumber = (container: t, identifier: string, value: float): t =>
container->define(identifier, EvNumber(value))
let defineModule = (container: t, identifier: string, value: t): t =>
container->define(identifier, toExpressionValue(value))

View File

@ -123,7 +123,7 @@ let evaluateUsingOptions = (
let anExternalBindings = switch externalBindings {
| Some(bindings) => bindings
| None => ReducerInterface_ExpressionValue.defaultExternalBindings
| None => ReducerInterface_DefaultExternalBindings.defaultExternalBindings
}
let bindings = anExternalBindings->Bindings.fromExternalBindings

View File

@ -2,25 +2,25 @@ module ErrorValue = Reducer_ErrorValue
module ExpressionT = Reducer_Expression_T
module ExpressionValue = ReducerInterface.ExpressionValue
module Result = Belt.Result
module BindingsManager = Reducer_Manager_Bindings
module Bindings = Reducer_Category_Bindings
type errorValue = Reducer_ErrorValue.errorValue
type expression = ExpressionT.expression
type expressionValue = ExpressionValue.expressionValue
type externalBindings = ReducerInterface_ExpressionValue.externalBindings
let emptyBindings = Reducer_Manager_Bindings.emptyBindings
let emptyBindings = Reducer_Category_Bindings.emptyBindings
let typeAliasesKey = BindingsManager.typeAliasesKey
let typeReferencesKey = BindingsManager.typeReferencesKey
let typeAliasesKey = Bindings.typeAliasesKey
let typeReferencesKey = Bindings.typeReferencesKey
let toExternalBindings = (bindings: ExpressionT.bindings): externalBindings =>
BindingsManager.toRecord(bindings)
Bindings.toRecord(bindings)
let fromExternalBindings = (externalBindings: externalBindings): ExpressionT.bindings =>
BindingsManager.fromRecord(externalBindings)
Bindings.fromRecord(externalBindings)
let fromValue = (aValue: expressionValue) => BindingsManager.fromExpressionValue(aValue)
let fromValue = (aValue: expressionValue) => Bindings.fromExpressionValue(aValue)
let isMacroName = (fName: string): bool => fName->Js.String2.startsWith("$$")

View File

@ -0,0 +1,6 @@
module Bindings = Reducer_Category_Bindings
let defaultInternalBindings = Bindings.emptyBindings->SquiggleLibrary_Math.makeBindings
@genType
let defaultExternalBindings = defaultInternalBindings->Bindings.toRecord

View File

@ -4,7 +4,6 @@
*/
module Extra_Array = Reducer_Extra_Array
module ErrorValue = Reducer_ErrorValue
@genType.opaque
type internalCode = Object
@ -34,9 +33,6 @@ and lambdaValue = {
}
and lambdaDeclaration = Declaration.declaration<lambdaValue>
@genType
let defaultExternalBindings: externalBindings = Js.Dict.empty()
type functionCall = (string, array<expressionValue>)
let rec toString = aValue =>

View File

@ -1 +1,8 @@
module Bindings = Reducer_Category_Bindings
module Module = Reducer_Category_Module
let m =
Module.emptyModule->Module.defineNumber("pi", Js.Math._PI)->Module.defineNumber("e", Js.Math._E)
let makeBindings = (previousBindings: Bindings.t): Bindings.t =>
previousBindings->Bindings.defineModule("Math", m)