Page 2 of 3

Re: Extend Bool() as a ternary operator?

Posted: Thu Nov 10, 2016 6:43 am
by TI-994A
Mistrel wrote:I would rather see support for the ternary operator.
+1

Would really be cool for PureBasic to implement the ternary operator. :idea:

Re: Extend Bool() as a ternary operator?

Posted: Wed Nov 30, 2016 10:29 am
by Niffo
+1

Re: Extend Bool() as a ternary operator?

Posted: Wed Nov 30, 2016 11:11 am
by #NULL
+1
but not as Bool(). it should work with any type as already mentioned like strings ..(n>0, "pieces", "piece") for example.

Re: Extend Bool() as a ternary operator?

Posted: Thu Feb 09, 2017 2:37 am
by Damion12
I was just looking for an iif() solution.
so

+1

---
after reading it some of the posts, I think it's worth noting that it doesn't make sense to re-use bool(); instead, if makes sense to implement iif() and re-use it.
since Bool( this-or-that ) would be the same as iif( this-or-that,1,0) and could easily be a 'built in' macro to make it seamless....
Just my thoughts.

Re: Extend Bool() as a ternary operator?

Posted: Thu Feb 09, 2017 4:20 pm
by Thunder93
+1 :wink:

Board index ‹ PureBasic ‹ Feature Requests and Wishlists: ternary if or iif
Lunasole wrote:Well, the IIF() is absolutely required as it can be seen from many posts about it. It is simple nice thing w/o noticeable side-effects and I wonder why not to add it not as operator, but as built-in function to some library.

This function is not as widely used as other logical, the little overhead produced by such function call looks definitely better than code trashed with a temp variables and lot of IF/ENDIF.

Code: Select all

Define S$ = IIF (expression, A, B) ; assuming IIF is implemented for return arguments of every base type. the compiler selects which implementation to use basing on type of A variable here (is returned if expression = true), B has to be the same type

Define S2$ = IIF( Bool(IIF (expression, 100, 200)  = 100), "100", "something else") ; something nested

MessageRequester (IIF (expression, "str1", "str2"), "")

Re: Extend Bool() as a ternary operator?

Posted: Wed Apr 29, 2020 4:34 pm
by Blue
+1 for iif()

Great suggestion, Justin ! (... but formulated as written by Kenmo)

Would be very useful, indeed; it’s no biggie, but it certainly would make coding in PB somewhat speedier and much cleaner.

Neatly explained by Danilo in his 5:13 post on the previous page:
Danilo wrote: Just a shortcut to If..Else..EndIf, with the additional advantage that it can be used as an expression.
yes sir !

Re: Extend Bool() as a ternary operator?

Posted: Wed Apr 29, 2020 4:43 pm
by Danilo
You could also allow If-Expressions, maybe it is more „Pure BASIC“ syntax.

Code: Select all

var.i = If x=2 { 12 } Else { 14 }

If-Expressions could be used everywhere where Expressions are allowed.

Code: Select all

TheFunction( 13, 14, If a=0 {„Yes“} Else {„No“} )

Re: Extend Bool() as a ternary operator?

Posted: Wed Apr 29, 2020 5:29 pm
by NicTheQuick
What about some Python syntax :lol:

Code: Select all

text.s = "pieces" If n > 1 Else "piece"
It's a bit strange to look at but helps separating the parts better without brackets or something similar.

Re: Extend Bool() as a ternary operator?

Posted: Wed Apr 29, 2020 5:37 pm
by Little John
+1 for

IIf(expression, trueValue, falseValue)

Re: Extend Bool() as a ternary operator?

Posted: Fri Feb 04, 2022 10:12 pm
by mdp
Agreed - as I was just checking if it was added.

Nonetheless, since sometimes it can take a few moments before realizing that some features are easily obtainable on the user's side:

Code: Select all

Macro TernS(expression,ifTrue,ifFalse)
	TernaryS(Bool(expression),ifTrue,ifFalse)
EndMacro

Procedure.s TernaryS(condition, t.s, f.s)
	If condition : ProcedureReturn t : Else : ProcedureReturn f : EndIf
EndProcedure


Debug "Ternary operator: "+TernS(a>b,"T","F")
...Just your usual template material.

Re: Extend Bool() as a ternary operator?

Posted: Sun Feb 06, 2022 5:35 am
by idle
c backend

Code: Select all

Macro IIF(res,cond,true,false) 
  !v_#res = cond ? true : false;
EndMacro    

a = 6 
b.s = "hello" 
c.s = "byebye"
r.s = ""  

a = 6 
iif(r, v_a > 5, v_b ,v_c) 
Debug r 
a-1
iif(r, v_a > 5, v_b ,v_c) 
Debug r 


