News for hackers. Stuff that matters.
Re:Stack-Oriented Programming by tombert on Tuesday January 14, 2020 @04:24PM (#22049381)
Can 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.

Re:Stack-Oriented Programming by RodgerTheGreat on Tuesday January 14, 2020 @04:37PM (#22049492)
It's lower-level than C. The primitive datatypes, to the extent there are datatypes, are in terms of bytes and machine-words. Most Forths include a dialect for embedding machine instructions in word definitions.

It's higher-level than C. Forth allows you to layer on dialects with custom domain-specific syntax and do other kinds of fancy metaprogramming. You can use Forth to interactively extend itself while you're working. Many Forths include a "bootstrapping" script, written entirely in forth, which defines such niceties as control structures and the syntax for comments.

Re:Stack-Oriented Programming by rabidrat on Tuesday January 14, 2020 @04:37PM (#22049503)
Forth is special. It's lower-level than C, in that you have more control over every aspect of the system; but you can refactor mercilessly and change the parser with a few lines of code, so you can develop your own DSL for the particular domain of problems you're solving.

The best way to achieve maximum flexibility and efficiency is to have the freedom to hop layers. To specify the high-level logic with a suitably abstract description, and then be able to reach into the lowest layers so that you can do what's necessary without needing to contort the logic to conform to the middle layers. If you do this once, it's a hack; if you hack repeatedly, you have a hot mess of entangled spaghetti; but if you design with this hackability in mind, you can evolve a system with abstractions at just the right level, that can be hacked as needed, without it turning into spaghetti. Requires discipline though.

Re:Stack-Oriented Programming by astrobe_ on Tuesday January 14, 2020 @06:05PM (#22050164)
Neither.

I recall that the first thing that jumped out at me was that Chuck's code didn't have all those layers that my code had. There was the abstracted code on top and the hardware drivers on the bottom and very little in between. There was not a clear distinction in style at various levels as in my code where there were always someone else's code layered under mine that I could not or dared not change. Chuck's code looked abstracted and high level right from the bottom, the top used almost as many primitives as the bottom code. [1]

The confusion seems to come from the idea that a piece of code has to be either "abstract" all the way or "close to the metal" all the way. Why should it be? If you need to flip a bit in a peripheral between a call to some filesystem function and a call to memory page management function, just do it directly, don't invent a pseudo-high-level function just for that. Bare metal ain't dirty.

[1] http://www.ultratechnology.com/levels.htm

Re:Stack-Oriented Programming by pjc50 on Wednesday January 15, 2020 @05:55AM (#22053224)
> Bare metal ain't dirty.

No, but it's not as solid as it sounds. What happens when version 2 of the hardware appears, and moves the bit to a different register?

Re:Stack-Oriented Programming by naikrovek on Wednesday January 15, 2020 @10:31AM (#22055046)
> What happens when version 2 of the hardware appears, and moves the bit to a different register?

You adapt your code for that platform. I don't know why developers today have such a love affair with unnecessary abstraction. Abstract when you need to; never before.

Re:Stack-Oriented Programming by pjmlp on Wednesday January 15, 2020 @06:01AM (#22053247)
The same thing when a new OS release break some API call.
Re:Stack-Oriented Programming by pjc50 on Wednesday January 15, 2020 @06:54AM (#22053466)
.. which they do far less often than hardware changes. You have a reasonable chance of running a 25-year-old Win32 program or 20-year-old statically linked ELF Linux program.
Re:Stack-Oriented Programming by jerf on Tuesday January 14, 2020 @04:30PM (#22049434)
It'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.)
Re:Stack-Oriented Programming by ngcc_hk on Tuesday January 14, 2020 @09:11PM (#22051193)
Can 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.

Re:Stack-Oriented Programming by int_19h on Tuesday January 14, 2020 @10:32PM (#22051549)
Immediate words in Forth are somewhat analogous to macros, and therefore can do this kind of stuff if need be - not evaluating something, or evaluating something several times. Take a look at a sample implementation of IF/ELSE/THEN and DO/LOOP: https://github.com/nornagon/jonesforth/blob/4f853252f715132e...

Forth also has anonymous words (:NONAME), so you can dynamically define a computation and pass it around on the stack, and later execute it. I don't think there's any way to clean them up, though (no GC).

Re:Stack-Oriented Programming by RodgerTheGreat on Tuesday January 14, 2020 @10:20PM (#22051503)
Of 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/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.

Re:Stack-Oriented Programming by ngcc_hk on Wednesday January 15, 2020 @06:23AM (#22053328)
Thanks. If I start today to learn as I have searched a bit ... it seems to have a dialect retroForth whilst I see a 10 years old recommend swiftforth ...

Actually the tutorial that host a forth on js (like clojurescript just self implement).

Which path to go?

Do not want to learn lisp ended up in scheme.

Re:Stack-Oriented Programming by lebuffon on Wednesday January 15, 2020 @09:29AM (#22054381)
Forth can be a little troublesome in that area. There is a standard language but it is primitive. Extensions for real world problems tend to diverge in syntax. This has lead to a saying: "If you've seen one Forth, you've seen one Forth" :)

It's better than in the past with an ISO standard (IMHO) but know that if you change language providers there will be differences for things like external library linking (if available), GUI interaces etc.

There are demo versions of commericial systems at:

https://www.mpeforth.com/resource-links/downloads/

https://www.forth.com/download/

And of course a major pass-time in the Forth community is writing your own Forth. There is even a few Forths written in Python. (for instructional purposes only I assume)

http://openbookproject.net/py4fun/forth/forth.html

Re:Stack-Oriented Programming by RodgerTheGreat on Wednesday January 15, 2020 @07:30AM (#22053623)
On desktops I've used gforth[0]. It's rather large and heavyweight, but easy to install and has decent documentation; not a bad starting point. Alternatively, JonesForth[1] is an excellent extensively-commented implementation suitable for reading start-to-finish.

[0] https://www.gnu.org/software/gforth/

[1] https://github.com/nornagon/jonesforth/blob/master/jonesfort...

Re:Stack-Oriented Programming by marcosdumay on Tuesday January 14, 2020 @04:37PM (#22049501)
Expressivity and execution costs are inversely correlated, but not perfectly so.

Forth is more expressive than C, but just a bit more expensive. It being more expressive means it is in a higher level on my book, but it's outside of the usual hierarchy, so I would say it's in a higher level, but not on the same ladder.

Re:Stack-Oriented Programming by rwmj on Wednesday January 15, 2020 @05:06AM (#22053049)
Re:Stack-Oriented Programming by agumonkey on Tuesday January 14, 2020 @05:33PM (#22049936)
Because there's no grammar in forth. You are the parser you can go as high as you want, yet you're still chaining near native instructions.

A double edged sword too