nimpk

Types

NimPkError = object of CatchableError
  report*: string
Catchable errors for NimPk.
NpVar = object
Object of NimPk variable.
NpVm = ref object of RootObj
Ref object of NimPk VM.

Procs

proc `$`(v: NpVar): string {....raises: [NimPkError, Exception],
                             tags: [RootEffect], forbids: [].}
proc `==`(a: NpVar; b: NpVar): bool {....raises: [Exception, NimPkError],
                                      tags: [RootEffect], forbids: [].}
Returns true if both variables are equal.
proc `=copy`(x: var NpVar; y: NpVar) {....raises: [], tags: [], forbids: [].}
=copy hook of NpVar.
proc `=destroy`(vm: typeof(NpVm()[])) {....raises: [], tags: [], forbids: [].}
=destroy hook of NimPK VM.
proc `=destroy`(x: NpVar) {....raises: [], tags: [], forbids: [].}
=destroy hook of NpVar.
proc `=sink`(x: var NpVar; y: NpVar) {....raises: [], tags: [], forbids: [].}
=sink hook of NpVar.
proc `[]`(vm: NpVm; module: string): NpVar {.inline,
    ...raises: [NimPkError, Exception], tags: [RootEffect], forbids: [].}
Syntax sugar to import a module and initialize it.
proc `[]`(vm: NpVm; slot: int): NpVar {....raises: [NimPkError], tags: [],
                                        forbids: [].}
Get low level VM slots as NpVar object. Among all operations in NimPk, only [] and []= won't broken other VM slots. Any other operations (even vm.new) use slots as temproary buffer.
proc `[]=`(vm: NpVm; slot: int; v: NpVar) {....raises: [], tags: [], forbids: [].}
Set NpVar object to low level VM slots. Among all operations in NimPk, only [] and []= won't broken other VM slots. Any other operations (even vm.new) use slots as temproary buffer.
proc `[]=`[T](v: NpVar; x: T)
Set value to instance of nim type binded class.
proc `[]=`[T](vm: NpVm; slot: int; x: T)
Set nim value to slot. Supports string, cstring, char, bool, SomeNumber, enum, HSlice, typeof(nil); and array, seq, or openarray of above.
proc addModule(vm: NpVm; module: string): NpVar {.discardable, ...raises: [],
    tags: [], forbids: [].}
Add a new module to the vm.
proc addSearchPath(vm: NpVm; path: string) {....raises: [], tags: [], forbids: [].}
Adds a new search paht to the VM, the path will be appended to the list of search paths. Search path orders are the same as the registered order.
proc addSource(module: NpVar; source: string) {....raises: [NimPkError, Exception],
    tags: [RootEffect], forbids: [].}
Add script code into the module and then initialize (run) it.
proc call(v: NpVar; args: varargs[NpVar]): NpVar {.discardable,
    ...raises: [NimPkError, Exception], tags: [RootEffect], forbids: [].}
Call a "callable" variable, aka method bind, closure, or class.
proc call(v: NpVar; attr: static[string]; args: varargs[NpVar]): NpVar {.
    discardable.}
Call a method of a object.
proc class(v: NpVar): NpVar {....raises: [NimPkError], tags: [], forbids: [].}
Get a class object of a variable.
proc free(vm: NpVm) {....raises: [], tags: [], forbids: [].}
Free the vm forcefully.
proc getCurrentPocketLangStackReport(): string {....raises: [Exception], tags: [],
    forbids: [].}
Retrieves the stack report that was attached to the current NimPkError.
proc getVm(pkvm: ptr PKVM): NpVm {....raises: [], tags: [], forbids: [].}
Get NpVm from a raw PkVm.
proc `import`(vm: NpVm; module: string): NpVar {.
    ...raises: [NimPkError, Exception], tags: [RootEffect], forbids: [].}
