The Elements of an NSB/App Studio Program: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
 
(17 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__NOTOC__
A program is a set of Statements. Each program line may consist of the following elements: <br />
A program in NSB/App Studio is a set of Statements. Each NSB/App Studio program line may consist of the following elements: <br />
<pre>
KEYWORD arguments 'comment <br />
KEYWORD arguments 'comment  
A KEYWORD is a word from the language that NSB/App Studio understands. Examples are PRINT, INPUTBOX and IF. The Statement and its arguments determine what action (if any) will be taken by NSB/App Studio when the line is executed. <br />
</pre>
Any text following ' on a line is a comment, and is ignored by NSB/App Studio.
A KEYWORD is a word from the language that AppStudio understands. Examples are PRINT, INPUTBOX and IF. The Statement and its arguments determine what action (if any) will be taken by AppStudio when the line is executed. <br />
Any text following ' on a line is a comment, and is ignored by AppStudio.




== 2.2.1 Multi-Line Statements ==
== Multi-Line Statements ==


Each NSB/App Studio statement normally ends at the end of the statement line. If you have a very complex statement the line can be very long. This can make your programs difficult to read. You may split long statements by using the line-continuation sequence, a space followed by an underscore, ( _) at the end of the line. NSB/App Studio combines the current line with the next line if the current line ends in ( _). Here is an example of line continuation: <br />
Each statement normally ends at the end of the statement line. If you have a very complex statement the line can be very long. This can make your programs difficult to read. You may split long statements by using the line-continuation sequence, a space followed by an underscore, ( _) at the end of the line. AppStudio combines the current line with the next line if the current line ends in ( _). Here is an example of line continuation:
<tt>PRINT "Long statements are no problem for"_</tt>
<pre>
::<tt>& " NSB/App Studio"</tt>
PRINT "Long statements are no problem for" _
  & " AppStudio"
</pre>


== Multiple Statements per Line ==


== 2.2.2 Multiple Statements per Line ==
By using a ":" character as a separator, you can put more than one statement on a line:
<pre>
a=1: b=2: c=3
</pre>


By using a ":" character as a separator, you can put more than one statement on a line: <br />
== Literals, Data Types and Variables ==
<tt>a=1: b=2: c=3</tt>


Literals are literal values you use in your programs. You use them all the time: to set the initial value of a variable, to establish the starting and ending values of a FOR...NEXT loop, and so on. When you define a constant using the CONST Statement, the name of your constant can be treated as a literal. You cannot change the value of a literal.


== 2.2.3 Literals, Data Types and Variables ==
Variables are the named holders of your data. A variable's value may be changed as needed. All variables are of variants, which means they assume the type of the value that is assigned to them. The type of a variable can change without restriction.


Literals are literal values you use in your programs. You use them all the time: to set the initial value of a variable, to establish the starting and ending values of a FOR...NEXT loop, and so on. When you define a constant using the CONST Statement, the name of your constant can be treated as a literal. You cannot change the value of a literal. <br />
==  Variable Names ==
Variables are the named holders of your data. A variable's value may be changed as needed. All variables are of variants, which means they assume the type of the value that is assigned to them. The type of a variable can change without restriction.
 
A variable is a name that holds a value. The name consists of a sequence of alphabetic and numeric characters, and the underscore (_) characters. There is no limit to the length of a variable name in AppStudio, and every character in the name is significant. Variable names are not case sensitive, and spaces and other special characters may not be used. Variable names must start with a letter. AppStudio constants may not be used as variable names. For a complete list of constants, see Appendix B.
 
The following list shows some variable names that are allowed by AppStudio:
<pre>
text
LLAMAS
Jupiter
W1Spec
SouthPark
</pre>
 
And some that are not allowed:
<pre>
1table &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'starts with a number
X&Ycords &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'uses special character &
first counter &nbsp; 'has a space
%correct &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'does not start with a letter
print &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'Keyword
</pre>
 
== Numeric Data Types ==


All numbers in AppStudio are internally stored as 64bit (8 bytes) floating point numbers, yielding an effective range of 5e-324 (negative) to 1.7976931348623157e+308 (positive).


== 2.2.4 Numeric Data Types ==
Integers are considered reliable (numbers without a period or exponent notation) to 15 digits (9e15). Floating point numbers are considered only as reliable as possible and no more. This is an especially important concept to understand for currency manipulation as 0.06 + 0.01 resolves to 0.06999999999999999 instead of 0.07. To round it properly, use [[Round]](0.06 + 0.01, 2).


All numbers in NSB/App Studio are internally stored as 64bit (8 bytes) floating point numbers, yielding an effective range of 5e-324 (negative) to 1.7976931348623157e+308 (positive). <br />
Integers are considered reliable (numbers without a period or exponent notation) to 15 digits (9e15). Floating point numbers are considered only as reliable as possible and no more. This is an especially important concept to understand for currency manipulation as 0.06 + 0.01 resolves to 0.06999999999999999 instead of 0.07. <br />
Hex values are expressed with a leading 0x. Octal values just have a leading 0. So, 0x500 = 1280, while 0500 = 320.
Hex values are expressed with a leading 0x. Octal values just have a leading 0. So, 0x500 = 1280, while 0500 = 320.


== 2.2.5 Boolean Data Type ==
== Boolean Data Type ==


Booleans consists of two values: TRUE and FALSE. This data type is used with the IF Statement. It tests the Boolean value of an expression and selects the THEN Statement if it is TRUE, or the ELSE Statement if it is FALSE. The numeric value for TRUE is -1, and the numeric value for FALSE is 0.
Booleans consists of two values: TRUE and FALSE. This data type is used with the IF Statement. It tests the Boolean value of an expression and selects the THEN Statement if it is TRUE, or the ELSE Statement if it is FALSE. The numeric value for TRUE is -1, and the numeric value for FALSE is 0.




== 2.2.6 Color Data Type ==
== Color Data Type ==


Colors are hex strings from “#000000” to “#FFFFFF”. A color value is created by mixing values for red, green, and blue that vary from 0 to 255, where 0 is dark and 255 if bright. The mixing formula is: <br />
Colors are hex strings from “#000000” to “#FFFFFF”. A color value is created by mixing values for red, green, and blue that vary from 0 to 255, where 0 is dark and 255 if bright. The mixing formula is: <br />
Line 54: Line 81:
|}
|}


