Nasal Hello World: Difference between revisions
m (Link to special directory articles) |
Philosopher (talk | contribs) m (nothing much) |
||
Line 1: | Line 1: | ||
{{Nasal Navigation}} | {{Nasal Navigation}} | ||
A simple hello world example in Nasal would be (to be saved in [[$FG_ROOT]]/Nasal): | A simple hello world example in Nasal would be (to be saved in [[$FG_ROOT]]/Nasal): | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
# [[$FG_ROOT]]/Nasal/hello.nas | |||
print('Hello World!'); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 17: | Line 15: | ||
Strings in Nasal can also use double quotes which support escaping: | Strings in Nasal can also use double quotes which support escaping: | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
# hello.nas | |||
print("Hello\nWorld!"); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Double quotes support typical escape sequences: | Double quotes support some of the typical escape sequences: | ||
* \n Newline | * \n Newline | ||
* \t Horizontal Tab | * \t Horizontal Tab | ||
* \r Carriage Return | * \r Carriage Return | ||
* \\ Backslash | * \\ Backslash | ||
* \" Double quote | * \" Double quote | ||
* \x''yy'' Hexadecimal-specified character (yy stands for two hexadecimal digits; \x is literal) | |||
For example, to print a new line, use: | For example, to print a new line, use: | ||
<syntaxhighlight lang="php"> | |||
print ("\n"); | |||
</syntaxhighlight> | |||
To print a quoted string, use: | To print a quoted string, use: | ||
<syntaxhighlight lang="php"> | |||
print ("\"quoted string\""); | |||
</syntaxhighlight> | |||
and so on. | and so on. |
Revision as of 22:39, 25 October 2013
The FlightGear forum has a subforum related to: Nasal Scripting |
Nasal scripting |
---|
Nasal internals |
---|
Memory Management (GC) |
A simple hello world example in Nasal would be (to be saved in $FG_ROOT/Nasal):
# [[$FG_ROOT]]/Nasal/hello.nas
print('Hello World!');
This will show the "Hello World" string during startup in the console window. The hash sign (#) just introduces comments (i.e. will be ignored by the interpreter).
Note: Script-specific symbols such as global variables (or functions) will be put into a scope (namespace) based on the script's name, scripts embedded via aircraft-set.xml files can separately specify a corresponding module name (see Howto: Make an aircraft for details).
Strings in Nasal can also use double quotes which support escaping:
# hello.nas
print("Hello\nWorld!");
Double quotes support some of the typical escape sequences:
- \n Newline
- \t Horizontal Tab
- \r Carriage Return
- \\ Backslash
- \" Double quote
- \xyy Hexadecimal-specified character (yy stands for two hexadecimal digits; \x is literal)
For example, to print a new line, use:
print ("\n");
To print a quoted string, use:
print ("\"quoted string\"");
and so on.
Single quotes treat everything as literal except for embedded single quotes (including embedded whitespace like newlines).
Nasal strings are always arrays of bytes (never characters: see the utf8 library if you want character-based equivalents of substr() et. al.). They can be indexed just like in C (although note that there is no nul termination -- get the length with size()):