Re: Extend Bool() as a ternary operator?

Posted: Sun Feb 06, 2022 6:02 am
by juergenkulow
@idle
you have a sideeffect, v_r=v_c; do not copy the string in v_c.

Code: Select all

Macro IIF(res,cond,true,false) 
  !v_#res = cond ? true : false;
EndMacro    

a = 6 
b.s = "hello" 
c.s = "byebye"
r.s = ""  

a = 6 
iif(r, v_a > 5, v_b ,v_c) 
Debug r 
a-1
iif(r, v_a > 5, v_b ,v_c) 
c="Ätsch"
Debug r 
; hello
; Ätsch

Re: Extend Bool() as a ternary operator?

Posted: Sun Feb 06, 2022 6:23 am
by idle
It's only copying the pointer what do you expect?

Re: Extend Bool() as a ternary operator?

Posted: Sun Feb 06, 2022 12:44 pm
by mdp
idle wrote: Sun Feb 06, 2022 5:35 am c backend
Issue for some will be, one of the value points of PureBasic is to be lean and quick - like a calculator, you want its results to be prompt.

So, a C backend is a nice thing to have, but the FASM one remaining unvaluable, some would not bind their code to the "less daily" backend. By the way, did someone compute the overhead ratio between the FASM and C backends?

Re: Extend Bool() as a ternary operator?

Posted: Tue Feb 08, 2022 12:53 am
by juergenkulow

Code: Select all

; iff condition in calc
Define q.q,d.d,f.f,l.l,s.s, a=1
! __asm__("NOP"); 
! v_q=(v_a==1 ? 4711LL : 42LL);
! __asm__("NOP"); 
! v_d=(v_a==1 ? 47.11  : 42.0);
! __asm__("NOP"); 
! v_f=(v_a==1 ? 47.11  : 42.0); 
! __asm__("NOP"); 
! v_l=(v_a==1 ? 47L    : 42L );
! __asm__("NOP"); 
;!v_s=(v_a==1 ? "4711" : "42");
If a=1 : s="4711" : Else : s="42" : EndIf 
! __asm__("NOP"); 
SetClipboardText(Str(q)+" "+StrD(d)+" "+StrF(f)+" "+Str(l)+" "+s)
; 4711 47.11 47.1100006104 47 4711
! __asm__("int3"); 
; 

;X64 
; 00000001400010FE | 90                       | nop                                                           |
; 00000001400010FF | 48:8B05 62440000         | mov rax,qword ptr ds:[140005568]                              |
; 0000000140001106 | 48:83F8 01               | cmp rax,1                                                     |
; 000000014000110A | 75 07                    | jne iff.140001113                                             |
; 000000014000110C | B8 67120000              | mov eax,1267                                                  |
; 0000000140001111 | EB 05                    | jmp iff.140001118                                             |
; 0000000140001113 | B8 2A000000              | mov eax,2A                                                    | 2A:'*'
; 0000000140001118 | 48:8905 61440000         | mov qword ptr ds:[140005580],rax                              |
; 000000014000111F | 90                       | nop                                                           |
; 0000000140001120 | 48:8B05 41440000         | mov rax,qword ptr ds:[140005568]                              |
; 0000000140001127 | 48:83F8 01               | cmp rax,1                                                     |
; 000000014000112B | 75 0A                    | jne iff.140001137                                             |
; 000000014000112D | F2:0F1005 43200000       | movsd xmm0,qword ptr ds:[140003178]                           |
; 0000000140001135 | EB 08                    | jmp iff.14000113F                                             |
; 0000000140001137 | F2:0F1005 41200000       | movsd xmm0,qword ptr ds:[140003180]                           |
; 000000014000113F | F2:0F1105 29440000       | movsd qword ptr ds:[140005570],xmm0                           |
; 0000000140001147 | 90                       | nop                                                           |
; 0000000140001148 | 48:8B05 19440000         | mov rax,qword ptr ds:[140005568]                              |
; 000000014000114F | 48:83F8 01               | cmp rax,1                                                     |
; 0000000140001153 | 75 0A                    | jne iff.14000115F                                             |
; 0000000140001155 | F3:0F1005 2B200000       | movss xmm0,dword ptr ds:[140003188]                           | 0000000140003188:"¤p<B"
; 000000014000115D | EB 08                    | jmp iff.140001167                                             |
; 000000014000115F | F3:0F1005 25200000       | movss xmm0,dword ptr ds:[14000318C]                           |
; 0000000140001167 | F3:0F1105 09440000       | movss dword ptr ds:[140005578],xmm0                           | 0000000140005578:"¤p<B/"
; 000000014000116F | 90                       | nop                                                           |
; 0000000140001170 | 48:8B05 F1430000         | mov rax,qword ptr ds:[140005568]                              |
; 0000000140001177 | 48:83F8 01               | cmp rax,1                                                     |
; 000000014000117B | 75 07                    | jne iff.140001184                                             |
; 000000014000117D | B8 2F000000              | mov eax,2F                                                    | 2F:'/'
; 0000000140001182 | EB 05                    | jmp iff.140001189                                             |
; 0000000140001184 | B8 2A000000              | mov eax,2A                                                    | 2A:'*'
; 0000000140001189 | 8905 ED430000            | mov dword ptr ds:[14000557C],eax                              |
; 000000014000118F | 90                       | nop                                                           |
; 0000000140001190 | 48:8B05 D1430000         | mov rax,qword ptr ds:[140005568]                              |
; 0000000140001197 | 48:83F8 01               | cmp rax,1                                                     |
; 000000014000119B | 75 15                    | jne iff.1400011B2                                             |
; 000000014000119D | 48:8D15 7C3E0000         | lea rdx,qword ptr ds:[140005020]                              | 0000000140005020:L"4711"
; 00000001400011A4 | 48:8D0D DD430000         | lea rcx,qword ptr ds:[140005588]                              | 0000000140005588:&L"4711"
; 00000001400011AB | E8 100B0000              | call iff.140001CC0                                            |
; 00000001400011B0 | EB 14                    | jmp iff.1400011C6                                             |
; 00000001400011B2 | 90                       | nop                                                           |
; 00000001400011B3 | 48:8D15 5E3E0000         | lea rdx,qword ptr ds:[140005018]                              |
; 00000001400011BA | 48:8D0D C7430000         | lea rcx,qword ptr ds:[140005588]                              | 0000000140005588:&L"4711"
; 00000001400011C1 | E8 FA0A0000              | call iff.140001CC0                                            |
; 00000001400011C6 | 90                       | nop                                                           |

