JortSort: Difference between revisions

m
→‎{{header|Phix}}: added syntax colouring, marked p2js compatible
(Added solution for Action!)
m (→‎{{header|Phix}}: added syntax colouring, marked p2js compatible)
Line 1,074:
 
=={{header|Phix}}==
<!--<lang Phix>type JortSort(sequence sphixonline)-->
<span style="color: #008080;">type</span> <span style="color: #000000;">JortSort</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
return s=sort(s)
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
end type</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">type</span>
<!--</lang>-->
Then any variable or constant delared as type JortSort raises an error if used incorrectly, eg
<!--<lang Phix>JortSort ok = {1,2,3}(phixonline)-->
<span style="color: #000000;">JortSort</span> <span style="color: #000000;">ok</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}</span>
JortSort bad = {5,4,6}</lang>
<span style="color: #000000;">JortSort</span> <span style="color: #000000;">bad</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">}</span>
<!--</lang>-->
{{out}}
Note that while the above is fine under pwa/p2js and can be invoked explicitly, it does not automatically trigger the error.
<pre>
C:\Program Files (x86)\Phix\test.exw:2
Line 1,086 ⟶ 1,091:
</pre>
Amusingly the compiler itself uses a variant of jortsort, in that pttree.e declares a whole bunch of ternary tree node constants for all the language keywords and builtins such as
<!--<lang Phix>(phixonline)-->
<lang Phix>global constant T_while = 336 tt_stringF("while",T_while)</lang>
<span style="color: #008080;">global</span> <span style="color: #008080;">constant</span> <span style="color: #000000;">T_while</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">336</span> <span style="color: #000000;">tt_stringF</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"while"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">T_while</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
and if you change that to 338 and try to recompile the compiler, you'll immediately get:
<pre>
while should be 336(not 338)
</pre>
It does that because at the lowest level a cmp imm is at least twice as fast as a cmp [mem], and the only other way it could know these constants at compile-time would be to (re)build a 5000-node ternary tree, though I will concede that any sane person would have written a program to write an include file rather than hacking these things by hand.<br>
There is a similar thing in pwa\src\p2js_keywords.e, though it automatically checks, prompts, and auto-rebuilds that for you.
 
=={{header|PicoLisp}}==
7,813

edits