@@ -891,138 +891,198 @@ void logDuration(string label) =>
891891 addInspectionLogEntry ? . Invoke (
892892 label + " duration: " + CommandLineInterface . FormatIntegerForDisplay ( clock . ElapsedMilliseconds ) + " ms" ) ;
893893
894- return
895- buildCompilerResult
896- . MapError ( err => "Failed to build compiler: " + err )
897- . AndThen ( elmCompiler =>
898- buildPineEvalContextTask . Result
899- . MapError ( error => "Failed to build initial Pine eval context: " + error )
900- . AndThen ( buildPineEvalContextOk =>
901- {
902- clock . Restart ( ) ;
894+ if ( buildCompilerResult . IsErrOrNull ( ) is { } buildCompilerErr )
895+ {
896+ return "Failed to build Elm compiler: " + buildCompilerErr ;
897+ }
903898
904- var parseSubmissionResult =
905- ParseInteractiveSubmission (
906- elmCompiler ,
907- pineVM ,
908- submission : submission ,
909- addInspectionLogEntry : compileEntry => addInspectionLogEntry ? . Invoke ( "Parse: " + compileEntry ) ) ;
899+ if ( buildCompilerResult . IsOkOrNull ( ) is not { } elmCompiler )
900+ {
901+ throw new NotImplementedException (
902+ "Unexpected build compiler result type: " + buildCompilerResult . GetType ( ) ) ;
903+ }
910904
911- return
912- parseSubmissionResult
913- . MapError ( error => "Failed to parse submission: " + error )
914- . AndThen ( parsedSubmissionOk =>
915- {
916- return
917- elmCompilerCache . PineValueDecodedAsElmValue ( parsedSubmissionOk )
918- . MapError ( error => "Failed parsing result as Elm value: " + error )
919- . AndThen ( parsedSubmissionAsElmValue =>
920- {
921- logDuration ( "parse" ) ;
922-
923- clock . Restart ( ) ;
924-
925- var environmentEncodedForCompiler =
926- elmCompilerCache . EncodeValueForCompiler ( buildPineEvalContextOk ) ;
927-
928- logDuration ( "compile - encode environment" ) ;
929-
930- clock . Restart ( ) ;
931-
932- var compileParsedResult =
933- ElmInteractiveEnvironment . ApplyFunction (
934- pineVM ,
935- elmCompiler . CompileParsedInteractiveSubmission ,
936- arguments :
937- [
938- environmentEncodedForCompiler ,
939- parsedSubmissionOk
940- ] ) ;
941-
942- if ( compileParsedResult . IsOkOrNull ( ) is not { } compileParsedOk )
943- {
944- return
945- "Failed compiling parsed submission (" +
946- parsedSubmissionAsElmValue + "): " +
947- compileParsedResult . Unpack ( err => err , ok => "Not an err" ) ;
948- }
949-
950- logDuration ( "compile - apply" ) ;
951-
952- clock . Restart ( ) ;
953-
954- var compileParsedOkAsElmValue =
955- elmCompilerCache . PineValueDecodedAsElmValue ( compileParsedOk )
956- . Extract ( err => throw new Exception ( "Failed decoding as Elm value: " + err ) ) ;
957-
958- logDuration ( "compile - decode result" ) ;
959-
960- clock . Restart ( ) ;
961-
962- if ( compileParsedOkAsElmValue is not ElmValue . ElmTag compiledResultTag )
963- {
964- return "Unexpected return value: No tag: " + compileParsedOkAsElmValue ;
965- }
966-
967- if ( compiledResultTag . TagName is not "Ok" || compiledResultTag . Arguments . Count is not 1 )
968- {
969- return "Failed compile: " + compiledResultTag ;
970- }
971-
972- var decodeExpressionResult =
973- elmCompilerCache . DecodeExpressionFromElmValue ( compiledResultTag . Arguments [ 0 ] ) ;
974-
975- return
976- decodeExpressionResult
977- . MapError ( error => "Failed decoding compiled expression: " + error )
978- . AndThen ( resultingExpr =>
979- {
980- logDuration ( "compile - decode expression" ) ;
981-
982- clock . Restart ( ) ;
983-
984- var evalResult = pineVM . EvaluateExpression ( resultingExpr , buildPineEvalContextOk ) ;
985-
986- logDuration ( "eval" ) ;
987-
988- return
989- evalResult
990- . MapError ( error => "Failed to evaluate expression in PineVM: " + error )
991- . AndThen ( evalOk =>
992- {
993- if ( evalOk is not PineValue . ListValue evalResultListComponent )
994- {
995- return
996- "Type mismatch: Pine expression evaluated to a blob" ;
997- }
998-
999- if ( evalResultListComponent . Elements . Length is not 2 )
1000- {
1001- return
1002- "Type mismatch: Pine expression evaluated to a list with unexpected number of elements: " +
1003- evalResultListComponent . Elements . Length +
1004- " instead of 2" ;
1005- }
1006-
1007- buildPineEvalContextTask = System . Threading . Tasks . Task . FromResult (
1008- Result < string , PineValue > . ok ( evalResultListComponent . Elements . Span [ 0 ] ) ) ;
1009-
1010- clock . Restart ( ) ;
1011-
1012- var parseSubmissionResponseResult =
1013- ElmInteractive . SubmissionResponseFromResponsePineValue (
1014- response : evalResultListComponent . Elements . Span [ 1 ] ) ;
1015-
1016- logDuration ( "parse-result" ) ;
1017-
1018- return
1019- parseSubmissionResponseResult
1020- . MapError ( error => "Failed to parse submission response: " + error ) ;
1021- } ) ;
1022- } ) ;
1023- } ) ;
1024- } ) ;
1025- } ) ) ;
905+ if ( buildPineEvalContextTask . Result . IsErrOrNull ( ) is { } buildPineEvalContextErr )
906+ {
907+ return "Failed to build initial Pine eval context: " + buildPineEvalContextErr ;
908+ }
909+
910+ if ( buildPineEvalContextTask . Result . IsOkOrNull ( ) is not { } buildPineEvalContextOk )
911+ {
912+ throw new NotImplementedException (
913+ "Unexpected build Pine eval context result type: " + buildPineEvalContextTask . Result . GetType ( ) ) ;
914+ }
915+
916+ clock . Restart ( ) ;
917+
918+ var parseSubmissionResult =
919+ ParseInteractiveSubmission (
920+ elmCompiler ,
921+ pineVM ,
922+ submission : submission ,
923+ addInspectionLogEntry : compileEntry => addInspectionLogEntry ? . Invoke ( "Parse: " + compileEntry ) ) ;
924+
925+ if ( parseSubmissionResult . IsErrOrNull ( ) is { } parseSubmissionErr )
926+ {
927+ return "Failed to parse submission: " + parseSubmissionErr ;
928+ }
929+
930+ if ( parseSubmissionResult . IsOkOrNull ( ) is not { } parsedSubmissionOk )
931+ {
932+ throw new NotImplementedException (
933+ "Unexpected parse submission result type: " + parseSubmissionResult . GetType ( ) ) ;
934+ }
935+
936+ var parseSubmissionAsElmValueResult =
937+ elmCompilerCache . PineValueDecodedAsElmValue ( parsedSubmissionOk ) ;
938+
939+ if ( parseSubmissionAsElmValueResult . IsErrOrNull ( ) is { } parseSubmissionAsElmValueErr )
940+ {
941+ return "Failed parsing submission response as Elm value: " + parseSubmissionAsElmValueErr ;
942+ }
943+
944+ if ( parseSubmissionAsElmValueResult . IsOkOrNull ( ) is not { } parsedSubmissionAsElmValue )
945+ {
946+ throw new NotImplementedException (
947+ "Unexpected parse submission as Elm value result type: " + parseSubmissionAsElmValueResult . GetType ( ) ) ;
948+ }
949+
950+ logDuration ( "parse" ) ;
951+
952+ clock . Restart ( ) ;
953+
954+ var environmentEncodedForCompiler =
955+ elmCompilerCache . EncodeValueForCompiler ( buildPineEvalContextOk ) ;
956+
957+ logDuration ( "compile - encode environment" ) ;
958+
959+ clock . Restart ( ) ;
960+
961+ var compileParsedResult =
962+ ElmInteractiveEnvironment . ApplyFunction (
963+ pineVM ,
964+ elmCompiler . CompileParsedInteractiveSubmission ,
965+ arguments :
966+ [
967+ environmentEncodedForCompiler ,
968+ parsedSubmissionOk
969+ ] ) ;
970+
971+ if ( compileParsedResult . IsErrOrNull ( ) is { } compileParsedErr )
972+ {
973+ return "Failed compiling parsed submission (" +
974+ parsedSubmissionAsElmValue + "): " + compileParsedErr ;
975+ }
976+
977+ if ( compileParsedResult . IsOkOrNull ( ) is not { } compileParsedOk )
978+ {
979+ return
980+ "Failed compiling parsed submission (" +
981+ parsedSubmissionAsElmValue + "): " +
982+ compileParsedResult . Unpack ( err => err , ok => "Not an err" ) ;
983+ }
984+
985+ logDuration ( "compile - apply" ) ;
986+
987+ clock . Restart ( ) ;
988+
989+ var compileParsedOkAsElmValueResult =
990+ elmCompilerCache . PineValueDecodedAsElmValue ( compileParsedOk ) ;
991+
992+ if ( compileParsedOkAsElmValueResult . IsErrOrNull ( ) is { } compileParsedOkAsElmValueErr )
993+ {
994+ return "Failed decoding compiled expression: " + compileParsedOkAsElmValueErr ;
995+ }
996+
997+ if ( compileParsedOkAsElmValueResult . IsOkOrNull ( ) is not { } compileParsedOkAsElmValue )
998+ {
999+ throw new NotImplementedException (
1000+ "Unexpected compile parsed ok as Elm value result type: " + compileParsedOkAsElmValueResult . GetType ( ) ) ;
1001+ }
1002+
1003+ logDuration ( "compile - decode result" ) ;
1004+
1005+ clock . Restart ( ) ;
1006+
1007+ if ( compileParsedOkAsElmValue is not ElmValue . ElmTag compiledResultTag )
1008+ {
1009+ return "Unexpected return value: No tag: " + compileParsedOkAsElmValue ;
1010+ }
1011+
1012+ if ( compiledResultTag . TagName is not "Ok" || compiledResultTag . Arguments . Count is not 1 )
1013+ {
1014+ return "Failed compile: " + compiledResultTag ;
1015+ }
1016+
1017+ var decodeExpressionResult =
1018+ elmCompilerCache . DecodeExpressionFromElmValue ( compiledResultTag . Arguments [ 0 ] ) ;
1019+
1020+ if ( decodeExpressionResult . IsErrOrNull ( ) is { } decodeExpressionErr )
1021+ {
1022+ return "Failed decoding compiled expression: " + decodeExpressionErr ;
1023+ }
1024+
1025+ if ( decodeExpressionResult . IsOkOrNull ( ) is not { } resultingExpr )
1026+ {
1027+ throw new NotImplementedException (
1028+ "Unexpected decode expression result type: " + decodeExpressionResult . GetType ( ) ) ;
1029+ }
1030+
1031+ logDuration ( "compile - decode expression" ) ;
1032+
1033+ clock . Restart ( ) ;
1034+
1035+ var evalResult = pineVM . EvaluateExpression ( resultingExpr , buildPineEvalContextOk ) ;
1036+
1037+ logDuration ( "eval" ) ;
1038+
1039+ if ( evalResult . IsErrOrNull ( ) is { } evalErr )
1040+ {
1041+ return "Failed to evaluate compiled expression in PineVM: " + evalErr ;
1042+ }
1043+
1044+ if ( evalResult . IsOkOrNull ( ) is not { } evalOk )
1045+ {
1046+ throw new NotImplementedException (
1047+ "Unexpected eval result type: " + evalResult . GetType ( ) ) ;
1048+ }
1049+
1050+ if ( evalOk is not PineValue . ListValue evalResultListComponent )
1051+ {
1052+ return "Type mismatch: Pine expression not evaluated to a list: " + evalOk ;
1053+ }
1054+
1055+ if ( evalResultListComponent . Elements . Length is not 2 )
1056+ {
1057+ return
1058+ "Type mismatch: Pine expression evaluated to a list with unexpected number of elements: " +
1059+ evalResultListComponent . Elements . Length +
1060+ " instead of 2" ;
1061+ }
1062+
1063+ buildPineEvalContextTask = System . Threading . Tasks . Task . FromResult (
1064+ Result < string , PineValue > . ok ( evalResultListComponent . Elements . Span [ 0 ] ) ) ;
1065+
1066+ clock . Restart ( ) ;
1067+
1068+ var parseSubmissionResponseResult =
1069+ ElmInteractive . SubmissionResponseFromResponsePineValue (
1070+ response : evalResultListComponent . Elements . Span [ 1 ] ) ;
1071+
1072+ logDuration ( "parse-result" ) ;
1073+
1074+ if ( parseSubmissionResponseResult . IsErrOrNull ( ) is { } parseSubmissionResponseErr )
1075+ {
1076+ return "Failed to parse submission response: " + parseSubmissionResponseErr ;
1077+ }
1078+
1079+ if ( parseSubmissionResponseResult . IsOkOrNull ( ) is not { } parseSubmissionResponseOk )
1080+ {
1081+ throw new NotImplementedException (
1082+ "Unexpected parse submission response result type: " + parseSubmissionResponseResult . GetType ( ) ) ;
1083+ }
1084+
1085+ return parseSubmissionResponseOk ;
10261086 }
10271087 }
10281088
0 commit comments