; // If a=1 : s="4711" : Else : s="42" : EndIf 
; If (!((v_a==1))) { Goto no2; }
; SYS_FastAllocateStringFree4(&v_s,_S1);
; Goto endif1;
; no2:;
; SYS_FastAllocateStringFree4(&v_s,_S2);
; endif1:                              ;


; 	movq	v_a(%rip), %rax
; 	cmpq	$1, %rax
; 	jne	.L17
; 	movl	$4711, %eax
; 	jmp	.L18
; .L17:
; 	movl	$42, %eax
; .L18:
; 	movq	%rax, v_q(%rip)
; /APP
;  # 121 "purebasic.c" 1
; 	NOP
;  # 0 "" 2
; /NO_APP
; 	movq	v_a(%rip), %rax
; 	cmpq	$1, %rax
; 	jne	.L19
; 	movsd	.LC2(%rip), %xmm0
; 	jmp	.L20
; .L19:
; 	movsd	.LC3(%rip), %xmm0
; .L20:
; 	movsd	%xmm0, v_d(%rip)
; /APP
;  # 125 "purebasic.c" 1
; 	NOP
;  # 0 "" 2
; /NO_APP
; 	movq	v_a(%rip), %rax
; 	cmpq	$1, %rax
; 	jne	.L21
; 	movss	.LC4(%rip), %xmm0
; 	jmp	.L22
; .L21:
; 	movss	.LC5(%rip), %xmm0
; .L22:
; 	movss	%xmm0, v_f(%rip)
; /APP
;  # 129 "purebasic.c" 1
; 	NOP
;  # 0 "" 2
; /NO_APP
; 	movq	v_a(%rip), %rax
; 	cmpq	$1, %rax
; 	jne	.L23
; 	movl	$47, %eax
; 	jmp	.L24
; .L23:
; 	movl	$42, %eax
; .L24:
; 	movl	%eax, v_l(%rip)
; /APP
;  # 133 "purebasic.c" 1
; 	NOP
;  # 0 "" 2
; /NO_APP
; 	movq	v_a(%rip), %rax
; 	cmpq	$1, %rax
; 	jne	.L29
; 	leaq	_S1(%rip), %rax
; 	movq	%rax, %rdx
; 	leaq	v_s(%rip), %rax
; 	movq	%rax, %rcx
; 	call	SYS_FastAllocateStringFree4
; 	jmp	.L27
; .L29:
; 	nop
; .L26:
; 	leaq	_S2(%rip), %rax
; 	movq	%rax, %rdx
; 	leaq	v_s(%rip), %rax
; 	movq	%rax, %rcx
; 	call	SYS_FastAllocateStringFree4
; .L27:

