home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8708 / 26 < prev    next >
Encoding:
Internet Message Format  |  1990-07-13  |  8.4 KB

  1. From: kjepo@comix.liu.se.UUCP (Kjell Post)
  2. Newsgroups: comp.sources.misc
  3. Subject: bournebasic
  4. Keywords: basic, bourneshell, molasses
  5. Message-ID: <4210@ncoast.UUCP>
  6. Date: 19 Aug 87 01:54:12 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Organization: CIS Dept, Univ of Linkoping, Sweden
  9. Lines: 225
  10. Approved: allbery@ncoast.UUCP
  11. X-Archive: comp.sources.misc/8708/26
  12.  
  13.  
  14. Here's a useful BASIC interpreter written in Bourne shell.
  15. There's no manual but this demo shows the most salient features:
  16.  
  17. 10 print "Enter any number and I will double it for you ";
  18. 20 input x
  19. 30 y = 2*x
  20. 40 print "The answer is ";
  21. 50 print y
  22. 60 print "Enter -1 if you want to continue."
  23. 70 input c
  24. 80 if c = -1 then 10
  25. 90 print "Bye"
  26.  
  27. Also, the commands 'load', 'save', 'list', 'run', and 'new'
  28. work as expected. Enjoy!
  29.  
  30.     d-duck@obelix.liu.se
  31.     k-post@obelix.liu.se
  32.  
  33.  
  34. ------cut here-------------------------------------
  35. tfiles=/tmp/bas$$
  36. export tfiles
  37. trap "rm ${tfiles?Bug}*; exit" 2
  38. > ${tfiles}
  39. echo 'Hi there!'
  40. while true
  41. do
  42.     echo -n '>'
  43.     read com com2 || exit
  44.     case $com in
  45.         list)
  46.             cat ${tfiles}
  47.             ;;
  48.         run)
  49.             echo 0 > ${tfiles}.ln
  50.             while true
  51.             do
  52.             while true
  53.             do
  54.                 case `cat ${tfiles}.ln` in
  55.                     0)
  56.                         read lineno line || exit
  57.                         ;;
  58.                     *)
  59.                         while true
  60.                         do
  61.                             read lineno line || exit
  62.                             match=`cat ${tfiles}.ln`
  63.                             case $lineno in
  64.                                 $match)
  65.                                     break
  66.                                     ;;
  67.                                 *)
  68.                                     ;;
  69.                             esac
  70.                         done
  71.                 esac
  72.                 while true
  73.                 do
  74.                     
  75.                     case $line in
  76.                         print*)
  77.                             expr=`echo $line | sed 's/print\ *//'`
  78.                             case $expr in
  79.                                 \"*)
  80.                                     echo -n `echo "$expr" | sed 's/^\"//
  81.                                                                s/\".*$//'`
  82.                                     case $line in
  83.                                         *\;)
  84.                                             ;;
  85.                                         *)
  86.                                             echo
  87.                                             ;;
  88.                                     esac
  89.                                     ;;
  90.                                 *)
  91.                                     > ${tfiles}.ex
  92.                                     files=`echo ${tfiles}.?`
  93.                                     case $files in
  94.                                         ${tfiles}.\?)
  95.                                             ;;
  96.                                         *)
  97.                                             for i in $files
  98.                                         do
  99.                                             cat $i >> ${tfiles}.ex
  100.                                         done
  101.                                         ;;
  102.                                     esac
  103.                                 echo $expr >> ${tfiles}.ex
  104.                                     bc < ${tfiles}.ex > ${tfiles}.res
  105.                                     cat ${tfiles}.res
  106.                                     ;;
  107.                             esac
  108.                             ;;
  109.                         if*)
  110.                             expr=`echo $line | sed '{
  111.                                                     s/if\ */\(/
  112.                                                     s/[#=]/\)-\(/
  113.                                                     s/\ *then.*/\)/
  114.                                                     }'`
  115.                             target=`echo $line | sed 's/.*then\ *//'`
  116.                             > ${tfiles}.ex
  117.                             files=`echo ${tfiles}.?`
  118.                             case $files in
  119.                                 ${tfiles}.\?)
  120.                                     ;;
  121.                                 *)
  122.                                     for i in $files
  123.                                 do
  124.                                     cat $i >> ${tfiles}.ex
  125.                                 done
  126.                                 ;;
  127.                         esac
  128.                         echo $expr >>${tfiles}.ex
  129.                             res=`bc < ${tfiles}.ex`
  130.                             case $res in
  131.                                 0*)
  132.                                     case $line in
  133.                                         *=*)
  134.                                             echo $target > ${tfiles}.ln
  135.                                             exit
  136.                                             ;;
  137.                                         *)
  138.                                             ;;
  139.                                     esac
  140.                                     ;;
  141.                                 *)
  142.                                     case $line in
  143.                                         *#*)
  144.                                             echo $target > ${tfiles}.ln
  145.                                             exit
  146.                                             ;;
  147.                                         *)
  148.                                             ;;
  149.                                     esac
  150.                                     ;;
  151.                             esac
  152.                             ;;
  153.                         end*)
  154.                             echo 0 > ${tfiles}.ln
  155.                             exit
  156.                             ;;
  157.                         goto*)
  158.                             expr=`echo $line | sed 's/goto\ *//'`
  159.                             echo $expr > ${tfiles}.ln
  160.                             exit
  161.                             ;;
  162.                         input*)
  163.                             var=`echo $line | sed 's/input\ *//'`
  164.                             echo -n "?"
  165.                             echo -n "$var=" > ${tfiles}.$var
  166.                             while true
  167.                             do
  168.                                 read val
  169.                                 echo $val
  170.                                 break
  171.                             done < /dev/tty >> ${tfiles}.$var
  172.                             ;;
  173.                         *)
  174.                             > ${tfiles}.ex
  175.                             files=`echo ${tfiles}.?`
  176.                             case $files in
  177.                                 ${tfiles}.\?)
  178.                                     ;;
  179.                                 *)
  180.                                     for i in $files
  181.                                 do
  182.                                     cat $i >> ${tfiles}.ex
  183.                                 done
  184.                                 ;;
  185.                         esac
  186.                         echo $line >>${tfiles}.ex
  187.                             var=`echo $line | sed 's/\ *=.*//'`
  188.                             echo $var >> ${tfiles}.ex
  189.                             bc < ${tfiles}.ex > ${tfiles}.res
  190.                             echo -n $var= > ${tfiles}.$var
  191.                             cat ${tfiles}.res >> ${tfiles}.$var
  192.                             ;;
  193.                     esac
  194.                     read lineno line || exit
  195.                 done
  196.             done < ${tfiles}
  197.             case `cat ${tfiles}.ln` in
  198.                 0*)
  199.                     break
  200.                     ;;
  201.                 *)
  202.                     ;;
  203.             esac
  204.             done
  205.             ;;
  206.         new)
  207.             >${tfiles}
  208.             ;;
  209.         quit)
  210.             echo 'Thank you for your patience.'
  211.             rm ${tfiles?Bug}*
  212.             exit
  213.             ;;
  214.         load)
  215.             cp $com2 ${tfiles}
  216.             ;;
  217.         save)
  218.             cp ${tfiles} $com2
  219.             ;;
  220.         [0-9]*)
  221.             echo $com $com2 > ${tfiles}.nl
  222.             grep -v "^$com" ${tfiles} >> ${tfiles}.nl
  223.             sort -n ${tfiles}.nl > ${tfiles}
  224.             ;;
  225.     "")
  226.         ;;
  227.         *)
  228.             echo 'Illegal command'
  229.     esac
  230. done
  231. ------------cut here too while you're at it!----------------------
  232.  
  233. -- 
  234. -----------------------------------------------------------------------------
  235. "The nice thing about standards is that you have so many to choose from"
  236. Dept of Computer & Info Science  ...liuida!majestix.liu.se!kjepo
  237. Univ of Linkoping, Sweden        (kjepo@majestix.liu.se)
  238.