Page 1 of 1

running commands

Posted: Tue Jul 25, 2006 9:08 am
by simmy
I am working on designing a dos-like or shell interface which can take in commands and perform the task given.

for example:

$ add 2 and 3
would return....
$ 5

how do i go about making such a thing? i have cygwin, with all of the packages installed, what other tools would i need?

Re:running commands

Posted: Tue Jul 25, 2006 12:00 pm
by Cheery
First, you must define the syntax you want to use, then you must write something you can use to parse it, then you must interpret the parsed code or compile it into something you target on.

Re:running commands

Posted: Fri Aug 04, 2006 8:45 pm
by earlz
I have not written a compiler/interpretor(yet...) but my idea is if your going to have everything space seperated, just go through and replace the spaces with nulls(\0) and then read the string in and do a strcmp() on it and do it accordingly and then just advance the pointer to strlen()+1 and then do an atoll()(I think thats it) to convert it to a number and then you can figure it out from there

Re:running commands

Posted: Sun Aug 06, 2006 3:34 am
by Pype.Clicker
the main question is whether you want to have it working with "add 2 and 3" or rather "2 + 3" ... or "what do you get if you multiply six by nine" ...

if we consider a syntax such as "add 2 and 3" or "print 5" things are fairly simple: your first token (space-separated item) fully decide of what to do. when you hit the thing like "2 + 3" and have to translate that into "keep constant 2 for later use"; remind we'll have to add stored constant with the next "item" and finally "keep constant 3, oh, we had to add things, well, let's do it, then you really start needing a compiler/interpreter logic.

If you really want something simple, ask your user to use RPN, that is he can type

Code: Select all

add 2 3  ;; system will print out 5
add 1 2 3 4 ;; system will print 10
add (mul 2 3 ) (mul 4 5) ;; system will print 25
that's not that user-friendly, but that's something you can simply analyse with recursive parsing and process almost naturally. For other syntaxes, you have to recognize larger expressions, produce a list of actions and execute them. e.g. using a 'shift/reduce' stack

Re:running commands

Posted: Sun Aug 06, 2006 5:00 am
by Cheery
Pype.Clicker, I must interrupt you now. :) The syntax you shown is not RPN, it is PN. Logo is an example from such syntax. And Lisp.

RPN is like this:

Code: Select all

2 3 add
1 2 add 3 add 4 add
2 3 mul 4 5 mul add
And you don't necessarily need such names, you can just use normal arithmetic symbols: + - / *

The both syntaxes are excellent. Both of them can be used for structured data and programming. And they are easy to parse.

Re:running commands

Posted: Sun Aug 06, 2006 5:11 am
by Pype.Clicker
Cheery wrote: Pype.Clicker, I must interrupt you now. :) The syntax you shown is not RPN, it is PN. Logo is an example from such syntax. And Lisp.
whoops ...
(where's the smiley that smiles while eetching one's backhead with drooling eyes and a manga-like teardrop on the face ?)

[tt]^^"[/tt]

Re:running commands

Posted: Sun Aug 06, 2006 8:11 am
by earlz
if your anything serious about this then you may want to read the attached file, it was a tutorial I got from somewhere though it teaches it all in pascal

ok since the forum has a 30k limit its here
http://jouleos.galekus.com/temp/tokens.txt