Category:Sparkling: Difference between revisions

 
(4 intermediate revisions by the same user not shown)
Line 29:
A somewhat more complex (unnecessarily complex for this purpose) version demonstrates some more language elements:
 
<lang sparkling>constextern helloWorldString = "Hello, world!";
 
functionlet printMessage = fn (msg) {
varlet numCharsWritten = printf("%s\n", msg);
stdout.printf("%d characters written\n", numCharsWritten);
};
 
printMessage(helloWorldString);
Line 57:
* Number ("number"): either a signed integer (C's <tt>long</tt> type) or a double-precision floating-point number. The only type of implicit conversion in the language is between integers and floating-point numbers (if an arithmetic operator has an integer and a floating-point operand, it will treat both values as floating-point and it will produce a floating-point result as well.)
* String ("string"): a sequence of bytes, which is not necessarily a human-readable string (it can contain arbitrary binary data). When exposed through the C API, a terminating 0 byte is always appended to the end of string. It does not count against the length of the string object, but it makes standard C functions to operate on Sparkling strings easily.
* Array ("array"): Simple integer-indexed array. Arrays are mutable. Arrays are bounds-checked (an out-of-bounds access causes a runtime error).
* Array ("array"): a fast associative array. The array is the only mutable data structure in the language. The type of its keys and values can be of any type, but indexing by the floating-point <tt>NaN</tt> value is not permitted. Sparkling's array implementation is a hybrid data structure: in general, all keys are stored in a hash table, but sufficiently small integer keys are stored implicitly in a vector, so if an array is being used as a non-associative, indexed array, then it has very good performance.
* Hash tables ("hashmap"): Mutable associative container. The type of its keys and values can be of any type, but indexing by <tt>nil</tt> or by the floating-point <tt>NaN</tt> value is not permitted.
* Function ("function"): in Sparkling, functions are first-class values. They can be passed to functions as arguments and returned by functions. There is a literal syntax for creating - possibly unnamed - function objects. Sparkling has lexical closures, so functions capture variables (by value) from outer scopes. All functions are variadic; it is not an error to call a function with more or less arguments than the number of arguments it is defined with.
* User information objects ("userinfo"): in order to facilitate the creation of third-party extension libraries, Sparkling has a "joker" data type, the user info. User info objects have two subtypes: weak and strong. Weak user info values hold an arbitrary, unmanaged, generic C pointer (<tt>void *</tt>), while strong user info objects contain a managed, reference-counted Sparkling object of an user-defined "class". (The Sparkling API makes it possible for users to define their own classes -- memory-intensive built-in objects like strings and arrays already use this mechanism for managing memory.)
Line 76 ⟶ 77:
== The standard library ==
 
Similarly to almost allmost modern languages, the Sparkling distribution comes with a bunch of utility and run-time support functions bundled in various "packages" of the Sparkling standard library. These packages can be loaded separately by the host program (i. e. the native environment that runs the Sparkling interpreter); in the default mode, however, all standard functions are loaded at the beginning of a Sparkling session (represented by an "<tt>SpnContext"</tt> object, from an API point of view). Library functions are not special in the sense that they are just normal native extension functions. There is one exception, though: standard library functions assume the use of the context API, and as such, they require their user info argument to point to a valid <tt>SpnContext</tt> object. This is done so that these functions can use the error reporting facilities of the virtual machine.
 
The currently available standard packages are:
 
* I/O routines (writing/reading to/from standard streams and files)
* String manipulation: searching for and creating substrings, creating formatted strings, etc.
* Array and associative array manipulation, including sorting, searching and functional-style list processing, such as <tt>map()</tt> and <tt>reduce()</tt>
* Floating-point, integral and complex mathematical functions
* Shell and environment access (including system date and time), and access to the Sparkling API itself (e. g. compiling to bytecode, stack traces, etc.)
 
== Implementation ==
Line 100 ⟶ 101:
== Extensibility ==
 
Users can write and load native extension functions with the aid of the Sparkling C API. Extension functions must be written in C (or in C++ with external C linkage, or any language that supports C linkage and calling conventions), and they must follow a predefined signature, which makes it possible for the Sparkling virtual machine to call such a function. FunctionThe arguments are passed in an array of Sparkling value ("SpnValue") objects, and the return valuesignature of a functionnative - as seen by a Sparkling script - should be moved into place using a pointer to another SpnValue. The (actual, integer) return value of the nativeextension function determines whetheris the virtual machine continues the execution of a program (zero) or raises a runtime exception (non-zero).following:
 
<lang C>int native_ext_fn(SpnValue *retVal, int argc, SpnValue *argv, void *context);</lang>
 
Function arguments are passed in an array of Sparkling value (<tt>SpnValue</tt>) objects, and the return value of a function - as seen by a Sparkling script - should be moved into place using a pointer to another <tt>SpnValue</tt>. The (actual, integer) return value of the native function determines whether the virtual machine continues the execution of a program (zero) or raises a runtime exception (non-zero).
 
The use of compiled, platform-dependent, native dynamic libraries is a work in progress. It is desired that they be added to the library with support for conditional compilation, in the same way libreadline and libobjc is used by the engine. This would mean that on systems which support dynamic loading and expose a C API, users will be able to benefit from the use of pre-compiled libraries, meanwhile on platforms that do not support dynamic code loading, this feature would not impose an irresolvable dependency, so Sparkling could continue to run on those platforms as well, without the ability to load dynamic libraries.
 
== Debugging ==
Anonymous user