Extend your language: Difference between revisions

m (→‎{{header|Amazing Hopper}}: added a google translation)
 
(12 intermediate revisions by 7 users not shown)
Line 206:
 
=={{header|Amazing Hopper}}==
<p>HOPPER permite definir muchas extensiones del lenguaje a través de su potentísimo preprocesador. A continuación, veremos un par de ejemplos.</p>
<p>OBSERVACION: Se puede preguntar por opciones falsas, reemplazando "JNT" (Jump if Not True) por "JT" (Jump if True".</p>
 
Ejemplo #1:
Line 245 ⟶ 246:
#include <jambo.h>
 
#defn Sitodassonverdaderas(*) ##CODEIF,{1},#GENCODE $$$*$$$ opción#ITV=0;#ATCMLIST;\
#ATCMLIST;cpy(opción#ITV);and;#ENDGEN;,jnt(#ENDIF)
#synon Sitodassonverdaderas Sitodaslasopciones,Sitodasestas,Sitodas
#defn Sonverdaderas(*) jmp(%%CODEIF), %ENDIF:, {1}, #GENCODE $$$*$$$ \
#defn Sonverdaderas(*) jmp(%%CODEIF), %ENDIF:, {1}, #GENCODE $$$*$$$ #ATCMLIST;and;#ENDGEN;jnt(#ENDIF),\
and;#ENDGEN;jnt(#ENDIF),
#defn Esla(_X_) jmp(%%CODEIF), %ENDIF:, {_X_}, jnt(#ENDIF),
#defn Esverdaderalaopción(_X_) jmp(%%CODEIF), %ENDIF:, {opción_X_}, jnt(#ENDIF),
#defn Sonverdaderaslasopciones(*) jmp(%%CODEIF), %ENDIF:,{1};#GENCODE $$$*$$$ {opción#LIST};\
and;#ENDGEN;jnt(#ENDIF),
#synon Sonverdaderaslasopciones Sonlasopciones
#synon Else Noesningunaopción?
#synon EndIf FindelSi
#define verdadera? ;
#synon verdadera? verdaderas?,verdadera,verdaderas,sonverdaderas,esverdadera
 
Main
True(v), True(x), True(y)
Si todas son verdaderasestas 'v, {0.025} Is equal to (10), x, y' son verdaderas
Set ("Todas las opciones son verdaderas")
 
Line 265 ⟶ 271:
 
Son verdaderas las opciones '1,3,4'
Set ("Son verdaderas V, X e Y")
 
Son las opciones '1,3,4' verdaderas?
Set ("Son verdaderas V, X e Y")
 
Line 272 ⟶ 281:
/* podríamos seguir preguntando... */
 
Es verdadera la 'opción 4'2'
Set ("Al menos, la igualdad es verdadera")
 
Es la 'opción 4' verdadera?
Set ("Al menos, Y es verdadera")
 
Line 310 ⟶ 322:
=== google translation ===
<pre>
HOPPER allows you to define many language extensions through its very powerful preprocessor. Next, we will see a couple of examples.
 
NOTE: You can ask for false options, replacing "JNT" (Jump if Not True) with "JT" (Jump if True).
 
NOTE 2: These examples are executable in HOPPER, because HOPPER allows you to define "flavors" of programming languages ​​in any language (respecting the use of the character " ' " as a synonym for parentheses).
 
Example 1:
Line 316 ⟶ 332:
#include <jambo.h>
 
#defn If both are trueIfbotharetrue(_X_,_Y_) ##CODEIF,__firstop__=0;#ATOM#CMPLX;cpy(__firstop__),\
__secondop__=0;#ATOM#CMPLX;cpy(__secondop__);\
and;jnt(#ENDIF),
#defn IsonlythefirstoptionItisonlythefirstoption jmp(%%CODEIF), %ENDIF:, {__firstop__}, jnt(#ENDIF),
#defn IsonlythesecondoptionItisonlythesecondoption jmp(%%CODEIF), %ENDIF:, {__secondop__}, jnt(#ENDIF),
#synon Else Isn't there an option Isnotthereanoption?
#synon EndIf FindelSi
 
Main
False(v), True(w)
If both are true ( v, w )
printnlPrintnl("Both options are true")
 
It's is only the first option
printnlPrintnl("The first option is true")
 
It's is only the second option
printnlPrintnl("The second option is true")
 
It isIs not there an option?
Printnl ("Nothing is fulfilled")
endEnd of yesIf
 
End
 
Output:
 
The second option is true
 
Example #2:
 
The HOPPER preprocessor allows you to extend the "IF" (or "SI" isin Spanish) structure, to any number of logical expressions as an option:
 
#include <jambo.h>
 
#defn Ifallaretrue(*) ##CODEIF,{1},#GENCODE $$$*$$$ option#ITV=0;#ATCMLIST;\
#ATCMLIST;cpy(#ITV option);and;#ENDGEN;,jnt(#ENDIF)
#synon Ifallaretrue Ifalloptions, Ifallofthese, Ifall
#defn AreTrue(*) jmp(%%CODEIF), %ENDIF:, {1}, #GENCODE $$$*$$$ \
#defn Aretrue(*) jmp(%%CODEIF), %ENDIF:, {1}, #GENCODE $$$*$$$ #ATCMLIST;and;#ENDGEN;jnt(#ENDIF),\
#defn Esla(_X_) jmp(%%CODEIF), %ENDIF:, {_X_}, jnt(#ENDIF),
#defn Aretheoptionstrue(*) jmp(%%CODEIF), %ENDIF:,{1};#GENCODE $$$*$$$ {option#LIST};\
and;#ENDGEN;jnt(#ENDIF),
#synon Aretrue Theyaretrue
#synon Else Isn't there an option?
#defn Is(_X_) jmp(%%CODEIF), %ENDIF:, {_X_}, jnt(#ENDIF),
#synon EndIf FindelSi
#defn Option(_X_) jmp(%%CODEIF), %ENDIF:, {option_X_}, jnt(#ENDIF),
#defn Aretheoptions(*) jmp(%%CODEIF), %ENDIF:,{1};#GENCODE $$$*$$$ {option#LIST};\
and;#ENDGEN;jnt(#ENDIF),
#synon Aretheoptions Theoptions
#synon Else Isnotthereanoption?
#define true? ;
#synon true? true, aretrue?, aretrue, istrue
 
Main
True(v), True(x), True(y)
If all areof truethese 'v, {0.025} Is equal to (10), x, y' are true
Set("All options are true")
 
Line 368 ⟶ 391:
 
The options '1,3,4' are true
Set("V, X and Y are true")
 
Are the options '1,3,4' true?
Set("V, X and Y are true")
 
Line 375 ⟶ 401:
/* we could keep asking... */
 
It'sOption 'option 42' is true
Set("At least, equality is true")
 
Is 'option 4' true?
Set ("At least, Y is true")
 
It isIs not there an option?
Set ("Nothing is fulfilled")
 
endEnd of yesIf
prnlPrnl
End
 
Output:
 
V, X and Y are true
 
 
Example #3:
 
Line 396 ⟶ 429:
If ( var 'v' » 'first option', And ( w » 'second option' ) )
printnlPrintnl("Both options are true")
 
Else If ( first option )
printnlPrintnl("The first option is true")
 
Else If ( second option )
printnlPrintnl("The second option is true")
elseElse
printnlPrintnl ("None is true")
End If
End
 
Output:
 
The first option is true
</pre>
Line 1,689 ⟶ 1,724:
});
</syntaxhighlight>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq'''
 
'''With tweak, works with jaq'''
 
As is the case with C and some other languages with entries on this page, jq lacks the flexibility to meet the requirement that "The new conditional expression should look, nest and behave analogously to the language's built-in 'if' statement."
 
However, as with D, for example,
it is easy to define an `if3` that is semantically analogous to the built-in `if` statement because
function arguments can be evaluated lazily. Furthermore, in the case of jq, one can specify that
an argument should be evaluated eagerly, which turns out to be helpful in the present case as it makes short work of
avoiding the semantic error of double evaluation of conditionals that have side-effects.
 
Here, then, is a definition of `if3` that imposes no restrictions on its arguments in the same sense that the built-in `if` has none:
<syntaxhighlight lang=jq>
def if2($c1; $c2; both; first; second; neither):
if $c1 and $c2 then both
elif $c1 then first
elif $c2 then second
else neither
end;
</syntaxhighlight>
 
### jaq
Since jaq does not currently support $-variables in function headers,
the definition would have to be modified to:
<syntaxhighlight lang=jq>
def if2(c1; c2; both; first; second; neither):
c1 as $c1
| c2 as $c2
| if $c1 and $c2 then both
elif $c1 then first
elif $c2 then second
else neither
end;
</syntaxhighlight>
 
 
=={{header|Julia}}==
Line 1,933 ⟶ 2,007:
 
=={{header|langur}}==
Langur doesn't currently have macros, but the following will otherwise accomplish the task, using the fact that a givenswitch expression may accept multiple test expressions. No extension of the language is necessary. This is not limited to 2 conditions.
 
We could use the nxor (logical equivalence) operator to test truthiness.
 
<syntaxhighlight lang="langur">givenswitch[and] .x nxor, .y nxor {
case true: ... # both true
case true, false: ... # first true, second false
Line 1,946 ⟶ 2,020:
To test directly for Boolean values (not truthiness), we could use the default == comparison.
 
<syntaxhighlight lang="langur">givenswitch[and] .x, .y {
case true: ... # both true
case true, false: ... # first true, second false
case false, true: ... # first false, second true
case null, _: ... # first null, second irrelevant
default: ... # other
}</syntaxhighlight>
Line 3,250 ⟶ 3,324:
a = 0, b = 1 => second positive
a = 0, b = 0 => neither positive
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
In RPL, the IFTE function returns either the first or the second level of the stack, depending on whether the third level is false or true respectively. The extension of this concept to the one proposed by the task is quite simple, and allows a true quadruple branching, as shown in the demonstration.
≪ 4 ROLL SWAP 4 ROLLD 4 →LIST
SWAP SIGN 2 * ROT SIGN + 1 + GET ≫
‘IF2TE’ STO
 
1 1 "Both" "First" "Second" "None" IF2TE
0 1 "Both" "First" "Second" "None" IF2TE
{{out}}
<pre>
2: "Both"
1: "Second"
</pre>
===Quadruple branching example===
≪ → hour min
≪ min 30 MOD min 30 <
≪ min →STR " PAST " + hour →STR + ≫
≪ 60 min - →STR " TO " + hour 1 + →STR + ≫
≪ hour →STR " PRECISELY" + ≫
≪ "HALF PAST " hour →STR + ≫
IF2TE EVAL
'SAYTIME' STO
 
3 25 SAYTIME
1 55 SAYTIME
6 30 SAYTIME
7 0 SAYTIME
{{out}}
<pre>
4: "25 PAST 3"
3: "5 TO 2"
2: "HALF PAST 6"
1: "7 PRECISELY"
</pre>
 
Line 3,688 ⟶ 3,800:
{{trans|Kotlin}}
Like Kotlin Wren does not have macros but, using a combination of chained functions and methods, it's possible (if you squint a little) to create something which closely resembles a language extension.
<syntaxhighlight lang="ecmascriptwren">class IfBoth {
construct new(cond1, cond2) {
_cond1 = cond1
Line 3,915 ⟶ 4,027:
{{omit from|Commodore BASIC}}
{{omit from|Integer BASIC}}
{{omit from|Java}}
{{omit from|Lily}}
{{omit from|NetRexx}}
Line 3,923 ⟶ 4,034:
{{omit from|Yorick}}
{{omit from|zkl}}
{{omit from|Insitux}}
890

edits