Jump to content

[SOLVED] How would I...


mattd8752

Recommended Posts

I was thinking of making a PHP based programming language.  I am stumped almost right away.

 

I am thinking of having it work something like this:

(y) = 1

(x) = (y)

(x) = (x) + 1

 

SHOW {X is... (x).  X + 1 is {(x)+1}}

 

I could parse all of that easily, but how would I make it read info going as deep as the user wants into the code.  I would want the SHOW command to output: X is 2.  X + 1 is 3.

 

I am rather confused as to where I should start.

I want code to be able to run inside of scripts by putting the script to be parsed in curly braces.

 

I would want variables to also be replaced, in brackets.

 

Where should I start?  My idea is to parse the variable name and do:

$a_$varname = $value;

Then, it would be naming the variables a_their name.

I have no problem with that, but I am not even sure what functions to use for running code INSIDE of code.  I am not a super regex user, I do know how it works, and if you EXPLAIN it to me, I can figure it out.

 

Thanks, Matt

 

Please, just give me an idea of where I could get started.

Link to comment
Share on other sites

You do not know what recursion is and you are trying to make your own language? Wow dude. google.com  recursive functions.

 

Read up on it, that is a major key to save time with a lot of codes. Just you are lucky that programming languages like PHP provide array functions for you.

 

--FrosT

Link to comment
Share on other sites

From what I understand you are reffering to while

No I'm not, recursion is slightly similar to loops, but different

function parse($code)
{
$lines=explode(";",$code);
for($count=0;$count<num($lines);$count++)
{
if(preg_match("code within code",$lines[$count],$matches))
{
parse($resulting_code);
}
}
}
[code]
that's recursion

[/code]

Link to comment
Share on other sites

As fert has shown, recursion is simply a defined function being used within its own definition. I'm sure there is a lengthy amount on wikipedia if you need to understand more, however, most of that information is probably way more than you need. Try a few simple examples on your own and you will see how it works very quickly.

Link to comment
Share on other sites

Okay, so recursion is rather calling the function again, making it move back and repeat?

 

Anyway, I'm not concerned about splitting the code, however I'm not sure how I would parse it.

 

I know how to separate it, and run the code through the parser, but I am rather confused on how I would get the name of the variable and the value out of:

.a. = (value)

 

I decided to wrap variable in dots normal words in brackets.  Code in curly braces.  Anyway, how would I read this, and parse it.

 

Would a simple explode do the trick?

Link to comment
Share on other sites

Ok, firstly either don't wrap the variable names at all or use a single character. (I quite like PHP's use of $)

The dots aren't a good choice and they and using two is overkill.

What you do is when you find the variable character such as $ you set some sort of flag to tell your parser you are reading a variable name, then you keep reading characters until you find a non-variable character such as a space.

So, $somevar = (that); will start at $, gobble up somevar, stop and then you can record the data on the right side of the equals.

 

Parsing is not extremely difficult but still if you look at any interpreter such as Zend I am willing to bet there is a hefty amount of code behind code parsing!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.