Jump to content
Sign in to follow this  
ramidavis

...

Recommended Posts

Forth is a stack-oriented language and uses postfix all over the place. If you order a veggie burger, don't expect beef. (I'm explicitly neutral about diet here.)

 

Most programming languages don't formulate equations. They formulate terms for calculation, though. You (and me as well) may be familiar with infix notation, but this does not fit into the Forth concept.

Edited by mizapf

Share this post


Link to post
Share on other sites

Postfix notation is driving me up the walls.

I am sorry, but my brain just does not see something like "4 2 3 + *" as being a valid math equation that equals 20.

attachicon.gifforth.png :?

+ adds the first 2 numbers, which are 4 and 2: 4+2=6

then * multiplies the numbers left on the stack, 3 and 6: 3*6=18

... At least that is the way i keep seeing it in my mind. :dunce:

(2+3)*4 on the other hand makes perfect sense. :thumbsup:

Someone please tell me there is either:

a. a way to force forth to interpret infix notation

or b. a infix to postfix convertor that i can paste a math formula into and get something i can use in forth in return.

 

This is probably my just deserts for not paying close attention in math class.

I'm positive they touched on postfix, even if only briefly. But apparently it did not stick.

 

No. + does not add the first two numbers. It adds the top two numbers.

 

When you type 4 2 3 you have 3 at the top of the stack, 2 underneath it, and 4 at the bottom. It's a stack. Like a stack of plates.

 

So, + removes 3 and 2 from the stack, adds them, and puts the result (5) on the stack.

 

The stack now looks like this: 4 5

 

5 is at the top of the stack

 

Then, * removes the 5 and 4, multiplies them, and puts the product on the stack. 20.

 

You should read this. It will work with GForth.

 

Type .s to see the stack.

 

It's hard at first. Hang in there!

  • Like 2

Share this post


Link to post
Share on other sites

(2+3)*4 on the other hand makes perfect sense. :thumbsup:

 

Nope. Sorry, but you're wrong. :)

 

Just like "regular" in-fix, there are ways to layout the expression to make it much easier to read. And when you get the layout correct, it reads as English, and is MUCH easier to understand that in-fix with it's nested parenthesis.

 

Your example was: 4 2 3 + *

 

However, look what happens if you organise it like this:

 

2 3 + 4 *

 

Here's how the above reads in English:

  • Put 2 on the stack
  • Put 3 on the stack
  • Add them (the stack now contains 5)
  • Put 4 on the stack
  • Multiply them

 

Ta dah! Simple! :D :thumbsup:

Share this post


Link to post
Share on other sites

Forth inc.'s own swift forth does not know what to do with FORGET. Niether does yforth and gforth.

Only pforth knew what to do.

swift forth:

: test ; ok

forget test forget ?

 

gforth:

: test ; ok

forget test

:2: Undefined word

>>>forget<<< test

Backtrace:

$B7170EDC throw

$B717D638 no.extensions

$B7171054 interpreter-notfound1

 

yforth:

: test ;

ok

forget test

[forget] error(2): unknown word.

 

pforth:

: test ;

ok

Stack<10>

forget test

ok

Stack<10>

 

It's sad but true, you are shooting a moving target.

 

Forth has been going through standards changes.

Forget is easy to implement in a simple system and near impossible in a complex native code system like Swiftforth.

 

So it has been deprecated. The new word is MARKER used as below.

 

MARKER STARTOVER

 

 

Then you type STARTOVER and the system goes back to the point where the MARKER was compiled.

 

But FORGET is part of TurboForth and FB-Forth.

Edited by TheBF

Share this post


Link to post
Share on other sites

Forth inc.'s own swift forth does not know what to do with FORGET. Niether does yforth and gforth.

Only pforth knew what to do.

 

FORGET is a dangerous word as you might expect. It is still in the current (2016) proposed ANS Forth standard, but is there declared obsolescent. It is in TI Forth, fbForth and TurboForth. It is probably also in CAMEL99 Forth—Brian will have to confirm.

 

...lee

Share this post


Link to post
Share on other sites

 

FORGET is a dangerous word as you might expect. It is still in the current (2016) proposed ANS Forth standard, but is there declared obsolescent. It is in TI Forth, fbForth and TurboForth. It is probably also in CAMEL99 Forth—Brian will have to confirm.

 

...lee

 

I did not implement FORGET, but I should. It's still very handy for testing new code. I do miss it.

Share this post


Link to post
Share on other sites

Postfix notation is driving me up the walls.

I am sorry, but my brain just does not see something like "4 2 3 + *" as being a valid math equation that equals 20.

attachicon.gifforth.png :?

+ adds the first 2 numbers, which are 4 and 2: 4+2=6

then * multiplies the numbers left on the stack, 3 and 6: 3*6=18

... At least that is the way i keep seeing it in my mind. :dunce:

(2+3)*4 on the other hand makes perfect sense. :thumbsup:

Someone please tell me there is either:

a. a way to force forth to interpret infix notation

or b. a infix to postfix convertor that i can paste a math formula into and get something i can use in forth in return.

 

This is probably my just deserts for not paying close attention in math class.

I'm positive they touched on postfix, even if only briefly. But apparently it did not stick.

 

I feel your pain with postfix. We have been pretty solidly brainwashed into thinking in "infix" notation after just a few years at school.

Willsy has given you some excellent insights on how postfix in Forth really works.

 

The thing that saves Forth for me is the interpreter. I type in the inputs and use the math operators and see what comes out.

No compiling, no linking no waiting. When it works I give it name. It's now in the can and locked down.

 

Once you have a named function that it is no different than calling TI-BASIC FUNCTION which is PREFIX notation.

 

So the interpreter is your friend and making little words to do pieces of math are your friend.

Share this post


Link to post
Share on other sites

So... RamiDavis,

 

Has your brain turned upside down yet? :-)

 

Just wondering on your progress.

Share this post


Link to post
Share on other sites

Unfortunate. He found the infix convertor and actually if you just test your thoughts with the interpreter it's pretty easy.

 

99 1 + . 100 ok

 

It just sits there waiting for your input. How hard is that?

I believe it's good for neural networks to get re-wired now and then. Keeps them healthy.

 

I learned to play the trumpet at 57. (My mouth still hurts)

Share this post


Link to post
Share on other sites

I learned to play the trumpet at 57. (My mouth still hurts)

No. It's the THIN end that goes in your mouth ;-)

  • Like 2

Share this post


Link to post
Share on other sites

Can't understand why anyone can't deal with postfix. All it requires is the *patience* to sit down and *learn*.

 

It takes all of 60 seconds to understand the concept. Maybe a day or two of practice until it feels natural.

 

I'm still in love with it.

  • Like 1

Share this post


Link to post
Share on other sites

For me, the key was realizing that the stack (a LIFO, i.e., “Last In First Out”, buffer) is where all operands normally go and that words (operators) normally must deal with them as they find them on top of the stack. I say “normally” because it takes a little more work to get at items that are not on top. Even then, most words that mess with the stack order put the desired cell (16 bits or 2 bytes) on top for use.

 

...lee

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...