This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,22 @@
'''Inverted syntax with conditional expressions'''
In traditional syntax conditional expressions are usually shown before the action within a statement or code block:
<lang pseudocode> IF raining=true THEN needumbrella=true </lang>
In inverted syntax, the action is listed before the conditional expression in the statement or code block:
<lang pseudocode> needumbrella=true IF raining=true </lang>
'''Inverted syntax with assignment'''
In traditional syntax, assignments are usually expressed with the variable appearing before the expression:
<lang pseudocode> a = 6</lang>
In inverted syntax, the expression appears before the variable:
<lang pseudocode> 6 = a</lang>
'''Task'''
The task is to demonstrate support for inverted syntax forms within the language by showing both the traditional and inverted forms.

View file

@ -0,0 +1,5 @@
Foo := 1;
loop
exit when Foo = 10;
Foo := Foo + 1;
end loop;

View file

@ -0,0 +1,3 @@
double=.!arg+!arg;
(=.!arg+!arg):(=?double); { inverted assignment syntax, same result as above. }

View file

@ -0,0 +1,2 @@
foo=3+7 { assigns 3+7 to foo }
3+7:?foo { assigns 10 to foo }

View file

@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
#define otherwise do { register int _o = 2; do { switch (_o) { case 1:
#define given(Mc) ;case 0: break; case 2: _o = !!(Mc); continue; } break; } while (1); } while (0)
int foo() { return 1; }
main()
{
int a = 0;
otherwise a = 4 given (foo());
printf("%d\n", a);
exit(0);
}

View file

@ -0,0 +1,21 @@
main()
{
int a = 0;
do {
register int _o = 2;
do {
switch (_o) {
case 1:
a = 4;
case 0:
break;
case 2:
_o = !!(foo());
continue;
} break;
} while (1);
} while (0);
printf("%d\n", a);
exit(0);
}

View file