== 2.2.7 String Data Type ==
== String Data Type ==


Strings consist of a series of characters. A string can be approximately 2 billion characters long. There are a number of functions that manipulate strings. The concatenation operator (&) is used to join the string representations of two variables together. A string literal is enclosed in quotation marks: <br />
Strings consist of a series of characters. A string can be approximately 2 billion characters long. There are a number of functions that manipulate strings. The concatenation operator (&) is used to join the string representations of two variables together. A string literal is enclosed in quotation marks:  
<tt>"This is a string literal"</tt>
<pre>
"This is a string literal"
</pre>


Strings are 1 indexed: the first character of a string is character 1 in NS Basic/App Studio string functions. Strings are enclosed in double quotes. A single quote(‘) is allowed inside a string. Use two double quotes ("") inside a quoted string to get a single double quote in the string.
Strings are 1 indexed: the first character of a string is character 1 in string functions. Strings are enclosed in double quotes. A single quote(‘) is allowed inside a string. Use two double quotes ("") inside a quoted string to get a single double quote in the string.




== 2.2.8 Array Data Type ==
== Array Data Type ==


Arrays are lists of values stored with a single name. Each element in the array is referred to by a number or a string in square brackets after the variable name. The first element of an array is always zero (0), and each array can have many elements. ARR[2] refers to the third element in the ARR array. Each element in an array can be of any variable type, including another array. Parenthesis can be used instead of square brackets in most cases.
Arrays are lists of values stored with a single name. Each element in the array is referred to by a number or a string in square brackets after the variable name. The first element of an array is always zero (0), and each array can have many elements. ARR[2] refers to the third element in the ARR array. Each element in an array can be of any variable type, including another array. Parenthesis can be used instead of square brackets in most cases.


