TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: JohnK on November 15, 2011, 06:51:25 PM

Title: (Teaser) A new pseudocode/prototype language
Post by: JohnK on November 15, 2011, 06:51:25 PM
I probably shouldn't be posting this just yet because this project is no where near ready to be released but I got so excited when my latest idea started to work I almost fell out of my chair with excitement.

My latest idea was to create a language that was easier to write/read that could be used to prototype or pseudocode functions in AutoLisp with however, I didn't want to just define a new language (just define a new syntax or something). I wanted to have the ability to actually generate AutoLisp code from it so...I am writing a program to read this new language and produce AutoLisp code from it.

Inputing this:
Code: [Select]
abc(x)
Generates this:
Code: [Select]
(abc x)
Inputing this:
Code: [Select]
1 + 2 * 3 - 5
Generates this:
Code: [Select]
(- (+ 1 (* 2 3)) 5)
Inputing this:
Code: [Select]
def fib(x)
    if (x < 3) then
        1
    else
        fib(x-1)+fib(x-2);

Generates this:
Code: [Select]
(defun fib (x)
    (if (< x 3)
        1
        (+ (fib (- x 1)) (fib (- x 2)))) )

I'm sorry I don't have anything (a compiled program) for you to play with but this program is still very "green" and there is a LOT of work left to be done before it is anywhere near usable--and if I'm totally honest, I hit quite a few brick walls just getting this far so I could slam into another one tonight and have to start over-.

So what do you think?
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: dgorsman on November 15, 2011, 07:02:52 PM
Could probably save some development time using XML, XSD (validation and Intellisense in various editors), and then applying XSL transforms to convert the XML data to "raw" LISP.  The hierarchical nature of XML should lend itself well to the matching parenthesis requires by LISP.
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: JohnK on November 16, 2011, 12:35:04 PM
Not quite sure I follow your thought (this isn't editor specific). Are you saying to just create the XSLT for an editor to apply it/them?

I don't know what route to take this. I think I can make a standalone interpreter from it, or add it to another tool like LiFP, or both I suppose (LiFP is only an "engine" of sorts...it doesn't really care about file types or the contents of them so this would be just another tool in it's tool box I guess). ...I dunno. *shrug*

At any rate, whatever the outcome is, I learned a lot, had quite a bit of fun, and lost plenty of sleep, doing it sofar. *lol*
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: Pad on November 17, 2011, 05:49:10 AM
looks good Se7en
Good luck with the development of it.
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: JohnK on November 22, 2011, 02:05:33 PM
Update:

This is where the language stands. The snip-it(s) below doesn't actually make any sense as a program, I was just typing out lines to see how well this program can parse the different syntax nuances (notice the multiple statements in the ELSE portion of the IF and the BODY of the WHILE).

Code: [Select]
def foo(x)
    if (x < 3) then
        1;
    else
        foo(x-1)
        foo(x-2);
    var y = 1;
    cons a, y;
    while i < y do
          i + y
          y + a;
end;

The above would produce the following (with a little bit of reformatting; my current method for formatting is a bit wonky at the moment and I need to rethink a way to actually do that better).

Code: [Select]
(defun foo( x )
  (if (< x 3)
    1
    (progn
      (foo(- x 1))
      (foo(- x 2))
      )
    )
  (setq y 1)
  (cons a y)
  (while (< i y)
         (+ i y)
         (+ y a)
   )
 )

I did have a major setback but I was able to re-purpose/use some code from the LLVM/CLANG project and thus get back to where I was and even put this program in better shape (Those guys working on that project must be/are super smart).


Do [you] have any thoughts (do you think this is easier to read than plain lisp?, do you think this is a great idea?, do you think this is the dumbest idea you have ever seen?)?
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: Jeff H on November 22, 2011, 02:22:25 PM
From someone who is not familiar or use to AutoLisp it is easier read.
 
I can glance at the first block and quickly tell what it is doing, or wonder why 'a' & 'y' is not initialized or set.
 
I understand it was for an example and not meant for critiquing, but seriously if code could be written like that I would use it instead of creating a .NET app for simple things.
 
 
 
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: MexicanCustard on November 22, 2011, 04:10:14 PM
What would really be awesome is something that would translate LISP back into your psuedo code. Then .NET programers could understand how LISP guys are performing certain task. Since there are 10 times as many LISP examples around the internet than .NET examples.

I've always thought translating some of Lee's stuff to .NET would be a cool way to start understanding LISP. Then I get about 4 lines into it and say to hell with it because its so hard to read.
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: JohnK on November 22, 2011, 05:14:59 PM
@Jeff
That's great.

Wow!? I don't know if I could ever take it to a level that would support/allow for full blown tool development (I don't know if I could support all the built-in AutoLisp functions or the Visual Lisp functions)...I was aiming for the ability to "prototype" several different kinds of ideas.

