duckscript icon indicating copy to clipboard operation
duckscript copied to clipboard

Confusion about condition statement

Open xxchan opened this issue 2 years ago • 6 comments

Describe The Bug

a = get_env ...
b = get_env ...

if not ${a} and ${b}
  ...
else
  ...
end

gets error

Error while running duckscript: Source: Unknown Line: 36 - Unexpected 'and'

To Reproduce

Error Stack

The error stack trace

Code Sample

/// paste code here

xxchan avatar May 05 '23 15:05 xxchan

Besides,

not false and false

gives true is a little confusing... Although I can kind of understand it.

xxchan avatar May 05 '23 19:05 xxchan

Hmmm, but why

# Unexpected value: false
if true and ( not false )

Above is in cargo-make. Using duckscript main I have the following result:

if true and ( not false )
    echo "1"
else
    echo "2"
end
1 
2

xxchan avatar May 05 '23 19:05 xxchan

It seems eval_condition_for_slice only support literals.. (so not cannot be used)

xxchan avatar May 05 '23 19:05 xxchan

sorry for the confusion, but conditions like in 'if' command are build either from

  1. variable
  2. command
  3. condition

in your case, i figure you think you have a condition, but a condition is defined by "A condition statement is made up of values, or/and keywords and '('/')' groups." see docs at: https://github.com/sagiegurari/duckscript/blob/master/docs/sdk.md#std__flowcontrol__If

the 'not' is not a condition keyword, its also a command: https://github.com/sagiegurari/duckscript/blob/master/docs/sdk.md#std__Not

which i guess where the confusion is coming from. so when you have a condition phrase, you can't use embedded commands inside since i have no idea where that command args stop and the condition phrase continues.

so you need to do

a = get_env ...
b = get_env ...
not_a = not ${a}

if ${not_a} and ${b}
  ...
else
  ...
end

sagiegurari avatar May 06 '23 14:05 sagiegurari

Yes, thanks for your answer and I’ve already fixed my problem in this way.

I don’t have strong opinion, and haven’t learned much about the design decisions of duckscript. Do you think make “not” a condition keyword can be more user-friendly?

xxchan avatar May 06 '23 15:05 xxchan

conditions are not part of duckscript. they are commands on their own. it has its limitations but also extendibility where the main goal was to be super super simple lang with nothing in it just simple 1 liner syntax. so i agree it can make things simpler, it is also not that clear where not ends and make things more complex at same time.

sagiegurari avatar May 08 '23 10:05 sagiegurari