Author Topic: Bytecode Interpreter concept  (Read 29586 times)

0 Members and 2 Guests are viewing this topic.

Aurel

  • Guest
Bytecode Interpreter concept
« on: May 20, 2014, 01:16:24 PM »
Hi to all... :)
For a start i will present here Ed Davis (our member) toy2....(i am not sure if name is right)
bytecode interpreter which he present in C,euphoria,VB script, qb64 and in FreeBasic version.
(Ed ..if i miss something ..let me know  ;) )

Ok here is FB version which i would like to convert to Oxygen Basic....


.

Mike Lobanovsky

  • Guest
Re: Bytecode Interpreter concept
« Reply #1 on: May 20, 2014, 01:35:35 PM »
Thanks Aurel,

...which i would like to convert to Oxygen Basic....
... and you have already made your first step in that direction: you've changed the file extension to .o2bas. ;D

Aurel

  • Guest
Re: Bytecode Interpreter concept
« Reply #2 on: May 20, 2014, 02:19:39 PM »
Ahh yes ...that is how editor save file ,and nothing else.
Also i am looking agian in DLib -source code file called parser
which is suposed to be source file -> bytcode file translator... ::)
Hmmm concept is very different with a PB awkward syntax...

Mike Lobanovsky

  • Guest
Re: Bytecode Interpreter concept
« Reply #3 on: May 21, 2014, 03:33:12 AM »
Hello Aurel,

Let us concentrate on toy2 first, OK? It will be easier to talk about extending it after it's been ported to O2.

Can you also provide the C language version of it, please?

Aurel

  • Guest
Re: Bytecode Interpreter concept
« Reply #4 on: May 21, 2014, 05:38:15 AM »
sorry Mike...i don't have C version but Ed have and maybe he can provide link to file
from QDepartment site ,,I cannot get this file because is something wrong with my yahoo
account.. :-\

Mike Lobanovsky

  • Guest
Re: Bytecode Interpreter concept
« Reply #5 on: May 21, 2014, 06:50:30 AM »
Forget it Aurel,

It isn't really necessary - the code is very straight-forward and can be ported to C as easily as to O2 with just two additional user functions, mid() and right().

Mike Lobanovsky

  • Guest
Re: Bytecode Interpreter concept
« Reply #6 on: May 23, 2014, 04:22:25 PM »
Hello Aurel,

I've got toy2 working in FBSL's BASIC so there won't be any problem to port it to Oxygen now.

Stay tuned.

.

JRS

  • Guest
Re: Bytecode Interpreter concept
« Reply #7 on: May 23, 2014, 06:00:12 PM »
You have my ear when you get to the string handling part.

Attached is all the Toy files from QDepartment.


.
« Last Edit: May 23, 2014, 06:20:27 PM by John »

Aurel

  • Guest
Re: Bytecode Interpreter concept
« Reply #8 on: May 23, 2014, 08:40:55 PM »
Hi Mike...
That is good ...
But where is the source ?
I would like to try FBSL version  :)

Mike Lobanovsky

  • Guest
Re: Bytecode Interpreter concept
« Reply #9 on: May 24, 2014, 05:09:55 AM »
Hi John,

Thanks a lot for this zip. I have a very old Yahoo account that doesn't work for new Yahoo services like QDepartment. I guess that's also the case with Aurel's account. Now I will also be able to try Ed's C version with my Dynamic C. His C version has a slightly different implementation but at a first glance, it should work out of the box too.

Hi Aurel,

Here comes the zip with the FBSL source script, precompiled executable, and test script primes.toy. You can execute the task in several different ways:

1. by dragging-and-dropping primes.toy on toy.exe;
2. if you have Fbsl.exe v3.5 associated with .FBS script files, by double-clicking on toy.fbs and entering the name of primes.toy into the console if primes.toy is in the same directory as toy.fbs, or a full pathname of primes.toy, if it is stored in some other directory; and
3. by opening up your shell console and dragging-and-dropping into it successively (with spaces in between!) your existing Fbsl.exe v3.5 file, the toy.fbs script file, and finally, the primes.toy test file and then hitting Enter to execute the bunch.

And of course, you can also execute toy.fbs from your Eclecta editor and enter the name of primes.toy into the console as described in Item 2 above.

.

Aurel

  • Guest
Re: Bytecode Interpreter concept
« Reply #10 on: May 24, 2014, 10:12:44 AM »
Thanks Mike  ;)
Works fine
Mike is this exe with dynC or pure FBSL?
I have try with 10000 iteration which need cca 4 sec and it looks to me like the
same result i get with FreeBasic version.
I would like to crete non-console o2 version with a 'dirty hack' editor  :D
ok thanks again  ;)

Mike Lobanovsky

  • Guest
Re: Bytecode Interpreter concept
« Reply #11 on: May 24, 2014, 11:56:28 AM »
You're welcome Aurel,

And no, this isn't DynC. This is FBSL's interpretative BASIC - toy.fbs "compiled" into a usual FBSL executable. I haven't yet tried Ed's C version in FBSL DynC but I'm sure it'll run a few dozen times faster than this script. I think the FreeBASIC version in fact runs also much much faster than it seems.

Anyway, you can time it yourself adding the following lines

.............
dim gtc = GetTickCount()
interpret()
print "completed in ",  (GetTickCount() - gtc) / 1000, " seconds"
.............

to sub main() to measure the exact time it takes to find, say, 10000 primes. You can also add similar timing to the FreeBASIC script in its sub main() using its Timer and Print keywords and recompile the script to see the real benchmark figures. Make sure your FbEdit Options->Build Options->Command contains the fbc -lang qb -s console parameters when recompiling it.

In either case, the bottleneck will be the Print statement which is rather slow and unpredictable speed-wise in every language. The arithmetics loop proper will run much faster without it and you can only let primes.toy print the last result to the console so that you can check if the code runs properly in the both languages you're trying to compare.

Finally, the "dirty hack" isn't so good for line parsing as it may seem at a first glance - it is very slow while doing it. Oxygen has a built-in getfile() function which is similar to FBSL's FileGet(BINARY). And you can use an Oxygen equivalent to my function get_line() (the bottommost function in the toy.fbs script) to parse the code buffer into successive code lines. This will be much faster than calling the richedit control to yield its lines of text. Richedit has to do quite a lot of conversion internally before it can give you what you want. :)

Kuron

  • Guest
Re: Bytecode Interpreter concept
« Reply #12 on: May 24, 2014, 12:57:09 PM »
Interesting thread for reading.  I don't have access to a Windows machine, so I can't test anything.

Mike Lobanovsky

  • Guest
Re: Bytecode Interpreter concept
« Reply #13 on: May 24, 2014, 01:22:52 PM »
BTW Aurel, now that I'm looking into my get_line() again, it doesn't seem very nice to me any more. It contains bloat left over from my earlier variants tested for the optimum strategy.

The following would be a neater one, of course, in terms of conventional BASIC syntax (FBSL can be much more laconic than that):

function get_line() as string // PARSE CODE BUFFER INTO SUCCESSIVE LINES
   static head as long = 1
   dim tail as long
   dim ret as string
   
   tail = instr(code, crlf, head, true)
   ret = mid(code, head, tail - head)

   if ret = "" then return "<eof>"
   head = tail + 2

   return ret
end function

Mike Lobanovsky

  • Guest
Re: Bytecode Interpreter concept
« Reply #14 on: May 24, 2014, 01:40:39 PM »
Kuron,

FBSL v3.5 runs well enough in Wine v1.7 under Linux and Mac OS X too. :)