Additional function and expression features

Let the developers know what you think of the software and what can be done to either improve the CFDG language or the Context Free program.

Moderators: MtnViewJohn, chris, mtnviewmark

Post Reply
User avatar
pakin
Posts: 43
Joined: Sat Apr 21, 2007 8:59 pm
Location: United States
Contact:

Additional function and expression features

Post by pakin »

For a function I was trying to implement, the following features sure would have come in handy:
  • bitwise operations (and, or, xor, not), either as operators (&, |, ~) or callable functions
  • variable declarations within functions, perhaps using the special form "let(var, value, expr)" (letting var have value while evaluating and returning expr)
  • mutually recursive functions
  • some sort of printf()-debugging feature, perhaps a function of the form "printf(format, args..., expr)", which does a printf(fmt, args...) then returns expr (modeled after the functions in Haskell's Debug.Trace module)
Although I don't have any suggestions on how to integrate this with the rest of the language, multiple return values would be really nice, too. I was trying to return x and y values and ended up have to replicate my function into one that does all the computation but returns only x and other that does the same computation but returns only y.

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Re: Additional function and expression features

Post by MtnViewJohn »

pakin wrote:For a function I was trying to implement, the following features sure would have come in handy:
  • bitwise operations (and, or, xor, not), either as operators (&, |, ~) or callable functions
  • variable declarations within functions, perhaps using the special form "let(var, value, expr)" (letting var have value while evaluating and returning expr)
  • mutually recursive functions
  • some sort of printf()-debugging feature, perhaps a function of the form "printf(format, args..., expr)", which does a printf(fmt, args...) then returns expr (modeled after the functions in Haskell's Debug.Trace module)
Although I don't have any suggestions on how to integrate this with the rest of the language, multiple return values would be really nice, too. I was trying to return x and y values and ended up have to replicate my function into one that does all the computation but returns only x and other that does the same computation but returns only y.
  • bitwise: ok, but everything is stored in double precision floating point numbers, so you would only have access to 52 bits
  • let: these seems just like declaring a function and using it just once
  • mutually recursive: this wouldn't be too hard
  • printf: interesting, I would have to add something for the format string
  • multiple return values: I was going to have functions that could return multiple values, adjustments, or replacements but I paired down the features so that I could stop coding and release something. I'll see if this can be added back. Mutually recursive functions would have to be single-valued

User avatar
pakin
Posts: 43
Joined: Sat Apr 21, 2007 8:59 pm
Location: United States
Contact:

Re: Additional function and expression features

Post by pakin »

MtnViewJohn wrote:
  • bitwise: ok, but everything is stored in double precision floating point numbers, so you would only have access to 52 bits
That should be plenty for most purposes.
MtnViewJohn wrote:
  • let: these seems just like declaring a function and using it just once
True. In fact, when I discovered that CF3 lacks a let function, I rewrote my code to declare a helper function so as to fake let functionality. The gotcha was that my original function was recursive, which means that I wound up with a pair of mutually recursive functions, which are also not implemented in CF3.
MtnViewJohn wrote:
  • mutually recursive: this wouldn't be too hard
Glad to hear it. A let function would still be convenient, though, so I don't have to create a helper function for every local variable defined in terms of a previous local variable.
MtnViewJohn wrote:
  • multiple return values: I was going to have functions that could return multiple values, adjustments, or replacements but I paired down the features so that I could stop coding and release something. I'll see if this can be added back. Mutually recursive functions would have to be single-valued
Cool. Out of curiosity, why would mutually recursive functions need to be single-valued? That's not obvious to me.

Thanks for considering my requests. I hope other Context Free users find these features useful, too.

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Re: Additional function and expression features

Post by MtnViewJohn »

pakin wrote: Cool. Out of curiosity, why would mutually recursive functions need to be single-valued? That's not obvious to me.
The parser is single-pass and not terribly smart. If you have a mutually recursive pair of functions then the first one will refer to the second one before the parser knows the type signature of the second function. The easiest thing to do is assume the the unknown function is single-valued.

But CF3 does support mutually referential shapes with parameters. If the parser encounters an unknown shape with parameters then at the end of parsing it throws away all the information except for the shape type info and launches a second pass. I could do the same for functions.

mycelium
Posts: 29
Joined: Tue Aug 28, 2007 4:21 am
Location: Italy
Contact:

Re: Additional function and expression features

Post by mycelium »

when i wrote the code for some designs i had to be very redundant in the code, especially in the 3d transformations.

I thought that to improve the quality of code could be useful to have:

1. user defined functions that return more then 1 parameter (maybe this is already implemented as written in the download page, but I didn't find an exemple or syntax reference)
2. vectors to store multiple values in a single variable / parameter.
3. maybe preprocessor macros that simply replace a placeholder with some more complex code.

with those could be easier to reuse parts of code.

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Re: Additional function and expression features

Post by MtnViewJohn »

mycelium wrote:when i wrote the code for some designs i had to be very redundant in the code, especially in the 3d transformations.

I thought that to improve the quality of code could be useful to have:

1. user defined functions that return more then 1 parameter (maybe this is already implemented as written in the download page, but I didn't find an exemple or syntax reference)
2. vectors to store multiple values in a single variable / parameter.
3. maybe preprocessor macros that simply replace a placeholder with some more complex code.

with those could be easier to reuse parts of code.
The current beta release allows you to return vectors of multiple numbers from a user function. The only thing that you can do with this is to use it in a multi-valued shape adjustment: x, size, skew, transform, time, and the two-argument forms of hue, saturation, brightness, and alpha. Or you could use it in a function that takes multiple arguments: bitor, bitand, bitxor, bitleft, bitright, atan2, mod, divides, div, min, max, rand_static, rand, and randint.

I could add a function for extracting a single value from a vector.

Code: Select all

startshape spiral

polar2rect(radius, angle) = 
  radius*cos(angle),
  radius*sin(angle)

hueRange = -60, 60

shape spiral {
  loop i = 360 [] {
    SQUARE[x polar2rect(i, i) r i s 5 h rand(hueRange) sat 1 b 1]
  }
}
You can store a vector in a variable but not pass a vector as a parameter. I was going to allow this but I took it out for a reason that is now obsolete. I should add vector parameters back.

A preprocessor would be nice but I don't have the energy to roll my own. I could use an existing preprocessor, but it would have to be portable to Mac, Windows, and Unix. And I would have to modify it to support cfdg tokenization rules, which have utf-8 encoded Unicode operators and identifiers.

mycelium
Posts: 29
Joined: Tue Aug 28, 2007 4:21 am
Location: Italy
Contact:

Re: Additional function and expression features

Post by mycelium »

hi John,
vectors as parameters for shapes and functions would be nice for me. and the possibility of manipulating single values.

also an alternative syntax for functions with a "return" statement may help to create more complex functions.
for example if all my returned values depend on a same complex computation I'd like to store it in a variable to reuse it, code is more compact and also more efficient in this case.

I think that powerful functions, in general, would mean the possibility of creating librarys of easly reuseable code (not only for 3D).

tnx for attention, i hope not to be tedious, just consider ideas or feedback, not request.

mycelium
Posts: 29
Joined: Tue Aug 28, 2007 4:21 am
Location: Italy
Contact:

Preprocessor ideas

Post by mycelium »

A "generalistic" idea about preprocessor is just to pass the code to a terminal-command or to a web-service and then get back the modified code that is used by CF.
In this case we should only specify a command or a URL at the beginning of the code.

For example:

Code: Select all

#Preprocessor "http://127.0.0.1/CFDG/a_preprocessor_for_CFDG.php"
or

Code: Select all

#Preprocessor "http://www.contextfreeart.org/official_preprocessor_for_CFDG.php"
I had this idea because I have developed a 3D preprocessor with PHP and I use it as web-service with a HTML IDE and the command line version of CF. But obviously everything would be nicer if it was integrated in Context Free Application. It also would be very easy to share it with other by installing the preprocessor on a website instead of asking to install it on a local web-server.

If anybody is interested about this I will post more.
Attachments
Output from preprocessor.
Output from preprocessor.
example.jpg (118.55 KiB) Viewed 54565 times

Post Reply