Inverted syntax: Difference between revisions

m
m (→‎{{header|Raku}}: fix a few old references to Perl 6)
 
(9 intermediate revisions by 5 users not shown)
Line 446:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Inverted_syntax}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition.
 
'''Inverted syntax with conditional expressions'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
The following is how is visualized the common IF expression:
In '''[https://formulae.org/?example=Inverted_syntax this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Inverted syntax 01.png]]
 
The following is the visualization of the inverted IF expression. Notice that they are different expressions, in order that they can be visualized in different form, but they reduce the same way.
 
[[File:Fōrmulæ - Inverted syntax 02.png]]
 
'''Inverted syntax with assignment'''
 
The assignment expression does not have an inverted form.
 
=={{header|Go}}==
Line 596 ⟶ 606:
 
@inv println("Wow! Lucky Guess!") if true else println("Not!") end</syntaxhighlight>
 
=={{header|Koka}}==
Using the byref function and infix operators we can get both inverted assignments as well as inverted conditionals in Koka! However, the branch to the conditional is evaluated prior to the condition unless you explicitly delay it or accept a closure.
 
<syntaxhighlight lang="koka">
fun (=:)(c: c, v: local-var<h, c>): <local<h>> ()
v := c
 
fun (?)(c: c, b: bool): e maybe<c>
if b then Just(c) else Nothing
 
fun main()
var x := 4
6 =: std/core/types/byref(x)
x.println
val res = "Yes" ? True
match res
Just(a) -> println(a)
Nothing -> throw("Nothing")
</syntaxhighlight>
 
{{out}}
<pre>
6
Yes
</pre>
 
=={{header|Kotlin}}==
Line 1,099 ⟶ 1,135:
A -> (set needumbrella A))
</syntaxhighlight>
 
=={{header|Quackery}}==
 
A Quackery program consists of numbers, words, and nests, collectively referred to as items. A nest is a sequence of zero or more items. Items are evaluated sequentially.
 
Some words can alter this sequential behaviour. <code>if</code> for example, causes the item following it to be skipped if the number on the top of data stack (henceforth just "the stack") is zero (i.e. false.)
 
So, given that the word <code>raining</code> returns a boolean (i.e. 0 or 1) indicating whether it is raining or not, and the word <code>use-umbrella</code> deploys an umbrella, the usual syntax would be <code>raining if use-umbrella</code>.
 
The word <code>'</code> alters the sequential flow by causing the item following it to be placed on the stack rather than being evaluated. The word <code>do</code> causes the item on the top of the stack to be evaluated. So we could define a new word, <code>if.1</code> with
 
<pre>[ swap do iff do else drop ] is if.1</pre>
 
which would allow us to use the syntax <code>' raining ' use-umbrella if.1</code>. (<code>iff</code> conditionally skips over two items, <code>else</code> unconditionally skips over one item.)
 
<code>'</code> is defined using the meta-control flow operator <code>]'[</code> thus: <code>[ ]'[ ] is '</code> Meta-control flow operators grant their behaviour to the word that calls them. So if we wanted the syntax <code>' use-umbrella if.2 raining</code> we could define <code>if.2</code> with
 
<pre>[ ]'[ do iff do else drop ] is if.2</pre>
 
Other syntaxes can be achieved by the judicious use of <code>'</code> and <code>]'[</code>. To demonstrate just one that avoids the use of <code>if ...</code> or <code>iff ... else ...</code> entirely, one could define a word <code>ifelse</code> as
 
<pre>[ not ]'[ nested ]'[ nested join swap peek do ] is ifelse</code></pre>
 
This would have the syntax <code>raining ifelse use-umbrella use-sunscreen</code> (for an appropriate definition of <code>use-sunscreen</code>.)
 
Quackery does not have variables, so there is not exact equivalent to assignment, but it does have ancillary stacks which can be used in a variable-like manner. <code>temp</code> is a predefined ancillary stack, and an item can be moved from the stack to it with the phrase <code>temp put</code>. As with the previous examples, <code>]'[</code> can be used to vary the syntax.
 
=={{header|R}}==
Line 1,204 ⟶ 1,266:
zz=444 / (7-a)
</pre>
 
=={{header|RPL}}==
Assigning values to variables using <code>→</code> or <code>STO</code> commands presents an inverted syntax in RPL. It's also possible to invert syntax for conditional expressions, by pushing the action in the stack as a lambda expression before doing the test, but this isn't idiomatic.
≪ 1 → raining <span style="color:grey">@ Set raining to true</span>
≪ ≪ NEEDUMBRELLA ≫
'''IF''' raining '''THEN''' EVAL '''ELSE''' DROP '''END''' <span style="color:grey">@ Execute above action if true, else pop it from stack</span>
≫ ≫ '<span style="color:blue">TASK</span>' STO <span style="color:grey">@ Store above program in TASK variable</span>
 
=={{header|Ruby}}==
Line 1,408 ⟶ 1,477:
 
Again, simulating inverted syntax with assignment is not possible.
<syntaxhighlight lang="ecmascriptwren">class IBool {
construct new(b) {
if (!(b is Bool)) Fiber.abort("B must be a boolean")
2,120

edits