- 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)
Additional function and expression features
Moderators: MtnViewJohn, chris, mtnviewmark
Additional function and expression features
For a function I was trying to implement, the following features sure would have come in handy:
- MtnViewJohn
- Site Admin
- Posts: 882
- Joined: Fri May 06, 2005 2:26 pm
- Location: Mountain View, California
- Contact:
Re: Additional function and expression features
pakin wrote:For a function I was trying to implement, the following features sure would have come in handy:
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 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)
- 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
Re: Additional function and expression features
That should be plenty for most purposes.MtnViewJohn wrote:
- bitwise: ok, but everything is stored in double precision floating point numbers, so you would only have access to 52 bits
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:
- let: these seems just like declaring a function and using it just once
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:
- mutually recursive: this wouldn't be too hard
Cool. Out of curiosity, why would mutually recursive functions need to be single-valued? That's not obvious to me.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
Thanks for considering my requests. I hope other Context Free users find these features useful, too.
- MtnViewJohn
- Site Admin
- Posts: 882
- Joined: Fri May 06, 2005 2:26 pm
- Location: Mountain View, California
- Contact:
Re: Additional function and expression features
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.pakin wrote: Cool. Out of curiosity, why would mutually recursive functions need to be single-valued? That's not obvious to me.
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.
Re: Additional function and expression features
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.
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.
- MtnViewJohn
- Site Admin
- Posts: 882
- Joined: Fri May 06, 2005 2:26 pm
- Location: Mountain View, California
- Contact:
Re: Additional function and expression features
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.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.
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]
}
}
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.
Re: Additional function and expression features
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.
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.
Preprocessor ideas
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:
or
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.
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"
Code: Select all
#Preprocessor "http://www.contextfreeart.org/official_preprocessor_for_CFDG.php"
If anybody is interested about this I will post more.
- Attachments
-
- Output from preprocessor.
- example.jpg (118.55 KiB) Viewed 56460 times