Hacker News new | ask | show | jobs
by etfb 4809 days ago
Concatenative language syntax is actually pretty easy at the basic level. I don't know Factor, but I used to dabble a lot in Forth. The single syntactical rule with Forth is that a symbol (called a "word") is one or more non-space characters, delimited by spaces. So the following three things are all words: SWAP ." -1

Next, there's the idea of the stack. Every word is executed, one after the other, and the words operate on the stack. So the four-word sequence 3 4 + . puts a 3 on the stack, then puts a 4 on the stack, then pops the top two numbers from the stack and puts their sum on the stack, then prints the top number on the stack. So the result, naturally, is 11. (Well, it is if you're operating in base 6 at the time. Didn't want to make it TOO easy for you!)

Oh, and there's also the concept of interpreting vs compiling. The colon word, which is spelled :, reads the very next word from the input and begins the definition of that word. Every word it encounters up to the next semi-colon is compiled, not executed. So if I did this

    3 4 + : FOO SWAP DROP ; .
... it would still print out the sum of 3 and 4, but in between it would also define a new word called FOO. Which would be a silly way to program, but it demonstrates the point.

So the short form is: begin reading at the top left, continue rightward and downward until you reach the bottom. There's no syntax as such, like Perl's die "Can't open file" unless $fileopen; because Forth-like languages don't read ahead to the end of the line.

[Edited: HN doesn't do Markdown. WTF? Also, I don't know my left and right.]