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 // Empty block
testMacro([], eBlock(list{}), "Ok(:undefined block)") //TODO: should be an error 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", () => { describe("lambda", () => {

View File

@ -33,7 +33,9 @@ open Reducer_TestHelpers
describe("Eval with Bindings", () => { describe("Eval with Bindings", () => {
testEvalBindingsToBe("x", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(1)") testEvalBindingsToBe("x", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(1)")
testEvalBindingsToBe("x+1", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(2)") 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; 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 => { let constructRecord = arrayOfPairs => {
Belt.Array.map(arrayOfPairs, pairValue => { Belt.Array.map(arrayOfPairs, pairValue =>
switch pairValue { switch pairValue {
| EvArray([EvString(key), valueValue]) => (key, valueValue) | EvArray([EvString(key), valueValue]) => (key, valueValue)
| _ => ("wrong key type", pairValue->toStringWithType->EvString) | _ => ("wrong key type", pairValue->toStringWithType->EvString)
} }
}) )
->Js.Dict.fromArray ->Js.Dict.fromArray
->EvRecord ->EvRecord
->Ok ->Ok

View File

@ -19,14 +19,10 @@ let dispatchMacroCall = (
environment, environment,
reduceExpression: ExpressionT.reducerFn, reduceExpression: ExpressionT.reducerFn,
): result<expression, errorValue> => { ): result<expression, errorValue> => {
let doBindStatement = (bindingExpr: expression, statement: expression, environment) => let doBindStatement = (bindingExpr: expression, statement: expression, environment) =>
switch statement { switch statement {
| ExpressionT.EList(list{ExpressionT.EValue(EvCall("$let")), symbolExpr, statement}) => { | ExpressionT.EList(list{ExpressionT.EValue(EvCall("$let")), symbolExpr, statement}) => {
let rExternalBindingsValue = reduceExpression( let rExternalBindingsValue = reduceExpression(bindingExpr, bindings, environment)
bindingExpr,
Bindings.defaultBindings,
environment,
)
rExternalBindingsValue->Result.flatMap(externalBindingsValue => { rExternalBindingsValue->Result.flatMap(externalBindingsValue => {
let newBindings = Bindings.fromValue(externalBindingsValue) let newBindings = Bindings.fromValue(externalBindingsValue)
@ -41,13 +37,15 @@ let dispatchMacroCall = (
} }
| _ => REAssignmentExpected->Error | _ => REAssignmentExpected->Error
} }
let doBindExpression = (bindingExpr: expression, statement: expression, environment) => let doBindExpression = (bindingExpr: expression, statement: expression, environment) =>
switch statement { switch statement {
| ExpressionT.EList(list{ExpressionT.EValue(EvCall("$let")), symbolExpr, statement}) => { | ExpressionT.EList(list{ExpressionT.EValue(EvCall("$let")), symbolExpr, statement}) => {
let rExternalBindingsValue = reduceExpression( let rExternalBindingsValue = reduceExpression(
bindingExpr, bindingExpr,
Bindings.defaultBindings, Belt.Map.String.fromArray([("x", ExpressionValue.EvNumber(666.))]),
// bindingsToHandDown,
environment, environment,
) )
@ -68,11 +66,7 @@ let dispatchMacroCall = (
}) })
} }
| _ => { | _ => {
let rExternalBindingsValue = reduceExpression( let rExternalBindingsValue = reduceExpression(bindingExpr, bindings, environment)
bindingExpr,
Bindings.defaultBindings,
environment,
)
rExternalBindingsValue->Result.flatMap(externalBindingsValue => { rExternalBindingsValue->Result.flatMap(externalBindingsValue => {
let newBindings = Bindings.fromValue(externalBindingsValue) let newBindings = Bindings.fromValue(externalBindingsValue)
let rNewStatement = Bindings.replaceSymbols(newBindings, statement) let rNewStatement = Bindings.replaceSymbols(newBindings, statement)
@ -80,7 +74,7 @@ let dispatchMacroCall = (
}) })
} }
} }
let doBlock = (exprs: list<expression>, _bindings: ExpressionT.bindings, _environment): result< let doBlock = (exprs: list<expression>, _bindings: ExpressionT.bindings, _environment): result<
expression, expression,
errorValue, errorValue,

View File

@ -32,7 +32,7 @@ let parse = (mathJsCode: string): result<t, errorValue> =>
let rec reduceExpression = (expression: t, bindings: T.bindings, environment: environment): result< let rec reduceExpression = (expression: t, bindings: T.bindings, environment: environment): result<
expressionValue, expressionValue,
'e, 'e,
> => { > =>
switch expression { switch expression {
| T.EValue(value) => value->Ok | T.EValue(value) => value->Ok
| T.EList(list) => | T.EList(list) =>
@ -46,7 +46,7 @@ let rec reduceExpression = (expression: t, bindings: T.bindings, environment: en
| _ => reduceExpressionList(list, bindings, environment) | _ => reduceExpressionList(list, bindings, environment)
} }
} }
}
and reduceExpressionList = ( and reduceExpressionList = (
expressions: list<t>, expressions: list<t>,
bindings: T.bindings, 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 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) { switch Js.typeof(jsValue) {
| "boolean" => jsValue->castBool->EvBool->Ok | "boolean" => jsValue->castBool->EvBool->Ok
| "number" => jsValue->castNumber->EvNumber->Ok | "number" => jsValue->castNumber->EvNumber->Ok
| "string" => jsValue->castString->EvString->Ok | "string" => jsValue->castString->EvString->Ok
| other => RETodo(`Unhandled MathJs literal type: ${Js.String.make(other)}`)->Error | 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<( let catchAndConvertTwoArgsToDists = (args: array<expressionValue>): option<(
DistributionTypes.genericDist, DistributionTypes.genericDist,
DistributionTypes.genericDist, DistributionTypes.genericDist,
)> => { )> =>
switch args { switch args {
| [EvDistribution(a), EvDistribution(b)] => Some((a, b)) | [EvDistribution(a), EvDistribution(b)] => Some((a, b))
| [EvNumber(a), EvDistribution(b)] => Some((GenericDist.fromFloat(a), b)) | [EvNumber(a), EvDistribution(b)] => Some((GenericDist.fromFloat(a), b))
| [EvDistribution(a), EvNumber(b)] => Some((a, GenericDist.fromFloat(b))) | [EvDistribution(a), EvNumber(b)] => Some((a, GenericDist.fromFloat(b)))
| _ => None | _ => None
} }
}
let toFloatFn = ( let toFloatFn = (
fnCall: DistributionTypes.DistributionOperation.toFloat, fnCall: DistributionTypes.DistributionOperation.toFloat,
@ -119,7 +119,7 @@ module Helpers = {
mixtureWithGivenWeights(distributions, weights) mixtureWithGivenWeights(distributions, weights)
} }
let mixture = (args: array<expressionValue>): DistributionOperation.outputType => { let mixture = (args: array<expressionValue>): DistributionOperation.outputType =>
switch E.A.last(args) { switch E.A.last(args) {
| Some(EvArray(b)) => { | Some(EvArray(b)) => {
let weights = parseNumberArray(b) let weights = parseNumberArray(b)
@ -138,7 +138,7 @@ module Helpers = {
} }
| _ => GenDistError(ArgumentError("Last argument of mx must be array or distribution")) | _ => GenDistError(ArgumentError("Last argument of mx must be array or distribution"))
} }
}
} }
module SymbolicConstructors = { module SymbolicConstructors = {