Arrays must be declared in a [[Dim]] statement. Arrays should have a Dim statement in each [[Code Modules|Code Module]] where they are used. For multidimensional arrays, A(2,3) refers to the same element as A[2][3].


== 2.2.9 Object Data Type ==
== Collections ==
----
An object is a collection of named values, called properties. To refer to a property of an object, use the object name, a period and the property name: <br />
:<tt>Customer.Name</tt> <br />
An object have any number of properties. Properties can be any datatype, including functions and other objects. <br />
<tt>Customer={Name: Lovelace, Age: 195, kids:{Byron: 1, Annabella: 2, Ralph: 3}}</tt>
 
 
== 2.2.10 Collections ==


A collection is an object used for grouping and managing related objects. It functions like an array, but with strings instead of numbers identifying elements in the array. <br />
A collection is an object used for grouping and managing related objects. It functions like an array, but with strings instead of numbers identifying elements in the array.  
<tt>Dim item1, item2, item3, item4 <br />
<pre>
item1="Knuth" <br />
Dim item1, item2, item3, item4
item2="Babbage" <br />
item1="Knuth"
item3="Kemeny" <br />
item2="Babbage"
item3="Kemeny"
item4="Kurtz"
item4="Kurtz"


'Create our collection <br />
'Create our collection
myCollection = new Object() <br />
myCollection = new Object()
myCollection.add(item1, "firstKey") <br />
myCollection.add(item1, "firstKey")
myCollection.add(item2, "secondKey") <br />
myCollection.add(item2, "secondKey")
myCollection.add(item3, "thirdKey") <br />
myCollection.add(item3, "thirdKey")
myCollection.add(item4, "fourthKey")
myCollection.add(item4, "fourthKey")


Print "After adding using myCollection.add(item, key):" <br />
Print "After adding using myCollection.add(item, key):"
For Each key, item in myCollection <br />
For Each key, item in myCollection
:Print key & "=" & item <br />
  Print key & "=" & item  
Next <br />
Next  
Print "myCollection[item1]=" & myCollection[item1] <br />
Print "myCollection[item1]=" & myCollection[item1]  
Print "myCollection[""Knuth""]=" & myCollection["Knuth"]
Print "myCollection[""Knuth""]=" & myCollection["Knuth"]


'Delete a member <br />
'Delete a member
myCollection.delete(item2)</tt>
myCollection.delete(item2)
</pre>
 
== Object Data Type ==
 
An object is a collection of named values, called properties. To refer to a property of an object, use the object name, a period and the property name: <pre>
Customer.Name
</pre>
An object can have any number of properties. Properties can be any datatype, including functions and other objects.
<pre>
Customer={Name: Lovelace, Age: 195, kids:{Byron: 1, Annabella: 2, Ralph: 3}}
</pre>
 
== Classes ==
 
Here are a couple of samples of Classes:
 
=== BASIC ===
<pre>
'Define a class like this
Function Person(name, gender)
  'Add object properties like this
  this.name = name
  this.gender = gender
End Function
 
'Add methods like this.  All Person objects will be able to invoke this
Function speakName()
  Print "Howdy, my name is " & this.name
End Function
 
Person.prototype.speak = Eval("speakName")
 
'Instantiate new objects with 'new'
bob = new Person("Bob", "M")
 
'Invoke methods like this
bob.speak() 'Prints "Howdy, my name is Bob"
</pre>
=== JavaScript ===
<pre>
// Define a class like this
function Person(name, gender){


  // Add object properties like this
  this.name = name;
  this.gender = gender;
}


== 2.2.11 Variable Names ==
// Add methods like this.  All Person objects will be able to invoke this
Person.prototype.speak = function(){
    alert("Howdy, my name is" + this.name);
}


