Mark Sibly 8 ani în urmă
părinte
comite
bedec805fd

+ 80 - 0
modules/monkey/docs/language/arrays.md

@@ -0,0 +1,80 @@
+
+### Arrays
+
+An array is a linear sequence of values that can be addressed using one or more integer indices.
+
+Each array has an associated element type. That is, the type of the values actually stored in the array. An array's element type is a purely static property. It is only known at compile time so arrays cannot be 'downcast' at runtime.
+
+The syntax used for declaring values and variables of array type is: 
+
+_ElementType_ `[` [`,`...] `]`
+
+An array can also be multidimensional, in which case the '[]' will contain 1 or more commas.
+
+Here are some example of declaring array variables:
+
+```
+Local ints:Int[]			'One dimensional int array.
+Local map[,]				'Two dimension int array.
+Local funcs:Int()[]			'One dimensional array of functions of type Int().
+Local stacks:Stack<Int>[]	'One dimensional array of stacks of type Int.
+```
+
+#### Creating arrays
+
+Declaring an array does not actually create an array. To do that you must use `New`.
+
+`New` can be used to create either an unintialized or preinitialized array. The syntax for creating an uninitialized array is:
+
+`New` _ElementType_ `[` _DimensionSizes_ `]`
+
+(Note: the elements of an 'uninitialized' array are actually initialized to 'Null'!)
+
+The syntax for creating an initialized array is:
+
+`New` _ElementType_[]( _Element0_`,`_Element1_`,`...etc )
+
+Here are some examples:
+
+```
+Local ints:Int[]=New Int[10]				'Creates a ten element integer array.
+Local flts:=New Float[]( 1.0,3,5.1,7,9.2 )	'Creates a 5 element float array initialized to 1.0,3,5.1,7,9.2 
+```
+
+#### Iterating through arrays
+
+You can iterate through the elements of an array using `Eachin`, eg:
+
+```
+Local arr:=New Int[]( 1,3,5,7,9 )
+For Local i:=Eachin arr
+	Print i
+Next
+```
+
+#### Slicing arrays
+
+One dimensional arrays can be sliced using the `Slice` method, eg:
+
+```
+Local ints:=New Int[]( 1,3,5,7,9 )
+ints=ints.Slice( 1,4 )	'ints now contains 3,5,7
+```
+
+For more information, see the [[types.Array.Slice|Array.Slice]] API documentation.
+
+
+#### Resizing arrays
+
+One dimensional arrays can be resized using the `Resize` method, eg:
+
+```
+Local ints:=New Int[]( 1,2,3 )
+ints=ints.Resize( 5 )	'ints now contains 1,2,3,0,0
+```
+
+(Note that resize does not actually resize the array! It actually returns a resized *copy* of the array.)
+
+Note that mutidimensional arrays cannot currently be sliced or resized, and you cannot create an initialized multidimensional arrays. These features are planned for the future though.
+
+For more information, see the [[types.Array.Resize|Array.Resize]] API documentation.

+ 3 - 0
modules/monkey/docs/language/language.md

@@ -10,6 +10,9 @@ Note: This documentation is currently WIP.
 @import modules.md
 @import namespaces.md
 @import types.md
+@import arrays.md
+@import strings.md
+@import variants.md
 @import variables.md
 @import functions.md
 @import expressions.md

+ 0 - 49
modules/monkey/docs/language/reflection.md

@@ -1,55 +1,6 @@
 
 ### Reflection
 
-#### Variants
-
-The Variant type is a primitive type that can be used to 'box' values of any type.
-
-The easiest way to create a variant is to cast a value to Variant (much like casting an Int to String etc), eg:
-
-`Local v:=Variant( 10 )`
-
-An uninitialized variant will contain a 'null' value (of type Void) until you assign something to it:
-
-```
-Local v:Variant
-v=10				'variant now contains an int 10.
-v="hello"			'variant now contains a string "hello".
-```
-
-A variant is 'true' if it contains any value with a non-void type (including a bool false value!) and 'false' if it is uninitialized and has no (void) type.
-
-Any type of value can be implicitly converted to a variant, so you can easily pass anything to a function with variant parameters:
-
-```
-Function Test( v:Variant )
-End
-
-Function Main()
-	Test( 1 )
-	Test( "Hello" )
-	Test( New Int[] )
-	Test( Main )
-End
-```
-
-To retrieve the value contained in a variant, you must explicitly cast the variant to the desired type:
-
-```
-Local v:=Variant( 100 )
-Print Cast<Int>( v )
-```
-
-Note that the cast must specify the exact type of the value already contained in the variant, or a runtime error will occur:
-
-```
-Local v:=Variant( 10 )
-Print Cast<String>( v )	'Runtime error! Variant contains an Int not a String!
-```
-
-The one exception to this is if the Variant contains a class object, in which case you can cast the variant to any valid base class of the object.
-
-
 #### Typeof and TypeInfo
 
 The Typeof operator return a TypeInfo object, that contains various properties and methods for inspecting types at runtime. There are 2 ways to use Typeof:

