diff --git a/docs/bytecode.md b/docs/bytecode.md index f8445a3..ab047e1 100644 --- a/docs/bytecode.md +++ b/docs/bytecode.md @@ -18,15 +18,15 @@ __Note__: The conventions about number literals described in the document laying ## Compile-time type specifiers -To distinguish the different kinds of values that JAPL can represent at compile time, type specifiers are prepended to a given series of bytes to tell the deserializer what kind of object that specific sequence should deserialize into. It is important that each compile-time object specifies the size of its value in bytes (referred to as "size specifier" from now on, without quotes), after the type specifier. The following sections about object representation assume the appropriate type and size specifiers have been used and will therefore omit them to avoid repetition. +To distinguish the different kinds of values that JAPL can represent at compile time, type specifiers are prepended to a given series of bytes to tell the deserializer what kind of object that specific sequence should deserialize into. It is important that each compile-time object specifies the size of its value in bytes (referred to as "size specifier" from now on, without quotes), after the type specifier. The following sections about object representation assume the appropriate type and size specifiers have been used and will therefore omit them to avoid repetition. Some types (such as singletons) do not need a size specifier as they're only one byte long: these cases are an exception rather than the rule and are explicitly marked as such in this document. Below a list of all type specifiers: -- `0xC` -> true -- `0xD` -> false -- `0xF` -> nil -- `0xA` -> nan -- `0xB` -> inf +- `0xC` -> true* +- `0xD` -> false* +- `0xF` -> nil* +- `0xA` -> nan* +- `0xB` -> inf* - `0x01` -> Number - `0x02` -> String - `0x03` -> List literal (An heterogeneous dynamic array) @@ -39,6 +39,8 @@ Below a list of all type specifiers: - `0x10` -> Lambda declarations (aka anonymous functions) +__Note__: The types whose name is followed by an asterisk require no size specifier, as they're 1 byte long and adding one would only waste space. + ### Object representation #### Numbers diff --git a/src/backend/serializer.nim b/src/backend/serializer.nim index 2ff94ba..689fe6d 100644 --- a/src/backend/serializer.nim +++ b/src/backend/serializer.nim @@ -124,7 +124,6 @@ proc dumpBytes*(self: Serializer, chunk: Chunk, file, filename: string): seq[byt result.add(0xB) else: self.error(&"unknown constant kind in chunk table ({constant.kind})") - proc dumpHex*(self: Serializer, chunk: Chunk, file, filename: string): string = @@ -132,3 +131,24 @@ proc dumpHex*(self: Serializer, chunk: Chunk, file, filename: string): string = ## instead of a seq[byte] for b in self.dumpBytes(chunk, file, filename): result.add(toHex(b)) + + +proc loadBytes(self: Serializer, stream: seq[byte]): Serialized = + ## Loads the result from dumpBytes to a Serializer object + ## for use in the VM or for inspection + + + + + + + + + + + + + + + +