A variable is a name that holds a value. The name consists of a sequence of alphabetic and numeric characters, and the underscore (_) characters. There is no limit to the length of a variable name in NSB/App Studio, and every character in the name is significant. (Some older BASICs could only use short names.) Variable names are not case sensitive, and spaces and other special characters may not be used. Variable names must start with a letter. NS Basic/App Studio constants may not be used as variable names. For a complete list of constants, see Appendix B.
// Instantiate new objects with 'new'
var person = new Person("Bob", "M");


The following list shows some variable names that are allowed by NS Basic/App Studio: <br />
// Invoke methods like this
<tt>text <br />
person.speak(); // alerts "Howdy, my name is Bob"
LLAMAS &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'same as llamas or Llamas <br />
</pre>
Jupiter <br />
W1Spec <br />
SouthPark</tt>


And some that are not allowed: <br />
<tt>1table &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'starts with a number <br />
X&Ycords &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'uses special character & <br />
first counter &nbsp; 'has a space <br />
%correct &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'does not start with a letter <br />
print &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'NS Basic/App Studio keyword</tt>


Next: [[Expressions_and_Operators|Expressions and Operators]]
Next: [[Expressions_and_Operators|Expressions and Operators]]

Latest revision as of 20:51, 17 March 2019

A program is a set of Statements. Each program line may consist of the following elements:

KEYWORD arguments 'comment 

A KEYWORD is a word from the language that AppStudio understands. Examples are PRINT, INPUTBOX and IF. The Statement and its arguments determine what action (if any) will be taken by AppStudio when the line is executed.
Any text following ' on a line is a comment, and is ignored by AppStudio.


Multi-Line Statements

Each statement normally ends at the end of the statement line. If you have a very complex statement the line can be very long. This can make your programs difficult to read. You may split long statements by using the line-continuation sequence, a space followed by an underscore, ( _) at the end of the line. AppStudio combines the current line with the next line if the current line ends in ( _). Here is an example of line continuation:

PRINT "Long statements are no problem for" _
   & " AppStudio"

Multiple Statements per Line

By using a ":" character as a separator, you can put more than one statement on a line:

a=1: b=2: c=3

Literals, Data Types and Variables

Literals are literal values you use in your programs. You use them all the time: to set the initial value of a variable, to establish the starting and ending values of a FOR...NEXT loop, and so on. When you define a constant using the CONST Statement, the name of your constant can be treated as a literal. You cannot change the value of a literal.

Variables are the named holders of your data. A variable's value may be changed as needed. All variables are of variants, which means they assume the type of the value that is assigned to them. The type of a variable can change without restriction.

Variable Names

A variable is a name that holds a value. The name consists of a sequence of alphabetic and numeric characters, and the underscore (_) characters. There is no limit to the length of a variable name in AppStudio, and every character in the name is significant. Variable names are not case sensitive, and spaces and other special characters may not be used. Variable names must start with a letter. AppStudio constants may not be used as variable names. For a complete list of constants, see Appendix B.

The following list shows some variable names that are allowed by AppStudio:

text
LLAMAS
Jupiter
W1Spec
SouthPark

And some that are not allowed:

1table          'starts with a number 
X&Ycords        'uses special character & 
first counter   'has a space 
%correct        'does not start with a letter
print           'Keyword

Numeric Data Types

All numbers in AppStudio are internally stored as 64bit (8 bytes) floating point numbers, yielding an effective range of 5e-324 (negative) to 1.7976931348623157e+308 (positive).

Integers are considered reliable (numbers without a period or exponent notation) to 15 digits (9e15). Floating point numbers are considered only as reliable as possible and no more. This is an especially important concept to understand for currency manipulation as 0.06 + 0.01 resolves to 0.06999999999999999 instead of 0.07. To round it properly, use Round(0.06 + 0.01, 2).

Hex values are expressed with a leading 0x. Octal values just have a leading 0. So, 0x500 = 1280, while 0500 = 320.

Boolean Data Type

