home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19556 < prev    next >
Encoding:
Text File  |  1993-01-11  |  2.9 KB  |  78 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!howland.reston.ans.net!spool.mu.edu!yale.edu!yale!gumby!destroyer!ncar!noao!amethyst!organpipe.uug.arizona.edu!news
  2. From: dave@cs.arizona.edu (Dave Schaumann)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Conversion of BASIC code to "C"
  5. Message-ID: <1993Jan12.003007.26709@organpipe.uug.arizona.edu>
  6. Date: 12 Jan 93 00:30:07 GMT
  7. References: <1993Jan8.183821.20711@adobe.com> <1993Jan10.131651.1@csbina.csubak.edu>
  8. Sender: news@organpipe.uug.arizona.edu
  9. Reply-To: dave@cs.arizona.edu (Dave Schaumann)
  10. Organization: University of Arizona
  11. Lines: 64
  12. In-Reply-To: camp5@csbina.csubak.edu
  13.  
  14. In article <1993Jan10.131651.1@csbina.csubak.edu>, camp5@csbina writes:
  15. >In article <1993Jan8.183821.20711@adobe.com>, (Scott Sivi) writes:
  16. >> Does there exist a method or application by which BASIC files can be
  17. >> converted 
  18. >> to "C".
  19. >You probably couldn't even do it by hand unless the BASIC program was 
  20. >completely flowed and never used a GOTO! The way BASIC programmers spaghetti-
  21. >code programs, I doubt it's possible. (I'm assuming you won't use goto in C.
  22. >I wouldn't.)
  23.  
  24. Eh, you could do it.  But given the sort of code you could generate, you'd
  25. probably be better off either hand-porting the code to C, or writing/buying
  26. a BASIC compiler.
  27.  
  28. For instance, consider the simple BASIC program
  29.  
  30.     100 FOR I=1 TO 10
  31.         110 GOSUB 200
  32.         120 NEXT I
  33.         130 END
  34.         200 PRINT "I=";I;"I^2=";I*I
  35.         210 RETURN
  36.  
  37. Might "compile" to something like
  38.  
  39. int line[] = { 100, 110, 120, 130, 200, 210, -1 } ;
  40.  
  41. main() {
  42.  
  43.   int pc = 0 ;
  44.   int uv_i ;
  45.   for( pc = 0 ; 1 ; pc++ )
  46.     switch( line[pc] ) {
  47.       case 100: uv_i = 1 ; break ;
  48.       case 110: GOSUB(200) ; break ;
  49.       case 120: uv_i++ ; pc = 1 ; break ;
  50.       case 130: exit(0) ;
  51.       case 200: printf( "I= %d I^2 = %d\n", uv_i, uv_i*uv_i ) ; break ;
  52.       case 210: RETURN ;
  53.       default : printf( "SYSTEM ERROR\n" ) ; exit(1) ;
  54.       }
  55.  
  56.   }
  57.  
  58. GOSUB() would be a function that would push the current value of "pc" onto
  59. a stack, look up the appropriate label in the line[] array, and make that
  60. index the new value of "pc".  RETURN would then be a macro that pops the
  61. top of the "pc" stack into pc.
  62.  
  63. Of course, if maintainability is a concern, you would probably make some
  64. changes to make the program look more like BASIC (perhaps defining
  65. macros for each BASIC statement...).  One obvious problem with this is
  66. that you're going to get one huge case statement inside one huge routine.
  67. Such a method is very likely to run into compiler limitations very quickly.
  68.  
  69. Perhaps a more "modern" version of BASIC (perhaps with some helpful
  70. constraints) could be translated in a more one-to-one manner.  Ultimately,
  71. I think you'll find that one of my original suggestions is the most practical,
  72. though...
  73.  
  74. -- 
  75. [An intertialess drive] is not probable at all, at least in any extrapolation
  76. of present-day science.  But as far as I can determine, it cannot be proved
  77. absolutely impossible, and that is enough for me.        -E. E. "Doc" Smith
  78.