Import a module and initialize it.
proc isNil(v: NpVar): bool {....raises: [], tags: [], forbids: [].}
Check a variable is null or not.
proc isNull(v: NpVar): bool {....raises: [], tags: [], forbids: [].}
Check a variable is null or not.
proc kind(v: NpVar): NpVarType {.inline, ...raises: [], tags: [], forbids: [].}
Return the kind of variable.
proc len(v: NpVar): int {....raises: [NimPkError, Exception], tags: [RootEffect],
                          forbids: [].}
Get length of string, list, or map, etc.
proc len(vm: NpVm): int {....raises: [], tags: [], forbids: [].}
Get the available number of slots.
proc list(vm: NpVm; L: Natural = 0): NpVar {....raises: [NimPkError, Exception],
    tags: [RootEffect], forbids: [].}
Create a new list.
proc list(vm: NpVm; o: object; valueOnly = false): NpVar
Create a new list from object.
proc list(vm: NpVm; t: tuple; valueOnly = true): NpVar
Create a new list from tuple.
proc list[T](vm: NpVm; s: set[T]): NpVar
Create a new list from set.
proc main(vm: NpVm): NpVar {....raises: [NimPkError], tags: [], forbids: [].}
Get the main module.
proc map(vm: NpVm): NpVar {....raises: [NimPkError], tags: [], forbids: [].}
Create a new map.
proc map(vm: NpVm; o: object): NpVar
Create a new map from object.
proc map(vm: NpVm; t: tuple): NpVar
Create a new map from tuple.
proc map[T](vm: NpVm; s: set[T]): NpVar
Create a new map from set.
proc native(v: NpVar): pointer {....raises: [NimPkError], tags: [], forbids: [].}
Get native pointer of a instance, return nil if there is no native pointer. Alias for this to avoid name conflict to injected this pointer.
proc new[T](vm: NpVm; x: T): NpVar
Create a new variable on the vm.
proc newVm(config: ptr PkConfiguration = nil): NpVm {....raises: [], tags: [],
    forbids: [].}
Create a new NimPk virtual machine.
proc newVm(T: typedesc; config: ptr PkConfiguration = nil): T:type
Create a new NimPk virtual machine with custom type to store user data. Custom type must be ref object of NpVm.
proc null(vm: NpVm): NpVar {....raises: [], tags: [], forbids: [].}
Returns a variable represent null value.
proc `of`(v: NpVar; class: NpVar): bool {....raises: [NimPkError], tags: [],
    forbids: [].}
Check if v is an instance of the class.
proc `of`(v: NpVar; typ: NpVarType): bool {.inline, ...raises: [], tags: [],
    forbids: [].}
Syntax sugar to check type of variable.
proc pop(v0: NpVar; index: int = -1): NpVar {.discardable,
    ...raises: [NimPkError, Exception], tags: [RootEffect], forbids: [].}
Returns an element (last by default) of the list and decreases list.length by one.
proc reraise(vm: NpVm) {....raises: [NimPkError, Exception], tags: [RootEffect],
                         forbids: [].}
Raise a nim NimPkError error if there is error in vm.
proc reserve(vm: NpVm; n: int; start = 0): seq[NpVar] {.discardable,
    ...raises: [NimPkError], tags: [], forbids: [].}
Reserve n slots count from start, can be restored later.
proc restore(vm: NpVm; s: seq[NpVar]; start = 0) {....raises: [], tags: [],
    forbids: [].}
Resotre the slots from start.
proc run(vm: NpVm; source: string): NpVar {.discardable,
    ...raises: [NimPkError, Exception], tags: [RootEffect], forbids: [].}
Run the source code with error handling. Convert pocketlang error into nim error.
proc runFile(vm: NpVm; path: string): PkResult {.discardable, ...raises: [],
    tags: [], forbids: [].}
Low-level run a file. No error handling.
proc runString(vm: NpVm; source: string): PkResult {.discardable, ...raises: [],
    tags: [], forbids: [].}
