![]() A Scripting Language |
|
Trident Rising eMeme
Embedding Kozmo API Reference
|
This is a very early draft of the Kozmo Users Guide and (IMHO) is not suitable for a
beginner. Suggestions for updates and clarifications are more than welcome - please email me
on my SourceForge Account.
IntroductionKozmo is a stack based language, expressions are evaluated in a strict left-to-right order. If you have had any previous experience with FORTH you will find it very similar.Every value in a Kozmo script is evaluated and the result placed on the stack. For atomic or built-in types this means the value you write is placed on the stack. For example, the simple expression 2 will result in the value 2 being the topmost value on the stack. The main thing to remember is that stack items are removed in the reverse order that they were placed on the stack. At times this can result in some bizarre looking code, but the simple left-to-right evaluation order is very easy to remember and contributes to the speed of execution. Standard Data TypesThe Kozmo interpreter supports five core data types in the current release. These types are described in detail below.Integers32 bit signed integers are an atomic data type in the Kozmo interpreter. Integers are expressed in scripts as a sequence of digits optionally prefixed by a sign (+ or -) character.StringsStrings are identified in scripts as character sequences beginning with the quote (") character and terminated by another quote character. Quote characters may included in the string by using the backslash (\) character as an escape. EG: "This is a \"string\ "" would result in the the string This is a "string".ClosuresClosures are Kozmos method of representing code written in Kozmo itself. Closures begin with the open curly bracket ({) and end with the close curly bracket (}). The Kozmo interpreter translates these sequences into an array of tokens and pushes the array on to the stack like any other value.The difference between closures and other data types is that closures are evaluated as if the token sequence had been given as a script command. For example ... > '++ { 1 + } def ... defines a function which adds 1 to the value currently on the top of the stack. Native FunctionsNative functions are implemented by the hosting application and are registered using the Bind() method of the Session instance. Native functions cannot be created any other way.IdentifiersIdentifiers server multiple purposes in the Kozmo language. Identifiers can be bound to values and used as variables or used as a native data type themselves.If an identifer is expressed by itself with no modifiers the interpreter finds the value the identifier is currently bound to and evaluates that value. If an identifier is bound to a string or an integer that value is pushed onto the stack, if the identifier is bound to a closure or a native function that code is executed. If the identifier is not bound to anything the value NULL is pushed on to the stack. The golden rule is that all identifiers evaluate to something. The @ modifier overrides this behaviour by forcing the contents of the identifier to be pushed on to the stack even if the contents are executable (closures or native functions). The ' modifier causes the identifier itself to be pushed on to the stack regardless of what it is bound to. FunctionsFunctions in Kozmo act on the values currently on the stack. There are two types of functions supported by the interpreter, native functions which are supplied by the runtime library or the hosting application and closures which are implemented by scripts.This section details the functions supplied by the standard Kozmo runtime library and shows you how native functions can be provided by the hosting application or by yourself in scripts. The Kozmo Runtime LibraryThe Kozmo interpreter provides a standard set of functions for every application that hosts it. These functions are available unless they are overridden by the application (not recommended) or by user scripts (which may happen to support operator overloading - in this case the overloaded function should support the same semantics as the original function).Integer Functions
String Functions
Stack Manipulation Functions
Binding Functions
Input/Output FunctionsKozmo has very limited support for input/output. The hosting application may provide up to two output streams - one for standard messages, the other for error messages but this is completely optional. If an appropriate output stream is not provided the Kozmo runtime will silently succeed on these functions without actually displaying any output.An additional stream is provided for DEBUG builds of the library, this stream uses the libutil tracing functions to add entries to the trace file. If the stream is used in RELEASE (the default) builds of the library the output is silently consumed.
Predicate FunctionsPredicates are used to test values on the stack. In Kozmo predicates generally end with the character ?. Predicates will consume the values on the stack and replace them with either a TRUE or FALSE value.
Control FunctionsFlow control is implemented in Kozmo in the same way as functions (most languages implement flow control as language specific statements). This simplifies the implementation and also allows you to implement your own flow control functions (something many other languages restrict you from doing).
Miscellaneous FunctionsFlow control is implemented in Kozmo in the same way as functions (most languages implement flow control as language specific statements). This simplifies the implementation and also allows you to implement your own flow control functions (something many other languages restrict you from doing).
User Defined FunctionsTODO: This section should contain a tutorial on defining your own functions. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||