home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / wp / brcalc.zip / CALC.M next >
Text File  |  1984-11-02  |  14KB  |  772 lines

  1. ;;; calculator program
  2. ;;; written by Mark U. Edwards
  3. ;;; 11-02-84
  4.  
  5. #define MAXSTRING 16
  6.  
  7. (macro calc
  8.     (
  9.         (int    c_orig_buffer 
  10.                 c_acc
  11.                 c_ans_buff
  12.                 c_base
  13.                 c_op
  14.                 c_first_digit
  15.                 c_stage
  16.                 c_carry
  17.                 c_number_in
  18.                 c_display
  19.                 c_tape_on
  20.                 c_line
  21.                 c_digits
  22.         )
  23.         (string c_number_out)
  24.         (global    c_number_in
  25.                     c_number_out
  26.                     c_acc
  27.                     c_ans_buff
  28.                     c_base
  29.                     c_op
  30.                     c_first_digit
  31.                     c_display
  32.                     c_tape_on
  33.                     c_line
  34.                     c_digits
  35.                     c_stage
  36.                     c_carry
  37.         )
  38.                         
  39.         (pause_on_error)
  40.         (= c_ans_buff (create_buffer "Calculator" "$c$.tmp" 1))
  41.         (= c_orig_buffer (inq_buffer))
  42.         (set_buffer c_ans_buff)
  43.         (create_window 52 21 77 19 "F1 for Help")
  44.         (attach_buffer c_ans_buff)
  45.         (refresh)
  46.         (keyboard_push)
  47.         (assign_to_key "#27" "exit")
  48.         (assign_to_key "#48" "c_n_0")
  49.         (assign_to_key "#49" "c_n_1")
  50.         (assign_to_key "#50" "c_n_2")
  51.         (assign_to_key "#51" "c_n_3")
  52.         (assign_to_key "#52" "c_n_4")
  53.         (assign_to_key "#53" "c_n_5")
  54.         (assign_to_key "#54" "c_n_6")
  55.         (assign_to_key "#55" "c_n_7")
  56.         (assign_to_key "#56" "c_n_8")
  57.         (assign_to_key "#57" "c_n_9")
  58.         (assign_to_key "#97" "c_n_A")
  59.         (assign_to_key "#65" "c_n_A")
  60.         (assign_to_key "#98" "c_n_B")
  61.         (assign_to_key "#66" "c_n_B")
  62.         (assign_to_key "#99" "c_n_C")
  63.         (assign_to_key "#67" "c_n_C")
  64.         (assign_to_key "#100" "c_n_D")
  65.         (assign_to_key "#68" "c_n_D")
  66.         (assign_to_key "#101" "c_n_E")
  67.         (assign_to_key "#69" "c_n_E")
  68.         (assign_to_key "#102" "c_n_F")
  69.         (assign_to_key "#70" "c_n_F")
  70.         (assign_to_key "%#46" "c_clr_ent")
  71.         (assign_to_key "%#30" "c_clr_acc")
  72.         (assign_to_key "#13" "c_equals")
  73.         (assign_to_key "%#48" "c_to_bin")
  74.         (assign_to_key "%#32" "c_to_dec")
  75.         (assign_to_key "%#35" "c_to_hex")
  76.         (assign_to_key "#43" "c_plus")
  77.         (assign_to_key "#42" "c_times")
  78.         (assign_to_key "#45" "c_minus")
  79.         (assign_to_key "#47" "c_div")
  80.         (assign_to_key "#37" "c_mod")
  81.         (assign_to_key "#35" "c_plus_minus")
  82.         (assign_to_key "%#59" "c_help")
  83.         (assign_to_key "%#20" "c_tape")
  84.         (assign_to_key "%#25" "c_print")
  85.         (= c_acc 0)
  86.         (= c_number_in 0)    ;;; initialize
  87.         (= c_tape_on 0)
  88.         (= c_base 10)
  89.         (= c_line 1)
  90.         (= c_digits 0)
  91.         (c_print_base)
  92.         (move_abs c_line 16)
  93.         (= c_op 0)                ;;; no operation at present
  94.         (= c_first_digit 0)    ;;; first digit not yet typed
  95.         (= c_stage 1)            ;;; entering first number
  96.         (= c_display 1)        ;;; displaying c_number_in
  97.         (process)
  98.         (keyboard_pop)
  99.         (delete_window)
  100.         (delete_buffer c_ans_buff)
  101.         (set_buffer c_orig_buffer)
  102.     )
  103. )
  104. (macro _num
  105.     (
  106.         (string digit)
  107.         (int d)
  108.         (get_parm 0 digit)
  109.         (get_parm 1 d)
  110.         (if (== c_number_in 0)
  111.             (
  112.                 (move_abs c_line 19)
  113.                 (delete_char)
  114.                 (insert " ")
  115.                 (= c_digits 0)
  116.             )
  117.         )
  118.         (if (== c_first_digit 0)    ;;; first digit of new num
  119.             (
  120.                 (= c_number_in 0)
  121.                 (c_print_base)
  122.                 (= c_first_digit 1)
  123.                 (= c_digits 0)
  124.             )
  125.         )
  126.         (= c_digits (+ c_digits 1))
  127.         (if (||    (&& (== c_base 2) (<= c_digits 16)) 
  128.              (||  (&& (== c_base 10) (<= c_digits 5)) 
  129.                      (&& (== c_base 16) (<= c_digits 4))))
  130.             (
  131.                 (if (== c_stage 3)
  132.                     (c_clr_acc)
  133.                 )
  134.                 (move_abs c_line 3)
  135.                 (delete_char)
  136.                 (move_abs c_line 19)
  137.                 (= c_display 1)
  138.                 (insert digit)
  139.                 (move_abs c_line 19)
  140.                 (= c_number_in (+ (* c_number_in c_base) d))
  141.             )
  142.             ;else
  143.             (beep)
  144.         )
  145.     )
  146. )
  147. (macro c_n_0
  148.     (
  149.         (_num "0" 0)
  150.     )
  151. )
  152. (macro c_n_1
  153.     (
  154.         (_num "1" 1)
  155.     )
  156. )
  157. (macro c_n_2
  158.     (
  159.         (if (> c_base 2)
  160.             (_num "2" 2)
  161.             ;else
  162.             (beep)
  163.         )
  164.     )
  165. )
  166. (macro c_n_3
  167.     (
  168.         (if (> c_base 2)
  169.             (_num "3" 3)
  170.             ;else
  171.             (beep)
  172.         )
  173.     )
  174. )
  175. (macro c_n_4
  176.     (
  177.         (if (> c_base 2)
  178.             (_num "4" 4)
  179.             ;else
  180.             (beep)
  181.         )
  182.     )
  183. )
  184. (macro c_n_5
  185.     (
  186.         (if (> c_base 2)
  187.             (_num "5" 5)
  188.             ;else
  189.             (beep)
  190.         )
  191.     )
  192. )
  193. (macro c_n_6
  194.     (
  195.         (if (> c_base 2)
  196.             (_num "6" 6)
  197.             ;else
  198.             (beep)
  199.         )
  200.     )
  201. )
  202. (macro c_n_7
  203.     (
  204.         (if (> c_base 2)
  205.             (_num "7" 7)
  206.             ;else
  207.             (beep)
  208.         )
  209.     )
  210. )
  211. (macro c_n_8
  212.     (
  213.         (if (> c_base 2)
  214.             (_num "8" 8)
  215.             ;else
  216.             (beep)
  217.         )
  218.     )
  219. )
  220. (macro c_n_9
  221.     (
  222.         (if (> c_base 2)
  223.             (_num "9" 9)
  224.             ;else
  225.             (beep)
  226.         )
  227.     )
  228. )
  229. (macro c_n_A
  230.     (
  231.         (if (> c_base 10)
  232.             (_num "A" 10)
  233.             ;else
  234.             (beep)
  235.         )
  236.     )
  237. )
  238. (macro c_n_B
  239.     (
  240.         (if (> c_base 10)
  241.             (_num "B" 11)
  242.             ;else
  243.             (beep)
  244.         )
  245.     )
  246. )
  247. (macro c_n_C
  248.     (
  249.         (if (> c_base 10)
  250.             (_num "C" 12)
  251.             ;else
  252.             (beep)
  253.         )
  254.     )
  255. )
  256. (macro c_n_D
  257.     (
  258.         (if (> c_base 10)
  259.             (_num "D" 13)
  260.             ;else
  261.             (beep)
  262.         )
  263.     )
  264. )
  265. (macro c_n_E
  266.     (
  267.         (if (> c_base 10)
  268.             (_num "E" 14)
  269.             ;else
  270.             (beep)
  271.         )
  272.     )
  273. )
  274. (macro c_n_F
  275.     (
  276.         (if (> c_base 10)
  277.             (_num "F" 15)
  278.             ;else
  279.             (beep)
  280.         )
  281.     )
  282. )
  283. (macro c_clr_ent
  284.     (
  285.         (move_abs c_line 1)
  286.         (delete_line)
  287.         (= c_number_in 0)
  288.         (c_print_base)
  289.         (= c_digits 0)
  290.         (move_abs c_line 19)
  291.     )
  292. )
  293. (macro c_clr_acc
  294.     (
  295.         (= c_acc 0)
  296.         (c_clr_ent)
  297.         (= c_stage 1)
  298.         (= c_op 0)
  299.         (move_abs 1 1)
  300.         (drop_anchor)
  301.         (end_of_buffer)
  302.         (delete_block)
  303.         (= c_line 1)
  304.         (c_print_base)
  305.         (move_abs c_line 19)        
  306.     )
  307. )
  308. (macro c_print_result
  309.     (
  310.         (string num)
  311.         (get_parm 0 num)
  312.         (move_abs c_line 3)
  313.         (delete_to_eol)
  314.         (if (< (strlen num) 20)
  315.             (move_abs c_line (- 20 (strlen num)))  
  316.             ;else
  317.             (
  318.                 (move_abs c_line 5)
  319.                 (= num "- Error -")
  320.                 (beep)
  321.             )
  322.         )
  323.         (insert num)
  324.         (move_abs c_line 19)
  325.     )
  326. )
  327. (macro c_to_bin
  328.     (
  329.         (= c_line (+ c_line 1))
  330.         (message "Converting...")
  331.         (if (== c_display 1)
  332.             (= c_number_out (c_dtob c_number_in))
  333.             ;else
  334.             (= c_number_out (c_dtob c_acc))
  335.         )
  336.         (= c_base 2)
  337.         (message "")
  338.         (= c_digits (strlen c_number_out))
  339.         (c_print_result c_number_out)
  340.         (c_print_base)
  341.         (move_abs c_line 19)
  342.     )
  343. )
  344. (macro c_to_dec
  345.     (
  346.         (= c_line (+ c_line 1))
  347.         (message "Converting...")
  348.         (if (== c_display 1)
  349.             (sprintf c_number_out "%d" c_number_in)    
  350.             ;else
  351.             (sprintf c_number_out "%d" c_acc)
  352.         )
  353.         (= c_base 10)
  354.         (message "")
  355.         (= c_digits (strlen c_number_out))
  356.         (c_print_result c_number_out)
  357.         (c_print_base)
  358.         (move_abs c_line 19)
  359.     )
  360. )
  361. (macro c_to_hex
  362.     (
  363.         (= c_line (+ c_line 1))
  364.         (message "Converting...")
  365.         (if (== c_display 1)
  366.             (= c_number_out (c_dtoh c_number_in))
  367.             ;else
  368.             (= c_number_out (c_dtoh c_acc))
  369.         )
  370.         (= c_base 16)
  371.         (message "")
  372.         (= c_digits (strlen c_number_out))
  373.         (c_print_result c_number_out)
  374.         (c_print_base)
  375.         (move_abs c_line 19)
  376.     )
  377. )
  378. (macro c_print_base
  379.     (
  380.         (move_abs c_line 21)
  381.         (delete_char)
  382.         (delete_char)
  383.         (delete_char)
  384.         (if (== c_base 2)
  385.             (insert "Bin")
  386.             ;else
  387.             (if (== c_base 10)
  388.                 (insert "Dec")
  389.                 ;else
  390.                 (insert "Hex")
  391.             )
  392.         )
  393.         (move_abs c_line 19)
  394.         (refresh)
  395.     )
  396. )
  397. (macro c_print_acc
  398.     (
  399.         (= c_display 2)    ;;; displaying accumulator
  400.         (if (!= c_base 10)
  401.             (
  402.                 (if (== c_base 2)
  403.                     (= c_number_out (c_dtob c_acc))
  404.                     ;else
  405.                     (= c_number_out (c_dtoh c_acc))
  406.                 )
  407.             )
  408.             ;else
  409.             (
  410.                 (sprintf c_number_out "%d" c_acc)
  411.             )
  412.         )
  413.         (c_print_result c_number_out)
  414.     )
  415. )
  416. (macro c_print_number_in
  417.     (
  418.         (if (!= c_base 10)
  419.             (
  420.                 (if (== c_base 2)
  421.                     (= c_number_out (c_dtob c_number_in))
  422.                     ;else
  423.                     (= c_number_out (c_dtoh c_number_in))
  424.                 )
  425.             )
  426.             ;else
  427.             (
  428.                 (sprintf c_number_out "%d" c_number_in)
  429.             )
  430.         )
  431.         (c_print_result c_number_out)
  432.     )
  433. )
  434.  
  435. (macro c_print_ans
  436.     (
  437.         (if (== c_op 1) 
  438.             (= c_acc (+ c_acc c_number_in))
  439.         )
  440.         (if (== c_op 2)
  441.             (= c_acc (- c_acc c_number_in))
  442.         )
  443.         (if (== c_op 3)
  444.             (= c_acc (* c_acc c_number_in))
  445.         )
  446.         (if (== c_op 4)
  447.             (if (!= c_number_in 0)
  448.                 (= c_acc (/ c_acc c_number_in))
  449.                 ;else
  450.                 (
  451.                     (message "Attempting to div by 0!")
  452.                     (beep)
  453.                     (c_clr_acc)
  454.                 )
  455.             )
  456.         )
  457.         (if (== c_op 5)
  458.             (if (!= c_number_in 0)
  459.                 (= c_acc (% c_acc c_number_in))
  460.                 ;else
  461.                 (
  462.                     (message "Attempting to div by 0!")
  463.                     (beep)
  464.                     (c_clr_acc)
  465.                 )
  466.             )
  467.         )
  468.         (= c_number_in 0)
  469.         (c_print_acc)
  470.         (c_print_base)
  471.         (message "")
  472.     )
  473. )
  474. (macro c_equals
  475.     (
  476.         (= c_line (+ c_line 1))
  477.         (move_abs c_line 2)
  478.         (delete_char)
  479.         (insert "=")
  480.         (c_print_ans)
  481.         (= c_stage 3)  ;;; signals that already calculated
  482.         (= c_first_digit 0)
  483.     )
  484. )
  485. (macro c_plus
  486.     (
  487.         (if (== c_stage 2)
  488.             (c_equals)
  489.         )
  490.         (= c_line (+ c_line 1))
  491.         (move_abs c_line 2)
  492.         (delete_char)
  493.         (insert "+")
  494.         (if (== c_stage 1)
  495.             (= c_acc c_number_in)
  496.         )
  497.         (= c_op 1)  ;;; next operation is addition
  498.         (= c_stage 2)
  499.         (= c_first_digit 0)
  500.     )
  501. )
  502. (macro c_times
  503.     (                  
  504.         (if (== c_stage 2)
  505.             (c_equals)
  506.         )
  507.         (= c_line (+ c_line 1))
  508.         (move_abs c_line 2)
  509.         (delete_char)
  510.         (insert "*")
  511.         (if (== c_stage 2)
  512.             (c_print_ans)
  513.         )
  514.         (if (== c_stage 1)
  515.             (= c_acc c_number_in)
  516.         )
  517.         (= c_op 3)  ;;; next operation is multiplication
  518.         (= c_stage 2)
  519.         (= c_first_digit 0)
  520.     )
  521. )
  522. (macro c_minus
  523.     (                  
  524.         (if (== c_stage 2)
  525.             (c_equals)
  526.         )
  527.         (= c_line (+ c_line 1))
  528.         (move_abs c_line 2)
  529.         (delete_char)
  530.         (insert "-")
  531.         (if (== c_stage 2)
  532.             (c_print_ans)
  533.         )
  534.         (if (== c_stage 1)
  535.             (= c_acc c_number_in)
  536.         )
  537.         (= c_op 2)  ;;; next operation is subtraction
  538.         (= c_stage 2)
  539.         (= c_first_digit 0)
  540.     )
  541. )
  542. (macro c_div
  543.     (                  
  544.         (if (== c_stage 2)
  545.             (c_equals)
  546.         )
  547.         (= c_line (+ c_line 1))
  548.         (move_abs c_line 2)
  549.         (delete_char)
  550.         (insert "/")
  551.         (if (== c_stage 2)
  552.             (c_print_ans)
  553.         )
  554.         (if (== c_stage 1)
  555.             (= c_acc c_number_in)
  556.         )
  557.         (= c_op 4)  ;;; next operation is division
  558.         (= c_stage 2)
  559.         (= c_first_digit 0)
  560.     )
  561. )
  562. (macro c_mod
  563.     (                  
  564.         (if (== c_stage 2)
  565.             (c_equals)
  566.         )
  567.         (= c_line (+ c_line 1))
  568.         (move_abs c_line 2)
  569.         (delete_char)
  570.         (insert "%")
  571.         (if (== c_stage 2)
  572.             (c_print_ans)
  573.         )
  574.         (if (== c_stage 1)
  575.             (= c_acc c_number_in)
  576.         )
  577.         (= c_op 5)  ;;; next operation is modulo
  578.         (= c_stage 2)
  579.         (= c_first_digit 0)
  580.     )
  581. )
  582. (macro c_dtoh
  583.     (
  584.         (int n)
  585.         (get_parm 0 n)
  586.         (string num)
  587.         (= num (c_dtob n))        ;;; first convert to binary string
  588.         (= num (c_btoh num))        ;;; then convert to hex string
  589.         (returns num)
  590.     )
  591. )
  592. (macro c_dtob
  593.     (
  594.         (string r remain)
  595.         (int i n rmdr neg)
  596.         (get_parm 0 n)
  597.         (= r "")
  598.         (= neg 0)    ;;; set neg flag off
  599.         (if (< n 0)
  600.             (
  601.                 (= n (+ n 1))    ;;; adjust for twos complement
  602.                 (= neg 1)        ;;; signal that negative number
  603.                 (= n (* n -1))
  604.             )
  605.         )
  606.         (= rmdr 0)
  607.         (= i 0)
  608.         (while (!= n 0)
  609.             (
  610.                 (= i (+ i 1))
  611.                 (message "Binary digit %d" i)
  612.                 (= rmdr (% n 2))
  613.                 (= n (/ n 2))
  614.                 (sprintf remain "%d" rmdr)
  615.                 (= r (+ remain r))
  616.             )
  617.         )
  618.         (if (== neg 1)
  619.             (= r (ones_complement r))
  620.         )
  621.         (if (== r "")
  622.             (= r "0")
  623.         )
  624.         (returns r)
  625.     )
  626. )
  627. (macro c_btoh        ;;; binary to hex, using strings
  628.     (
  629.         (string r n t)
  630.         (int i len j nn)
  631.         (get_parm 0 n)
  632.         (= len (strlen n))
  633.         (= j (% len 4))
  634.         (if (> j 0) (= n (+ (substr "000" j) n)))    ;;; make even packets
  635.         (= len (strlen n))
  636.         (= i 1)
  637.         (while (<= i len)
  638.             (
  639.                 (= t (substr n i 4))    ;;; pick up hex packet
  640.                 (= nn 0)
  641.                 (= j 1)
  642.                 (while (<= j 4)
  643.                     (
  644.                         (= nn (* nn 2))
  645.                         (if (== "1" (substr t j 1)) (= nn (+ nn 1)))
  646.                         (= j (+ j 1))
  647.                     )
  648.                 )
  649.                 (= r (+ r (substr "0123456789ABCDEF" (+ nn 1) 1)))
  650.                 (= i (+ i 4))
  651.             )
  652.         )
  653.         (returns r)
  654.     )
  655. )
  656. (macro ones_complement
  657.     (
  658.         (string n t)
  659.         (int i len)
  660.         (get_parm 0 n)
  661.         (= n (+ "0" n))
  662.         (= len (strlen n))
  663.         (= t "")
  664.         (= i 1)
  665.          (while (<= i len)                ;;; ones complement
  666.             (
  667.                 (if (== "0" (substr n i 1))
  668.                     (= t (+ t "1"))
  669.                     ;else
  670.                     (= t (+ t "0"))
  671.                 )
  672.                 (= i (+ i 1))
  673.             )
  674.         )
  675.         (while (< len MAXSTRING)    ;;; fill out to MAXSTRING digits
  676.             (
  677.                 (= t (+ "1" t))
  678.                 (= len (+ len 1))
  679.             )
  680.         )
  681.         (returns t)
  682.     )
  683. )
  684. (macro c_plus_minus
  685.     (
  686.         (= c_line (+ c_line 1))
  687.         (move_abs c_line 2)
  688.         (delete_char)
  689.         (insert "#")
  690.         (if (== c_display 1)
  691.             (
  692.                 (= c_number_in (* c_number_in -1))
  693.                 (c_print_number_in)
  694.             )
  695.             ;else
  696.             (
  697.                 (= c_acc (* c_acc -1))
  698.                 (c_print_acc)
  699.             )
  700.         )
  701.         (c_print_base)
  702.     )
  703. )
  704. (macro c_help
  705.     (
  706.         (int     c_help_buf
  707.                 c_calc_buf
  708.         )
  709.         (= c_help_buf (create_buffer "Help" "$h$.tmp" 1))
  710.         (= c_calc_buf (inq_buffer))
  711.         (set_buffer c_help_buf)
  712.         (create_window 2 16 50 1 "ESC to exit from Help")
  713.         (attach_buffer c_help_buf)
  714.         (refresh)
  715.         (keyboard_push)
  716.         (assign_to_key "#27" "exit")
  717.         (move_abs 1 1)
  718.         (insert " 16 Bit Calculator: range 32767 to -32768\n\n")
  719.         (insert " NumLck must be on for Number Pad to work\n")
  720.         (insert "             Operations\n")
  721.         (insert " + plus,   - minus,   * times,   / divide\n")
  722.         (insert " % modulo, # changes sign\n")
  723.         (insert " ALT-C Clear Entry  ALT-A clear Accumulator\n")
  724.         (insert " ALT-T toggle Tape  ALT-P Print tape\n")
  725.         (insert "            Changing Base\n")
  726.         (insert " ALT-D Decimal           [Digits 0..9]\n")
  727.         (insert " ALT-B Binary            [Digits 0..1]\n")
  728.         (insert " ALT-H Hex               [Digits 0..9, A..F]\n\n")
  729.         (insert "        ESC to exit from Calculator")
  730.         (refresh)
  731.         (process)
  732.         (keyboard_pop)
  733.         (delete_window)
  734.         (delete_buffer c_help_buf)
  735.         (set_buffer c_calc_buf)
  736.  
  737.     )
  738. )
  739. (macro c_tape
  740.     (
  741.         (if (== c_tape_on 0)
  742.             (
  743.                 (delete_window)
  744.                 (create_window 52 21 77 2 "F1 for Help")
  745.                 (attach_buffer c_ans_buff)
  746.                 (refresh)
  747.                 (= c_tape_on 1)
  748.                 ;(change_window 2)    ;;; correct direction??
  749.             )
  750.             ; else
  751.             (
  752.                 ;(change_window 0)    ;;; correct direction??
  753.                 (delete_window)
  754.                 (= c_tape_on 0)
  755.                 (create_window 52 21 77 19 "F1 for Help")
  756.                 (attach_buffer c_ans_buff)
  757.             )
  758.         )
  759.     )
  760. )
  761. (macro c_print
  762.     (
  763.         (int x y)
  764.         (inq_position y x)
  765.         (move_abs 1 1)
  766.         (drop_anchor)
  767.         (end_of_buffer)
  768.         (print)
  769.         (move_abs y x)
  770.     )
  771. )
  772.