Peon bytecode files can now be executed directly

This commit is contained in:
Mattia Giambirtone 2022-12-09 13:42:52 +01:00
parent f86925edba
commit b912d58cad
1 changed files with 35 additions and 32 deletions

View File

@ -177,39 +177,42 @@ proc runFile(f: string, fromString: bool = false, dump: bool = true, breakpoints
else: else:
input = f input = f
f = "<string>" f = "<string>"
tokens = tokenizer.lex(input, f) if not f.endsWith(".pbc"):
if tokens.len() == 0: tokens = tokenizer.lex(input, f)
return if tokens.len() == 0:
when debugLexer: return
styledEcho fgCyan, "Tokenization step:" when debugLexer:
for i, token in tokens: styledEcho fgCyan, "Tokenization step:"
if i == tokens.high(): for i, token in tokens:
# Who cares about EOF? if i == tokens.high():
break # Who cares about EOF?
styledEcho fgGreen, "\t", $token break
echo "" styledEcho fgGreen, "\t", $token
tree = parser.parse(tokens, f, tokenizer.getLines(), input) echo ""
if tree.len() == 0: tree = parser.parse(tokens, f, tokenizer.getLines(), input)
return if tree.len() == 0:
when debugParser: return
styledEcho fgCyan, "Parsing step:" when debugParser:
for node in tree: styledEcho fgCyan, "Parsing step:"
styledEcho fgGreen, "\t", $node for node in tree:
echo "" styledEcho fgGreen, "\t", $node
compiled = compiler.compile(tree, f, tokenizer.getLines(), input, disabledWarnings=warnings, showMismatches=mismatches, mode=mode) echo ""
when debugCompiler: compiled = compiler.compile(tree, f, tokenizer.getLines(), input, disabledWarnings=warnings, showMismatches=mismatches, mode=mode)
styledEcho fgCyan, "Compilation step:\n" when debugCompiler:
debugger.disassembleChunk(compiled, f) styledEcho fgCyan, "Compilation step:\n"
echo "" debugger.disassembleChunk(compiled, f)
var path = splitFile(f).dir echo ""
if path.len() > 0: var path = splitFile(f).dir
path &= "/" if path.len() > 0:
path &= splitFile(f).name & ".pbc" path &= "/"
if dump and not fromString: path &= splitFile(f).name & ".pbc"
serializer.dumpFile(compiled, f, path) if dump and not fromString:
serialized = serializer.loadFile(path) serializer.dumpFile(compiled, f, path)
serialized = serializer.loadFile(path)
else:
serialized = serializer.loadBytes(serializer.dumpBytes(compiled, f))
else: else:
serialized = serializer.loadBytes(serializer.dumpBytes(compiled, f)) serialized = serializer.loadFile(f)
when debugSerializer: when debugSerializer:
styledEcho fgCyan, "Serialization step: " styledEcho fgCyan, "Serialization step: "
styledEcho fgBlue, "\t- Peon version: ", fgYellow, &"{serialized.version.major}.{serialized.version.minor}.{serialized.version.patch}", fgBlue, " (commit ", fgYellow, serialized.commit[0..8], fgBlue, ") on branch ", fgYellow, serialized.branch styledEcho fgBlue, "\t- Peon version: ", fgYellow, &"{serialized.version.major}.{serialized.version.minor}.{serialized.version.patch}", fgBlue, " (commit ", fgYellow, serialized.commit[0..8], fgBlue, ") on branch ", fgYellow, serialized.branch