Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Post Made Community Wiki by Dennis
Length 13
Source Link
jwodder
  • 471
  • 3
  • 5

Length 13

1 2 3 4 d+ d.

As previously indicated, a Forth value's type is all in how you use it — if you treat a value as a pointer, it's a pointer, and if that value's not a good pointer, it's your fault for treating it as one. However, regardless of what type you're treating a value as, it will always take up one cell on the data stack; the exceptions are double cell or double precision integers. These are integers that are represented by two values on the stack, allowing you to perform arithmetic with twice as many bits as usual. The more significant or higher-bit cell is placed on top of the less significant or lower-bit one, so that 1 0 is the double cell representation of 1, and 0 1 is either 2^32 or 2^64, depending on how big your Forth's regular cells are. Naturally, in order to treat a double-cell value as such, we need to use words that explicitly operate on double-cell values; these are generally just d (or ud for unsigned) followed by the name of the corresponding single-cell word: d+ for addition, d< for comparison, d. for printing, etc.

Length 13

1 2 3 4 d+ d.

As previously indicated, a Forth value's type is all in how you use it — if you treat a value as a pointer, it's a pointer, and if that value's not a good pointer, it's your fault for treating it as one. However, regardless of what type you're treating a value as, it will always take up one cell on the data stack; the exceptions are double cell or double precision integers. These are integers that are represented by two values on the stack, allowing you to perform arithmetic with twice as many bits as usual. The more significant or higher-bit cell is placed on top of the less significant or lower-bit one, so that 1 0 is the double cell representation of 1, and 0 1 is either 2^32 or 2^64, depending on how big your Forth's regular cells are. Naturally, in order to treat a double-cell value as such, we need to use words that explicitly operate on double-cell values; these are generally just d (or ud for unsigned) followed by the name of the corresponding single-cell word: d+ for addition, d< for comparison, d. for printing, etc.

Length 6
Source Link
jwodder
  • 471
  • 3
  • 5

Length 6

." Hi"

Prints Hi. ." is a Forth word (which, like all Forth words, must be followed by whitespace or EOF) that reads the input stream up through the next " and prints out any bytes in between. If you put more than one space after the .", all but the space immediately after the ." will be printed. Escape sequences are not supported (so you can't print a string with a " in it with ."), but Gforth adds .\" to the language, which does support them.

Length 7

Length 7

Length 6

." Hi"

Prints Hi. ." is a Forth word (which, like all Forth words, must be followed by whitespace or EOF) that reads the input stream up through the next " and prints out any bytes in between. If you put more than one space after the .", all but the space immediately after the ." will be printed. Escape sequences are not supported (so you can't print a string with a " in it with ."), but Gforth adds .\" to the language, which does support them.

Length 7

Length 9
Source Link
jwodder
  • 471
  • 3
  • 5

Length 9

IF 1 THEN

Finally, something familiar! Obviously, this code tests whether its argument 1 is true and, if so, executes whatever's after the THEN, right? Wrong. When execution reaches the IF, the value on top of the stack is popped off, and if that value is true (i.e., nonzero), execution continues with whatever's inside the IF ... THEN and then whatever's after the THEN; if the value is zero, we just skip straight to after THEN. Note that, due to how these words are implemented internally (which is in terms of other Forth words!), IF and THEN can only be used inside a word definition, not in "interpret state."

Length 12

Length 12

Length 9

IF 1 THEN

Finally, something familiar! Obviously, this code tests whether its argument 1 is true and, if so, executes whatever's after the THEN, right? Wrong. When execution reaches the IF, the value on top of the stack is popped off, and if that value is true (i.e., nonzero), execution continues with whatever's inside the IF ... THEN and then whatever's after the THEN; if the value is zero, we just skip straight to after THEN. Note that, due to how these words are implemented internally (which is in terms of other Forth words!), IF and THEN can only be used inside a word definition, not in "interpret state."

Length 12

Length 12
Source Link
jwodder
  • 471
  • 3
  • 5
Loading
added 734 characters in body
Source Link
jwodder
  • 471
  • 3
  • 5
Loading
added 838 characters in body
Source Link
jwodder
  • 471
  • 3
  • 5
Loading
added 838 characters in body
Source Link
jwodder
  • 471
  • 3
  • 5
Loading
added 815 characters in body
Source Link
jwodder
  • 471
  • 3
  • 5
Loading
added 247 characters in body
Source Link
jwodder
  • 471
  • 3
  • 5
Loading
Source Link
jwodder
  • 471
  • 3
  • 5
Loading

AltStyle によって変換されたページ (->オリジナル) /