Advertisement

Compiling code

Started by June 03, 2001 03:52 PM
4 comments, last by INVERSED 23 years, 8 months ago
After reading the tutorial on writing precompiled scripts, I have a question, how exactly do you compile code (script or otherwise). I don''t exactly understand what you would have to do to it in order to turn it from a script file into code (or byte code for that matter what makes that different from compiled code). If anyone knows what''s the deal, I''d appreciate them sharing their knowledge. Thanx all.
Write more poetry.http://www.Me-Zine.org
In it''s simplest form "compiling" is just taking a script and making it easier/faster for a computer to interpret.
So it would remove the whitespace (which is useless for the computer and slows it down), replace variable names with shorter names to conserve memory, possibly replace recognized commands with integers (faster comparison, searching, etc). Byte code functions on this principle...interpreted code.

Of course you could try to compile the scripts into an actual DLL...but that''s beyond my skills.
Although I did make a compiler using VB.NET. :-)

Actually I just used the in-memory compiler for my scripts, and then create objects out of the compiled code. Of course this is only easy with the .NET platform. Maybe I''ll post some code sometime...but it will only be useful for those using a .NET language (Managed C++, C#, VB.NET, COBOL.NET, JScript.NET, etc)


Epolevne
Advertisement
what i did was read in the script into the compiler.
parse and store the op code into a vector

example

              // script function    if(x == 1)       myfunction();    else       myotherfunction();   // would translate into    100 SKIPTEST x==1 GOTO 103  # if fail, call myotherfunction   101 CALL     myfunction   102 GOTO     104   103 CALL     myotherfunction   104 ...            


SKIPTEST, CALL , LABEL and GOTO are op code's in my compiler.
then i just store the op codes into a binary file.

the binary file is then read by the intepreter within the game as instructions.



{ Stating the obvious never helped any situation !! }

Edited by - jwalker on June 3, 2001 6:03:38 PM
Hey thanx, that makes sense.
Write more poetry.http://www.Me-Zine.org
sure thing no problem....

just make a good parser, since that was the part i worked on the most..

also learn up some BNF (backnus naur form)
that will help you represent your language in mathematical form..



{ Stating the obvious never helped any situation !! }
i finished a script engine yesterday and this is my approach:

i just did this in my language to compile:
-number all the methods used
-compile the script line by line, running it trough a preprocessor that changes "dim_int test" and other types into a new variable in my varTable() and i did the same for labels used by goto statements (so i can use goto "start" instead of goto 1
-check if the line is a method or a var, then change it to the corresponding number (all vars and methods have a number)

to run the script i did this:
-load the table with the vars and the methods
-load the compiled lines and change the nrs back into normal names and call the methods/change the vars
-when the goto statement is called, it just runs all the code from that point up to the end of a script or a new goto statement

This topic is closed to new replies.

Advertisement