September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,53 @@
// version 1.0.6
data class IfBoth(val cond1: Boolean, val cond2: Boolean) {
fun elseFirst(func: () -> Unit): IfBoth {
if (cond1 && !cond2) func()
return this
}
fun elseSecond(func: () -> Unit): IfBoth {
if (cond2 && !cond1) func()
return this
}
fun elseNeither(func: () -> Unit): IfBoth {
if (!cond1 && !cond2) func()
return this // in case it's called out of order
}
}
fun ifBoth(cond1: Boolean, cond2: Boolean, func: () -> Unit): IfBoth {
if (cond1 && cond2) func()
return IfBoth(cond1, cond2)
}
fun main(args: Array<String>) {
var a = 0
var b = 1
ifBoth (a == 1, b == 3) {
println("a = 1 and b = 3")
}
.elseFirst {
println("a = 1 and b <> 3")
}
.elseSecond {
println("a <> 1 and b = 3")
}
.elseNeither {
println("a <> 1 and b <> 3")
}
// It's also possible to omit any (or all) of the 'else' clauses or to call them out of order
a = 1
b = 0
ifBoth (a == 1, b == 3) {
println("a = 1 and b = 3")
}
.elseNeither {
println("a <> 1 and b <> 3")
}
.elseFirst {
println("a = 1 and b <> 3")
}
}

View file

@ -1,9 +1,9 @@
my &if2 = -> \a, \b, &x { my @*IF2 = ?a,?b; x }
my &if-both = -> &x { x if @*IF2 ~~ (True,True) }
my &if-first = -> &x { x if @*IF2 ~~ (True,False) }
my &if-second = -> &x { x if @*IF2 ~~ (False,True) }
my &if-neither = -> &x { x if @*IF2 ~~ (False,False)}
my &if-both = -> &x { x if @*IF2 eq (True,True) }
my &if-first = -> &x { x if @*IF2 eq (True,False) }
my &if-second = -> &x { x if @*IF2 eq (False,True) }
my &if-neither = -> &x { x if @*IF2 eq (False,False)}
sub test ($a,$b) {
$_ = "G"; # Demo correct scoping of topic.
@ -18,7 +18,4 @@ sub test ($a,$b) {
}
}
test 1,1;
test 1,0;
test 0,1;
test 0,0;
say test |$_ for 1,0 X 1,0;

View file

@ -0,0 +1,6 @@
switch {condition1,condition2} do
case {true,true}:
case {true,false}:
case {false,true}:
case {false,false}:
end switch

View file

@ -0,0 +1,10 @@
function if2(bool c1, bool c2)
return c1*10+c2
end function
switch if2(condition1,condition2) do
case 11:
case 10:
case 01:
case 00:
end switch

View file

@ -0,0 +1,12 @@
enum BOTH = 0b11, FIRST = 0b10, SECOND = 0b01, NEITHER = 0b00
function if2(bool c1, bool c2)
return c1*2+c2
end function
integer r = if2(condition1,condition2)
if r=BOTH then
elsif r=FIRST then
elsif r=SECOND then
elsif r=NEITHER then
end if

View file

@ -0,0 +1,3 @@
global constant T_if2 = 5200 tt_stringF("if2",T_if2)
global constant T_else1 = 5200 tt_stringF("else1",T_else1)
global constant T_else2 = 5200 tt_stringF("else2",T_else2)

View file

@ -0,0 +1,2 @@
--constant T_endelseelsif = {T_end,T_else,T_elsif,T_case,T_default,T_fallthru,T_fallthrough}
constant T_endelseelsif = {T_end,T_else,T_else1,T_else2,T_elsif,T_case,T_default,T_fallthru,T_fallthrough}

View file

@ -0,0 +1 @@
elsif ttidx=T_if2 then DoIf2()

View file

@ -0,0 +1,12 @@
for N=10 to 20 do
printf(1,"%d is ",N)
if2 mod(N,2)=0, mod(N,3)=0 then
puts(1,"divisible by both two and three.\n")
else1
puts(1,"divisible by two, but not by three.\n")
else2
puts(1,"divisible by three, but not by two.\n")
else
puts(1,"neither divisible by two, nor by three.\n")
end if2
end for

View file

@ -0,0 +1,31 @@
#![allow(unused_variables)]
macro_rules! if2 {
($cond1: expr, $cond2: expr
=> $both:expr
=> $first: expr
=> $second:expr
=> $none:expr)
=> {
match ($cond1, $cond2) {
(true, true) => $both,
(true, _ ) => $first,
(_ , true) => $second,
_ => $none
}
}
}
fn main() {
let i = 1;
let j = 2;
if2!(i > j, i + j >= 3
=> {
// code blocks and statements can go here also
let k = i + j;
println!("both were true")
}
=> println!("the first was true")
=> println!("the second was true")
=> println!("neither were true")
)
}