duckscript icon indicating copy to clipboard operation
duckscript copied to clipboard

eval to support math comparison

Open sagiegurari opened this issue 5 years ago • 7 comments

sagiegurari avatar Jan 04 '20 12:01 sagiegurari

not sure what this would involve, but one feature that could fit in here is fish like command evaluation for inputs to other commands

so e.g. this could be supported:

if less_than (get_last_modified_time package.lock) (current_time)
    echo hi
end

ModProg avatar Feb 25 '22 07:02 ModProg

you have calc + less/greater/eq commands. isn't it working for your use case?

sagiegurari avatar Feb 25 '22 11:02 sagiegurari

It might just be that I'm not very experienced with duckscript, but this got rather lengthy.

package_lock_modified = get_last_modified_time yarn.lock 
current_time = current_time
current_time = calc ${current_time} - 48*60*60*1000
less_than_two_days_old = less_than ${package_lock_modified} ${current_time} 
package_json_newer = is_path_newer package.json yarn.lock
if ${less_than_two_days_old} or ${package_json_newer}
    exec --fail-on-error yarn
    exec touch yarn.lock
else
    echo "Packages up to date"
end

I would have prefered to be able to do something like this:

if (less_than 
        (get_last_modified_time yarn.lock)
        (calc (current_time) - 48*60*60*1000)) 
    or (is_path_newer package.json yarn.lock)
    exec --fail-on-error yarn
    exec touch yarn.lock
else
    echo "Packages up to date"
end

ModProg avatar Feb 25 '22 15:02 ModProg

ya the conditions are a bit basic in their capabilities and duckscript in general has 1 command per 1 line rule. also per the format of output=command args this one: calc (current_time) - 486060*1000 will probably never be implemented.

sagiegurari avatar Feb 27 '22 06:02 sagiegurari

calc (current_time) - 48*60*60*1000 will probably never be implemented.

Makes sense. I just thought it might be a useful feature to basically have a shortcut to always needing to move everything in a variable.

Fish follows a similar approach, wanting to have a simple syntax that prefers e.g. commands over language features, that's why I proposed their syntax.

Thinking about it, maybe something more inline with the variable expansions $(...) would maybe make more sense and also lowers the risk of accidentally triggering it.

It should also not increase the implementation complexity too much, as the implementation could basically be:

  1. Find $(inner)
  2. Replace it with ${var_name}
  3. Introduce new variable in line above var_name = inner

Probably makes not a lot of sense to actually implement it like that, but I would find it quite useful.

ModProg avatar Feb 27 '22 19:02 ModProg

can you explain a bit more? maybe put some small example?

sagiegurari avatar Feb 28 '22 07:02 sagiegurari

can you explain a bit more? maybe put some small example?

Basically my example from above.

A bit simplified, this:

if less_than $(get_last_modified_time file) $(calc $(current_time) - 100)
    echo true
else
    echo false
end

Should internally expand to

__get_last_modified_time = get_last_modified_time file
__current_time = current_time
__calc = calc ${__current_time} - 100
if less_than ${__get_last_modified_time} ${__calc}
    echo true
else
    echo false
end

I used __ to mark internal variables. But they probably don't even need names.

ModProg avatar Mar 01 '22 09:03 ModProg