Extend your language: Difference between revisions

m (syntax highlighting fixup automation)
 
(19 intermediate revisions by 8 users not shown)
Line 204:
end
end.</syntaxhighlight>
 
=={{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:
<syntaxhighlight lang="bennugd">
#include <jambo.h>
 
#defn Siambassonverdaderas(_X_,_Y_) ##CODEIF,__firstop__=0;#ATOM#CMPLX;cpy(__firstop__),\
__secondop__=0;#ATOM#CMPLX;cpy(__secondop__);\
and;jnt(#ENDIF),
#defn Essólolaprimeraopción jmp(%%CODEIF), %ENDIF:, {__firstop__}, jnt(#ENDIF),
#defn Essólolasegundaopción jmp(%%CODEIF), %ENDIF:, {__secondop__}, jnt(#ENDIF),
#synon Else Noesningunaopción?
#synon EndIf FindelSi
 
Main
False(v), True(w)
Si ambas son verdaderas ( v, w )
Printnl ("Son ambas opciones verdaderas")
 
Es sólo la primera opción
Printnl ("La primera opción es verdadera")
 
Es sólo la segunda opción
Printnl ("La segunda opción es verdadera")
 
No es ninguna opción?
Printnl ("Nada se cumple")
Fin del Si
 
End</syntaxhighlight>
{{out}}
<pre>La segunda opción es verdadera</pre>
 
<p>Ejemplo #2:</p>
<p>El preprocesador de HOPPER permite extender la estructura "IF" (o "SI" es español), a cualquier número de expresiones lógicas como opción:
</p>
<syntaxhighlight lang="bennugd">
#include <jambo.h>
 
#defn Sitodassonverdaderas(*) ##CODEIF,{1},#GENCODE $$$*$$$ opción#ITV=0;#ATCMLIST;\
cpy(opción#ITV);and;#ENDGEN;,jnt(#ENDIF)
#synon Sitodassonverdaderas Sitodaslasopciones,Sitodasestas,Sitodas
#defn Sonverdaderas(*) jmp(%%CODEIF), %ENDIF:, {1}, #GENCODE $$$*$$$ #ATCMLIST;\
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 estas 'v, {0.025} Is equal to (10), x, y' son verdaderas
Set ("Todas las opciones son verdaderas")
 
Son verdaderas 'opción 2, opción 3, opción 4'
Set ("Sólo son verdaderas la igualdad, X e Y")
 
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")
 
Son verdaderas 'opción 1, opción 3'
Set ("Son verdaderas V y X")
 
/* podríamos seguir preguntando... */
 
Es verdadera la opción '2'
Set ("Al menos, la igualdad es verdadera")
 
Es la 'opción 4' verdadera?
Set ("Al menos, Y es verdadera")
 
No es ninguna opción?
Set ("Nada se cumple")
 
Fin del Si
Prnl
End
</syntaxhighlight>
{{out}}
<pre>Son verdaderas V, X e Y</pre>
 
<p>Ejemplo #3:</p>
<p>Aquí sólo la macro "»" que significa "CPY" (copia el valor de memoria a la variable indicada pero sin removerlo) permite extender el uso de "IF" (que también es una macro que extiende el lenguaje base), de acuerdo al ejemplo:</p>
<syntaxhighlight lang="bennugd">
#include <jambo.h>
 
Main
True(v), False(w), first option=0, second option=0
If ( var 'v' » 'first option', And ( w » 'second option' ) )
Printnl ("Son ambas opciones verdaderas")
 
Else If ( first option )
Printnl ("La primera opción es verdadera")
 
Else If ( second option )
Printnl ("La segunda opción es verdadera")
Else
Printnl ("Ninguna es verdadera")
End If
End
</syntaxhighlight>
{{out}}
<pre>La primera opción es verdadera</pre>
=== 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:
 
#include <jambo.h>
 
#defn Ifbotharetrue(_X_,_Y_) ##CODEIF,__firstop__=0;#ATOM#CMPLX;cpy(__firstop__),\
__secondop__=0;#ATOM#CMPLX;cpy(__secondop__);\
and;jnt(#ENDIF),
#defn Itisonlythefirstoption jmp(%%CODEIF), %ENDIF:, {__firstop__}, jnt(#ENDIF),
#defn Itisonlythesecondoption jmp(%%CODEIF), %ENDIF:, {__secondop__}, jnt(#ENDIF),
#synon Else Isnotthereanoption?
 
Main
False(v), True(w)
If both are true ( v, w )
Printnl("Both options are true")
 
It is only the first option
Printnl("The first option is true")
 
It is only the second option
Printnl("The second option is true")
 
Is not there an option?
Printnl ("Nothing is fulfilled")
End If
 
End
 
Output:
 
The second option is true
 
Example #2:
 
The HOPPER preprocessor allows you to extend the "IF" (or "SI" in Spanish) structure, to any number of logical expressions as an option:
 
#include <jambo.h>
 
#defn Ifallaretrue(*) ##CODEIF,{1},#GENCODE $$$*$$$ option#ITV=0;#ATCMLIST;\
cpy(#ITV option);and;#ENDGEN;,jnt(#ENDIF)
#synon Ifallaretrue Ifalloptions, Ifallofthese, Ifall
#defn Aretrue(*) jmp(%%CODEIF), %ENDIF:, {1}, #GENCODE $$$*$$$ #ATCMLIST;\
and;#ENDGEN;jnt(#ENDIF),
#synon Aretrue Theyaretrue
#defn Is(_X_) jmp(%%CODEIF), %ENDIF:, {_X_}, jnt(#ENDIF),
#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 of these 'v, {0.025} Is equal to (10), x, y' are true
Set("All options are true")
 
Are true 'option 2, option 3, option 4'
Set ("Only equality, X and Y are true")
 
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")
 
They are true 'option 1, option 3'
Set("V and X are true")
 
/* we could keep asking... */
 
Option '2' is true
Set("At least, equality is true")
 
Is 'option 4' true?
Set ("At least, Y is true")
 
Is not there an option?
Set ("Nothing is fulfilled")
 
End If
Prnl
End
 
Output:
 
V, X and Y are true
 
 
Example #3:
 
Here only the macro "»" which means "CPY" (copy the memory value to the indicated variable but without removing it) allows to extend the use of "IF" (which is also a macro that extends the base language), according to the example:
 
#include <jambo.h>
 
Main
True(v), False(w), first option=0, second option=0
If ( var 'v' » 'first option', And ( w » 'second option' ) )
Printnl("Both options are true")
 
Else If ( first option )
Printnl("The first option is true")
 
Else If ( second option )
Printnl("The second option is true")
Else
Printnl ("None is true")
End If
End
 
Output:
 
The first option is true
</pre>
 
=={{header|Arturo}}==
Line 1,483 ⟶ 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,727 ⟶ 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,740 ⟶ 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,044 ⟶ 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,482 ⟶ 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,709 ⟶ 4,027:
{{omit from|Commodore BASIC}}
{{omit from|Integer BASIC}}
{{omit from|Java}}
{{omit from|Lily}}
{{omit from|NetRexx}}
Line 3,717 ⟶ 4,034:
{{omit from|Yorick}}
{{omit from|zkl}}
{{omit from|Insitux}}
885

edits