minid.minid
This module holds some useful API functions that don't fit anywhere else. This
also holds the important NewContext function, which is how you create a MiniD
interpreter to use in your app.
License:
Copyright (c) 2007 Jarrett Billingsley
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
- enum
MDStdlib
;
- This enumeration is used with the NewContext function to specify which standard libraries you
want to load into the new context. The base library is always loaded, so there is no
flag for it. You can choose which libraries you want to load by ORing together multiple
flags.
-
None
- Nothing but the base library will be loaded if you specify this flag.
-
Array
-
Array
manipulation.
-
Char
- Character classification.
-
IO
- Stream-based input and output.
-
Math
- Standard math functions.
-
String
-
String
manipulation.
-
Table
-
Table
manipulation.
-
OS
-
OS
-specific functionality.
-
Regexp
- Regular expressions.
-
Safe
- This flag is an OR of Array, Char, Math, String, Table, and Regexp. It represents all
the libraries which are "safe", i.e. malicious scripts would not be able to use the IO
or OS libraries to do bad things.
-
All
-
All
available standard libraries.
- MDContext
NewContext
(uint libs = cast(uint)255);
- Initializes a new MiniD context and loads any specified standard libraries into it. Each new
context is also given a new MDState as its main thread.
Params:
uint libs |
An ORing together of any standard libraries you want to load (see the MDStdlib enum).
Defaults to MDStdlib.All. |
Returns:
The newly-created context, into which you can import code and from which you can get the main
thread to run code.
- MDNamespace
loadModuleString
(MDState s, dchar[] source, MDValue [] params = null, char[] name = "<module string>");
- Compiles and initializes a module from a string, rather than loading one from a file.
Params:
MDState s |
The state that will be used to initialize the module after it has been compiled.
The module will be loaded into the global namespace of this state's context as well. |
dchar[] source |
The source code of the module, exactly as it would appear in a file. |
MDValue [] params |
An optional list of parameters. These are passed as the variadic parameters
to the top-level module function. Defaults to null (no parameters). |
char[] name |
The name which takes the place of the filename, used by the compiler to report
error messages. Defaults to "<module string>". |
Returns:
The top-level namespace of the module.
- uint
loadStatementString
(MDState s, dchar[] source, MDNamespace ns = cast(MDNamespace)null, MDValue [] params = null, char[] name = "<statement string>");
- Compiles a string containing a list of statements into a variadic function, calls it, and
returns the number of results that the function returned (which can be popped off the provided
state's stack). This is equivalent to the "loadString" baselib function in MiniD.
Params:
MDState s |
The state that will be used to execute the resulting function. |
dchar[] source |
The source code to be compiled. |
MDNamespace ns |
The namespace which will be used as the context in which the statements will be evaluated.
Defaults to the global namespace of the state's owning context. |
MDValue [] params |
An optional list of parameters. These are passed as the variadic parameters
to the compiled function. Defaults to null (no parameters). |
char[] name |
The name which takes the place of the filename, used by the compiler to report
error messages. Also used as the name of the function, used when reporting runtime
errors. Defaults to "<statement string>". |
Returns:
The number of return values which the compiled function has returned. These can then be
popped off the execution stack of the state that was passed in as the first parameter.
- MDValue
eval
(MDState s, dchar[] source, MDNamespace ns = cast(MDNamespace)null);
- Compile and evaluate a MiniD expression, and get the result. This is the equivalent of the "
eval
"
baselib function in MiniD.
Params:
MDState s |
The state that will be used to run the compiled expression. |
dchar[] source |
The string that holds the expression. |
MDNamespace ns |
The namespace which will be used as the context in which the expression will be evaluated.
Defaults to the global namespace of the state's owning context. |
- template
toJSON
(U)
alias
toJSONc
;
alias
toJSONw
;
alias
toJSONd
;
- Convert a table or an array object into JSON format. This will work with MDTable, MDArray, or any
convertible D array or AA type. Returns a string containing the JSON data. If you have the need
for converting lots of things to JSON, or for writing JSON to some kind of output, use the writeJSON
function instead, as it lets you specify a place to put the resulting data, rather than allocating
its own destination.
This function is inside a template which is parameterized on the return type, or rather, the character
type of the return string type. You can call it either as
toJSON
!(T)(...) where T is one of char,
wchar, or dchar, or you can call it using the aliases toJSONc, toJSONw, and toJSONd.
Params:
r |
The root of the data. Must be an MDTable, MDArray, or any convertible D array or AA type. |
pretty |
If true, inserts newlines and indentation in the output to make it more human-readable.
If false, elides all nonsignificant whitespace to make it as short as possible for transmission.
Defaults to false. |
Returns:
The converted data as a string.
- template
writeJSON
(U)
alias
writeJSONc
;
alias
writeJSONw
;
alias
writeJSONd
;
- Similar to toJSON, but instead of creating a string for the output and returning that, this allows you
to specify a Print instance that will be used to output the JSON as it's generated. Note that if there's
an error during conversion, some of the data will have been printed already, so it's up to you to make sure
that any unwanted data is cleaned up.
Just like toJSON, this is parameterized on the character type of the output, and can be called either as
writeJSON
!(T)(...) or as one of the aliases.
Params:
printer |
An instance of a Print parameterized on the type of the output. This is where the JSON will
be sent as it's being generated. |
r |
The root of the data. Must be an MDTable, MDArray, or any convertible D array or AA type. |
pretty |
If true, inserts newlines and indentation in the output to make it more human-readable.
If false, elides all nonsignificant whitespace to make it as short as possible for transmission.
Defaults to false. |
Examples:
import tango.io.Stdout;
...
// Stdout is a Print!(char) instance, so we use the char version of writeJSON.
// We could also write "writeJSON!(char)(...)" here.
writeJSONc(Stdout, ["hi"[]: 3, "bye": 6]);
|