@ -0,0 +1,12 @@
; normal
(if (= 1 1)
(print "Math works."))
; inverted
(->> (print "Math still works.")
(if (= 1 1)))
; a la Haskell
(->> (print a " is " b)
(let [a 'homoiconicity
b 'awesome]))

View file

@ -0,0 +1,4 @@
((fn [x] (* x x) 5) ; Define a lambda and call it with 5.
(macroexpand-1 '(->> 5 (fn [x] (* x x))))
(fn [x] (* x x) 5) ; Define a lambda that returns 5 regardless.

View file

@ -0,0 +1,2 @@
(= (mod (inc 42) 7)
(-> 42 inc (mod 7)))

View file

@ -0,0 +1,8 @@
alert "hello" if true
alert "hello again" unless false # the same as the above; unless is a negated if.
idx = 0
arr = (++idx while idx < 10) # arr is [1,2,3,4,5,6,7,8,9,10]
idx = 0
arr = (++idx until idx is 10) # same as above; until is an inverted while.

View file

@ -0,0 +1,10 @@
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun unrev-syntax (form)
(cond
((atom form) form)
((null (cddr form)) form)
(t (destructuring-bind (oper &rest args) (reverse form)
`(,oper ,@(mapcar #'unrev-syntax args)))))))
(defmacro rprogn (&body forms)
`(progn ,@(mapcar #'unrev-syntax forms)))

View file

@ -0,0 +1,14 @@
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun unrev-syntax (form)
(cond
((atom form) form) ;; atom: leave alone
((null (cdr form)) form) ;; one-element form: leave alone
((null (cddr form)) ;; two-element form: swap
(destructuring-bind (arg oper) form
`(,oper ,(unrev-syntax arg))))
(t ;; two or more args: swap last two, add others in reverse
(destructuring-bind (arg1 oper &rest args) (reverse form)
`(,oper ,(unrev-syntax arg1) ,@(mapcar #'unrev-syntax args)))))))
(defmacro rprogn (&body forms)
`(progn ,@(mapcar #'unrev-syntax forms)))

View file

@ -0,0 +1,2 @@
when :: Monad m => m () -> Bool -> m ()
action `when` condition = if condition then action else return ()

View file

@ -0,0 +1,2 @@
func a b x = (x + y) / y
where y = a * b

View file

@ -0,0 +1,3 @@
func a b x =
let y = a * b in
(x + y) / y

View file

@ -0,0 +1,5 @@
procedure main()
raining := TRUE := 1 # there is no true/false null/non-null will do
if \raining then needumbrella := TRUE # normal
needumbrella := 1(TRUE, \raining) # inverted (choose sub-expression 1)
end

View file

@ -0,0 +1,10 @@
a = 4
->4
b = 5
->5
If[1<2,
Print["This was expected"]
]
->This was expected

View file

@ -0,0 +1,8 @@
:- pred progress(int::in, int::in, int::out, int::out) is det.
progress(Past, Future, At, Total) :-
At = Past + 1,
Total = Past + Future.
progress(Past, Future, At, Total) :-
Past + Future = Total,
Past + 1 = At.

View file

@ -0,0 +1,8 @@
:- func example(int) = string.
example(N) = S :-
from_int(N) = S0,
pad_left(S0, '0', 3, S).
example(N) = S :-
pad_left(S0, '0', 3, S),
from_int(N) = S0.

View file

@ -0,0 +1,7 @@
main(IO0, IO) :-
io.write_string("Hello, ", IO0, IO1),
io.write_string("world!\n", IO1, IO).
main(!IO) :-
io.write_string("Hello, ", !IO),
io.write_string("world!\n", !IO).

View file

@ -0,0 +1,13 @@
main(!IO) :-
io.write_string(X, !IO), io.nl(!IO),
some_long_uninteresting_thing(X).
% this is the same:
main(!IO) :-
some_long_uninteresting_thing(X),
io.write_string(X, !IO), io.nl(!IO).
% but this is different!
main(!IO) :-
io.nl(!IO), io.write_string(X, !IO),
some_long_uninteresting_thing(X).

View file

@ -0,0 +1,4 @@
if ($guess == 6) { print "Wow! Lucky Guess!"; }; # Traditional syntax
print 'Wow! Lucky Guess!' if $guess == 6; # Inverted syntax (note missing braces and parens)
unless ($guess == 6) { print "Sorry, your guess was wrong!"; } # Traditional syntax
print 'Huh! You Guessed Wrong!' unless $guess == 6; # Inverted syntax

View file

@ -0,0 +1,4 @@
# Note that the results obtained by the inverted syntax form
# may produce differing results from the traditional syntax form
$a = $ok ? $b : $c; # Traditional syntax
($ok ? $b : $c) = $a; # Inverted syntax

View file

@ -0,0 +1,3 @@
sub assign { $_[1] = $_[0] }
$a = $b; # Traditional syntax
assign $b, $a; # Inverted syntax

View file

@ -0,0 +1,2 @@
(de rv Prg
(append (last Prg) (head -1 Prg)) )

View file

@ -0,0 +1 @@
x = truevalue if condition else falsevalue

View file

@ -0,0 +1 @@
do.if <- function(expr, cond) if(cond) expr

View file

@ -0,0 +1 @@
do.if(print("Wow! Lucky Guess!"), guess==6)

View file

@ -0,0 +1,3 @@
`%if%` <- function(expr, cond) if(cond) expr
print("Wow! Lucky Guess!") %if% (guess==6)

View file

@ -0,0 +1,14 @@
SIGNAL {ON|OFF} someCondition {name}
/*here is an example of a special case (for SYNTAX). */
signal on syntax
a=7
zz=444/(7-a)
return zz
syntax: say '***error***!'
say 'program is attempting to do division by zero.'
say 'REXX statement number is:' sigL
say 'the Rexx source statement is:'
say sourceLine(sigL)
exit 13

View file

@ -0,0 +1,7 @@
#lang racket
(when #t (displayln "true"))
((displayln "true") . when . #t)
(define a 6)
(set! a 5)
(a . set! . 6)

View file

@ -0,0 +1,15 @@
# Raise ArgumentError if n is negative.
if n < 0 then raise ArgumentError, "negative n" end
raise ArgumentError, "negative n" if n < 0
# Exit 1 unless we can call Process.fork.
unless Process.respond_to? :fork then exit 1 end
exit 1 unless Process.respond_to? :fork
# Empty an array, printing each element.
while ary.length > 0 do puts ary.shift end
puts ary.shift while ary.length > 0
# Another way to empty an array, printing each element.
until ary.empty? do puts ary.shift end
puts ary.shift until ary.empty?

View file

@ -0,0 +1,84 @@
# do.tcl --
#
# Tcl implementation of a "do ... while|until" loop.
#
# Originally written for the "Texas Tcl Shootout" programming contest
# at the 2000 Tcl Conference in Austin/Texas.
#
# Copyright (c) 2001 by Reinhard Max <Reinhard.Max@gmx.de>
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: do.tcl,v 1.6 2004/01/15 06:36:12 andreas_kupries Exp $
#
namespace eval ::control {
proc do {body args} {
#
# Implements a "do body while|until test" loop
#
# It is almost as fast as builtin "while" command for loops with
# more than just a few iterations.
#
set len [llength $args]
if {$len !=2 && $len != 0} {
set proc [namespace current]::[lindex [info level 0] 0]
return -code error "wrong # args: should be \"$proc body\" or \"$proc body \[until|while\] test\""
}
set test 0
foreach {whileOrUntil test} $args {
switch -exact -- $whileOrUntil {
"while" {}
"until" { set test !($test) }
default {
return -code error \
"bad option \"$whileOrUntil\": must be until, or while"
}
}
break
}
# the first invocation of the body
set code [catch { uplevel 1 $body } result]
# decide what to do upon the return code:
#
# 0 - the body executed successfully
# 1 - the body raised an error
# 2 - the body invoked [return]
# 3 - the body invoked [break]
# 4 - the body invoked [continue]
# everything else - return and pass on the results
#
switch -exact -- $code {
0 {}
1 {
return -errorinfo [ErrorInfoAsCaller uplevel do] \
-errorcode $::errorCode -code error $result
}
3 {
# FRINK: nocheck
return
}
4 {}
default {
return -code $code $result
}
}
# the rest of the loop
set code [catch {uplevel 1 [list while $test $body]} result]
if {$code == 1} {
return -errorinfo [ErrorInfoAsCaller while do] \
-errorcode $::errorCode -code error $result
}
return -code $code $result
}
}
#usage:
package require control
control::do {set i 0; puts "hello world"; incr i} until {$i > 0}

View file

@ -0,0 +1,12 @@
rename unknown __unknown
proc unknown {args} {
if {3 == [llength $args]} {
package require control
return [control::do {*}$args]
} else {
return [__unknown {*}$args]
}
}
#usage
% {set i 0; puts "hello world"; incr i} until {$i > 0}
hello world