Right now, this program can parse:
DEF - a definition
IF - a conditional
VAR - create a variable
CONS - create a list
WHILE and FOR - looping constructs

So I was thinking that if someone wanted to prototype the either a math function(s) or a list operation(s) they could do that with where the project stands right now (which are two fairly big areas of AutoLisp).

@MexicanCustard
Now that would be a tall order. I tried--and failed--at parsing the AutoLisp language at one time (I was only able to get "so far")...I just didn't have the knowledge at the time but I may be up to trying again (With the code/knowledge I mentioned above things just started clicking in place).

***
Thank you for the feedback. I will keep working on this project.
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: JohnK on December 06, 2011, 11:43:59 AM
I think I have a good enough alpha-stage program for everyone to play with but please don't be surprised that the indentation is/gets goofy (I really need to think up a better way to implement this feature) or the output isn't what you expected (see note #2).

I am attaching an installer that will extract an executable for you to test/play with (it is an interpreter). If you double click the exe you should note that this program is doing it's thing as you type--that means that, when you type something it knows how to parse, it reports back right away (Kinda like an overanxious child) and you will have to use CTRL-Z to exit the program. I have also included a text file which gives several usable examples. At this point, passing a file to the program would be the preferred method of interaction (see note #1), instead of launching it and typing outright (aka Double clicking the exe).

Introducing: "Kaylee".
Kaylee is a simple programming language, parser and code generator that will convert the Kaylee language to an AutoLisp representation.

Steps you need to take:
1. Use the installer to extract(install) the .exe (the default location is: C:\Kaylee).
2. Open a command prompt to the location you installed it (start->run->cmd.exe ... "cd C:\Kaylee\").
3. Issue the following on the command prompt (no quotes): "Kaylee.exe < Kaylee-Test.txt"

The Kaylee parser should have printed some AutoLisp back to the command prompt.

Note:
1. If you want a file output instead you can use the following (no quotes):
"Kaylee.exe < Kaylee-Test.txt > Kaylee-Test-Output.lsp"
2. The parser and code generator is easily confused so don't be surprised if wonky results are given.
3. The code examples above may not work. The "Kaylee-Test.txt" demonstrates proper syntax's but this is an example:

Code - C++: [Select]
  1. def forFoo(n)
  2.     for i = 1, i < n, 1.0 {
  3.         1 + 2
  4.         2 + 3
  5.         i + n
  6.     }
  7. end;
The above code-snip isn't really C++ code but the forum's C++ syntax highlighting will work to highlight the components in this example.

Will produce:
Code - Auto/Visual Lisp: [Select]
  1. (defun forFoo( n )
  2.    (setq i 1)
  3.    (while (< i n)
  4.       (+ 1 2)
  5.       (+ 2 3)
  6.       (+ i n)
  7.       (setq i (+ i 1))
  8.    )
  9. )

Enjoy.
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: BlackBox on December 06, 2011, 12:22:19 PM
Really neat idea, Se7en.

I'm not sure if you intended for the code posted here to be pseudo code, or actually functional, so forgive the critique in I've misunderstood... Be aware though, that these both yield the same result in LISP:

Code - Auto/Visual Lisp: [Select]
  1. (defun forFoo( n )
  2.    (setq i 1)
  3.    (while (< i n)
  4.       (+ 1 2)
  5.       (+ 2 3)
  6.       (+ i n)
  7.       (setq i (+ i 1))
  8.    )
  9. )

Code - Auto/Visual Lisp: [Select]
  1. (defun forFoo  (n)
  2.   (setq i 1)
  3.   (while (< i n)
  4.     (setq i (+ i 1))
  5.     )
  6.   )

This is because your original code performs calculations without storing them to a variable (using setq function), nor does your code output the returned values to the command line, etc..

Also, you may want to consider changing this:

Code - Auto/Visual Lisp: [Select]
  1. (defun forFoo(n)

... To this (formatted in VLIDE):

Code - Auto/Visual Lisp: [Select]
  1. (defun forFoo  (n)

Very neat though, I am interested to see how this evolves.

Cheers! :beer:
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: JohnK on December 06, 2011, 12:43:15 PM
Yes, the defFor was only a demonstration (not functional). However, the "Kaylee-Test.txt" contains several "real world" examples.
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: BlackBox on December 06, 2011, 12:53:07 PM
I am unable to install the app; corporate IT and all. 
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: JohnK on December 06, 2011, 01:03:18 PM
Well then, give me two minutes and you shall have some to view. *smile*


First: Kaylee
Second: Autolisp

Code - C++: [Select]
  1. def Fib(x)
  2.     if (x < 3) then
  3.         1
  4.     else
  5.         Fib(x-1)+Fib(x-2)
  6. end;
Code - Auto/Visual Lisp: [Select]
  1. (defun Fib( x )
  2.   (if (< x 3)
  3.     1
  4.     (+ (Fib (- x 1)) (Fib (- x 2)))
  5.     )
  6.   )


Code - C++: [Select]
  1. def copyTree(x)
  2.     # Copy a tree exactly as is.
  3.     if atom(x) then
  4.        x
  5.      else
  6.        cons copyTree(car(x)),
  7.             copyTree(cdr(x))
  8.  end;
Code - Auto/Visual Lisp: [Select]
  1. (defun copyTree( x )
  2.   (if (atom x)
  3.     x
  4.     (cons (copyTree (car x)) (copyTree (cdr x)))
  5.     )
  6.   )


Code - C++: [Select]
  1. def nthReplace(position newItem aList)
  2.     # replace nth item in a list
  3.     if null(aList) then
  4.         nil
  5.     else
  6.        cons
  7.            if (position = 0) then
  8.                newItem
  9.            else
  10.                car(aList),
  11.            nthReplace((position - 1), newItem, cdr(aList))
  12. end;
Code - Auto/Visual Lisp: [Select]
  1. (defun nthReplace( position  newItem  aList )
  2.   (if (null aList)
  3.     nil
  4.     (cons (if (= position 0)
  5.             newItem
  6.             (car aList)
  7.             )
  8.           (nthReplace (- position 1)newItem(cdr aList)))
  9.     )
  10.   )


Code - C++: [Select]
  1. def replace(aList position newItem)
  2.     if (null(aList)) then
  3.         if (position > 0) then
  4.             cons car(aList), (replace(cdr(aList), (position - 1), newItem))
  5.          else
  6.             cons newItem, cdr(aList)
  7. end;
Code - Auto/Visual Lisp: [Select]
  1. (defun replace( aList  position  newItem )
  2.   (if (null aList)
  3.     (if (> position 0)
  4.       (cons (car aList) (replace (cdr aList)(- position 1)newItem))
  5.       (cons newItem (cdr aList))
  6.       )
  7.     )
  8.   )
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: BlackBox on December 06, 2011, 01:22:22 PM
Well then, give me two minutes and you shall have some to view. *smile*


First: Kaylee
Second: Autolisp

Much appreciated; I wasn't expecting you to go through the effort on my (LISPer / .NET noob) account. *smile*
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: BlackBox on December 06, 2011, 02:28:28 PM
Just an observation...


Code - C++: [Select]
  1. def replace(aList position newItem)
  2.     if (null(aList)) then
  3.         if (position > 0) then
  4.             cons car(aList), (replace(cdr(aList), (position - 1), newItem))
  5.          else
  6.             cons newItem, cdr(aList)
  7. end;
Code - Auto/Visual Lisp: [Select]
  1. (defun replace( aList  position  newItem )
  2.   (if (null aList)
  3.     (if (> position 0)
  4.       (cons (car aList) (replace (cdr aList)(- position 1)newItem))
  5.       (cons newItem (cdr aList))
  6.       )
  7.     )
  8.   )


As written, if the aList argument has any non-Nil value, the first IF statement's test expression will evaluate to Nil, thus bypassing the nested code altogether. Whereas if the aList argument (again, as written) is Nil, the first IF statement's test expression will evaluate to T, stepping into the nested code, however this produces an undesired result:

Code - Auto/Visual Lisp: [Select]
  1. REPLACE
  2. _$ (replace '("FOO1" "FOO2" "FOO3") 1 "NEW")
  3. nil
  4. _$ (replace nil 1 "NEW")
  5. (nil "NEW")
  6. _$

Perhaps instead, you might consider this variation *wink*:

(Completely guessing at the pseudo code)
Code - C++: [Select]
  1. def replace2(aList position newItem)
  2.     if aList then
  3.         if (position > 0) then
  4.             cons car(aList), (replace2(cdr(aList), (position - 1), newItem))
  5.          else
  6.             cons newItem, cdr(aList)
  7. end;
Code - Auto/Visual Lisp: [Select]
  1. (defun replace2  (aList position newItem)
  2.   (if aList
  3.     (if (> position 0)
  4.       (cons (car aList) (replace2 (cdr aList) (- position 1) newItem))
  5.       (cons newItem (cdr aList))
  6.       )
  7.     )
  8.   )

As the first IF statement's test expression no longer ensures that the aList argument is Nil, the desired result is obtained:

Code - Auto/Visual Lisp: [Select]
  1. REPLACE2
  2. _$ (replace2 nil 1 "NEW")
  3. nil
  4. _$ (replace2 '("FOO1" "FOO2" "FOO3") 1 "NEW")
  5. ("FOO1" "NEW" "FOO3")
  6. _$
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: JohnK on December 06, 2011, 02:35:36 PM
Awesome! Thank you for the adjustment.


> (Completely guessing at the pseudo code)

You're a natural! Your code produced exactly what you wanted it to.
Code - Auto/Visual Lisp: [Select]
  1. (defun replace( aList  position  newItem )
  2.   (if aList
  3.     (if (> position 0)
  4.       (cons (car aList) (replace (cdr aList)(- position 1)newItem))
  5.       (cons newItem (cdr aList))
  6.       )
  7.     )
  8.   )
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: BlackBox on December 06, 2011, 03:10:27 PM
Awesome! Thank you for the adjustment.


> (Completely guessing at the pseudo code)

You're a natural! Your code produced exactly what you wanted it to.

You're very welcome; that is kind of you to say.

Cheers! :beer:
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: BlackBox on December 06, 2011, 07:19:20 PM
As a simple matter of efficiency (which recursive functions are not exactly known for), and as a simple point of I guess you could say the "elegance" of LISP... I think you'll find this variation even more robust, and efficient.

The replace2 function, in addition to that which the replace function already does, is to check that all arguments are non-Nil, and to ensure that the user does not attempt to replace a value within a list of lesser length (meaning that the fifth item of a list of three cannot be replaced).

My recent variation (posted above):

Code - Auto/Visual Lisp: [Select]
  1. (defun replace  (aList position newItem)
  2.   (if aList
  3.     (if (> position 0)
  4.       (cons (car aList) (replace (cdr aList) (- position 1) newItem))
  5.       (cons newItem (cdr aList))
  6.       )
  7.     )
  8.   )

Current offering:

Code - Auto/Visual Lisp: [Select]
  1. (defun replace2 (aList position newItem)
  2.   (if (and aList
  3.            position
  4.            newItem
  5.            (>= (1- (length aList)) position))
  6.     (subst newItem (nth position aList) aList)
  7.     )
  8.   )

Bench test results from VLIDE Console (100K iterations):

Code - Auto/Visual Lisp: [Select]
  1. _$
  2.  
  3. REPLACE
  4. _$
  5.  
  6. REPLACE2
  7. _$
  8. _$ (bench '(replace replace2) (list '("FOO1" "FOO2" "FOO3") 2 "NEW") 100000)
  9.  
  10. REPLACE
  11. Elapsed: 608
  12. Average: 0.0061
  13.  
  14. REPLACE2
  15. Elapsed: 562
  16. Average: 0.0056
  17. _$

Taking another hand-typed stab at the pseudo code syntax:

Code - C++: [Select]
  1. def replace(aList position newItem)
  2.    if and(aList, position, newItem, >=((1-(length(aList))), position)) then
  3.         subst(newItem, nth(position, aList), aList)
  4. end;

Enjoy!

Title: Re: (Teaser) A new pseudocode/prototype language
Post by: JohnK on December 06, 2011, 08:27:59 PM
Oh, I didn't build in support for '>='...

For the record, the language formatting is really flexible--meant to allow you to format your code anyway you want to help you read it better-. For example, math functions are meant to be typed outright. like so:

This:
Code: [Select]
1 + 2 - 4 * 5 - 1
Would produce this:
Code - Auto/Visual Lisp: [Select]
  1. (- (- (+ 1 2) (* 4 5)) 1)
So, we can have either of these (this won't work right at this moment because I forgot all about '>=' and the like).
Code - C++: [Select]
  1. def replace (aList position newItem)
  2.    if and(aList, position, newItem, length(aList) - 1 >= position)
  3.       then
  4.         subst(newItem, nth(position, aList), aList)
  5. end;

Code - C++: [Select]
  1. def replace (aList position newItem)
  2.    if and(aList,
  3.           position,
  4.           newItem,
  5.           length(aList) - 1 >= position)
  6.    then
  7.        subst(newItem,
  8.              nth(position, aList),
  9.              aList)
  10. end;

Title: Re: (Teaser) A new pseudocode/prototype language
Post by: BlackBox on December 06, 2011, 09:02:41 PM
No worries; I can appreciate that aspect.

For clarity "1+" and "1-" are valid (LISP) functions, hence my usage. LoL

As for the other functions and operators, I believe the other thread has a good list of most (if not all) functions and operators, no?

That reminds me that I've still not finished that VB.NET.php file for you... things are hectic right now. *Shrug* LoL
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: Lee Mac on December 07, 2011, 06:03:37 AM
[ Aside: ]

Current offering:

Code - Auto/Visual Lisp: [Select]
  1. (defun replace2 (aList position newItem)
  2.   (if (and aList
  3.            position
  4.            newItem
  5.            (>= (1- (length aList)) position))
  6.     (subst newItem (nth position aList) aList)
  7.     )
  8.   )

However, note that subst will replace all occurrences of an item:

Code: [Select]
_$ (replace2 '(1 2 3 3 2 1) 3 4)
(1 2 4 4 2 1)

An alternative for your consideration:

Code: [Select]
(defun replace3 ( alist position newitem / i )
    (setq i -1)
    (mapcar '(lambda ( x ) (if (= position (setq i (1+ i))) newitem x)) alist)
)
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: ElpanovEvgeniy on December 07, 2011, 06:26:35 AM
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l a b)
  2.   (cond ((< a 2) (cons b (cdr l)))
  3.         ((cons (car l) (f (cdr l) (1- a) b)))
  4.   )
  5. )
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: BlackBox on December 07, 2011, 07:57:32 AM
However, note that subst will replace all occurrences of an item:

Ohhh; I did fail to consider that in my limited example. *oops*

Good catch, Lee... Thanks for the correction.
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: irneb on December 07, 2011, 11:45:49 AM
Se7en, your code reminds me of some mixed breed between Pascal and Python. Less verbose than Pascal, but not as much as in Python / Lisp.

E.g. your test of an empty list using
Code: [Select]
if null(aList) then
  ...
Would strictly be translated in Lisp to
Code: [Select]
(if (null aList)
  ...
However Python disallows this non-standard comparison, e.g. a direct translation would be
Code: [Select]
if null = aList
  ...
In both Lisp and Python its recommended to use the idea that an empty list evaluates to false. So the more "correct" translation would be
Code: [Select]
(if (not aList)
  ...
Code: [Select]
if not aList
   ...

But I suppose that's just splitting hairs  :pissed: .

Though it shows one of the aspects where non-lispers have difficulty figuring out what's going on. Apart from no then and else keywords, in lisp you'd use the falseness of an empty list to your advantage and thus re-arange your if statement. Something like this:
Code: [Select]
if null(aList) then
  ... do whatever is required when the list is empty;
else
  ... do whatever's to be done with a list containing at least one value;
Translated to lisp:
Code: [Select]
(if aList
  (... do whatever's to be done with a list containing at least one value)
  (... do whatever is required when the list is empty)
)
Which makes the back-translation even more problematic. You either need to re-arrange the if portions, which you can't always do since there may not be an else portion. Or you need to change the (if aList into
Code: [Select]
if not (null (aList)) then
Title: Re: (Teaser) A new pseudocode/prototype language
Post by: Jeremy on December 07, 2011, 03:57:22 PM
I'm glad to see I'm not the only crazy person trying to create their own language. You might find my approach interesting. I write my code as an MTEXT entity in a dwg file and translate it into a lsp file. This allows me to do all my annotating with AutoCAD itself and I can draw diagrams (even 3D ones) right next to the code as god intended. Why use some lousy 2D editor when we have AutoCAD itself! Long lists I create as a table and reference simply in my code. This moves large bothersome pieces over to the side so that the main features of the program are still visible. Navigation is far easier because you can now pan over or zoom in and out. Move your code pieces arround in any pattern you want and drop color patches behind them to visually group them. Do what you want.

I can write functions with multple arguments in my language because they are translated out as functions that take the arguments in as a list. So (+ 2 3) would be translated to (lwp-+ '(2 3)) and lwp-+ would be a rewritten AutoLISP function that you would load in amongst others containing all of the AutoLISP functions rewritten to take in lists of arguments. My translator does the dirty work of turning the arguments into lists. This is my way of bypassing Autodesk's negligence on improving LISP. I am not ready for unleashing this yet as there is much I must do. I call my language LIWP (LIsp Without Parentheses). Actually it doesn't get rid of all of the parentheses but it does get rid of the left ones.