scScript Packages
Packages in scScript are implemented as efficient C routines and
provide useful utility functions (and often defined constants) for
invocation from within scripts. They are called
using Pkg.FuncName() or Pkg.#DefName notation,
but functions in the main (sc) package (and constants defined
therein) can be used directly, without the Pkg name and dot
prefix. So Write(...) and sc.Write(...) both
call the the same output function in the main package
In addition, the main package also includes Inline Functions Ids
(IFIDs) which are pre-packaged bytecode sequences that are
inserted into the compiled code instead of a C function call. These are
generally language features such as the Map iterators or
Array/Dict constructors. They are invoked with
a seemingly normal func call from the script, as
in Array(12); but the generated bytecode is more efficient
and skips the function set-up and call. Like any function in scScript,
these can be called with fewer or more args. MapI
and MapK also differ from normal functions in having a body
(code statements) to execute for each iteration.
Main Package (sc)
| IFIDs: (Inline Functions) | |
|---|---|
| MapI MapI(S)(V, I) {... } |
Iterates over every cell (even unpopulated ones) in S (array or dict). V gets the cell value and I gets its numeric (0-based) index. "MapI(S)(V, I);" does NOTHING as it has an empty body! V only copies the value, use S[I] = 12; to change elts in S. |
| MapK MapK(S)(V, K) {... } |
Iterates over every elt (only populated cells) in S (array or dict). V gets the elt value and K its key if dict or its (0-based) index if array. MapK(S)(V, K); does NOTHING as it has an empty body! |
| Array A = Array(N); |
Returns an empty array with N (unpopulated) cells. Uses default minimum if N is 0 or missing. |
| Dict D = Dict(N); |
Returns an empty dict sized optimally for N pairs. Uses default size if N is 0 or missing. |
| Int X = Int(N); |
Returns the value of N converted to an integer.
|
| Flt X = Flt(N); |
Returns the value of N converted to a float (double).
|
| GetRC R = GetRC(); |
Returns RC (RunContext) of enclosing routine. RC is used for non-local return/yield as in "Return X OutOf R;" Tail recursion optimization is NOT performed if function gets RC. |
| Submit x = Submit(); |
Coroutine (P) submits its RC to its original caller (M) and goes to sleep. M can RESUME P later, P can SUBMIT only once. Submit always returns 1 to its own routine. |
| Resume N = Resume(P_RC); |
M calls sleeping P to execute. RESUME will return what P will YIELD/RETURN. P_RC must be RC previously returned from P when it did original SUBMIT. |
| Release x = Release(S_RC); |
M Releases (zaps) sleeping P. RELEASE always returns 1. P_RC must be RC previously returned from P when it did original SUBMIT. |
| Yield and Return | Yield and Return, are part of the language, not IFIDs! |
| Functions: | |
|---|---|
| TypeOf X = TypeOf(V); |
Returns the type constant for the value type: #Type_Unknown... #Type_Script. |
| SizeOf X = SizeOf(V); |
Returns the conceptual (current) cell count of V. Capacity for Arr and Dict, byte count for Strs and Clos, 1 for all others. |
| CountOf X = CountOf(V); Y = CountOf(ArrA, I); Z = CountOf(DictA, K); |
Returns the conceptual (current) elt count of V. Counts populated cells for Arr, pairs for Dict, enclosed functions for Clos, and char count (UTF-8 chars are 1-4 bytes) for Strs. 2-arg version returns 1 if the ArrA[I] or DictA[K] is populated, 0 if not. |
| Write X = Write(U, V, W...); |
Writes out concatenation of all in args to end of *Output* buffer. Returns total number of bytes written. Write accepts any valid value! |
| Writeln X = Writeln(U, V, W...); |
Same as Write, but adds an extra \n at the end. (Yes, Pascal was a good language :-) |
| Defs: | |
|---|---|
| Logic | #True, #False |
| Types | #Type_Unknown, #Type_Int, #Type_Flt, #Type_Str, #Type_Arr, #Type_Dict, #Type_RC, #Type_Pat, #Type_Func, #Type_Script #Type_Ext, #Type_GExt, #Type_FExt Closures are exposed as #Type_Func to script writers. There are 3 different external memory blocks:
|