PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Branches/1.2/ScintillaNET/Configuration/Samples/forth

#
Forth | 26 lines | 20 code | 6 blank | 0 comment | 1 complexity | 6d458563946dbfeb84b1feeb7b858602 MD5 | raw file
Possible License(s): MIT
  1. \ Note 1: Text after a backslash is a comment until end-of-line.
  2. \ Note 2: Text within parentheses like "( n -- )" is also a comment.
  3. \ Note 3: To be strictly ANSI compliant, the code below is in UPPERCASE.
  4. \ Most PC Forth implementations are (optionally) case insensitive.
  5. : STAR ( -- ) \ Print a single star
  6. 42 EMIT ; \ 42 is the ASCII code for *
  7. : STARS ( n -- ) \ Print n stars
  8. 0 DO STAR LOOP ; \ Loop n times (0 up to n-1) and execute STAR
  9. : SQUARE ( n -- ) \ Print an n-line square of stars
  10. DUP 0 DO \ Loop n times, keeping (DUP-licating) n on the stack
  11. DUP STARS CR \ Each time, print n stars then print CR
  12. LOOP DROP ; \ After loop is done, drop the n from the stack
  13. : TRIANGLE ( n -- ) \ Print an n-line triangle
  14. 1 + 1 DO \ Loop n times from 1 to n (instead of 0 to n-1)
  15. I STARS CR \ This time use the inner loop index I
  16. LOOP ;
  17. : TOWER ( n -- ) \ Print a "tower" with an base of size n
  18. DUP \ DUP-licate n (since it is used twice below)
  19. 1 - TRIANGLE \ Print a triangle 1 size smaller than n
  20. SQUARE ; \ Print a square base of size n