Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


On interpreters...

This topic is 5806 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Originally, I had intentions to build a compiler for a custom language of mine. I wanted the compiler to turn my source into Intel-32 bit assembly language. Then, combined with my compiler would be NASM, the free and excellent assembler that is often used. The assembly source would then be assembled by NASM and turned into an executable. After much thinking I realized I felt like that would be incomplete, since I was using NASM, and not my own assembler. So, I thought about the possibility of creating an assembler and linker module along with my compiler. Mind you this language was simple and not very robust, so it wasn''t a huge, complex language or anything; so I thought it would be possible. However, I figured an assembler would be very hard since I''d need lots of documentation on the binary codes for different assembly commands and combinations. Thus, it would easily get confusing and difficult. Now lately I''ve thought about just making an interpreter, similar to the old BASIC or Python of today. I have questions though. * What is the basic theory of an interpreter? a) Essentially, how do they work? b) Is any of the code translated into assembly? c) Does the interpreter rely on the library commands of the language that is building the interpreter? In other words, if I have a "print" command in my language, is that really just using "cout" in C++ behind the scenes? I hope you understand what I mean. I''m not sure how they work, so a brief explaination would help a lot. I''m not sure if it''s really just an interface layer between my language and the build language, or a virtual machine kind of thing, I really have no idea. I just want the concepts behind the old BASIC or Python. Thanks a lot for any help!

Share this post

Link to post
Share on other sites
Your interpreter reads the source file, and carries out actions that are specified in the source file. There is no conversion from HLL to assembly, or to any other language, it is all carried out in the interpreter itself.

Think of what a batch file is in Windows, and you kind of get the idea.

Share this post

Link to post
Share on other sites
Some interpreters (like java) do compile to a lower level language, but not to assembly. They take a high-level command and convert it to some bytes the program can easily read, like if you have "X=5+A" it would generate maybe
which is the code for a simple stack machine. Stack machines work by putting things on a stack (list) and then each operator (like 'add') takes however many things it needs off and does something (add in this case) and puts the answer back on the stack. The command 'pop' is used to take a value off of the stack and then store it somewhere.

In order to convert something high level into the virtual assembly, you need to build a parse tree (I think thats the name anyways). For example, if you have the code "x = GetPlayerSpeed(player1)*3+5-GetPlayerSpeed(player2)/10" the parse tree would look something like:

If you recursively navigate the tree, you can generate the instructions, like
push player1
call GetPlayerSpeed
push 3
push 5
push player2
call GetPlayerSpeed
push 10
pop x

And you could convert each one to a few bytes that your program could read in and easily 'execute' to get the desired result. Since its in a really simple form, its a lot faster for the computer to parse it (but you need a compiler or something to convert it to the list of simple instructions. you could do that during load time or make it so your script editor does it every time you save or something like that)

Any 'library' functions would be special cases, and the program would basically map them to the appropriate c++ command. Often, there will be c/c++ functions in between that take commands from the interpreter and use the information they get to properly call a c/c++ function to do something. Those functions in the middle are often called bindings.

There are several tutorials on gamedev about creating scripting languages.

[edited by - Extrarius on January 19, 2003 2:00:54 PM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!