home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / program / a / c_interp / Manual
Encoding:
Text File  |  1994-03-15  |  17.9 KB  |  462 lines

  1. C Interpreter © The Serial Port 1993
  2. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  3.  
  4. By Nigel Brown & Bob Voisey
  5. Version 0.90g
  6.  
  7. Overview
  8. --------
  9.  
  10. !Interpret implements a subset of the C programming language.  Unlike
  11. traditional C compilers, however, this program interprets the source
  12. programs without undergoing an intermediate compilation stage.
  13.  
  14. Initially developed for use as a script language within our software
  15. products (Arcterm, ArcBBS & Sourcerer), !Interpret has now been
  16. released as a stand alone interpreter in the hope that somebody might
  17. find it useful.
  18.  
  19. Interpreters have some advantages over compilers:..
  20.  
  21.   - Easy to use; no need to worry about complex cli switches
  22.   - Fast debugging; the lengthy compilation step is eliminated
  23.   - Compact on disk; no intermediate object files are created
  24.  
  25. ..they also have some disadvantages:
  26.  
  27.   - Slow execution; interpreters are much slower than compiled code
  28.   - Poor management; it can be harder to keep your sources organised
  29.  
  30. Because the C programming language was designed for use with a
  31. compiler, the syntax does not lend itself to interpretation so readily
  32. as that of languages such as BASIC.  As a result interpreted C does
  33. not generally run as quickly as, for example, BBC Basic.  Despite
  34. this many programmers may prefer to code in the elegant syntax of C.
  35.  
  36.  
  37. About this manual
  38. -----------------
  39.  
  40. This document is not a C tutorial guide.  Rather, its purpose is to
  41. define the subset of C implemented by the interpreter, and to outline
  42. the differences between that and regular C.  If you have not encountered
  43. the C programming language before, !Interpret might be a good program
  44. with which to start - however you WILL need a programming guide to
  45. fully understand some of the facilities.
  46.  
  47. Information within this manual is given in good faith, however The Serial
  48. Port cannot guarantee it's accuracy, nor the suitability of !Interpret
  49. for any particular task.
  50.  
  51.  
  52. The Examples directory
  53. ----------------------
  54.  
  55. Along with !Interpret you will find an Examples directory.  Contained
  56. within are a number of sources specifically designed to demonstrate
  57. the facilities provided by the interpreter.  First time C programmers
  58. may find these files useful, since theoretical C and practical C
  59. sources are sometimes at variance.
  60.  
  61.  
  62. Restrictions of !Interpret
  63. --------------------------
  64.  
  65. They say you never get something for nothing.  In this case you _do_
  66. get quite a lot of C, but certainly not an entire implementation.
  67. Specifically, !Interpret does not support:
  68.  
  69.   - Structures (ie struct {} types)
  70.   - Typedefs (user definable types)
  71.   - Enumerated variables (ie enum {} types)
  72.   - Floating point (ie long, double etc)
  73.   - Miscellaneous constructs (notably do and switch)
  74.   - Multiple source files (including #include directives)
  75.  
  76. Furthermore, !Interpret handles some C syntax in a slightly different
  77. way to regular C.  These differences will be discussed in the appropriate
  78. sections of this manual.
  79.  
  80. Although the above limitations are rather depressing news for experienced
  81. C coders, the greater part of the language definition is implemented and
  82. expressions are evaluated in just the same way as in regular C (with a
  83. recursive descent parser).
  84.  
  85.  
  86. Running !Interpret
  87. ------------------
  88.  
  89. There are two ways to run !Interpret:
  90.  
  91.  1.  Double click the application, whereupon you will be prompted to
  92.      enter the name of the source file to be interpreted.  If the file
  93.      is not in the current directory you will need to specify a full
  94.      file path.  To assist you, an Obey file called !Here is included
  95.      in the Examples directory to change the current directory to
  96.      that location.
  97.  
  98.  2.  Run !Interpret by hand, from the cli or from an Obey file.
  99.      Syntax: !Interpret <Source filename>
  100.          Eg: !Interpret Examples.MySource
  101.  
  102. Once a source file has been loaded, execution will commence immediately.
  103. To abort execution, hit the Escape key.  If an error occurs, execution
  104. will terminate and the error will be reported.  !Interpret has no
  105. equivalent of the BBC Basic interactive command mode.
  106.  
  107.  
  108. Comments
  109. --------
  110.  
  111. Blocks of source can be marked as comments, in which case the interpreter
  112. completely ignores them.  These blocks can be embedded anywhere within
  113. the source.  The delimiters are @* <comment> *@.
  114.  
  115.  @* This is a comment *@
  116.  my_func(int i @* This comment won't be parsed *@)
  117.  {
  118.  }
  119.  
  120. Experienced C programmers will probably have noticed that these are not
  121. the traditional comment delimiters.  Unfortunately this change was
  122. necessary to avoid a more complex bug within the interpreter.  It is
  123. anticipated that this problem will be fixed for the next release so
  124. that we can return to the much loved /* ... */ syntax.
  125.  
  126.  
  127. Functions
  128. ---------
  129.  
  130. Each source file must have at least one function - the approximate
  131. equivalent of BBC Basic procedures and functions.  A function is
  132. declared by specifying it's name and a parameter list.
  133.  
  134.  run_print(char *name, int record)
  135.  {
  136.  }
  137.  
  138. This syntax differs from regular C in the following ways:
  139.  
  140.    i You must not declare the function type
  141.   ii Functions need not be predeclared
  142.  
  143. Item (i) comes about because an interpreted language need not know
  144. in advance the type of data to be passed between functions.  If you
  145. attempt to declare the function type the interpreter will assume
  146. that you're declaring a variable, and will not run your code.
  147.  
  148. Item (ii) is brought about for similar reasons, the consequence being
  149. that you can forward-reference functions without having to predeclare
  150. prototypes.
  151.  
  152. Values are returned by functions in the same was as with regular C.
  153.  
  154.  run_print(char *name, int record)
  155.  {
  156.  record++;
  157.  return record;
  158.  }
  159.  
  160.  
  161. Global & Local Variables
  162. ------------------------
  163.  
  164. Variables are declared in the same way as with regular C, either near
  165. the top of the source (before any functions) for globals or at the top
  166. of a function (before any expressions) for locals.  The following
  167. types of variable are supported:
  168.  
  169.   - int     32-bit signed integer
  170.   - int[n]  Array of int, with n elements
  171.   - int *   Pointer to int
  172.   - char    8-bit signed byte (ASCII conversion supported with 'x')
  173.   - char[n] Array of char, with n elements (commonly used for strings)
  174.   - char *  Pointer to char
  175.  
  176. Pointers to variables are processed in the same way as with regular C.
  177. To indirect an expression, the * operator is used:
  178.  
  179.  char string[32];
  180.  char c;
  181.  
  182.  *string = 0;     @* Sets first byte of the char array to NULL *@
  183.  
  184.  c = *(string+2); @* These are *@
  185.  c = string[2];   @* analogous *@
  186.  
  187. To specify the address of an expression, the & operator is used:
  188.  
  189.  int i;
  190.  
  191.  scanf("%d",&i);  @* Store the value at the int pointed to by &i *@
  192.  
  193. !Interpret is not as fussy as regular C with regard type casting.  In
  194. general the int type can be used to represent any 32 bit quantity and
  195. the char type, any 8 bit quantity.  This is particularly true in the
  196. context of library function calls, as explained later.
  197.  
  198.  
  199. Expressions
  200. -----------
  201.  
  202. !Interpret parses expressions in the same was as C.  As with compiled C,
  203. function elements within an expression may be evaluated in any order
  204. and may be nested to any practical depth.  The following numerical
  205. operators are valid within an expression:
  206.  
  207.   ++   Auto increment (post or pre indexed)
  208.   --   Auto decrement (post or pre indexed)
  209.    +   Add
  210.    -   Sub
  211.    *   Multiply
  212.    /   Divide
  213.    %   Modulo
  214.  
  215. The following logical operators are valid within an expression:
  216.  
  217.   ==   Equality
  218.   !=   Non equality
  219.    <   Less than
  220.    >   Greater than
  221.   >=   Greater than or equal to
  222.   <=   Less than or equal to
  223.   &&   Logical AND
  224.   ||   Logical OR
  225.    !   Logical NOT
  226.  
  227. Expressions can be enclosed within parenthesis to define the order of
  228. calculation.  For example, the following expression is valid:
  229.  
  230.  printf("The result is: %d",(i=get_int(++num_to_get%2)));
  231.  
  232.  
  233. Construct Syntax
  234. ----------------
  235.  
  236. !Interpret implements the following regular C constructs:
  237.  
  238.   - if() {} else {}
  239.   - for( ; ; ) {}
  240.   - while() {}
  241.  
  242. The if() construct has the following format:
  243.  
  244.  if(condition)
  245.    {
  246.    ...
  247.    }
  248.  else
  249.    {
  250.    ...
  251.    }
  252.  
  253.  If condition is true the first program block will execute, otherwise the
  254.  second block will run.
  255.  
  256. The for(;;) construct has the following format:
  257.  
  258.  for(init_command; condition; loop_command)
  259.    {
  260.    ...
  261.    }
  262.  
  263.  The init_command is executed once, then loop_command and the program
  264.  block are executed repeatedly until condition is true.
  265.  
  266. The while() construct has the following format:
  267.  
  268.  while(condition)
  269.    {
  270.    ...
  271.    }
  272.  
  273.  The program block will execute repeatedly until condition becomes false.
  274.  
  275. If the break command is executed within any of the program blocks for
  276. the above constructs, execution will jump past the construct to the
  277. next piece of code.  For example:
  278.  
  279.  while(!quit)
  280.    {
  281.    puts("Run until quit..");
  282.    if(escape_pressed())
  283.      break;
  284.    }
  285.  puts("..or until escape is pressed!");
  286.  
  287. If the return command is executed within any of the program blocks for
  288. the above constructs, any nested structures will be unwound correctly
  289. and execution will return to the calling function.  For example:
  290.  
  291.  main()
  292.  {
  293.  wait_for_key();
  294.  }
  295.  
  296.  wait_for_key()
  297.  {
  298.  while(!time_out())
  299.    {
  300.    if(key_pressed())
  301.      return;
  302.    }
  303.  }
  304.  
  305.  
  306. Library Functions
  307. -----------------
  308.  
  309. A number of useful functions are built into !Interpret.  You can call
  310. these like any other function and, where valid, they will return values
  311. like any other function.  Where necessary, the interpreter performs
  312. type casting on your behalf.
  313.  
  314. It is beyond the scope of this manual to fully document the library,
  315. however a brief summary of each function is provided below.
  316.  
  317. Function                                               Returns
  318. --------------------------------------------------------------
  319. bbc_vdu(int character)                                 none
  320. bbc_vduw(int word)                                     none
  321. bbc_stringprint(char *string_to_print)                 none
  322. bbc_cls()                                              none
  323. bbc_colour(int text_colour)                            none
  324. bbc_tab(int x, int y)                                  none
  325. bbc_plot(int plotcode, int x, int y)                   none
  326. bbc_mode(int mode)                                     none
  327. bbc_move(int x, int y)                                 none
  328. bbc_moveby(int x, int y)                               none
  329. bbc_draw(int x, int y)                                 none
  330. bbc_drawby(int x, int y)                               none
  331. bbc_rectangle(int x, int y, int h, int w)              none
  332. bbc_rectanglefill(int x, int y, int h, int w)          none
  333. bbc_circle(int x, int y, int r)                        none
  334. bbc_circlefill(int x, int y, int r)                    none
  335. bbc_origin(int x, int y)                               none
  336. bbc_gwindow(int x0, y0, x1, y1)                        none
  337. bbc_clg()                                              none
  338. bbc_fill(int x, int y)                                 none
  339. bbc_gcol(int plottype, int col)                        none
  340. bbc_tint(int col, int tint)                            none
  341. bbc_palette(int log, int phys, int r, int g, int b)    none
  342. bbc_point(int x, int y)                                int colour
  343. bbc_vduvars(int *in, int *out)                         none
  344. bbc_modevar(int mode, int varno)                       int modevar
  345. bbc_get()                                              int key
  346. bbc_cursor(int curs_type)                              none
  347. bbc_adval(int buffnum)                                 int val
  348. bbc_getbeat()                                          int beat_val
  349. bbc_getbeats()                                         int cycl_len
  350. bbc_gettempo()                                         int beat_rate
  351. bbc_inkey(int time_num)                                int key
  352. bbc_setbeats(int cycl_len)                             none
  353. bbc_settempo(int beat_rate)                            none
  354. bbc_sound(int chan, int amp, int pitch, int dur)       none
  355. bbc_soundoff()                                         none
  356. bbc_soundon()                                          none
  357. bbc_stereo(int chan, int pos)                          none
  358. bbc_voices(int num_chan)                               none
  359.  
  360. isalnum(char character)                                int true_or_false
  361. isalpha(char character)                                int true_or_false
  362. iscntrl(char character)                                int true_or_false
  363. isdigit(char character)                                int true_or_false
  364. isgraph(char character)                                int true_or_false
  365. islower(char character)                                int true_or_false
  366. isprint(char character)                                int true_or_false
  367. ispunct(char character)                                int true_or_false
  368. isspace(char character)                                int true_or_false
  369. isupper(char character)                                int true_or_false
  370. isxdigit(char character)                               int true_or_false
  371. tolower(char character)                                char character
  372. toupper(char character)                                char character
  373.  
  374. remove(char *fname)                                    int success_flag
  375. rename(char *old, char *new)                           int success_flag
  376. tmpfile()                                              int file_handle
  377. tmpname(char *result)                                  char fname
  378. fclose(int file_handle)                                int success_flag
  379. fflush(int file_handle)                                int EOF_flag
  380. fopen(char *fname, char *mode)                         int file_handle
  381. freopen(char *fname, char *mode, int file_handle)      int file_handle
  382. setbuf(int file_handle, char *buf)                     none
  383. setvbuf(int file_handle,char *buf, int mode,int size)  int success_flag
  384. fprintf(int file_handle, char *format, ...)            int num_chars
  385. printf(char *format, ...)                              int num_chars
  386. sprintf(char *string, char *format, ...)               int num_chars
  387. fscanf(int file_handle, char *format, ...)             int num_items
  388. scanf(char *format, ...)                               int num_items
  389. sscanf(char *string, char *format, ...)                int num_items
  390. fgetc(int file_handle)                                 char character
  391. fgets(char *buf, int buf_size, int file_handle)        char *string
  392. fputc(char character, int file_handle)                 int EOF_flag
  393. fputs(char *string, int file_handle)                   int EOF_flag
  394. getc(int file_handle)                                  char character
  395. getchar()                                              char character
  396. gets(char *buf)                                        char *string
  397. putc(char character, int file_handle)                  int EOF_flag
  398. putchar(char character)                                int EOF_flag
  399. puts(char *string)                                     int EOF_flag
  400. ungetc(char character, int file_handle)                int EOF_flag
  401. fread(int *ptr, int size, int num, int file_handle)    int num_read
  402. fwrite(int *ptr, int size, int num, int file_handle)   int num_write
  403. fgetpos(int file_handle, int *pos)                     int success_flag
  404. fseek(int file_handle, int offset, int whence)         int success_flag
  405. fsetpos(int file_handle, int *pos)                     int success_flag
  406. ftell(int file_handle)                                 int file_pos
  407. rewind(int file_handle)                                none
  408. clearerr(int file_handle)                              none
  409. feof(int file_handle)                                  int EOF_flag
  410. ferror(int file_handle)                                int err_flag;
  411. perror(char *string)                                   none
  412.  
  413. atoi(char *string)                                     int int_value
  414. atol(char *string)                                     int int_value
  415. rand()                                                 int rand_value
  416. srand(int seed)                                        none
  417. malloc(int size)                                       int *ptr
  418. calloc(int num, int size)                              int *ptr
  419. realloc(int *ptr, int size)                            int *ptr
  420. free(int *ptr)                                         none
  421. system(char *command)                                  int success_value
  422. getenv(char *name)                                     char *result
  423. abs(int num)                                           int result
  424. clock()                                                int time
  425.  
  426. memset(int *ptr, char character, int size)             none
  427. memchr(char *string, char character, int size)         int *ptr
  428. memcmp(int *ptr1, int *ptr2, int size)                 int same_flag
  429. memmove(int *ptr1, int *ptr2, int size)                int *ptr1
  430. memcpy(int *ptr1, int *ptr2, int size)                 int *ptr1
  431. strtok(char *string1, char *string2)                   char *result
  432. strstr(char *string1, char *string2)                   char *result
  433. strspn(char *string1, char *string2)                   int size
  434. strchr(char *string, char character)                   char *result
  435. strpbrk(char *string1, char *string2)                  char *result
  436. strcspn(char *string1, char *string2)                  int size
  437. strrchr(char *string, char character)                  char *result
  438. strcfrm(char *string1, char *string2, int num)         int size
  439. strcoll(char *string1, char *string2)                  int result
  440. strncmp(char *string1, char *string2, int num)         int result
  441. strcmp(char *string1, char *string2)                   int result
  442. strncat(char *string1, char *string2, int num)         char *result
  443. strcat(char *string1, char *string2)                   char *result
  444. strncpy(char *string1, char *string2, int num)         char *result
  445. strcpy(char *string1, char *string2)                   char *result
  446.  
  447. os_swi(int *regs[])                                    none
  448.  
  449. For detailed descriptions of these functions refer to the BBC Basic
  450. Guide and any basic C tutorial guide.
  451.  
  452.  
  453. Technical Support
  454. -----------------
  455.  
  456. The Serial Port regret that they cannot provide technical support for
  457. this freeware program.  The authors will be pleased to receive any
  458. bug reports or comments via email, however, at the following address:
  459.  
  460.     bob@cryton.demon.co.uk
  461. or  Bob Voisey (#9) on The World of Cryton BBS (2:252/102)
  462.