1,361
edits
m (→Conditionals: https://sourceforge.net/p/flightgear/mailman/message/36020489/) |
No edit summary |
||
| Line 7: | Line 7: | ||
var condition = 1; # this evaluates to boolean true | var condition = 1; # this evaluates to boolean true | ||
if (condition) { | if (condition) { | ||
# true block, will | # true block, will be executed if condition is true | ||
} else { | } else { | ||
# false block, will be executed if condition is false | # false block, will be executed if condition is false | ||
| Line 18: | Line 18: | ||
var condition = 1; # boolean true | var condition = 1; # boolean true | ||
if (!condition) { | if (!condition) { | ||
# false block, will | # false block, will be executed if condition is true | ||
} else { | } else { | ||
# true block, will be executed if condition is false | # true block, will be executed if condition is false | ||
| Line 30: | Line 30: | ||
if (condition) { | if (condition) { | ||
# true block, will | # true block, will be executed if condition is true i.e. FlightGear is paused | ||
} else { | } else { | ||
# false block, will be executed if condition is false | # false block, will be executed if condition is false | ||
| Line 37: | Line 37: | ||
The same thing could be accomplished by using a call to getprop() as part of the condition: | The same thing could be accomplished by using a call to getprop() as part of the condition: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
if (var condition = getprop("/sim/signals/freeze")) { | if (var condition = getprop("/sim/signals/freeze")) { | ||
# true block, will | # true block, will be executed if condition is true | ||
} else { | } else { | ||
# false block, will be executed if condition is false | # false block, will be executed if condition is false | ||
| Line 48: | Line 47: | ||
Note that you are never using the condition variable here, so you could also omit the whole declaration and directly use the getprop call: | Note that you are never using the condition variable here, so you could also omit the whole declaration and directly use the getprop call: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
if (getprop("/sim/signals/freeze")) { | if (getprop("/sim/signals/freeze")) { | ||
# true block, will | # true block, will be executed if condition is true | ||
} else { | } else { | ||
# false block, will be executed if condition is false | # false block, will be executed if condition is false | ||
| Line 66: | Line 64: | ||
var condition = (1 == 1 and 2 == 2 and 3 == 3) or (4 != 5 and !0); | var condition = (1 == 1 and 2 == 2 and 3 == 3) or (4 != 5 and !0); | ||
if (condition) { | if (condition) { | ||
# true block, will | # true block, will be executed if condition is true | ||
} else { | } else { | ||
# false block, will be executed if condition is false | # false block, will be executed if condition is false | ||
| Line 79: | Line 77: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
var abs = func(n) { | var abs = func(n) { | ||
if(n<0) | if (n < 0) { | ||
return -n | |||
else | } else { | ||
return n | |||
} | } | ||
}; | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 91: | Line 90: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
var abs = func(n) { | var abs = func(n) { | ||
return if (n<0) | return if (n < 0) { | ||
-n | |||
else | } else { | ||
n | |||
} | } | ||
}; | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 118: | Line 118: | ||
print("this is printed regardless\n") | print("this is printed regardless\n") | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Instead of a switch statement one can use | Instead of a switch statement one can use | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
if (1==2) { | if (1 == 2) { | ||
print("wrong"); | print("wrong"); | ||
} else if (1 == 3) { # NOTE the space between else and if | } else if (1 == 3) { # NOTE the space between else and if | ||
| Line 131: | Line 130: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
which produces the expected output of <code>don't know</code>. | which produces the expected output of <code>don't know</code>. | ||
| Line 138: | Line 138: | ||
if (1 == 2) { | if (1 == 2) { | ||
print("wrong"); | print("wrong"); | ||
} elsif (1 == 3) { | } elsif (1 == 3) { | ||
print("wronger"); | print("wronger"); | ||
} else { | } else { | ||
| Line 144: | Line 144: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
The <tt>nil</tt> logic is actually quite logical, let's just restate the obvious: | The <tt>nil</tt> logic is actually quite logical, let's just restate the obvious: | ||
| Line 155: | Line 154: | ||
}; | }; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Nasal's binary boolean operators are "and" and "or", unlike C. Unary not is still "!" however. They short-circuit like you expect: | Nasal's binary boolean operators are "and" and "or", unlike C. Unary not is still "!" however. They short-circuit like you expect: | ||
| Line 194: | Line 191: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
var x = getprop("/velocities/airspeed-kts") or 0.00; # to ensure that x is always valid number | var x = getprop("/velocities/airspeed-kts") or 0.00; # to ensure that x is always valid number | ||
var foo = 10*3.1*x; # ... for this computation | var foo = 10 * 3.1 * x; # ... for this computation | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 222: | Line 219: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
var do_something = func return 0; | var do_something = func return 0; | ||
if (a==1) { | if (a == 1) { | ||
do_something() or print("failed"); | do_something() or print("failed"); | ||
} | } | ||
var do_something = func return 0; | var do_something = func return 0; | ||
( (a==1) and do_something() ) or print("failed"); | ((a == 1) and do_something()) or print("failed"); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 234: | Line 231: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
var do_something = func return 0; | var do_something = func return 0; | ||
( a and do_something() ) or print("failed"); | (a and do_something()) or print("failed"); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 242: | Line 239: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
if (a==1) function_a(); | if (a == 1) function_a(); | ||
else | else | ||
if (a==2) function_b(); | if (a == 2) function_b(); | ||
else | else | ||
if (a==3) function_c(); | if (a == 3) function_c(); | ||
else | else | ||
if (a==4) function_d(); | if (a == 4) function_d(); | ||
else | else | ||
if (a==5) function_e(); | if (a == 5) function_e(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 256: | Line 253: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
var mapping = {1:function_a, 2:function_b, 3:function_c, 4:function_d,5:function_e}; | var mapping = { 1: function_a, 2: function_b, 3: function_c, 4: function_d, 5: function_e }; | ||
mapping[a] (); | mapping[a](); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 267: | Line 264: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
if (code == "altocumulus_sky"){weather_tiles.set_altocumulus_tile();} | if (code == "altocumulus_sky") { weather_tiles.set_altocumulus_tile(); } | ||
else if (code == "broken_layers") {weather_tiles.set_broken_layers_tile();} | else if (code == "broken_layers") { weather_tiles.set_broken_layers_tile(); } | ||
else if (code == "stratus") {weather_tiles.set_overcast_stratus_tile();} | else if (code == "stratus") { weather_tiles.set_overcast_stratus_tile(); } | ||
else if (code == "cumulus_sky") {weather_tiles.set_fair_weather_tile();} | else if (code == "cumulus_sky") { weather_tiles.set_fair_weather_tile(); } | ||
else if (code == "gliders_sky") {weather_tiles.set_gliders_sky_tile();} | else if (code == "gliders_sky") { weather_tiles.set_gliders_sky_tile(); } | ||
else if (code == "blue_thermals") {weather_tiles.set_blue_thermals_tile();} | else if (code == "blue_thermals") { weather_tiles.set_blue_thermals_tile(); } | ||
else if (code == "summer_rain") {weather_tiles.set_summer_rain_tile();} | else if (code == "summer_rain") { weather_tiles.set_summer_rain_tile(); } | ||
else if (code == "high_pressure_core") {weather_tiles.set_high_pressure_core_tile();} | else if (code == "high_pressure_core") { weather_tiles.set_high_pressure_core_tile(); } | ||
else if (code == "high_pressure") {weather_tiles.set_high_pressure_tile();} | else if (code == "high_pressure") { weather_tiles.set_high_pressure_tile(); } | ||
else if (code == "high_pressure_border") {weather_tiles.set_high_pressure_border_tile();} | else if (code == "high_pressure_border") { weather_tiles.set_high_pressure_border_tile(); } | ||
else if (code == "low_pressure_border") {weather_tiles.set_low_pressure_border_tile();} | else if (code == "low_pressure_border") { weather_tiles.set_low_pressure_border_tile(); } | ||
else if (code == "low_pressure") {weather_tiles.set_low_pressure_tile();} | else if (code == "low_pressure") { weather_tiles.set_low_pressure_tile(); } | ||
else if (code == "low_pressure_core") {weather_tiles.set_low_pressure_core_tile();} | else if (code == "low_pressure_core") { weather_tiles.set_low_pressure_core_tile(); } | ||
else if (code == "cold_sector") {weather_tiles.set_cold_sector_tile();} | else if (code == "cold_sector") { weather_tiles.set_cold_sector_tile(); } | ||
else if (code == "warm_sector") {weather_tiles.set_warm_sector_tile();} | else if (code == "warm_sector") { weather_tiles.set_warm_sector_tile(); } | ||
else if (code == "tropical_weather") {weather_tiles.set_tropical_weather_tile();} | else if (code == "tropical_weather") { weather_tiles.set_tropical_weather_tile(); } | ||
else if (code == "test") {weather_tiles.set_4_8_stratus_tile();} | else if (code == "test") { weather_tiles.set_4_8_stratus_tile(); } | ||
else ... | else ... | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 290: | Line 287: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
weather_tiles["set_"~code~"_tile"](); # naming convention | weather_tiles["set_" ~ code ~ "_tile"](); # naming convention | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 302: | Line 299: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
hash[key] (arguments...); | hash[key](arguments...); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 308: | Line 305: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
var makeFuncString = func(c) return tolower("set_"~c~"_tile"); | var makeFuncString = func(c) return tolower("set_" ~ c ~ "_tile"); | ||
var isFunc = func(f) typeof(f)=='func'; | var isFunc = func(f) typeof(f) == 'func'; | ||
var hasMethod = func(h,m) contains(h,m) and isFunc; | var hasMethod = func(h,m) contains(h, m) and isFunc; | ||
var callIfAvailable = func(hash, method, unavailable=func{} ) { | var callIfAvailable = func(hash, method, unavailable = func {}) { | ||
var c=hasMethod(hash,makeFuncString(m) ) or unavailable(); | var c = hasMethod(hash, makeFuncString(m)) or unavailable(); | ||
hash[makeFuncString(m)] (); | hash[makeFuncString(m)](); | ||
} | } | ||
callIfAvailable( weather_tiles,code, func {die("key not found in hash or not a func");} ); | callIfAvailable(weather_tiles, code, func { die("key not found in hash or not a func"); }); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
edits