Low-level run source code. No error handling.
proc startRepl(vm: NpVm) {....raises: [], tags: [], forbids: [].}
Run pocketlang REPL mode.
proc this(v: NpVar): pointer {....raises: [NimPkError], tags: [], forbids: [].}
Get native pointer of a instance, return nil if there is no native pointer.
proc to[T](v: NpVar): T
Convert NpVar into nim type. For instance of native class, it can be converted to corresponding nim type. For PocketLang builtin types, following type are supported: bool|int|float|string|cstring|char|SomeNumber|enum|range|HSlice|array|seq|NpVar.
proc `{}`(vm: NpVm; name: string): NpVar {....raises: [NimPkError], tags: [],
    forbids: [].}
Get the builtin funciton or class.

Iterators

iterator items(v: NpVar): NpVar {....raises: [Exception, NimPkError],
                                  tags: [RootEffect], forbids: [].}
The default items iterator for a NpVar object.
iterator pairs(v: NpVar): (NpVar, NpVar) {....raises: [Exception, NimPkError],
    tags: [RootEffect], forbids: [].}
The default pairs iterator for a NpVar object.

Converters

converter convertNpVmToPkVm(vm: NpVm): ptr PKVM {.inline, ...raises: [], tags: [],
    forbids: [].}
converter toBiggestFloat(v: NpVar): BiggestFloat {....raises: [NimPkError],
    tags: [], forbids: [].}
BiggestFloat v convert variable into BiggestFloat.
converter toBiggestInt(v: NpVar): BiggestInt {....raises: [NimPkError], tags: [],
    forbids: [].}
BiggestInt v convert variable into BiggestInt.
converter toBool(v: NpVar): bool {....raises: [NimPkError], tags: [], forbids: [].}
bool v convert variable into bool.
converter toCString(v: NpVar): cstring {....raises: [NimPkError], tags: [],
    forbids: [].}
cstring v convert variable into cstring (without copying).
converter toFloat(v: NpVar): float {....raises: [NimPkError], tags: [], forbids: [].}
float v convert variable into float.
converter toInt(v: NpVar): int {....raises: [NimPkError], tags: [], forbids: [].}
int v convert variable into int.
converter toString(v: NpVar): string {....raises: [NimPkError], tags: [],
                                       forbids: [].}
string v convert variable into string.

Macros

macro `()`(v: NpVar; args: varargs[untyped]): untyped
Syntax suger to call a "callable" variable, aka method bind, closure, or class. Support automatic value conversion.
macro `()`(x: untyped; vm: NpVm; args: varargs[untyped]): untyped
Fix compile error to call vm.list or vm.map, etc.
macro `.()`(v: NpVar; attr: untyped; args: varargs[untyped]): untyped
Syntax suger to call a method of a object. Support automatic value conversion.
macro `.()`(vm: NpVm; attr: untyped; args: varargs[untyped]): untyped
Syntax sugar to call a builtin function or class. Support automatic value conversion.
macro addFn(module: NpVar; fn: untyped; rename = ""): untyped
Add nim proc as a new function to the module. Parameters of nim proc can be NpVm to pass the vm.
macro addFn(module: NpVar; name: string; fn: proc): untyped
This is a overloaded addFn macro to support lambda proc.
macro addFn(vm: NpVm; fn: untyped; rename = ""): untyped
Add nim proc as a builtin function to the vm. Parameters of nim proc can be NpVm to pass the vm.
macro addFn(vm: NpVm; name: string; fn: proc): untyped
This is a overloaded addFn macro to support lambda proc.
macro addFnDoc(module: NpVar; fn: untyped; doc: static[string] = ""; rename = ""): untyped
Add nim proc as a new function to the module. Parameters of nim proc can be NpVm to pass the vm. This macro don't extract document from proc but use custom one.
macro addFnDoc(vm: NpVm; fn: untyped; doc: static[string] = ""; rename = "")
Add nim proc as a builtin function to the vm. Parameters of nim proc can be NpVm to pass the vm. This macro don't extract document from proc but use custom one.
macro addMethod(class: NpVar; fn: untyped; rename = ""): untyped
Add nim proc as a method to specified class. Parameters of nim proc can be NpVm to pass the vm. First parameter except NpVm will be self, it should correspond to the nim type of the class.
macro addMethod(class: NpVar; name: string; fn: proc): untyped
This is a overloaded addMethod macro to support lambda proc.
macro addMethodDoc(class: NpVar; fn: untyped; doc: static[string] = "";
                   rename = ""): untyped
