Advertisement
lilo_booter

Plotify

Nov 17th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.80 KB | None | 0 0
  1. \ PLOTIFY
  2.  
  3. \ A very silly sample FORTH program which provides a means to plot graphs in a
  4. \ standard terminal.
  5.  
  6. \ TERMINAL CONTROL
  7.  
  8.     \ Standard ASCII Codes (not comprehensive)
  9.  
  10.         \ Emits a bell
  11.         : t-bel 7 emit ;
  12.  
  13.         \ Emits a backspace
  14.         : t-bs 8 emit ;
  15.  
  16.         \ Emits a tab
  17.         : t-tab 9 emit ;
  18.  
  19.         \ Emits a line feed
  20.         : t-lf 10 emit ;
  21.  
  22.         \ Emits a vertical tab
  23.         : t-tab-v 11 emit ;
  24.  
  25.         \ Emits a form feed
  26.         : t-ff 12 emit ;
  27.  
  28.         \ Emits a carriage return
  29.         : t-cr 13 emit ;
  30.  
  31.         \ Emits an escape
  32.         : t-esc 27 emit ;
  33.  
  34.         \ Emits a delete
  35.         : t-del 127 emit ;
  36.  
  37.     \ Control Sequence Introducer (CSI) commands (not comprehensive)
  38.  
  39.         \ Emit CSI
  40.         : t-csi ( -- ) t-esc '[' emit ;
  41.  
  42.         \ Returns cursor to top left
  43.         : t-home ( -- ) t-csi 'H' emit ;
  44.  
  45.         \ Places cursor at the requested column and row
  46.         : t-at ( col row -- ) t-csi .nb ';' emit .nb 'H' emit ;
  47.  
  48.         \ Erase from cursor until end of screen
  49.         : t-cleol ( -- ) t-csi ." J" ;
  50.  
  51.         \ Clear the entire screen and move cursor to top left
  52.         : t-cls ( -- ) t-csi ." 2J" t-home ;
  53.  
  54.         \ Sets cursor to default foreground
  55.         : t-default t-csi ." 0;0m" ;
  56.  
  57.         \ Sets foreground color to red
  58.         : t-red t-csi ." 0;31m" ;
  59.  
  60.         \ Sets foreground color to green
  61.         : t-green t-csi ." 0;32m" ;
  62.  
  63.         \ Sets foreground color to yellow
  64.         : t-yellow t-csi ." 0;33m" ;
  65.  
  66.         \ Sets foreground color to blue
  67.         : t-blue t-csi ." 0;34m" ;
  68.  
  69.         \ Sets foreground color to magenta
  70.         : t-magenta t-csi ." 0;35m" ;
  71.  
  72.         \ Sets foreground color to cyan
  73.         : t-cyan t-csi ." 0;36m" ;
  74.  
  75.         \ Sets foreground color to white
  76.         : t-white t-csi ." 0;37m" ;
  77.  
  78.         \ Set custom foreground
  79.         : t-rgb-fg ( r g b -- ) t-csi ." 38;2;" rot .nb ';' emit swap .nb ';' emit .nb 'm' emit ;
  80.  
  81.         \ Set custom background
  82.         : t-rgb-bg ( r g b -- ) t-csi ." 48;2;" rot .nb ';' emit swap .nb ';' emit .nb 'm' emit ;
  83.  
  84.         \ Provides the terminal width
  85.         : t-width ( -- cols ) cols ;
  86.  
  87.         \ Provides the terminal height
  88.         : t-height ( -- rows ) rows ;
  89.  
  90.         \ Save the terminal position
  91.         : t-save ( -- ) t-csi ." s" ;
  92.  
  93.         \ Returns position to previously saved
  94.         : t-restore ( -- ) t-csi ." u" ;
  95.  
  96.  
  97. \ GENERAL PURPOSE WORDS
  98.  
  99.     \ Negative pi for convenience
  100.     : -pi ( -- F: -pi ) pi fnegate ;
  101.  
  102.     \ Order the top 2 ints on the stack such that the lowest value is tos
  103.     : max-min ( a b -- a b | b a ) 2dup > if swap endif ;
  104.  
  105.     \ Duplicate the top two floats on the float stack
  106.     : f2dup ( F: a F: b -- F: a F: b F: a F: b ) fover fover ;
  107.  
  108.     \ Order the top 2 floats on the stack such that the lowest value is tos
  109.     : fmax-min ( F: a F: b -- F: a F: b | F: b F: a ) f2dup f> if fswap endif ;
  110.  
  111.     \ Create and assign a new variable
  112.     : var! ( r "name" -- ) create , does> ;
  113.  
  114.     \ Create and assign a new float variable
  115.     : fvar! ( r "name" -- ) create f, does> ;
  116.  
  117.  
  118. \ GRAPH PLOTTING
  119.  
  120.     \ We'll use the following variables to define the ranges of x and y and specify
  121.     \ the current plot character
  122.     -10e fvar! x-lower
  123.     10e  fvar! x-upper
  124.     -10e fvar! y-lower
  125.     10e  fvar! y-upper
  126.     '*'  var!  xy-symbol
  127.  
  128.     \ Graphs are drawn in all but the last 5 rows of the terminal
  129.     : xy-height ( -- rows ) t-height 5 - ;
  130.  
  131.     \ Returns the usable width of the terminal
  132.     : xy-width ( -- cols ) t-width ;
  133.  
  134.     \ Places cursor below graph after rendering
  135.     : xy-home ( -- ) 0 xy-height t-at t-default t-cleol ;
  136.  
  137.     \ Allows us to redefine the range of x
  138.     : x-range ( F: lower F: upper -- ) x-upper f! x-lower f! ;
  139.  
  140.     \ Allows us to redefine the range of y
  141.     : y-range ( F: lower F: upper -- ) y-upper f! y-lower f! ;
  142.  
  143.     \ Allows us to change the character plotted
  144.     : set-xy-symbol ( char -- ) xy-symbol ! ;
  145.  
  146.     \ Determines the floating point increment of x
  147.     : x-increment x-upper f@ x-lower f@ f- xy-width s>f f/ ;
  148.  
  149.     \ Determines the floating point increment of y
  150.     : y-increment y-upper f@ y-lower f@ f- xy-height s>f f/ ;
  151.  
  152.     \ Determines the number of terminal columns covered by the value r
  153.     : r-to-cols x-increment f/ fround f>s ;
  154.  
  155.     \ Maps x value to column
  156.     : x-to-col ( F: x -- column ) x-lower f@ f- x-increment f/ fround f>s ;
  157.  
  158.     \ Maps y value to column
  159.     : y-to-row ( F: y -- row ) y-lower f@ f- y-increment f/ fround f>s ;
  160.  
  161.     \ Render xy-symbol at col,row associated to x,y
  162.     \ FIXME: Should confirm that derived row,col are valid
  163.     : xy-plot
  164.         y-lower f@ f- y-increment f/ xy-height s>f fswap f- fswap
  165.         x-lower f@ f- x-increment f/ fswap
  166.         f>s f>s swap
  167.         t-at xy-symbol @ emit
  168.     ;
  169.  
  170.     \ Map incoming col to x
  171.     : col-to-x ( col -- F: x ) s>f x-increment f* x-lower f@ f+ ;
  172.  
  173.     \ Iterate through x-range evaluate equation for each col
  174.     \ Examples:
  175.     \ t-cls xy-home
  176.     \ t-red s" fdup" x-loop xy-home
  177.     \ t-green s" fdup fnegate" x-loop xy-home
  178.     \ t-blue s" fdup fsin" x-loop xy-home
  179.     \ t-magenta s" fdup 2e f** 10e f-" x-loop xy-home
  180.     : x-loop x-upper f@ x-to-col x-lower f@ x-to-col do 2dup i col-to-x evaluate xy-plot loop 2drop ;
  181.  
  182.  
Tags: Forth
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement