vmbc/vm2.pb

60 lines
No EOL
1.2 KiB
Text

; vm
Structure opi
code.l
args.s
dat.s
EndStructure
; In theory this is a byte code interpreter
; Also mad science and may not work
Procedure writeblock (fid, *opco.opi)
; used by compile to write compiled code to file
WriteByte(fid, *opco\code)
WriteLong(fid, Len(*opco\args))
WriteData(fid, @*opco\args, MemorySize(@*opco\args))
WriteLong(fid, Len(*opco\dat))
WriteData(fid, @*opco\dat, MemorySize(@*opco\dat))
EndProcedure
Procedure readblock (fid, *opci.opi)
; should not need to be explained
*opci\code = ReadByte(fid)
*opci\args = Space(ReadLong(fid))
ReadData(fid, @*opci\args, MemorySize(@*opci\args))
*opci\dat = Space(ReadLong(fid))
ReadData(fid, @*opci\dat, MemorySize(@*opci\dat))
EndProcedure
;for demo purposes we hard wire the code
opcobj.opi\code = $01
opcobj.opi\args = "Hi world"
opcobj.opi\dat = "Bye world!"
; now write bytecode to file
OpenFile(0,"test.byc")
writeblock(0, opcobj)
CloseFile(0)
; now read
OpenFile(1,"test.byc")
readblock(1, opcoobj.opi)
CloseFile(1)
;run
Select opcoobj\code
Case $01 : MessageRequester("ouput",opcoobj\dat)
EndSelect
; IDE Options = PureBasic 6.00 LTS (Linux - x64)
; CursorPosition = 35
; Folding = -
; EnableXP