;X86 
; 0040108F | 90                       | nop                                     |
; 00401090 | 833D 30444000 01         | cmp dword ptr ds:[404430],1             |
; 00401097 | 0F84 A3010000            | je purebasic.401240                     |
; 0040109D | B8 2A000000              | mov eax,2A                              | 2A:'*'
; 004010A2 | 31D2                     | XOr edx,edx                             | edx:EntryPoint
; 004010A4 | A3 18444000              | mov dword ptr ds:[404418],eax           |
; 004010A9 | 8915 1C444000            | mov dword ptr ds:[40441C],edx           | edx:EntryPoint
; 004010AF | 90                       | nop                                     |
; 004010B0 | 833D 30444000 01         | cmp dword ptr ds:[404430],1             |
; 004010B7 | 0F84 93010000            | je purebasic.401250                     |
; 004010BD | D905 08204000            | fld st(0),dword ptr ds:[402008]         |
; 004010C3 | DD1D 28444000            | fstp qword ptr ds:[404428],st(0)        |
; 004010C9 | 90                       | nop                                     |
; 004010CA | 833D 30444000 01         | cmp dword ptr ds:[404430],1             |
; 004010D1 | A1 08204000              | mov eax,dword ptr ds:[402008]           |
; 004010D6 | 0F4405 0C204000          | cmove eax,dword ptr ds:[40200C]         |
; 004010DD | A3 24444000              | mov dword ptr ds:[404424],eax           |
; 004010E2 | 90                       | nop                                     |
; 004010E3 | 31C0                     | XOr eax,eax                             |
; 004010E5 | 833D 30444000 01         | cmp dword ptr ds:[404430],1             |
; 004010EC | 0F94C0                   | sete al                                 |
; 004010EF | 8D4480 2A                | lea eax,dword ptr ds:[eax+eax*4+2A]     |
; 004010F3 | A3 20444000              | mov dword ptr ds:[404420],eax           |
; 004010F8 | 90                       | nop                                     |
; 004010F9 | 833D 30444000 01         | cmp dword ptr ds:[404430],1             |
; 00401100 | 0F84 5A010000            | je purebasic.401260                     |
; 00401106 | C74424 04 10404000       | mov dword ptr ss:[esp+4],purebasic.4040 |
; 0040110E | C70424 14444000          | mov dword ptr ss:[esp],purebasic.404414 |
; 00401115 | E8 A60A0000              | call purebasic.401BC0                   |
; 0040111A | 83EC 08                  | sub esp,8                               |
; 0040111D | 90                       | nop                                     |

; /NO_APP
; 	movl	_v_a, %eax
; 	cmpl	$1, %eax
; 	jne	L17
; 	movl	$4711, %eax
; 	movl	$0, %edx
; 	jmp	L18
; L17:
; 	movl	$42, %eax
; 	movl	$0, %edx
; L18:
; 	movl	%eax, _v_q
; 	movl	%edx, _v_q+4
; /APP
;  # 122 "purebasic.c" 1
; 	NOP
;  # 0 "" 2
; /NO_APP
; 	movl	_v_a, %eax
; 	cmpl	$1, %eax
; 	jne	L19
; 	fldl	LC3
; 	jmp	L20
; L19:
; 	fldl	LC4
; L20:
; 	fstpl	_v_d
; /APP
;  # 126 "purebasic.c" 1
; 	NOP
;  # 0 "" 2
; /NO_APP
; 	movl	_v_a, %eax
; 	cmpl	$1, %eax
; 	jne	L21
; 	flds	LC5
; 	jmp	L22
; L21:
; 	flds	LC6
; L22:
; 	fstps	_v_f
; /APP
;  # 130 "purebasic.c" 1
; 	NOP
;  # 0 "" 2
; /NO_APP
; 	movl	_v_a, %eax
; 	cmpl	$1, %eax
; 	jne	L23
; 	movl	$47, %eax
; 	jmp	L24
; L23:
; 	movl	$42, %eax
; L24:
; 	movl	%eax, _v_l
; /APP
;  # 134 "purebasic.c" 1
; 	NOP
;  # 0 "" 2
; /NO_APP
; 	movl	_v_a, %eax
; 	cmpl	$1, %eax
; 	jne	L29
; 	movl	$__S1, 4(%esp)
; 	movl	$_v_s, (%esp)
; 	call	_SYS_FastAllocateStringFree4@8
; 	subl	$8, %esp
; 	jmp	L27
; L29:
; 	nop
; L26:
; 	movl	$__S2, 4(%esp)
; 	movl	$_v_s, (%esp)
; 	call	_SYS_FastAllocateStringFree4@8
; 	subl	$8, %esp
; L27:
Who will take the next step? Macro iffX(cond,t,f) : ! asm goto (... : EndMacro