bug fixed

logs removed
This commit is contained in:
Umur Ozkul 2022-04-29 18:14:02 +02:00
parent 8e318a8aa9
commit 351381339c
7 changed files with 41 additions and 24 deletions

View File

@ -99,6 +99,27 @@ describe("block", () => {
)
// Empty block
testMacro([], eBlock(list{}), "Ok(:undefined block)") //TODO: should be an error
// :$$block (:$$block (:$let :y (:add :x 1)) :y)"
testMacro(
[],
eBlock(list{
eBlock(list{
eLetStatement("y", eFunction("add", list{eSymbol("x"), eNumber(1.)})),
eSymbol("y"),
}),
}),
"Ok((:$$bindExpression (:$$block (:$let :y (:add :x 1)) :y)))",
)
MyOnly.testMacroEval(
[("x", EvNumber(1.))],
eBlock(list{
eBlock(list{
eLetStatement("y", eFunction("add", list{eSymbol("x"), eNumber(1.)})),
eSymbol("y"),
}),
}),
"Ok(2)",
)
})
describe("lambda", () => {

View File

@ -33,7 +33,9 @@ open Reducer_TestHelpers
describe("Eval with Bindings", () => {
testEvalBindingsToBe("x", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(1)")
testEvalBindingsToBe("x+1", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(2)")
testParseToBe("y = x+1; y", "Ok((:$$block (:$$block (:$let :y (:add :x 1)) :y)))")
testEvalBindingsToBe("y = x+1; y", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(2)")
testEvalBindingsToBe("y = x+1", list{("x", ExpressionValue.EvNumber(1.))}, "Ok({x: 1,y: 2})")
})
/*

View File

@ -21,12 +21,12 @@ let callInternal = (call: functionCall, _environment): result<'b, errorValue> =>
}
let constructRecord = arrayOfPairs => {
Belt.Array.map(arrayOfPairs, pairValue => {
Belt.Array.map(arrayOfPairs, pairValue =>
switch pairValue {
| EvArray([EvString(key), valueValue]) => (key, valueValue)
| _ => ("wrong key type", pairValue->toStringWithType->EvString)
}
})
)
->Js.Dict.fromArray
->EvRecord
->Ok

View File

@ -22,11 +22,7 @@ let dispatchMacroCall = (
let doBindStatement = (bindingExpr: expression, statement: expression, environment) =>
switch statement {
| ExpressionT.EList(list{ExpressionT.EValue(EvCall("$let")), symbolExpr, statement}) => {
let rExternalBindingsValue = reduceExpression(
bindingExpr,
Bindings.defaultBindings,
environment,
)
let rExternalBindingsValue = reduceExpression(bindingExpr, bindings, environment)
rExternalBindingsValue->Result.flatMap(externalBindingsValue => {
let newBindings = Bindings.fromValue(externalBindingsValue)
@ -42,12 +38,14 @@ let dispatchMacroCall = (
| _ => REAssignmentExpected->Error
}
let doBindExpression = (bindingExpr: expression, statement: expression, environment) =>
switch statement {
| ExpressionT.EList(list{ExpressionT.EValue(EvCall("$let")), symbolExpr, statement}) => {
let rExternalBindingsValue = reduceExpression(
bindingExpr,
Bindings.defaultBindings,
Belt.Map.String.fromArray([("x", ExpressionValue.EvNumber(666.))]),
// bindingsToHandDown,
environment,
)
@ -68,11 +66,7 @@ let dispatchMacroCall = (
})
}
| _ => {
let rExternalBindingsValue = reduceExpression(
bindingExpr,
Bindings.defaultBindings,
environment,
)
let rExternalBindingsValue = reduceExpression(bindingExpr, bindings, environment)
rExternalBindingsValue->Result.flatMap(externalBindingsValue => {
let newBindings = Bindings.fromValue(externalBindingsValue)
let rNewStatement = Bindings.replaceSymbols(newBindings, statement)

View File

@ -32,7 +32,7 @@ let parse = (mathJsCode: string): result<t, errorValue> =>
let rec reduceExpression = (expression: t, bindings: T.bindings, environment: environment): result<
expressionValue,
'e,
> => {
> =>
switch expression {
| T.EValue(value) => value->Ok
| T.EList(list) =>
@ -46,7 +46,7 @@ let rec reduceExpression = (expression: t, bindings: T.bindings, environment: en
| _ => reduceExpressionList(list, bindings, environment)
}
}
}
and reduceExpressionList = (
expressions: list<t>,
bindings: T.bindings,

View File

@ -8,11 +8,11 @@ external castString: unit => string = "%identity"
/*
As JavaScript returns us any type, we need to type check and cast type propertype before using it
*/
let jsToEv = (jsValue): result<expressionValue, errorValue> => {
let jsToEv = (jsValue): result<expressionValue, errorValue> =>
switch Js.typeof(jsValue) {
| "boolean" => jsValue->castBool->EvBool->Ok
| "number" => jsValue->castNumber->EvNumber->Ok
| "string" => jsValue->castString->EvString->Ok
| other => RETodo(`Unhandled MathJs literal type: ${Js.String.make(other)}`)->Error
}
}

View File

@ -28,14 +28,14 @@ module Helpers = {
let catchAndConvertTwoArgsToDists = (args: array<expressionValue>): option<(
DistributionTypes.genericDist,
DistributionTypes.genericDist,
)> => {
)> =>
switch args {
| [EvDistribution(a), EvDistribution(b)] => Some((a, b))
| [EvNumber(a), EvDistribution(b)] => Some((GenericDist.fromFloat(a), b))
| [EvDistribution(a), EvNumber(b)] => Some((a, GenericDist.fromFloat(b)))
| _ => None
}
}
let toFloatFn = (
fnCall: DistributionTypes.DistributionOperation.toFloat,
@ -119,7 +119,7 @@ module Helpers = {
mixtureWithGivenWeights(distributions, weights)
}
let mixture = (args: array<expressionValue>): DistributionOperation.outputType => {
let mixture = (args: array<expressionValue>): DistributionOperation.outputType =>
switch E.A.last(args) {
| Some(EvArray(b)) => {
let weights = parseNumberArray(b)
@ -138,7 +138,7 @@ module Helpers = {
}
| _ => GenDistError(ArgumentError("Last argument of mx must be array or distribution"))
}
}
}
module SymbolicConstructors = {