Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / creating a interpeted language, help needed please.

Author
Message
CloseToPerfect
23
Years of Service
User Offline
Joined: 20th Dec 2002
Location: United States
Posted: 3rd Aug 2003 04:46
I'm tring to create a simple programming language, like LOGO. I have it where you can enter your code, it checks for know commands, like run,list,ect and it lets you create a full source. My problem comes in executing the code you make.
The way I'm doing is like this.
MOVE FORWARD 10
breaks string into words 'MOVE','FORWARD','10'
runs though the commands until it finds 'MOVE'
if command$ = "MOVE" then
this then has 2 possible arguments FORWARD or BACKWARD
2 more 'if thens' to find FORWARD
then I can just call a function with the value of 10 in it.

this seems very sloppy and to make it worse, this is where I really need help, It competly breaks down using this method when you try to use case opperatives like 'IF THEN ELSE'.

Does anyone have any helpful suggestions on doing this, or some code to share, or even some websites to read up on it.

thank you
CTP
Rob K
Retired Moderator
23
Years of Service
User Offline
Joined: 10th Sep 2002
Location: Surrey, United Kingdom
Posted: 3rd Aug 2003 05:40
The first thing is to brake it down (tokenize) it. That means seperate it into words seperated with spaces (as you have already done).

First I think you should check the token and determine whether it is a keyword (like IF). If it is, then you can send the code off to deal with that particular construct.

To make life easy, start each construct on a new line.

if "IF" is the first token of a line, then an expression (a=b) should be next etc.


Once you have determined that it is not a keyword (in the case of "MOVE") then you can check it against your command list.

This isn't working code, just a scratch up.

Note: The next token function iterates through your array of tokens and returns the next one.

Your IF construct would automatically result in a search for ENDIF if the "IF" test failed, and you would carry on examining tokens from there.

Similarly loops would result in only tokens between the start and the end of the loop to be run.



Rob K
Retired Moderator
23
Years of Service
User Offline
Joined: 10th Sep 2002
Location: Surrey, United Kingdom
Posted: 3rd Aug 2003 05:43
No doubt there are a million better ways to do it. You could also have a system whereby you identified the command, pushed the next say one or two tokens onto a stack (which would become the parameters) and actually execute the command later on in the code.

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Aug 2003 13:26
A good starting point is here : http://compilers.iecc.com/crenshaw/

It will show you the way to build a recursive-descent parser. Ignore the fact that he's coding in Pascal and outputting 68000 machine code - just concentrate on the techniques.
David T
Retired Moderator
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 3rd Aug 2003 15:39
If you don't mind not using DB for the IDE, the microsoft scripting control allows to to create your own language in a tool such as VB. Your VB app could then parse the language and output some raw info to a text file, such as this, which might move an object forward 60:

0
0
60


(the x,y,z pos will be repeatedly stored)

and when it recieve a command saying "back 40", then next line might say

0
0
20


and you DBP program would read this, and execute the moves.

You are the th person to view this signature.
GRAVITY: I fought the law but the law won
CloseToPerfect
23
Years of Service
User Offline
Joined: 20th Dec 2002
Location: United States
Posted: 4th Aug 2003 02:58
Rob, thx some of that was helpful, most was about where I was at, but Thanks indeed!

Ian, that is just a wonderful piece of infomation, more then I'll ever need but great none the less!

David, I want to make a run time language, scritpting out and then running from another program isn't really what I want. I want it to be a multiplayer programming contest, where are contestants are given the same goal and race to program the solution. Should be some fun mental stress.

Thanks all, I appreciate the help.
CTP
David T
Retired Moderator
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 4th Aug 2003 14:51
... Then you'll have to take into account the speed of peoples' typing too!

You are the th person to view this signature.
GRAVITY: I fought the law but the law won
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 4th Aug 2003 17:01
I remember a piece of code I had on the AtariST, it was from a book called GFA Basic for Experts or something, anyway it had a really amazing maths parser, which had variables and everything. You could basically get it calculate any formula you threw at it. I reckon a similar approach would help you.

In your language, your variables will need a lot of flexibility, so I suggest storing all your variable names in an array, and storing the value as text in an array too. You need to be able to pass a variable name to a function and get it's value, it's about the only neat way to handle live variables like this.

At each line, you will have to replace each variable name with it's value, then you would do your maths passes. Most languages use brackets to seperate complex formulas, so you'd look for small calculations inside brackets, calculate then remove the brackets, basically replace each set of brackets with a value. Pass over the line until there are no more brackets then calculate each pair of values, so 432+5342-321*2 would be passed as:

432+5342=5774, so line is 5774-321*2
5774-321=5453, so line is 5453*2
5453*2=10906, so line is 10906

See how it gets broken into little bits that can be easily calculated with a function, just find each value and what is happening to them, then return the result which is then put in place of the original equation.

I hope your not wondering what this has to do with IF statement, because after you've done your math passes, you might have something like:

IF valuea=(32+5342-321*2)

Which would be converted to:

IF 4423=10906

Then, it's easy to check this with a function in a similar fashion to the calculator function, even if the function returned a 1 if the check passed, 'IF 1' could be an indicator to perform the lines until the ENDIF.


Van-B

My cats breath smells of cat food.

Login to post a reply

Server time is: 2026-07-22 10:04:38
Your offset time is: 2026-07-22 10:04:38