japl/tests/japl/shadowing.jpl

81 lines
1.3 KiB
Plaintext
Raw Normal View History

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