Booleans consists of two values: TRUE and FALSE. This data type is used with the IF Statement. It tests the Boolean value of an expression and selects the THEN Statement if it is TRUE, or the ELSE Statement if it is FALSE. The numeric value for TRUE is -1, and the numeric value for FALSE is 0.


Color Data Type

Colors are hex strings from “#000000” to “#FFFFFF”. A color value is created by mixing values for red, green, and blue that vary from 0 to 255, where 0 is dark and 255 if bright. The mixing formula is:

color = (red*65536) + (green * 256) + blue

Shades of black and white are created when equal amounts of red, green, and blue are used. See Appendix A for a list of common color constants.

Color Name Red, Green, Blue Color Value
Black 0, 0, 0 #000000, vbBLACK
Dark gray 128, 128, 128 #808080
Light gray 192, 192, 192 #COCOCO
White 255, 255, 255 #FFFFFF vbWHITE

String Data Type

Strings consist of a series of characters. A string can be approximately 2 billion characters long. There are a number of functions that manipulate strings. The concatenation operator (&) is used to join the string representations of two variables together. A string literal is enclosed in quotation marks:

"This is a string literal"

Strings are 1 indexed: the first character of a string is character 1 in string functions. Strings are enclosed in double quotes. A single quote(‘) is allowed inside a string. Use two double quotes ("") inside a quoted string to get a single double quote in the string.


Array Data Type

Arrays are lists of values stored with a single name. Each element in the array is referred to by a number or a string in square brackets after the variable name. The first element of an array is always zero (0), and each array can have many elements. ARR[2] refers to the third element in the ARR array. Each element in an array can be of any variable type, including another array. Parenthesis can be used instead of square brackets in most cases.

Arrays must be declared in a Dim statement. Arrays should have a Dim statement in each Code Module where they are used. For multidimensional arrays, A(2,3) refers to the same element as A[2][3].

Collections

A collection is an object used for grouping and managing related objects. It functions like an array, but with strings instead of numbers identifying elements in the array.

Dim item1, item2, item3, item4
item1="Knuth"
item2="Babbage"
item3="Kemeny"
item4="Kurtz"

'Create our collection
myCollection = new Object()
myCollection.add(item1, "firstKey")
myCollection.add(item2, "secondKey")
myCollection.add(item3, "thirdKey")
myCollection.add(item4, "fourthKey")

Print "After adding using myCollection.add(item, key):"
For Each key, item in myCollection
  Print key & "=" & item 
Next 
Print "myCollection[item1]=" & myCollection[item1] 
Print "myCollection[""Knuth""]=" & myCollection["Knuth"]

'Delete a member
myCollection.delete(item2)

Object Data Type

An object is a collection of named values, called properties. To refer to a property of an object, use the object name, a period and the property name:

Customer.Name

An object can have any number of properties. Properties can be any datatype, including functions and other objects.

Customer={Name: Lovelace, Age: 195, kids:{Byron: 1, Annabella: 2, Ralph: 3}}

Classes

Here are a couple of samples of Classes:

BASIC

'Define a class like this
Function Person(name, gender)
   'Add object properties like this
   this.name = name
   this.gender = gender
End Function

'Add methods like this.  All Person objects will be able to invoke this
Function speakName()
  Print "Howdy, my name is " & this.name
End Function

Person.prototype.speak = Eval("speakName")

'Instantiate new objects with 'new'
bob = new Person("Bob", "M")

'Invoke methods like this
bob.speak() 'Prints "Howdy, my name is Bob"

JavaScript

// Define a class like this
function Person(name, gender){

   // Add object properties like this
   this.name = name;
   this.gender = gender;
}

// Add methods like this.  All Person objects will be able to invoke this
Person.prototype.speak = function(){
    alert("Howdy, my name is" + this.name);
}

// Instantiate new objects with 'new'
var person = new Person("Bob", "M");

// Invoke methods like this
person.speak(); // alerts "Howdy, my name is Bob"


Next: Expressions and Operators