Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- \ PLOTIFY
- \ A very silly sample FORTH program which provides a means to plot graphs in a
- \ standard terminal.
- \ TERMINAL CONTROL
- \ Standard ASCII Codes (not comprehensive)
- \ Emits a bell
- : t-bel 7 emit ;
- \ Emits a backspace
- : t-bs 8 emit ;
- \ Emits a tab
- : t-tab 9 emit ;
- \ Emits a line feed
- : t-lf 10 emit ;
- \ Emits a vertical tab
- : t-tab-v 11 emit ;
- \ Emits a form feed
- : t-ff 12 emit ;
- \ Emits a carriage return
- : t-cr 13 emit ;
- \ Emits an escape
- : t-esc 27 emit ;
- \ Emits a delete
- : t-del 127 emit ;
- \ Control Sequence Introducer (CSI) commands (not comprehensive)
- \ Emit CSI
- : t-csi ( -- ) t-esc '[' emit ;
- \ Returns cursor to top left
- : t-home ( -- ) t-csi 'H' emit ;
- \ Places cursor at the requested column and row
- : t-at ( col row -- ) t-csi .nb ';' emit .nb 'H' emit ;
- \ Erase from cursor until end of screen
- : t-cleol ( -- ) t-csi ." J" ;
- \ Clear the entire screen and move cursor to top left
- : t-cls ( -- ) t-csi ." 2J" t-home ;
- \ Sets cursor to default foreground
- : t-default t-csi ." 0;0m" ;
- \ Sets foreground color to red
- : t-red t-csi ." 0;31m" ;
- \ Sets foreground color to green
- : t-green t-csi ." 0;32m" ;
- \ Sets foreground color to yellow
- : t-yellow t-csi ." 0;33m" ;
- \ Sets foreground color to blue
- : t-blue t-csi ." 0;34m" ;
- \ Sets foreground color to magenta
- : t-magenta t-csi ." 0;35m" ;
- \ Sets foreground color to cyan
- : t-cyan t-csi ." 0;36m" ;
- \ Sets foreground color to white
- : t-white t-csi ." 0;37m" ;
- \ Set custom foreground
- : t-rgb-fg ( r g b -- ) t-csi ." 38;2;" rot .nb ';' emit swap .nb ';' emit .nb 'm' emit ;
- \ Set custom background
- : t-rgb-bg ( r g b -- ) t-csi ." 48;2;" rot .nb ';' emit swap .nb ';' emit .nb 'm' emit ;
- \ Provides the terminal width
- : t-width ( -- cols ) cols ;
- \ Provides the terminal height
- : t-height ( -- rows ) rows ;
- \ Save the terminal position
- : t-save ( -- ) t-csi ." s" ;
- \ Returns position to previously saved
- : t-restore ( -- ) t-csi ." u" ;
- \ GENERAL PURPOSE WORDS
- \ Negative pi for convenience
- : -pi ( -- F: -pi ) pi fnegate ;
- \ Order the top 2 ints on the stack such that the lowest value is tos
- : max-min ( a b -- a b | b a ) 2dup > if swap endif ;
- \ Duplicate the top two floats on the float stack
- : f2dup ( F: a F: b -- F: a F: b F: a F: b ) fover fover ;
- \ Order the top 2 floats on the stack such that the lowest value is tos
- : fmax-min ( F: a F: b -- F: a F: b | F: b F: a ) f2dup f> if fswap endif ;
- \ Create and assign a new variable
- : var! ( r "name" -- ) create , does> ;
- \ Create and assign a new float variable
- : fvar! ( r "name" -- ) create f, does> ;
- \ GRAPH PLOTTING
- \ We'll use the following variables to define the ranges of x and y and specify
- \ the current plot character
- -10e fvar! x-lower
- 10e fvar! x-upper
- -10e fvar! y-lower
- 10e fvar! y-upper
- '*' var! xy-symbol
- \ Graphs are drawn in all but the last 5 rows of the terminal
- : xy-height ( -- rows ) t-height 5 - ;
- \ Returns the usable width of the terminal
- : xy-width ( -- cols ) t-width ;
- \ Places cursor below graph after rendering
- : xy-home ( -- ) 0 xy-height t-at t-default t-cleol ;
- \ Allows us to redefine the range of x
- : x-range ( F: lower F: upper -- ) x-upper f! x-lower f! ;
- \ Allows us to redefine the range of y
- : y-range ( F: lower F: upper -- ) y-upper f! y-lower f! ;
- \ Allows us to change the character plotted
- : set-xy-symbol ( char -- ) xy-symbol ! ;
- \ Determines the floating point increment of x
- : x-increment x-upper f@ x-lower f@ f- xy-width s>f f/ ;
- \ Determines the floating point increment of y
- : y-increment y-upper f@ y-lower f@ f- xy-height s>f f/ ;
- \ Determines the number of terminal columns covered by the value r
- : r-to-cols x-increment f/ fround f>s ;
- \ Maps x value to column
- : x-to-col ( F: x -- column ) x-lower f@ f- x-increment f/ fround f>s ;
- \ Maps y value to column
- : y-to-row ( F: y -- row ) y-lower f@ f- y-increment f/ fround f>s ;
- \ Render xy-symbol at col,row associated to x,y
- \ FIXME: Should confirm that derived row,col are valid
- : xy-plot
- y-lower f@ f- y-increment f/ xy-height s>f fswap f- fswap
- x-lower f@ f- x-increment f/ fswap
- f>s f>s swap
- t-at xy-symbol @ emit
- ;
- \ Map incoming col to x
- : col-to-x ( col -- F: x ) s>f x-increment f* x-lower f@ f+ ;
- \ Iterate through x-range evaluate equation for each col
- \ Examples:
- \ t-cls xy-home
- \ t-red s" fdup" x-loop xy-home
- \ t-green s" fdup fnegate" x-loop xy-home
- \ t-blue s" fdup fsin" x-loop xy-home
- \ t-magenta s" fdup 2e f** 10e f-" x-loop xy-home
- : 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 ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement