diff --git a/docs/bytecode.md b/docs/bytecode.md index ab047e1..576ebc5 100644 --- a/docs/bytecode.md +++ b/docs/bytecode.md @@ -68,7 +68,7 @@ Once a JAPL source file (i.e. one with a ".jpl" extension, without quotes) has b An object file starts with the headers, namely: -- A 13-byte constant string with the value `"JAPL_BYTECODE"` (without quotes) +- A 13-byte constant string with the value `"JAPL_BYTECODE"` (without quotes) encoded as a sequence of integers of the ASCII encoding of each character in the string - A 3-byte version header composed of 3 unsigned integers representing the major, minor and patch version of the compiler used to generate the file, respectively. JAPL follows the SemVer standard for versioning - A string representing the branch name of the git repo from which JAPL was compiled, prepended with its size represented as a single 8-bit unsigned integer. Due to this encoding the branch name can't be longer than 256 characters, which is a length deemed appropriate for this purpose - A 40 bytes hexadecimal string, pinpointing the version of the compiler down to the exact commit hash in the JAPL repository, particularly useful when testing development versions @@ -77,7 +77,7 @@ An object file starts with the headers, namely: ### Constant section -This section of the file follows the headers and is meant to store all constants needed upon startup by the JAPL virtual machine. For example, the code `var x = 1;` would have the number one as a constant. Constants are a compile-time view of the state of the VM's stack at runtime. +This section of the file follows the headers and is meant to store all constants needed upon startup by the JAPL virtual machine. For example, the code `var x = 1;` would have the number one as a constant. ## Behavior diff --git a/src/backend/serializer.nim b/src/backend/serializer.nim index 9fe0bbb..0a59a98 100644 --- a/src/backend/serializer.nim +++ b/src/backend/serializer.nim @@ -36,9 +36,10 @@ type ## procedures to store ## metadata fileHash*: string - japlVer*: string + japlVer*: tuple[major, minor, patch: int] japlBranch*: string commitHash*: string + compileDate*: int chunk*: Chunk @@ -68,6 +69,15 @@ proc toBytes(self: Serializer, d: SHA256Digest): seq[byte] = result.add(b) +proc bytesToString(self: Serializer, input: seq[byte]): string = + for b in input: + result.add(char(b)) + + +proc bytesToInt(self: Serializer, input: seq[byte]): int = + copyMem(result.addr, input.unsafeAddr, sizeof(int)) + + proc extend[T](s: var seq[T], a: openarray[T]) = for e in a: s.add(e) @@ -80,6 +90,9 @@ proc dumpBytes*(self: Serializer, chunk: Chunk, file, filename: string): seq[byt self.filename = filename self.chunk = chunk result.extend(self.toBytes(BYTECODE_MARKER)) + result.add(byte(JAPL_VERSION.major)) + result.add(byte(JAPL_VERSION.minor)) + result.add(byte(JAPL_VERSION.patch)) result.add(byte(len(JAPL_BRANCH))) result.extend(self.toBytes(JAPL_BRANCH)) if len(JAPL_COMMIT_HASH) != 40: @@ -131,12 +144,23 @@ proc loadBytes*(self: Serializer, stream: seq[byte]): Serialized = new(result) result.chunk = newChunk() var stream = stream - if stream[0..