10 ms·
Stack-Oriented Programming
- n4r9 7y agoI went through a basic Forth tutorial [0] a couple of years ago and really enjoyed the process. It gets you to the point of building a real game of "snake", although it slightly glosses over the actual canvas interface. [0] https://skilldrick.github.io/easyforth/ https://skilldrick.github.io/easyforth/
- ngcc_hk 7y agoInteresting. But in iphone we do not have arrow keys sadly. Possibly can hack ... but not forth.
- ngcc_hk 7y agoI try to find a better way to try this and after reading hackaday on Forth 2017 which highlight the latest (2017) update on Forth development: https://hackaday.com/tag/forth/ https://hackaday.com/tag/forth/ (mainly this: https://hackaday.com/2017/08/04/take-the-blue-pill-and-go-forth/#more-267209 https://hackaday.com/2017/08/04/take-the-blue-pill-and-go-fo... ) I find this and you can run the whole thing on my MacBook in 5 minutes. It is great: https://jeelabs.org/article/1720b/ https://jeelabs.org/article/1720b/
- int_19h 7y agoThe really interesting thing about Forth is just how minimalistic a bare-metal Forth implementation can be - not even an interpreter, but literally a few words handcoded in machine code, and the rest built from there (and then possibly optimized with more machine code, if you need that). Given the ease with which Forth can be ported to other platforms and architectures, I often wonder why there's no C-to-Forth translator - it seems like it would be just about the easiest way to bootstrap things.
- pjmlp 7y agoBare metal like on the Ace.:) https://en.wikipedia.org/wiki/Jupiter_Ace https://en.wikipedia.org/wiki/Jupiter_Ace http://jupiter-ace.co.uk/ http://jupiter-ace.co.uk/
- jandrese 7y agoThe old RoboWar game on the Mac featured a stack oriented language for programming your battle robots. It was a great introduction to RPN and thinking about the stack. One of the interesting aspects of the game was the way the CPU was limited to a very small number of instructions per tick. At most you would get 50. So if you had a complex task that had to be executed in one tick, like filling the screen with bullets before your robot shut down due to lack of power, the way to do it was to preload all of the arguments onto the stack, wait for the tick to turn over, then call all of the functions at once. Basically allowing you to bank processing time for the turn you needed it.
- skocznymroczny 7y agoIn CoreWars, which used assembly-like language to create programs fighting each other, one of the most powerful strategies was to inject a SPL instruction (basically fork) into an enemy program, preferably jumping to the SPL instruction itself. As a result, the enemy program would quickly be split into 64 processes (usual maximum number), so the actual logic would be running at 1/64 speed, bogged down by the dummy processes.
- flir 7y agoIf you could successfully do that, why wouldn't you just bomb a DAT in instead?
- brazzy 7y agoBecause the opponent could already be running multiple processes - killing one would only speed up the others. The programs that uses the SPL-bomb tactic usually tried to catch all the enemy processes (slowed down to uselessness by the SPL-bomb) in the same place before finishing them off with a DAT.
- flir 7y agoI see. I don't think the original version of CoreWars had multiple program counters? Got to admit it's been a while.
- joncp 7y agoThis makes me miss my HP 28S calculator from the 80s. What a beautiful machine that was.
- smabie 7y agoI loved my HP-50g, allowed my to program all day long while stuck in stupid high school classes. RPL is a nice little language, a bit hard to read after a break though. And the fact that HP calculators throw away all whitespace and indention after saving a program certainly doesn't help.
- munk-a 7y agoI'm a bit confused why "using postfix notation" is considered a programming paradigm - I'm semi-fond of post-fix notation (it is the second best notation after prefix after all) but[1] it's incredibly trivial to convert between prefix and postfix notation and most programming language expressions use prefix already - it's just the infix ones that are a bit weird... Allllso, expressing if statements without any infix notation is sort of terrible, I've never seen an option to do it that isn't incredibly misleading or confusable. A core question, though, is why? Is tab-oriented programming have a wikipedia page where programs are written predominantly using tab stops instead of spaces? It's honestly a very similar sort of distinction to make. 1. I'm trying not to stray too far into all turing complete languages are turing complete and thus difference is meaningless land.
- triska 7y agoAs I see it, the key characteristic of stack-oriented programming is not that it "uses postfix notation". Rather, the key characteristic is that there is an important implicit feature available, namely the stack (or, as for example in the case of PostScript, several stacks), by which arguments can be passed along. As a consequence, you get a form of compositionality of language elements that is not available in languages that lack this feature: A language element may consume, change and create entities on the stack, and the next language element can pick them up implicitly. This often allows very concise and elegant code, and can also be interpreted very efficiently.
- jerf 7y agoBest advocacy I know of: http://evincarofautumn.blogspot.com/2012/02/why-concatenative-programming-matters.html http://evincarofautumn.blogspot.com/2012/02/why-concatenativ... Presented by way of being an interesting link, and the educational nature of reading the best arguments in favor of something. I haven't done anything in concatenative programming in about 20 years, since I left math class and didn't need my HP 48G anymore.
- munk-a 7y agoThat's quite helpful, it may be a lack of in-depth reading of the page but the persistence and implicit passing of computation between operations is helpful to understand the differentiation. In more classic procedural languages you'll usually have a function stack that is being pushed and popped two but actual program values are handled using entirely arbitrarily loaded references that get juggled around as needed for computation - does stack programming often have a similar concept to frame popping when an issue with I/O or something similar is encountered or do we just get a panic that aborts computation?
- stinos 7y agoI read this title and thought, hmm is this another name for 'lock-in' or similar? You know like pick a stack of tools and then only use those, to the point you can hardly do anything at all without them. (which isn't necessarily bad, if it allow you to get things done fast and good) On topic: always good to read up on less common programming principles just to, erm, avoid lock-in
- rckoepke 7y agoI believe that's called resume oriented development.
- virtuous_signal 7y agoI recently wrote an accepted answer on cs.stackexchange about how to implement sorting on a stack machine: https://cs.stackexchange.com/questions/117949/stack-permutation-algorithm/117975#117975 https://cs.stackexchange.com/questions/117949/stack-permutat... The asker isolated the question well, so I had no idea what Forth or stack machines are. Still it's a nice exercise to to implement common algorithms with a restricted set of moves.
- tombert 7y agoCan someone who knows more about Forth clarify this for me: what "level" is something like Forth? Is it higher or lower-level than something like C? The reason I'm confused is that I see Forth in a lot of areas that look like they're typically for "C and lower", but looking at the syntax, Forth looks like a high-level language.
- jerf 7y agoIt's a lot like Lisp, especially as it was decades ago. The language itself is low-level but you can construct some very high-level-like constructs in it yourself. Where it differs is that Lisp is a low-level language for a machine that doesn't really exist unless you have a Lisp machine, and Forth is for your actual hardware... as viewed through a set of stacks, anyhow. I suspect had it ever been used for large code bases it would have had a similar problem that Lisp had, where any given program might be elegant but there would be too much convention differences between any two programs to let them work together very well, similar to the problem that Lisp has sometimes been considered to have with macros. (It's great to be able to synthesize your own entire world, but no two programmers synthesize the same world.)
- ngcc_hk 7y agoCan forth handle delay execution. Or a conditional where one later parameter never need is not reached. It seems all parameters at least needed to be in first, say a rough lisp like use case. (Def ExecN func n infinite-series) (ExecN + 10 integer) ... can it be done in forth.
- RodgerTheGreat 7y agoOf course. You can build whatever functional abstractions you like in Forth. Some time ago I did a number of experiments with lazy generators: https://github.com/JohnEarnest/Mako/blob/master/lib/Algorithms/Lazy.fs#L95-L104 https://github.com/JohnEarnest/Mako/blob/master/lib/Algorith... The above dialect uses { } to delimit anonymous inline word definitions. RetroForth has a similar feature, and Factor uses the same syntax for "quotations", which have slightly different semantics but roughly the same meaning.
- mzs 7y ago8087 had an 8 level stack https://en.wikipedia.org/wiki/Intel_8087#Registers https://en.wikipedia.org/wiki/Intel_8087#Registers
- enriquto 7y ago> 8087 had an 8 level stack Why "had"? The x87 instructions are alive and well in modern intel processors. You can also generate code for them (for example, if you use "long double" in C) with all major compilers.
- zzo38computer 7y agoI have used Forth, PostScript, dc, and other stack-based prorgamming. However, mostly in PostScript code I have not seen the Forth-like notation for the stack effect, although I do use that in my own PostScript programming, because I think it is better than the notation Adobe uses. Some virtual machine instruction sets (such as Glulx and Z-machine code) still require instruction operands but allow some or all of them to be optionally specified as stack operands. This allows you to have both stack and a register-like programming.
- xiphias2 7y agoBitcoin also uses stack oriented language, and the interpreter is actually very short and simple: https://en.bitcoin.it/wiki/Script https://en.bitcoin.it/wiki/Script https://github.com/bitcoin/bitcoin/blob/master/src/script/script.cpp https://github.com/bitcoin/bitcoin/blob/master/src/script/sc...
- triska 7y agoYes! Stack-based abstract machines are often used because they allow compact code that is very easy to create and interpret. As another example, Pascal p-code (UCSD p-code) is also stack-based: https://en.wikipedia.org/wiki/P-code_machine https://en.wikipedia.org/wiki/P-code_machine The Java Virtual Machine (JVM) also uses an operand stack: https://en.wikipedia.org/wiki/Java_virtual_machine https://en.wikipedia.org/wiki/Java_virtual_machine
- jacobush 7y agoThat P-code was not (quite!) portable between computers, was a lost opportunity. How cool it would have been.
- souprock 7y agoOddly, that article doesn't link to the most interesting modern use. It's for security exploits. People have built compilers that output a binary that consists largely of return addresses that point into the middle of normal functions. https://en.wikipedia.org/wiki/Return-oriented_programming https://en.wikipedia.org/wiki/Return-oriented_programming
- halayli 7y agoyou're mixing 2 different concepts. Yes they end in oriented-programming in the end but one is an exploit technique and one is a programming model.
- 9214 7y agoThis deserves a bit of clarification: ROP is an application of threaded code technique. Conventional Forth (proto-father of stack-based/concatenative languages, alongside with Joy) is based on indirect-threaded code (ITC), so the connection between ROP and stack-based programming is rather indirect (pun intended). For anyone interested, Book by Loeliger [1] and "Moving Forth" series [2] are essential readings on this topic. [1]: https://archive.org/details/R.G.LoeligerThreadedInterpretiveLanguagesTheirDesignAndImplementationByteBooks1981 https://archive.org/details/R.G.LoeligerThreadedInterpretive... [2]: https://www.bradrodriguez.com/papers/ https://www.bradrodriguez.com/papers/
- quintenk 7y agoFun to see this, I just finished implementing a stack-based language of my own design this weekend. The language is called UnoScript, inspired by the UNO card game. UNO has a natural stack built-in (thinking of the draw and discard piles) so there's a really natural connection. Link here if you're curious: https://github.com/berlinquin/UnoScript https://github.com/berlinquin/UnoScript
- empressplay 7y agoMay the Forth Be With You! :)
- CarlRJ 7y agoFORTH LOVE IF HONK THEN
- punkbrwstr 7y agoThe stack-oriented approach can also be used within modern languages to realize the benefits of simplicity and code-reusablility. I created a python package for data analysis that treats a data frame like a stack of columns and lets you manipulate columns using postfix operators: https://github.com/punkbrwstr/pynto https://github.com/punkbrwstr/pynto
- reedwolf 7y agoWow, this looks really interesting. Was there any particular reason you decided on this approach?
- heavenlyblue 7y agoHow is that more interesting than simple .modify_column(params)?
- punkbrwstr 7y agoYou definitely could do a similar column-level functional approach that way, but I think the simple syntax of the stack-oriented approach makes it easier to read and catch errors. The code would be a lot longer that way.
- punkbrwstr 7y agoBuilding up complicated time series transformations by composing simple functions helps me be sure I'm doing what I think I'm doing. Since the transformations are tacit expressions that don't define specific parameters they are then very easy to re-use and combine. And I have some combinators that can apply the functions in pretty flexible ways. Also, since its all lazy-evaluated on a per-column level I can work with huge tables, but only end up operating on the subset I need.
- marviel 7y agoJust saw this in your class this morning!
- pagutierrezn 7y agoI expected this to be a joke about Stack(Overflow)-Oriented Programming
- raxxorrax 7y agoThere are stack based languages that have an intrinsic elegance. For example: https://esolangs.org/wiki/Pancake_Stack https://esolangs.org/wiki/Pancake_Stack And remember to Eat all of the pancakes! An interpreter can be written rather quickly (compared to any productive program in the respective language) and it is interesting how much you can actually achieve with such primitive tools.
- pipingdog 7y agoA Brainfuck tape can be thought of as two stacks and a register. ‘<‘ or move-left becomes push the register onto the right stack and pop the left stack into the register.
- 91aintprime 7y agoisn't this also the relationship between Turing machines and double stacks in general?
- wodenokoto 7y agoIs Human Resource Machine [1] basically also a stack oriented programming game, or am I confusing the physical stacks of paper you order the little guy to move around with computer stacks? [1] https://tomorrowcorporation.com/humanresourcemachine https://tomorrowcorporation.com/humanresourcemachine