Csound Csound-dev Csound-tekno Search About

[Csound] idea: switch/case statements

Date2015-10-17 11:19
FromKevin Welsh
Subject[Csound] idea: switch/case statements
I think I brought this idea up once before in somebody else's thread,
but being off topic I think it got lost and forgotten as crosstalk...
so I figured I'll mention it again on it's own and see what everyone
thinks.

I would find it very useful if csound had a switch/case statement
similar to c/c++/php or even bash style if it's easier to handle the
parsing that way.

The idea is to save some typing on large if/elseif statements.  While
I assume most on this list would be familiar with the concept, just in
case anyone isn't here's a pseudo-code example modeled after bash.

; block of code using if/elseif/else
if (kval==1)
  do something
elseif (kval==2)
  do something else
elseif (kval==3)
  one other thing to do
else
  a fall through if nothing else was met
endif

; block of code using case
case kval in
  1) do something ;;
  2) do something else ;;
  3) one other thing to do ;;
  *) a fall through if nothing was met ;;
esac

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2015-10-21 21:59
FromSteven Yi
SubjectRe: [Csound] idea: switch/case statements
Hi Kevin,

I think you (tgrey1?) filed an issue for this:

https://github.com/csound/csound/issues/380

I think we would need to continue to develop the idea further before
implementing.  Some things that would help:

1. Example syntax from various proposals: should we use switch, case,
etc. as the keyword?  do we add a break keyword?

2. Types: what types of variables do we allow switching on?  Do we
limit this to numbers, or open it up to all types?

3. Should the cases compare to constants, or should we allow
predicates as well?  i.e.

case myVar of
  (myVar > 3)  then
     ...code...
  (myVar > 2)  then
     ...code...
   else
   ...code...
end

(I'm not sure the above saves any typing, and probably adds more lines
of code...)

The tricky part about case/esac is that there is a delimiter between
statements, but in Csound there is only newlines. We'd have to figure
out details around this, or limit to only one statement per case line,
or etc.  (Ruby seems to use a then keyword when placing a command on
the same line as a when, when using case [1]).

steven

[1] - http://ruby.about.com/od/control/a/The-Case-Statement.htm


On Sat, Oct 17, 2015 at 6:19 AM, Kevin Welsh  wrote:
> I think I brought this idea up once before in somebody else's thread,
> but being off topic I think it got lost and forgotten as crosstalk...
> so I figured I'll mention it again on it's own and see what everyone
> thinks.
>
> I would find it very useful if csound had a switch/case statement
> similar to c/c++/php or even bash style if it's easier to handle the
> parsing that way.
>
> The idea is to save some typing on large if/elseif statements.  While
> I assume most on this list would be familiar with the concept, just in
> case anyone isn't here's a pseudo-code example modeled after bash.
>
> ; block of code using if/elseif/else
> if (kval==1)
>   do something
> elseif (kval==2)
>   do something else
> elseif (kval==3)
>   one other thing to do
> else
>   a fall through if nothing else was met
> endif
>
> ; block of code using case
> case kval in
>   1) do something ;;
>   2) do something else ;;
>   3) one other thing to do ;;
>   *) a fall through if nothing was met ;;
> esac
>
> Csound mailing list
> Csound@listserv.heanet.ie
> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
> Send bugs reports to
>         https://github.com/csound/csound/issues
> Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2015-10-22 03:29
FromKevin Welsh
SubjectRe: [Csound] idea: switch/case statements
Hey Steven, thanks for the reply.

You're right, I had submitted that! I remembered mentioning it in a
thread way back then, but for some reason I didn't remember any
discussion about it after that.  I'm surprised I had opened a git
suggestion for it.

A lot of the syntax decisions I figured I would leave up to you
guys... I don't know a lot about the parser or what goes on behind the
scenes.  But here are my thoughts as best as I can answer your
questions.

I think it'd be most useful if this were available for all variable
rates, working similar in that fashion to if statements.  I don't see
a reason it couldn't be performed on other variable types such as
strings.  My opinion is that allowing comparisons like in your example
adds flexibility, but at the expense of actually saving any typing.
That looks more similar to an if statement.  I would suggest a
structure like this, with an example at the end using an if statement
for comparing:

case myVar in
  1) ..do something here..
    break
  1.5) ..do something else here..
    break
  2) ..do another thing here..
    break
  *) if (myVar>=3) then
       ..do last stuff here..
      else
        ..do stuff for when var is <=0 here..
      endif
      break
esac

The main idea (in my opinion) would be to offer a simpler method to
nested statement looking for an equal comparison, I write an awful lot
of
if (myVar==1) then
  ..do something..
elseif (myVar==2) then
  ..do something else..
etc and it seems there should be a more elegant way to write this.

I'm open to suggestions as to how it would work tho, especially from
those that know the code making it all happen.

On Wed, Oct 21, 2015 at 4:59 PM, Steven Yi  wrote:
> Hi Kevin,
>
> I think you (tgrey1?) filed an issue for this:
>
> https://github.com/csound/csound/issues/380
>
> I think we would need to continue to develop the idea further before
> implementing.  Some things that would help:
>
> 1. Example syntax from various proposals: should we use switch, case,
> etc. as the keyword?  do we add a break keyword?
>
> 2. Types: what types of variables do we allow switching on?  Do we
> limit this to numbers, or open it up to all types?
>
> 3. Should the cases compare to constants, or should we allow
> predicates as well?  i.e.
>
> case myVar of
>   (myVar > 3)  then
>      ...code...
>   (myVar > 2)  then
>      ...code...
>    else
>    ...code...
> end
>
> (I'm not sure the above saves any typing, and probably adds more lines
> of code...)
>
> The tricky part about case/esac is that there is a delimiter between
> statements, but in Csound there is only newlines. We'd have to figure
> out details around this, or limit to only one statement per case line,
> or etc.  (Ruby seems to use a then keyword when placing a command on
> the same line as a when, when using case [1]).
>
> steven
>
> [1] - http://ruby.about.com/od/control/a/The-Case-Statement.htm
>
>
> On Sat, Oct 17, 2015 at 6:19 AM, Kevin Welsh  wrote:
>> I think I brought this idea up once before in somebody else's thread,
>> but being off topic I think it got lost and forgotten as crosstalk...
>> so I figured I'll mention it again on it's own and see what everyone
>> thinks.
>>
>> I would find it very useful if csound had a switch/case statement
>> similar to c/c++/php or even bash style if it's easier to handle the
>> parsing that way.
>>
>> The idea is to save some typing on large if/elseif statements.  While
>> I assume most on this list would be familiar with the concept, just in
>> case anyone isn't here's a pseudo-code example modeled after bash.
>>
>> ; block of code using if/elseif/else
>> if (kval==1)
>>   do something
>> elseif (kval==2)
>>   do something else
>> elseif (kval==3)
>>   one other thing to do
>> else
>>   a fall through if nothing else was met
>> endif
>>
>> ; block of code using case
>> case kval in
>>   1) do something ;;
>>   2) do something else ;;
>>   3) one other thing to do ;;
>>   *) a fall through if nothing was met ;;
>> esac
>>
>> Csound mailing list
>> Csound@listserv.heanet.ie
>> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
>> Send bugs reports to
>>         https://github.com/csound/csound/issues
>> Discussions of bugs and features can be posted here
>
> Csound mailing list
> Csound@listserv.heanet.ie
> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
> Send bugs reports to
>         https://github.com/csound/csound/issues
> Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here