japl/tests/japl/shadowing.jpl

81 lines
1.3 KiB
Plaintext

//[Test: shadowing]
//[source: mixed]
//similar to vars.jpl, but more focused on shadowing
// simple shadowing
var x = 4;
{
var x = 5;
print(x);//stdout:5
}
print(x);//stdout:4
// type changing shadowing
var y = true;
{
var y = 2;
print(y);//stdout:2
}
print(y);//stdout:true
// no shadowing here
var z = 3;
{
z = true;
print(z);//stdout:true
}
print(z);//stdout:true
//in-function shadowing
fun shadow(x) {
//will be called once with the input 3
print(x);//stdout:3
{
var x = 4;
print(x);//stdout:4
}
print(x);//stdout:3
x = nil;
print(x);//stdout:nil
return x;
}
print(shadow(3));//stdout:nil
//shadowing functions
fun hello() {
print("hello");
}
hello();//stdout:hello
{
fun hello() {
print("hello in");
}
hello();//stdout:hello in
{
fun hello() {
print("hello inmost");
}
hello();//stdout:hello inmost
}
hello();//stdout:hello in
}
hello();//stdout:hello
//functions shadowing with type change
fun eat() {
print("nom nom nom");
}
eat();//stdout:nom nom nom
{
var eat = 4;
print(eat);//stdout:4
{{{{{
eat = 5;
}}}}} //multiple scopes haha
print(eat);//stdout:5
}
eat();//stdout:nom nom nom
//[end]
//[end]