Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD Beta Forum
Rainchild
Wizard


Joined: 10 Oct 2000
Posts: 1551
Location: Australia

PostPosted: Mon Apr 10, 2006 10:40 pm   

While looking at the scripts, can we get a #SWITCH/CASE func
 
Syntax would need to be something like:

Code:
#SWITCH @blah
{
  #CASE 1 { do something }
  #CASE 2 { do something else }
  #CASE 3 { or maybe do this }
  #CASE ELSE { otherwise do this }
}


Mostly because the whole nested if's gets a bit confusing.
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Mon Apr 10, 2006 11:01 pm   
 
Here is what I just posted over in the zMUDXP Wishlist thread:

-----
Actually, your wishes are answered!

In the first beta of CMUD there is a new command and function called SWITCH:

#SWITCH (expr1) {commands1} (expr2) {commands2} ...

and the function:

%switch(expr1,string1,expr2,string2,...)

With the #SWITCH command, CMUD tests each expression and when it finds an expression that is true, it executes the commands following it and then skips the rest of the command. With the %switch function, CMUD tests each expression and when it finds an expression that is true, it returns the string value following it and skips the rest of the function.
-----

It doesn't have an "else" clause...that would require some tricky syntax. Neither zMUD nor CMUD are set up to parse the type of syntax that you suggested RainChild. But even without an "else" clause, I think it will still be useful.


Last edited by Zugg on Wed Apr 12, 2006 1:22 am; edited 1 time in total
Reply with quote
Guinn
Wizard


Joined: 03 Mar 2001
Posts: 1127
Location: London

PostPosted: Tue Apr 11, 2006 12:19 am   
 
Surely we could make up an else...

#SWITCH (expr1) {commands1} (expr2) {commands2} (true) {my ELSE equiv}

or it true doesn't work then the 'else' expression could just be (1=1) or something else trivial?
_________________
CMUD Pro, Windows Vista x64
Core2 Q6600, 4GB RAM, GeForce 8800GT
Because you need it for text... ;)
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Tue Apr 11, 2006 12:49 am   
 
Quote:

It doesn't have an "else" clause...that would require some tricky syntax.


You're the one looking at all the Delphi code, but based on how you described the setup couldn't you just include a test for "else" when evaluating the expression at hand? For example (pseudocode):

Code:

for each expression in argslist
   if (expression = "else") = true then
      run else codeblock
   else
      if (expression) = true then run expression codeblock
   end if
next expression
_________________
EDIT: I didn't like my old signature
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Tue Apr 11, 2006 12:53 am   
 
Actually since once it finds one it does it then skips the rest this would work:
#SWITCH (@a=1) {a commands} (@b=1) {b commands} (1) {else commands, since 1 is always true and the rest weren't}

Now if the parser would just be happy with me putting line breaks in there where I want I could format it all up so I am happy.
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Tue Apr 11, 2006 4:51 pm   
 
Well, that's true...don't know why I didn't think about that. So of course it would be easy to handle an "else" case by just doing a simple string match of the expression. And yes, using "true" should also work.

Vijilante, the new compiler also allows line breaks in more places. So, in this case you could do this:
Code:
#SWITCH
  (@a=1) {a commands}
  (@b=1) {b commands}
  (else) {else commands}

