4 ms·
This is always annoying to read as they wasted ")" to some shit. "(" should be: Push next word to X-stack ")" should be: pop and execute top of X-stack. So "
by timonoko 4y ago
This is always annoying to read as they wasted ")" to some shit.
"(" should be: Push next word to X-stack
")" should be: pop and execute top of X-stack.
So "(+ m n)" is equal to "m n +"
And "(IF (< A B) (THEN C ELSE D)))" ==
"A B < IF C ELSE D THEN"
- timonoko 4y agoIF-sentence was totally wrong. Per se. (IF (< A B)) (THEN C ELSE D) looks about right. This is about readability, all else stays the same. You can avoid the minute overhead of "(" by not using it on a critical situation.
- shadowofneptune 4y agoI am not sure what you mean. "(" and ")" as their own words form comments in Forth. As an example: (if (< a b) Would be parsed completely differently from: ( if ( < a b ) The first will have the interpreter look up and execute the words "(if", "(<", "a", and "b)". The second will have the interpeter look up the word "(", which will skip all the text that comes before a ")".
- timonoko 4y agoOf course we fix the parser too, so that ( and ) are always separate. Otherwise too ugly.
- flohofwoe 4y agoLooks like Lisp, ugh ;) I guess Polish Notation wasn't as uncommon in the 70's as it is today, AFAIK pocket calculators used this too. And once you switch your mind over it feels just natural.
- b112 4y agoAnd once you switch your mind over it feels just natural. Yet what if the result is, your mind has become unnatural? !!!
- 0x445442 4y agoRPN with pocket calculators was nice because of the screen real estate restrictions. But on computers there’s a lot of screen real estate to show the program and the variable assignments to intermediate results. Even with long procedures, it’s easy to scroll, read and understand the execution flow with that extra screen space.
- dandanua 4y agoThe reverse Polish notation (postfix) is the most natural notation if you want just to compute things, IMO. But if you want to grasp what is going on without computation - other notations can be better for readability.
- bmacho 4y ago> "(" should be: Push next word to X-stack > ")" should be: pop and execute top of X-stack. > So "(+ m n)" is equal to "m n +" I don't get it, what is the X-stack? Also can you elaborate this example in more details?
- timonoko 4y agoX is separate execution stack. It is very short depending how many brackets you use. When I made such Forth some 50 years ago, the "pop and execute" was one cycle on Nova 1200, no overhead. And x-stack was one 8-bit page.
- bmacho 4y agoThanks. Am I getting right, ( + 1 ( - 2 3 ) ) parses as s x + s 1 x + s 1 x + - s 1 2 x + - s 1 2 3 x + - s 1 2 3 - x + s 1 2 3 - + x which should evaluate to 0?
- deleted 4y ago[deleted]
- boomlinde 4y agoYou can certainly override the ( word to replace its functionality. You can even do so late in the compilation process so that the new semantics only apply to the code you write without affecting the rest of the dictionary. I don't see why you would, though. Even in your trivial example "(IF (< A B) (THEN C ELSE D)))" the brackets are unbalanced and even if they were, IF would be executed after THEN and ELSE. When you push a word to the "X-stack" you correspondingly have to push a word to an analogue of a stack in your mind to know what happens when ')' executes. You failed to do this and therefore your example is broken. In idiomatic Forth, you wouldn't have to worry: in most cases you only have to consider the data stack, and in most cases the sequences you just read to your left imply what's on top of that, not what you read ten words back. Even if it was slightly easier to read than idiomatic Forth (I disagree that it is), in-line comments strike me as more important "shit" than superficial syntax sugar.
- wjbr 4y agoHow would it work with more than two arguments? (+ 1 2 3)