+ 49 - 0
modules/monkey/docs/language/strings.md

@@ -0,0 +1,49 @@
+
+### Strings
+
+Values of type String are used to represent sequences of characters, such as text. The exact size of each character in a string value is target dependent, but is at least 8 bits.
+
+String variables are declared using the type name `String`, for example:
+
+```
+Local test:String="Hello World"
+```
+
+String literals are sequences of characters enclosed in "" (quotation marks). String literals may also include escape sequences, special sequences of characters used to represent unprintable characters.
+
+You can use the following escape sequences in string literals:
+
+| Escape sequence	| Character code
+|:------------------|:--------------
+|~q					| 34 (quotation mark ")
+|~n					| 10 (newline)
+|~r					| 13 (return)
+|~t					| 9 (tab)
+|~z					| 0 (null)
+|~~	 				| 126 (tilde ~)
+
+For example, to include literal quotation marks in a string...
+
+```
+Local test:="~qHello World~q" 
+```
+
+You can index a string using the `[]' operator, eg:
+```
+Local str:="Hello World"
+For Local i:=0 Until str.Length
+	Print str[i]
+Next
+```
+
+Indexing a string will return the character code at a given string index as an int.
+
+You can iterate through the characters in a string using `Eachin`, eg:
+
+```
+For Local chr:=Eachin "Hello World"
+	Print chr
+Next
+```
+
+For more information on strings, please see the [[types.String|String]] API reference.

+ 2 - 2
modules/monkey/docs/language/types.md

@@ -28,9 +28,9 @@ The following compound types are supported by monkey2:
 
 | Type						| Description
 |:--------------------------|:-----------
-| _Type_ `[]`				| Array type
+| _Type_ `[` [,...] `]`				| Array type
 | _Type_ `Ptr`				| Pointer type
-| _Type_ `(` _Types_ `)`		| Function type
+| _Type_ `(` _Types_ `)`	| Function type
 
 
 #### Implicit type conversions

+ 1 - 1
modules/monkey/docs/language/variables.md

@@ -1,5 +1,5 @@
 
-### Variables and Consts
+### Variables
 
 #### Local variables
 

+ 47 - 0
modules/monkey/docs/language/variants.md

@@ -0,0 +1,47 @@
+### Variants
+
+The Variant type is a primitive type that can be used to 'box' values of any type.
+
+The easiest way to create a variant is to cast a value to Variant (much like casting an Int to String etc), eg:
+
+`Local v:=Variant( 10 )`
+
+An uninitialized variant will contain a 'null' value (of type Void) until you assign something to it:
+
+```
+Local v:Variant
+v=10				'variant now contains an int 10.
+v="hello"			'variant now contains a string "hello".
+```
+
+A variant is 'true' if it contains any value with a non-void type (including a bool false value!) and 'false' if it is uninitialized and has no (void) type.
+
+Any type of value can be implicitly converted to a variant, so you can easily pass anything to a function with variant parameters:
+
+```
+Function Test( v:Variant )
+End
+
+Function Main()
+	Test( 1 )
+	Test( "Hello" )
+	Test( New Int[] )
+	Test( Main )
+End
+```
+
+To retrieve the value contained in a variant, you must explicitly cast the variant to the desired type:
+
+```
+Local v:=Variant( 100 )
+Print Cast<Int>( v )
+```
+
+Note that the cast must specify the exact type of the value already contained in the variant, or a runtime error will occur:
+
+```
+Local v:=Variant( 10 )
+Print Cast<String>( v )	'Runtime error! Variant contains an Int not a String!
+```
+
+The one exception to this is if the Variant contains a class object, in which case you can cast the variant to any valid base class of the object.