The rule is that the continuation lines must have space at the beginning, and the first non-space character must be a ( or { character.
Reply with quote
Tarn
GURU


Joined: 10 Oct 2000
Posts: 873
Location: USA

PostPosted: Tue Apr 11, 2006 9:15 pm   
 
Could it be called something like "IFELSE" or "MIF"(multiple if)?

SWITCH would be confusing to C/C++ people.

For anyone who doesn't know, in C/C++ and similar languages, the structure looks like:

switch (expression) {
case VAL1:
statements to execute
case VAL2:
statements to execute
default:
statements to execute
}

The program evaluates expression once, and then looks through the list of VAL1, VAL2, etc. to find the value of expression. (It doesn't evaluate a series of expressions looking for one that is true). I'm deliberately leaving out the part about breaks.

-Tarn
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Tue Apr 11, 2006 10:48 pm   
 
Well SWITCH in C/C++ is the same as CASE in zMUD and Delphi. I choose SWITCH because the other people who started this thread suggested it. I don't like the "IFELSE" or "MIF" either. And it's still very close to what SWITCH does. C/C++ still evaluates each of the case values (VAL1,VAL2,etc). It just compares the result to the original expression itself.

Maybe someone who knows about other languages can suggest something that is already used somewhere else. Seems like I remember Perl having something like this, but maybe I'm wrong.

Even though it's different than the C/C++ version, at least it's similar in concept and people who understand Switch in C/C++ should know enough about programming not to be confused very much by this. It's easier to remember something similar in concept than it is to remember a completely new made-up term I think.
Reply with quote
Rainchild
Wizard


Joined: 10 Oct 2000
Posts: 1551
Location: Australia

PostPosted: Tue Apr 11, 2006 11:03 pm   
 
I'm a C/C++/C# programmer and I have no issue with the syntax Zugg has got, although I would prefer it to be in { }... eg

Code:
#SWITCH
{
  (@a=1) {a commands}
  (@b=1) {b commands}
  (else) {else commands}
}


Mostly because that logically does the multiple line thing etc.

Well, I mean I would prefer it to be entirely C-like, but it's zScript, not cScript ;)

Btw Visual Basic uses 'Select Case' but I still prefer switch.
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Tue Apr 11, 2006 11:16 pm   
 
Rainchild: Yeah, I'd like the extra {} also, but that requires some significant kludges to the parser. The parser would treat everything inside of the outer {} as a single argument to the #SWITCH command. And since the syntax inside the outer {} violates the normal command syntax, it would give errors.

One thing that's important to know about the CMUD parser/compiler (which is similar to the syntax checker in zMUD) is that the parser doesn't actually know details about the commands or functions. If you have ever run the CMDEDIT.EXE program that comes with the Developer's Kit, you'll know that the command list in zMUD is really a database. Each command and function has a name, and then each parameter is given a description and type. It's the parameter "type" that determines how the command or function is parsed.

For example, the first parameter of #TRIGGER is set as a "Pattern" type. The first parameter of #IF is set as an "Expression" type, etc.

So, in order to implement the above syntax, I'd have to create a new parameter type just for the #SWITCH command and add it's own section to the yacc parser syntax definition. And I'm not sure it's a good idea to start giving commands non-standard syntax.

Stuff like the custom syntax used for #YESNO and #PICK (for overriding the item titles, and the i:0 stuff) is already driving me crazy. My desire with CMUD is to move towards a more "regular" and standarized syntax and remove "wierdnesses" rather than adding new ones.
Reply with quote
edb6377
Magician


Joined: 29 Nov 2005
Posts: 482

PostPosted: Tue Apr 11, 2006 11:35 pm   
 
Quote:
#SWITCH @blah
{
#CASE 1 { do something }
#CASE 2 { do something else }
#CASE 3 { or maybe do this }
#CASE ELSE { otherwise do this }
}


This is one of the features i am most excited about. Nested #IF statements are probably my most despised situation. I have had to rework many scripts into serious conditionals because i cant stand the nested IFs.

Bravo on this. I agree though we need to have a DEFAULT or ELSE situation. I usually use this part to do SYNTAX ERRORS or a list of valid commands for aliases etc.

Question?
Quote:

#SWITCH (expr1) {commands1} (expr2) {commands2} ...

and the function:

%function(expr1,string1,expr2,string2,...)


Any reason the new function isnt %switch etc.
Reply with quote
Rainchild
Wizard


Joined: 10 Oct 2000
Posts: 1551
Location: Australia

PostPosted: Wed Apr 12, 2006 12:30 am   
 
Probably just a typo, I imagine, I didn't even pick up on that :)
Reply with quote
Taz
GURU


Joined: 28 Sep 2000
Posts: 1395
Location: United Kingdom

PostPosted: Wed Apr 12, 2006 1:10 am   
 
*lol*

I did notice it and in the zMUDXP Wishlist thread I edited it to replace it with switch.

I was unable to do so in this forum, no CMUD gurus yet Wink
_________________
Taz :)
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Wed Apr 12, 2006 1:23 am   
 
Fixed the typo here too. And I'm off to change the forum permissions for Gurus Razz
Reply with quote
Seb
Wizard


Joined: 14 Aug 2004
Posts: 1269

PostPosted: Wed Apr 12, 2006 2:46 pm   
 
Personally I never liked the #CASE command in zMUD because it didn't do what I was expecting (and I couldn't find an obvious practical use for it). This command would be really useful though. I have lots of #IF statements in some of my code, but I don't bother nesting them because I know the same value can't take two different values. However, this makes for inefficient code as we do needless #IF comparisons most of the time. I don't suppose there is any chance of changing #CASE, but FYI T-SQL implements CASE in one of two ways:

Code:
In T-SQL...

Simple CASE function:

CASE input_expression
    WHEN when_expression THEN result_expression
        [ ...n ]
    [
        ELSE else_result_expression
    ]
END

Searched CASE function:

CASE
    WHEN Boolean_expression THEN result_expression
        [ ...n ]
    [
        ELSE else_result_expression
    ]
END

Obviously this is doesn't fit zScript at all, but I thought it might be of interest. It uses CASE at the beginning and then WHEN to differentiate the different cases.
So compared with C/C++: switch --> CASE; case --> WHEN; default --> ELSE.

In Python, and I think JavaScript, I believe one has to use "if" ... "elif" or "else if", which only gets checked if the first "if" or earlier "elif"s didn't evaluate true. The final "else" is, of course, the default.
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Wed Apr 12, 2006 5:18 pm   
 
Yes, I've always been aware that #CASE in zMUD didn't work like it did in most languages. You have to remember that the simple parser used early-on in zMUD couldn't handle any sort of complex syntax, and the CASE statement is usually the most complex statement in any language (as your examples demonstrate).

The main use I've seen for #CASE is to easily send out random responses:
Code:
#CASE %random(100) {Hello} {Hi there!} {Greetings}

which is why the #CASE command wraps around like it does. It was never intended to be a "real" CASE statement.

Existing commands, like #CASE, will not be changed. That breaks too much compatibility. CMUD adds new commands rather than changing old commands.
Reply with quote
Caled
Sorcerer


Joined: 21 Oct 2000
Posts: 821
Location: Australia

PostPosted: Tue Apr 25, 2006 2:27 am   
 
The best use I found for #CASE was..

var1=1
var2=2
var3=4
NB: The above vars are either the value I set them at just then OR zero. Triggers control this, and they are never anything but.

#MATH combined {@var1+@var2+@var3}
#CASE combined {alias1} {alias2} etc

The point being to do a unique action based on the unique combination of three flag variables, without the need to do a long nested if statement.

var1=1
var2=0
var3=4
combined=5


#SWITCH is very cool. I wont be restricted to only three flags in this design, and even with just the three it was one of the most useful and important components of my curing script for IRE muds. ThankyouX100.
_________________
Athlon 64 3200+
Win XP Pro x64
Reply with quote
slicertool
Magician


Joined: 09 Oct 2003
Posts: 459
Location: USA

PostPosted: Mon Oct 09, 2006 3:22 pm   
 
just bumping the switch thread until it gets added into the help files.
_________________
Ichthus on SWmud: http://www.swmud.org/
Reply with quote
edb6377
Magician


Joined: 29 Nov 2005
Posts: 482

PostPosted: Tue Oct 10, 2006 3:42 am   
 
i posted an example of #CASE
_________________
Confucious say "Bugs in Programs need Hammer"
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD Beta Forum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net