Extend your language: Difference between revisions
→{{header|langur}}
m (→{{header|Amazing Hopper}}: added a google translation) |
Langurmonkey (talk | contribs) |
||
(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;\
#synon Sitodassonverdaderas Sitodaslasopciones,Sitodasestas,Sitodas
#defn Sonverdaderas(*) jmp(%%CODEIF), %ENDIF:, {1}, #GENCODE
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
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
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
#defn
#defn
#synon Else
Main
False(v), True(w)
If both are true ( v, w )
It
It
Printnl ("Nothing is fulfilled")
End
Output:
The second option is true
Example #2:
The HOPPER preprocessor allows you to extend the "IF" (or "SI"
#include <jambo.h>
#defn Ifallaretrue(*) ##CODEIF,{1},#GENCODE $$$*$$$ option#ITV=0;#ATCMLIST;\
#synon Ifallaretrue Ifalloptions, Ifallofthese, Ifall
#defn Aretrue(*) jmp(%%CODEIF), %ENDIF:, {1}, #GENCODE
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
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... */
Set("At least, equality is true")
Is 'option 4' true?
Set ("At least, Y is true")
Set ("Nothing is fulfilled")
End
Output:
V, X and Y are true
Example #3:
Line 396 ⟶ 429:
If ( var 'v' » 'first option', And ( w » 'second option' ) )
Else If ( first option )
Else If ( second option )
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
We could use the nxor (logical equivalence) operator to test truthiness.
<syntaxhighlight lang="langur">
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">
case true: ... # both true
case true, false: ... # first true, second false
case false, true: ... # first false, second true
case null, _: ... # first null
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="
construct new(cond1, cond2) {
_cond1 = cond1
Line 3,915 ⟶ 4,027:
{{omit from|Commodore BASIC}}
{{omit from|Integer BASIC}}
{{omit from|Lily}}
{{omit from|NetRexx}}
Line 3,923 ⟶ 4,034:
{{omit from|Yorick}}
{{omit from|zkl}}
{{omit from|Insitux}}
|