Add nim proc as a method to specified class. Parameters of nim proc can be NpVm to pass the vm. First parameter except NpVm will be self, it should correspond to the nim type of the class. This macro don't extract document from proc but use custom one.
macro def(module: NpVar; body: untyped): untyped
PocketLang DSL to add builtin functions to a modules.
macro def(vm: NpVm; body: untyped): untyped
PocketLang DSL to add builtin functions and modules to the vm.

Templates

template `()`[T](vm: NpVm; x: T): untyped
Syntax sugar for new(vm, ...), vm(x) = vm.new(x).
template `.`(v: NpVar; attr: untyped): NpVar
Syntax sugar to get the attribute.
template `.`(vm: NpVm; attr: untyped): NpVar
Syntax sugar to get the builtin function.
template `.=`[T](v: NpVar; attr: untyped; x: T)
Syntax sugar to set the attribute.
template `[]`(v0: NpVar; key: untyped): NpVar
Get a subscript value.
template `[]=`(v0: NpVar; key: untyped; value: untyped)
Set subscript value with the key.
template add(v: NpVar; x: untyped)
Add an element into list.
template addClass(module0: NpVar; name: string; base0: NpVar;
                  doc: static[string]): NpVar
Add a class to the module with a static docstring, but no ctor or dtor. If the base is vm.null by default it'll set to Object class
template addClass(module0: NpVar; name: string; base0: NpVar;
                  doc: static[string]; ctor: untyped; dtor: untyped): NpVar
Add a class to the module with a static docstring, ctor, and dtor. If the base is vm.null by default it'll set to Object class
template addFn(module0: NpVar; name: string; doc: static[string]; body: untyped)
Add a new function to the module with a static docstring.
template addFn(vm1: NpVm; name: string; doc: static[string]; body: untyped)
Add a builtin function to the vm with a static docstring.
template addMethod(class0: NpVar; name: string; doc: static[string];
                   body: untyped)
Add a method to the class with a static docstring.
template addType(module0: NpVar; typ: typedesc; rename = "";
                 doc: static[string] = ""): untyped
Add any nim type as a class into specified module.
template addType(vm: NpVm; typ: typedesc; rename = ""; doc: static[string] = ""): untyped
Add nim type (enum|object|ref) as a class into lang module.
template error(vm: NpVm): NpVar
Get the current error or null if there is no error.
template error[T](vm: NpVm; err: T)
Set the error.
template exportNimPk(name: string; body: untyped)
Export native code to dynamic library. Inject self as the module object to export. Inject vm of NpVm.
template exportNimPk(t: typedesc; name: string; body: untyped)
Export native code to dynamic library. Inject self as the module object to export. Inject vm of custom type NpVM (must be ref object of NpVm).
template insert(v0: NpVar; x: untyped; index: int = 0)
Insert an element into list at index.
template lastError(vm: NpVm): NpVar
Get the last error of the vm in except part.
template lastError[T](vm: NpVm; err: T)
Set the last error.
template NpNil(): untyped
NpNull for default value of NpVar type.
template withNimPkVm(body: untyped)
Start a vm with default config.
template withNimPkVm(t: untyped; body: untyped)
Start a vm with custom type and default config.
template withNimPkVmConfig(config: ptr PkConfiguration; body: untyped)
Start a VM with given config.
template withNimPkVmConfig(t: typedesc; config: ptr PkConfiguration;
                           body: untyped)
Start a VM with custom type and given config.
template `{}`(v0: NpVar; attr: string): NpVar
Get the attribute of a variable.
template `{}=`[T](v0: NpVar; attr: string; x: T)
Set the attribute of a variable.