Fixed many bugs in the serializer and added (de)serialization output to main.nim
This commit is contained in:
parent
afa4369321
commit
5cc2a60c8c
|
@ -43,6 +43,10 @@ type
|
||||||
chunk*: Chunk
|
chunk*: Chunk
|
||||||
|
|
||||||
|
|
||||||
|
proc `$`*(self: Serialized): string =
|
||||||
|
result = &"Serialized(fileHash={self.fileHash}, version={self.japlVer.major}.{self.japlVer.minor}.{self.japlVer.patch}, branch={self.japlBranch}), commitHash={self.commitHash}, date={self.compileDate}, chunk={self.chunk[]}"
|
||||||
|
|
||||||
|
|
||||||
proc error(self: Serializer, message: string) =
|
proc error(self: Serializer, message: string) =
|
||||||
## Raises a formatted SerializationError exception
|
## Raises a formatted SerializationError exception
|
||||||
raise newException(SerializationError, &"A fatal error occurred while serializing '{self.filename}' -> {message}")
|
raise newException(SerializationError, &"A fatal error occurred while serializing '{self.filename}' -> {message}")
|
||||||
|
@ -74,7 +78,7 @@ proc bytesToString(self: Serializer, input: seq[byte]): string =
|
||||||
result.add(char(b))
|
result.add(char(b))
|
||||||
|
|
||||||
|
|
||||||
proc bytesToInt(self: Serializer, input: seq[byte]): int =
|
proc bytesToInt(self: Serializer, input: array[8, byte]): int =
|
||||||
copyMem(result.addr, input.unsafeAddr, sizeof(int))
|
copyMem(result.addr, input.unsafeAddr, sizeof(int))
|
||||||
|
|
||||||
|
|
||||||
|
@ -147,18 +151,18 @@ proc loadBytes*(self: Serializer, stream: seq[byte]): Serialized =
|
||||||
try:
|
try:
|
||||||
if stream[0..<len(BYTECODE_MARKER)] != self.toBytes(BYTECODE_MARKER):
|
if stream[0..<len(BYTECODE_MARKER)] != self.toBytes(BYTECODE_MARKER):
|
||||||
self.error("malformed bytecode marker")
|
self.error("malformed bytecode marker")
|
||||||
stream = stream[len(BYTECODE_MARKER) - 1..^1]
|
stream = stream[len(BYTECODE_MARKER)..^1]
|
||||||
result.japlVer = (major: int(stream[0]), minor: int(stream[1]), patch: int(stream[2]))
|
result.japlVer = (major: int(stream[0]), minor: int(stream[1]), patch: int(stream[2]))
|
||||||
stream = stream[2..^1]
|
stream = stream[3..^1]
|
||||||
let branchLength = stream[0]
|
let branchLength = stream[0]
|
||||||
stream = stream[1..^1]
|
stream = stream[1..^1]
|
||||||
result.japlBranch = self.bytesToString(stream[0..<branchLength])
|
result.japlBranch = self.bytesToString(stream[0..<branchLength])
|
||||||
stream = stream[branchLength..^1]
|
stream = stream[branchLength..^1]
|
||||||
result.commitHash = self.bytesToString(stream[0..<40]).toHex()
|
result.commitHash = self.bytesToString(stream[0..<40]).toLowerAscii()
|
||||||
stream = stream[39..^1]
|
stream = stream[40..^1]
|
||||||
result.compileDate = self.bytesToInt(stream[0..7])
|
result.compileDate = self.bytesToInt([stream[0], stream[1], stream[2], stream[3], stream[4], stream[5], stream[6], stream[7]])
|
||||||
stream = stream[7..^1]
|
stream = stream[8..^1]
|
||||||
result.fileHash = self.bytesToString(stream[0..<32]).toHex()
|
result.fileHash = self.bytesToString(stream[0..<32]).toHex().toLowerAscii()
|
||||||
except IndexDefect:
|
except IndexDefect:
|
||||||
self.error("truncated bytecode file")
|
self.error("truncated bytecode file")
|
||||||
|
|
||||||
|
|
27
src/main.nim
27
src/main.nim
|
@ -21,6 +21,9 @@ import backend/serializer
|
||||||
|
|
||||||
import strformat
|
import strformat
|
||||||
import strutils
|
import strutils
|
||||||
|
import sequtils
|
||||||
|
import times
|
||||||
|
import nimSHA2
|
||||||
|
|
||||||
|
|
||||||
proc hook() {.noconv.} =
|
proc hook() {.noconv.} =
|
||||||
|
@ -34,18 +37,15 @@ proc main() =
|
||||||
var tree: seq[ASTNode]
|
var tree: seq[ASTNode]
|
||||||
var optimized: tuple[tree: seq[ASTNode], warnings: seq[Warning]]
|
var optimized: tuple[tree: seq[ASTNode], warnings: seq[Warning]]
|
||||||
var compiled: Chunk
|
var compiled: Chunk
|
||||||
var serializedBytes: seq[byte]
|
var serialized: Serialized
|
||||||
|
var serializedRaw: seq[byte]
|
||||||
var lexer = initLexer()
|
var lexer = initLexer()
|
||||||
var parser = initParser()
|
var parser = initParser()
|
||||||
var optimizer = initOptimizer(foldConstants=false)
|
var optimizer = initOptimizer(foldConstants=false)
|
||||||
var compiler = initCompiler()
|
var compiler = initCompiler()
|
||||||
var serializer = initSerializer()
|
var serializer = initSerializer()
|
||||||
|
|
||||||
var japlBranch = ""
|
var hashMatches: bool
|
||||||
var japlVersion = ""
|
|
||||||
var japlCommitHash = ""
|
|
||||||
var fileHash = ""
|
|
||||||
var compileDate = 0
|
|
||||||
echo "NimVM REPL\n"
|
echo "NimVM REPL\n"
|
||||||
while true:
|
while true:
|
||||||
try:
|
try:
|
||||||
|
@ -97,10 +97,17 @@ proc main() =
|
||||||
disassembleChunk(compiled, filename)
|
disassembleChunk(compiled, filename)
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
serializedBytes = serializer.dumpBytes(compiled, source, filename)
|
serializedRaw = serializer.dumpBytes(compiled, source, filename)
|
||||||
echo "(De)Serialization step:"
|
echo "Serialization step: "
|
||||||
echo "\t"
|
echo &"\tRaw hex output: {serializedRaw.mapIt(toHex(it)).join(\"\").toLowerAscii()}"
|
||||||
echo &"\tRaw byte stream: [{serializedBytes.join(\", \")}]"
|
echo ""
|
||||||
|
|
||||||
|
serialized = serializer.loadBytes(serializedRaw)
|
||||||
|
hashMatches = if computeSHA256(source).toHex().toLowerAscii() == serialized.fileHash: true else: false
|
||||||
|
echo "Deserialization step:"
|
||||||
|
echo &"\t\t- File hash: {serialized.fileHash} (matches: {hashMatches})"
|
||||||
|
echo &"\t\t- JAPL version: {serialized.japlVer.major}.{serialized.japlVer.minor}.{serialized.japlVer.patch} (commit {serialized.commitHash[0..7]} on branch {serialized.japlBranch})"
|
||||||
|
echo &"\t\t- Compilation date & time: {fromUnix(serialized.compileDate).format(\"d/M/yyyy H:m:s\")}"
|
||||||
except:
|
except:
|
||||||
echo &"A Nim runtime exception occurred: {getCurrentExceptionMsg()}"
|
echo &"A Nim runtime exception occurred: {getCurrentExceptionMsg()}"
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -32,7 +32,7 @@ proc nl =
|
||||||
|
|
||||||
|
|
||||||
proc simpleInstruction(name: string, offset: int): int =
|
proc simpleInstruction(name: string, offset: int): int =
|
||||||
write stdout, &"DEBUG - Disassembler->\tInstruction :"
|
write stdout, &"DEBUG - Disassembler->\tInstruction: "
|
||||||
printName(name)
|
printName(name)
|
||||||
nl()
|
nl()
|
||||||
return offset + 1
|
return offset + 1
|
||||||
|
|
Loading…
Reference in New Issue