olety / cjass

Preserving cjass code from code.google.com/p/cjass
0 stars 0 forks source link

Ternar operator #2

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
C-like ternar operator:

a = condition ? 1 : 2

// --->

if condition then
    a = 1
else
    a = 2
endif

vJass also use ":" symbol in colon syntax extension, so be careful.

Original issue reported on code.google.com by adi...@gmail.com on 6 Aug 2011 at 10:08

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
The main idea is:

statement(c?t:f) 
->
if(c)then
  statement(t)
else
  statement(f)
endif

But there are some problems:
First is the need of condition check before statement begin even if it is not 
needed logically.
Second is doubling statement in code. It is not much except "if" statement. 
Temp boolean var is way around it.
Third is "elseif" conditions. Bad way is calculating before "if". Good way is 
cut "elseif" to "else if".

Can anyone see other problems?

Original comment by sbratchi...@gmail.com on 7 Aug 2011 at 12:12

GoogleCodeExporter commented 9 years ago
No-no, it should work like:

int max = a>b?a:b

It's an expression that returns a value...

In example above a and b are also expressions, not statements, they cannot be 
'set' or 'call' statements for example. But they can be functions, variables, 
literals.

Original comment by evgkoc...@gmail.com on 7 Aug 2011 at 6:23

GoogleCodeExporter commented 9 years ago
Ofcourse it must use any expression.

http://ru.wikipedia.org/wiki/%D0%A2%D0%B5%D1%80%D0%BD%D0%B0%D1%80%D0%BD%D0%B0%D1
%8F_%D1%83%D1%81%D0%BB%D0%BE%D0%B2%D0%BD%D0%B0%D1%8F_%D0%BE%D0%BF%D0%B5%D1%80%D0
%B0%D1%86%D0%B8%D1%8F

http://en.wikipedia.org/wiki/%3F:

a = foo () ? 1 : 0 + bar () 2 : 3

cJass do not process expression type=/ So it can't generate temp local.
It my be parsed to:

if (foo()) { 
    if (bar()) {
        a = 1 + 2
    } else {
        a = 1 + 3
    }
} else {
    if (bar()) {
        a = 0 + 2
    } else {
        a = 0 + 3
    }
}

It is bad?

Original comment by adi...@gmail.com on 7 Aug 2011 at 7:42

GoogleCodeExporter commented 9 years ago
> cJass do not process expression type
Condition can be boolean only, so no problems with temp local variable:

if( ((a>b)?(a):(b))<5 ){
    //not enough high
}elseif( ((a<b)?(a):(b))>0 ){
    //not enough low
}else{
    //ok
}
->
if(a>b){
    if( (a)<5 ){
        //not enough high
    }else{
        if( (a<b) ){
            if( (a)>0 ){
                //not enough low
            }else{
                //ok
            }
        }else{
            if( (b)>0 ){
                //not enough low
            }else{
                //ok
            }
        }
    }
}else{
    if( (b)<5 ){
        //not enough high
    }else{
        if( (a<b) ){
            if( (a)>0 ){
                //not enough low
            }else{
                //ok
            }
        }else{
            if( (b)>0 ){
                //not enough low
            }else{
                //ok
            }
        }
    }
}

or with one local:

local boolean c
if(a>b){
    c=(a)<5
}else{
    c=(b)<5
}
if( c ){
    //not enough high
}else{
    if(a<b){
        c=(a)>0
    }else{
        c=(b)>0
    }
    if( c ){
        //not enough low
    }else{
        //ok
    }
}

Original comment by sbratchi...@gmail.com on 8 Aug 2011 at 2:07

GoogleCodeExporter commented 9 years ago

Original comment by adi...@gmail.com on 9 Aug 2011 at 7:46

GoogleCodeExporter commented 9 years ago

Original comment by adi...@gmail.com on 16 Aug 2011 at 5:31

GoogleCodeExporter commented 9 years ago
Nice! This feature will shorten quite a few of my triggers. Thanks for working!

Original comment by evgkoc...@gmail.com on 20 Aug 2011 at 11:22

GoogleCodeExporter commented 9 years ago
> It is bad?
I don't think so, if it works correctly it should be fine.

I only need the basic use of ternary operator, in assignment operator:

string name = whichGoal == LEFT_GOAL ? udg_LeftTeamName : udg_RightTeamName

Can you explain the problem that you can't analyze expressoins? =(

Original comment by evgkoc...@gmail.com on 24 Aug 2011 at 5:26

GoogleCodeExporter commented 9 years ago
udg_ ? are you serious?

Original comment by erwgfifo...@gmail.com on 24 Aug 2011 at 2:31