Author Topic: (Teaser) A new pseudocode/prototype language  (Read 6348 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10139
(Teaser) A new pseudocode/prototype language
« 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?
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: (Teaser) A new pseudocode/prototype language
« Reply #1 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.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

JohnK

  • Administrator
  • Seagull
  • Posts: 10139
Re: (Teaser) A new pseudocode/prototype language
« Reply #2 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*
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Pad

  • Bull Frog
  • Posts: 329
Re: (Teaser) A new pseudocode/prototype language
« Reply #3 on: November 17, 2011, 05:49:10 AM »
looks good Se7en
Good luck with the development of it.

JohnK

  • Administrator
  • Seagull
  • Posts: 10139
Re: (Teaser) A new pseudocode/prototype language
« Reply #4 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?)?
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Jeff H

  • Needs a day job
  • Posts: 6113
Re: (Teaser) A new pseudocode/prototype language
« Reply #5 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.
 
 
 

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: (Teaser) A new pseudocode/prototype language
« Reply #6 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.
Revit 2019, AMEP 2019 64bit Win 10

JohnK

  • Administrator
  • Seagull
  • Posts: 10139
Re: (Teaser) A new pseudocode/prototype language
« Reply #7 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.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10139
Re: (Teaser) A new pseudocode/prototype language
« Reply #8 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.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

BlackBox

  • King Gator
  • Posts: 3756
Re: (Teaser) A new pseudocode/prototype language
« Reply #9 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:
"How we think determines what we do, and what we do determines what we get."

JohnK

  • Administrator
  • Seagull
  • Posts: 10139
Re: (Teaser) A new pseudocode/prototype language
« Reply #10 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.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

BlackBox

  • King Gator
  • Posts: 3756
Re: (Teaser) A new pseudocode/prototype language
« Reply #11 on: December 06, 2011, 12:53:07 PM »
I am unable to install the app; corporate IT and all. 
"How we think determines what we do, and what we do determines what we get."

JohnK

  • Administrator
  • Seagull
  • Posts: 10139
Re: (Teaser) A new pseudocode/prototype language
« Reply #12 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.   )
« Last Edit: December 06, 2011, 01:07:55 PM by Se7en »
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

BlackBox

  • King Gator
  • Posts: 3756
Re: (Teaser) A new pseudocode/prototype language
« Reply #13 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*
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3756
Re: (Teaser) A new pseudocode/prototype language
« Reply #14 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. _$
"How we think determines what we do, and what we do determines what we get."