Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
53
Task/Extend-your-language/PHL/extend-your-language.phl
Normal file
53
Task/Extend-your-language/PHL/extend-your-language.phl
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
module stmts;
|
||||
|
||||
import phl::lang::io;
|
||||
|
||||
/* LinkedList --> Each element contains a condition */
|
||||
struct @ConditionalChain {
|
||||
field @Boolean cond;
|
||||
field @ConditionalChain next;
|
||||
|
||||
@ConditionalChain init(@Boolean cond, @ConditionalChain next) [
|
||||
this::cond = cond;
|
||||
this::next = next;
|
||||
|
||||
return this;
|
||||
]
|
||||
|
||||
/*
|
||||
* If the condition is true executes the closure and returns a false element, otherwise returns the next condition
|
||||
*
|
||||
* Execution starts from the first element, and iterates until the right element is found.
|
||||
*/
|
||||
@ConditionalChain then(@Closure<@Void> closure) [
|
||||
if (isNull(next())) return new @ConditionalChain.init(false, null);
|
||||
if (cond()) {
|
||||
closure();
|
||||
return new @ConditionalChain.init(false, null);
|
||||
}
|
||||
else return next();
|
||||
]
|
||||
|
||||
/* Operators create a cool look */
|
||||
@ConditionalChain operator then(@Closure<@Void> closure) alias @ConditionalChain.then;
|
||||
@ConditionalChain operator else1(@Closure<@Void> closure) alias @ConditionalChain.then;
|
||||
@ConditionalChain operator else2(@Closure<@Void> closure) alias @ConditionalChain.then;
|
||||
@ConditionalChain operator orElse(@Closure<@Void> closure) alias @ConditionalChain.then;
|
||||
};
|
||||
|
||||
/* Returns linked list [a && b, a, b, true] */
|
||||
@ConditionalChain if2(@Boolean a, @Boolean b) [
|
||||
return new @ConditionalChain.init(a && b, new @ConditionalChain.init(a, new @ConditionalChain.init(b, new @ConditionalChain.init(true, null))));
|
||||
]
|
||||
|
||||
@Void main [
|
||||
if2(false, true) then [
|
||||
println("Not this!");
|
||||
] else1 [
|
||||
println("Not this!");
|
||||
] else2 [
|
||||
println("This!");
|
||||
] orElse [
|
||||
println("Not this!");
|
||||
];
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue