Structured Piet programming?
4 posters
Page 1 of 1
Structured Piet programming?
I've been thinking about how to make structured Piet programs, since it'd make for better reusability. One thought is to have a sort of function scheme much like in C, where the return address and arguments are placed on the stack. When a function is finished, it can then pass along into a common dispatch area that will use the return "address" to direct program flow to the appropriate spot. Calling a function would work much the same way, put its "address" on the stack, then enter a function dispatching area.
skwirl42- Posts : 7
Join date : 2009-08-06
Re: Structured Piet programming?
That would be very interesting! You could draw a loop with a special series of codels in it that turns the black codel at the end of the line into whitespace, so that conditional loops could be possible!
Re: Structured Piet programming?
I don't think Piet allows self-modifying code. If you check out my avatar, the little rainbow part in the bottom right is a "print string" function that outputs what's on the stack until it hits a null character. It's essentially a conditional loop, since it loops around until a condition is met. It's not the most elegant Hello World program, but it's the most structured I've seen.
I guess with my preference for structured programming, I just tend towards that kind of thing, even when writing in languages that don't directly support it. ie Piet and assembly.
I guess with my preference for structured programming, I just tend towards that kind of thing, even when writing in languages that don't directly support it. ie Piet and assembly.
skwirl42- Posts : 7
Join date : 2009-08-06
Re: Structured Piet programming?
that's very interesting!
Until I saw your code, I had yet to see structured coding, but maybe I'm not looking hard enough.
Until I saw your code, I had yet to see structured coding, but maybe I'm not looking hard enough.
Re: Structured Piet programming?
I think perhaps the 99 bottles of beer on samples page uses somewhat structured programming. I'd have to run it through npiet or something to see what the actual instructions are. I don't know that there's anyone who can reliably read Piet directly from the graphic. Certainly not me.
skwirl42- Posts : 7
Join date : 2009-08-06
Re: Structured Piet programming?
I've thought about this before. I have two ideas...
The first is something like this:
Imagine each line of color is a "subroutine". The black squares direct the flow back to the next "function" to be run. Most of the black squares define a channel back to a new routine, and each routine more or less selects where it will go to next based on where it places the block to the right of itself. Here routines 1 and 3 will goto 4 when they're done, 2 goes to 1, 4 goes to 5, and 5 goes to 2. With this setup, any function can go to any other function when it is done, but it must be known beforehand which. You may be able to do something clever with branches to make it work more dynamically, but it gets really confusing for me really fast. Maybe it could be machine generated though.
My favorite solution though would be to extend the language (maybe use half steps, maybe somehow incorporate alpha, maybe replace a current command) to include a call stack.
A jump command can pop two values and go to that x, y coordinate, pushing the current, x, y, dp, cc onto the call stack.
A return can then pop the last value from the call stack and jump there. It would be complicated, but may actually make it much much easier to do interesting things.
The first is something like this:
Imagine each line of color is a "subroutine". The black squares direct the flow back to the next "function" to be run. Most of the black squares define a channel back to a new routine, and each routine more or less selects where it will go to next based on where it places the block to the right of itself. Here routines 1 and 3 will goto 4 when they're done, 2 goes to 1, 4 goes to 5, and 5 goes to 2. With this setup, any function can go to any other function when it is done, but it must be known beforehand which. You may be able to do something clever with branches to make it work more dynamically, but it gets really confusing for me really fast. Maybe it could be machine generated though.
My favorite solution though would be to extend the language (maybe use half steps, maybe somehow incorporate alpha, maybe replace a current command) to include a call stack.
A jump command can pop two values and go to that x, y coordinate, pushing the current, x, y, dp, cc onto the call stack.
A return can then pop the last value from the call stack and jump there. It would be complicated, but may actually make it much much easier to do interesting things.
captncraig- Posts : 5
Join date : 2009-09-18
Re: Structured Piet programming?
I really like the thought of jump commands! maybe there could just be a command that stores the coordinates as codel number and then another command to jump to that codel number? that would be awesome.
Re: Structured Piet programming?
You really don't need to alter the Piet spec to implement any of these. Piet is already Turing complete. Check this out, for instance - it was generated from this source. You can have your jumps, loops, even recursive functions with what there is already. You just need to think about it in the right way - which is surely the whole point of choosing to work within the constraints of an esoteric language rather than, oh, C++
moonshadow- Posts : 2
Join date : 2010-02-27
Re: Structured Piet programming?
moonshadow wrote:You really don't need to alter the Piet spec to implement any of these. Piet is already Turing complete. Check this out, for instance - it was generated from this source. You can have your jumps, loops, even recursive functions with what there is already. You just need to think about it in the right way - which is surely the whole point of choosing to work within the constraints of an esoteric language rather than, oh, C++
O_O Wow, that's pretty cool!
By the way, unless mtptl starts making posts relevant to the topic and isn't just using their posts to have spam in their signature, they're going to get banned.
Similar topics
» pong in piet
» Piet assembler
» Possible Extentions to Piet Language
» High Level Language Compiled to Piet
» Piet assembler
» Possible Extentions to Piet Language
» High Level Language Compiled to Piet
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|