Skip to content

Commit

Permalink
Clean up implementation to search reductions for expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Viir committed Jun 16, 2024
1 parent aa98aaa commit 4f5ff3c
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions implement/pine/Pine/CompilePineToDotNet/ReducePineExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static Result<string, PineValue> TryEvaluateExpressionIndependent(
{
return
new PineVM.PineVM().EvaluateExpression(parseAndEvalExpr, PineValue.EmptyList)
.MapError(err => "Got independent environment, but failed to evaluated: " + err);
.MapError(err => "Got independent environment, but failed to evaluate: " + err);
}

return
Expand Down Expand Up @@ -89,7 +89,14 @@ public static Result<string, PineValue> TryEvaluateExpressionIndependent(
case "list_head":
{
if (rootKernelApp.argument is Expression.ListExpression argumentList)
return argumentList.List.FirstOrDefault();
{
return
argumentList.List.FirstOrDefault() ??
new Expression.LiteralExpression(PineValue.EmptyList);
}

if (rootKernelApp.argument is Expression.LiteralExpression literal)
return new Expression.LiteralExpression(KernelFunction.list_head(literal.Value));

return AttemptReduceViaEval();
}
Expand All @@ -105,6 +112,10 @@ public static Result<string, PineValue> TryEvaluateExpressionIndependent(
if (argumentList.List[1] is Expression.ListExpression partiallySkippedList)
return new Expression.ListExpression(
[.. partiallySkippedList.List.Skip((int)okSkipCount.Value)]);

if (argumentList.List[1] is Expression.LiteralExpression literal)
return new Expression.LiteralExpression(
KernelFunction.skip((int)okSkipCount.Value, literal.Value));
}
}

Expand Down Expand Up @@ -133,7 +144,11 @@ public static Result<string, PineValue> TryEvaluateExpressionIndependent(
?
conditional.ifTrue
:
conditional.ifFalse);
conditionValue == PineVMValues.FalseValue
?
conditional.ifFalse
:
new Expression.LiteralExpression(PineValue.EmptyList));
}

return AttemptReduceViaEval();
Expand Down Expand Up @@ -312,18 +327,32 @@ public static (Expression expr, bool referencesOriginalEnv) TransformPineExpress
"Expression type not implemented: " + expression.GetType().FullName);
}

public static Expression SearchForExpressionReductionRecursive(int maxDepth, Expression expression)
public static Expression SearchForExpressionReductionRecursive(
int maxDepth,
Expression expression,
Func<Expression, bool>? dontReduceExpression = null)
{
if (maxDepth < 1)
return expression;

var transformed =
TransformPineExpressionWithOptionalReplacement(SearchForExpressionReduction, expression).expr;
TransformPineExpressionWithOptionalReplacement(
expr =>
{
if (dontReduceExpression?.Invoke(expr) ?? false)
return null;
return SearchForExpressionReduction(expr);
}, expression).expr;

if (transformed == expression)
return transformed;

return SearchForExpressionReductionRecursive(maxDepth - 1, transformed);
return
SearchForExpressionReductionRecursive(
maxDepth - 1,
transformed,
dontReduceExpression);
}
}

0 comments on commit 4f5ff3c

Please sign in to comment.