+
Skip to content

Filter, Map, SortedBy #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Filter, Map, SortedBy #37

wants to merge 4 commits into from

Conversation

gilmore606
Copy link
Owner

@gilmore606 gilmore606 commented Jul 11, 2025

Now that we have lambdas, adding built-in List.map/filter ops seemed like a no-brainer -- and it mostly was, but for one small problem.

To run these ops, we need to execute the given lambda for each element, and get a value back. This means a bare expression (parsed as EXPRSTATEMENT) should return its value (otherwise we'd have to explicitly 'return x'). But since these EXPRSTATEMENTs execute O_DISCARD to avoid cluttering the stack with their presumably-extraneous values, the value isn't there to be returned.

The fix I went with: whenever we O_DISCARD, we overwrite VM.exprValue with the discarded value -- the idea being that the last bare expression in a verb is considered the expression-value of that verb's execution. On an implied return (i.e. hitting end-of-code) we simply return this value. This lets us maintain stack cleanliness but still obtain a value from evaluating a verb, when we need to.

Actually running the lambdas is done in Task.executeLambda(), which simply pushes the lambda onto the stack and asks the Task to execute until that lambda pops off with a return value.

consume(T_IDENTIFIER)?.also { args.add(N_IDENTIFIER(it.string)) }
consume(T_COMMA)
}
while (nextAre(T_IDENTIFIER, T_ARROW)) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bugfix for parsing lambdas -- the argless form { ... } could not be parsed if it started with an identifier, for instance { x > 10 } -- the parser saw this as an incomplete arg declaration.

data object Finished: Result
@JvmInline value class Suspend(val seconds: Int): Result
data class Finished(val v: Value?): Result
data class Suspend(val seconds: Int): Result
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was very little actual optimization value in making these jvminline value classes to begin with, so I'm not doing it here anymore.

is VInt -> v.sortedBy { (it as VInt).v }
is VFloat -> v.sortedBy { (it as VFloat).v }
is VInt -> v.sortedBy { (it as? VInt)?.v ?: 0 }
is VFloat -> v.sortedBy { (it as? VFloat)?.v ?: 0f }
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated bugfix for sorting mixed lists -- if you sort a list of mixed value types we do our best and assume you want them sorted by the value semantics of the first element.

@gilmore606 gilmore606 marked this pull request as ready for review July 11, 2025 20:10
@gilmore606 gilmore606 marked this pull request as draft July 12, 2025 08:24
@gilmore606 gilmore606 marked this pull request as ready for review July 12, 2025 22:24
@gilmore606 gilmore606 requested a review from melville July 12, 2025 22:27
}

// Add a VM to the stack to run an exe.
fun push(
// Execute an exe immediately for a return value. The task cannot suspend.
Copy link
Owner Author

@gilmore606 gilmore606 Jul 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally i was making a whole new Task to execute lambdas from the builtins, but I realized I could trick the existing Task into stopping at the depth of the new lambda exe and returning me the value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载