electrikmilk / cherri

Siri Shortcuts Programming Language 🍒
https://cherrilang.org
GNU General Public License v2.0
205 stars 12 forks source link

Refactor custom actions to accept arguments and behave more like functions #19

Closed electrikmilk closed 8 months ago

electrikmilk commented 1 year ago

The implementation of a simple addition "function" in Shortcuts in Cherri looks like this:

if ShortcutInput {
    const inputType = typeOf(ShortcutInput)
    if inputType == "Dictionary" {
        const input = getDictionary(ShortcutInput)
        const identifier = getValue(input, "cherri_functions")
        const valid = number(identifier)
        if valid == true {
            const function_name = getValue(input, "function")
            const function = "{function_name}"
            const args = getValue(input, "arguments")
            if function == "add" {
                const arg1 = getListItem(args, 0)
                const arg2 = getListItem(args, 1)
                const n1 = number(arg1)
                const n2 = number(arg2)
                const result = n1 + n2
                output("{result}")
            }
        }
    }
}

const operandOne = prompt("Operand 1", "Number", "2")
const operandTwo = prompt("Operand 2", "Number", "2")

const myFunctionCall = {
    "cherri_functions": 1,
    "function": "add",
    "arguments": ["{operandOne}", "{operandTwo}"]
}
const myFunctionReturn = runSelf(myFunctionCall)

alert(myFunctionReturn)

This has recently become possible through some focused work on this implementation and is tested to work.

This would allow custom actions to have their own scope.

We would want the definition for this to look like this:

action add(number n1, number n2) {
    const result = n1 + n2
    output("{result}")
}

add(2, 2)

So this will need to be generated based on custom action definitions and references.

erenyenigul commented 1 year ago

Hey @electrikmilk, I want to contribute to the project. I am trying to find your contact information for my detailed questions. Can you add your email or another information for me to reach you?

electrikmilk commented 1 year ago

Hey @electrikmilk, I want to contribute to the project. I am trying to find your contact information for my detailed questions. Can you add your email or another information for me to reach you?

Thank you for your interest in the project! I will send you an email from my personal email to the email listed on your site.

twilsonco commented 9 months ago

Came here to suggest “functions” but glad to see you’re already on it! Just finding out about this project via the RoutineHub blog post. Amazing work!

electrikmilk commented 9 months ago

Came here to suggest “functions” but glad to see you’re already on it! Just finding out about this project via the RoutineHub blog post. Amazing work!

Thank you also for your interest in the project! I'm really glad to see people are excited about this upcoming feature.

twilsonco commented 9 months ago

The alternative is, for more complicated shortcuts like this Photo collage tool I recently made, to develop "functions" as separate shortcuts and then use something like Join shortcuts to put everything together, but it requires so much more work. Something like what you're doing (also what JellyCuts does, though it's very buggy) makes it feel 1000X more approachable.

Also, the structure of your project makes me really want to get involved, so that I can PR all my selfish wish-list actions and features! Kudos again.

twilsonco commented 9 months ago

Sidenote: is there a way I can support your work?

electrikmilk commented 9 months ago

The alternative is, for more complicated shortcuts like this Photo collage tool I recently made, to develop "functions" as separate shortcuts and then use something like Join shortcuts to put everything together, but it requires so much more work. Something like what you're doing (also what JellyCuts does, though it's very buggy) makes it feel 1000X more approachable.

Also, the structure of your project makes me really want to get involved, so that I can PR all my selfish wish-list actions and features! Kudos again.

That's great to hear! Haha, yes, please do report issues you run into while making your Shortcuts :)

electrikmilk commented 9 months ago

Sidenote: is there a way I can support your work?

Hmm, perhaps I will need to look into setting up a Github sponsors 🤔

Thank you very much for offering!

electrikmilk commented 8 months ago

I'd like to do a bit more testing before merging into main, but this work is finally done.

I started fresh recently due to external changes made since I worked on the old branch.

I also had to fix an issue with action constants first.

This new implementation collects action definitions in a pre-parsing loop like the original branch implementation.

However, instead of replacing references to those defined actions, during the main parsing loop when we collect standard actions, if it's a custom action, we just create different tokens of a Dictionary and Run Shortcut actions like in the code above.

electrikmilk commented 8 months ago

However, instead of replacing references to those defined actions, during the main parsing loop when we collect standard actions, if it's a custom action, we just create different tokens of a Dictionary and Run Shortcut actions like in the code above.

We do this so that we don't have to separately keep track of variable references, users should be able to enter arguments to a custom action reference just like they normally would. This also doesn't change the contents of the file save for the generated header where all of the definitions live.

electrikmilk commented 8 months ago

I've decided macros will be a separate issue, but shouldn't take too long.

I also want to make that distinct from custom actions so it's understood what the difference is.