3 ms·
I've just recently started to dig into stack-based programming languages and would love to get hold of the Leo Brodie manuals. One question to programming lang
by zaf 13y ago
I've just recently started to dig into stack-based programming languages and would love to get hold of the Leo Brodie manuals.
One question to programming language experts: Is it possible to have a stack-based programming like forth but without the need for the extra characters needed in the syntax (like quotes, brackets etc..) and only use whitespace to separate the 'words'?
- dvdkhlng 13y agoStack-based programming does not need extra syntax characters (what you call quotes and brackets) to structure the program. arithmetic syntax: (a+b)*c lisp syntax (polish notation): (* (+ a b) c) forth syntax (reverse polish notation): a b + c * In fact all words in Forth are only separated by whitespace. However, to "emulate" more complex syntax, some words in Forth are marked to be executed immediately upon parsing (somewhat like "macros" in lisp) and then access the input stream to extract more than whitespace-separated tokens. Exampes: '(' parses comments : ( comment) 's"' compiles string literals : s" abc" '[if]' conditionally skips code fragments: 0 [IF] bla [THEN] Still the macro word itelsf needs to be whitespace-delimited.
- zaf 13y agoAh, so you mean that certain words can not only look at the stack but also look at manipulate the input word stream. Thanks for the explanation. I really ought to do a practical session with Forth.
- dvdkhlng 13y agoThere are various levels of abstraction when accessing the input stream. The lowest level is by inspecting the input (line) buffer. Just realized how that allows one to write a three-word quine [1] in ANS-compliant Forth: SOURCE TYPE BYE [1] https://en.wikipedia.org/wiki/Quine_%28computing%29 https://en.wikipedia.org/wiki/Quine_%28computing%29
- draegtun 13y ago>re: ... and only use whitespace to separate the 'words'? Have a look at Rebol which is a mix of Forth, Logo & Lisp. Here's an example of Rebol code (typed into the console (REPL))... >> multiply add 1 2 3 == 9 multiply and add are both functions which take two arguments. If I add parens then above becomes clearer. >> multiply (add 1 2) 3 == 9 So Rebol evaluates a list of words from left to right with non-variadic functions (ie. they have fixed argument arity). Here's an example of a conditional: >> if equal? 1 1 [print "1 is 1!!"] 1 is 1!! if is a function which takes two arguments (as is equal?). So this is code is evaluated like this: >> equal? 1 1 == true >> [print "1 is 1!!"] == [print "1 is 1!!"] If the first argument is true (which it is in this case!) then the second argument (which is a block) is evaluated... >> do [print "1 is 1!!"] 1 is 1!!