home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / DevTools / COWS / Code / COWSInterpreter.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-20  |  8.0 KB  |  252 lines

  1. /*
  2.     Copyright (C) 1994 Sean Luke
  3.  
  4.     COWSInterpreter.h
  5.     Version 1.0
  6.     Sean Luke
  7.     
  8. */
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15. #import <objc/Object.h>
  16. #import <appkit/appkit.h>        // for timed entry
  17. #import <objc/HashTable.h>
  18. #import <string.h>
  19. #import <stdio.h>
  20. #import <stdlib.h>
  21. #import "COWSLibrary.h"
  22. #import "COWSStack.h"
  23. #import "COWSArgumentList.h"
  24. #import "COWSNode.h"
  25. #import "COWSStringNode.h"
  26. #import "COWSSymbolNode.h"
  27. #import "COWSLibraryFunctionNode.h"
  28. #import "COWSStateNode.h"
  29. #import "COWSProtocols.h"
  30.  
  31.  
  32.  
  33. // Types of interpretation
  34. #define COWSINTERPRETATIONUNDEFINED    0
  35. #define COWSINTERPRETATIONLIBRARY    1    // a library function was requested
  36. #define COWSINTERPRETATIONFUNCTION    2    // a user's program was requested
  37.  
  38. // Limits
  39. #define COWSLARGENUMBER 256        // the longest valid string length for a number
  40.                                 // I define this because sprintf requires an
  41.                                 // array, and not just a character pointer.
  42.                                 // I wish there was another way around this,
  43.                                 // but I don't think so...
  44.  
  45. // Success Marker
  46. #define COWSSUCCESS                     0        // >=0 also means parse succeeded
  47.  
  48. // Interpreter Bugs
  49. #define COWSINTERNALERROR            -1        // bug in COWS
  50.  
  51. // Initial Parse Errors
  52. #define COWSERRORNOMORETOKENS        -2        // not enough tokens to parse
  53. #define COWSERRORSYNTAX                -3        // syntax error
  54. #define COWSERRORNOCLOSINGQUOTE        -4        // no closing quote to a string
  55. #define COWSERRORDUPLICATEGLOBAL    -5        // duplicate global variable
  56. #define COWSERRORDUPLICATEVARIABLE    -6        // duplicate variable or argument
  57. #define COWSERRORDUPLICATEARGUMENT    -7        // duplicate argument
  58. #define COWSERRORDUPLICATEFUNCTION    -18        // duplicate function
  59.  
  60. // Interpreter Syntax Errors
  61. #define COWSERRORNOSUCHFUNCTION        -8        // can't find function by that name
  62. #define COWSERRORNOTENOUGHARGUMENTS    -9        // too few arguments to function
  63. #define COWSERRORTOOMANYARGUMENTS    -10        // too many aguments to function
  64. #define COWSLIBRARYFUNCTIONERROR    -11        // library function error
  65. #define COWSERRORNOSUCHVARIABLE        -12        // can't find variable by that name
  66. //      COWSERRORNOMORETOKENS                // not enough tokens to parse
  67. #define COWSERRORNOFUNCTIONNAME        -13        // no function/keyword name after (
  68.  
  69. // Keyword Syntax Errors
  70. #define COWSERRORSETNOTVARIABLE        -14        // can't set; not a variable
  71. #define COWSERRORSETTOOMANYVALUES    -15        // can't set; too many values
  72. #define COWSERRORSETNOVALUE            -16        // can't set; no value
  73. #define COWSERRORSETNOSUCHVARIABLE    -17        // can't set; no such variable
  74. #define COWSERRORIFNOTHENCLAUSE        -19        // can't if; no then clause
  75. #define COWSERRORIFTOOMANYVALUES    -20        // can't if; too many values
  76. #define COWSERRORIFNOTENOUGHVALUES    -21        // can't if; not enough values
  77. #define COWSERRORWHILETOOMANYVALUES -22        // can't while; too many values
  78. #define COWSERRORWHILENOTENOUGHVALUES -23    // can't while; not enough values
  79. #define COWSERRORFORNOTAVARIABLE    -24        // can't for; not a variable
  80. #define COWSERRORFORSTARTNOTNUMBER    -25        // can't for; start value not a #
  81. #define COWSERRORFORSTOPNOTNUMBER    -26        // can't for; stop value not a #
  82. #define COWSERRORFORSTEPNOTNUMBER    -27        // can't for; step value not a #
  83. #define COWSERRORFORNOSUCHVARIABLE    -28        // can't for; no such variable
  84. #define COWSERRORFORTOOMANYVALUES    -29        // can't for; too many values
  85. #define COWSERRORFORNOTENOUGHVALUES    -30        // can't for; not enough values
  86.  
  87. // convenience functions
  88.  
  89. char* newstr(const char* ct);
  90.     // strcpys ct into a new string, returning the new string's pointer.
  91.  
  92. char* newstrn(const char*ct, int size);
  93.     // strncpys ct into a new string, returning the new string's pointer.
  94.     
  95.     
  96.  
  97. @interface COWSInterpreter:Object   <InterpreterControl, LibraryControl>
  98. {
  99.     COWSStringNode*    
  100.                 program;
  101.     BOOL        running;            
  102.     BOOL        function_completed;
  103.     BOOL        foreground;                // default is NO
  104.     BOOL        locked;                    // cannot be used by another process
  105.     BOOL        working;                // working on a task, paused or no.
  106.     int            repeats;
  107.     id            delegate;
  108.     DPSTimedEntry
  109.                 teNum;
  110.     float        te_speed;                // default 0.1
  111.     COWSStack*    stack;
  112.     HashTable*     function_dictionary;
  113.     HashTable*    library_dictionary;
  114.     HashTable*    global_dictionary;
  115.     HashTable*    current_dictionary;
  116.     COWSStringNode*
  117.                 current_function;
  118.     int            current_position;
  119.     id            pausing_function;
  120.     int            interpretation;            // when a user starts a request,
  121.                                         // this is set to whether it's a macro
  122.                                         // request or just a program function
  123.                                         // request...
  124. }
  125.  
  126. - init;
  127. - free;
  128. - addLibrary:this_library;                         // returns NULL if unable
  129. - addLibraryFunction:(const char*) this_name
  130.     selector: (SEL) this_selector
  131.     target: this_target;
  132. - clearLibraryFunctions;
  133. - setTimedEntrySpeed:(float)this_speed;                // in seconds
  134. - (float) timedEntrySpeed;
  135. - setForeground:(BOOL) yes_or_no;
  136. - (BOOL) foreground;
  137. - setLocked:(BOOL) yes_or_no;
  138. - (BOOL) locked;
  139. - (BOOL) working;                            // you can't change this
  140. - (BOOL) running;                            // you can't change this
  141. - (int) setProgram:(const char*) this_string;    
  142.     // parses through this_string and places pieces in various
  143.     // dictionaries.  Returns an error code.
  144. - printDictionaries;
  145. - printProgram;
  146. - setDelegate:this_delegate;
  147. - delegate;
  148. - setRepeats:(int) this_number;
  149. - (int) repeats;
  150. - interpretFunction:(const char*) this_name 
  151.     arguments:(COWSArgumentList*)these_arguments;
  152.         // interprets function this_name with arguments these_arguments
  153.         // it is your responbility to free these_arguments after calling
  154.         // this function.
  155.         // Returns the interpreter.  Don't free the return value!!!  :-)
  156. - stopInterpreting;
  157. - pauseInterpreting:calling_function;
  158. - resumeInterpretingWithValue:(COWSStringNode*)this_value;
  159.  
  160.  
  161. // Delegate Methods.  Just Declared here...it's what's sent to you!
  162.  
  163. - finishedInterpreting:(const char*)returnValue:(int)thisMessage:sender;
  164. - errorInterpreting:(int) thisError:(const char*)thisFunction:
  165.     (int)thisPosition:(const char*)thisString:sender;
  166.  
  167.  
  168.  
  169.  
  170. // Tokenizer.  Don't touch this.
  171.  
  172. - (int) _tokenize:(const char*) string:(int) pos:(COWSStringNode*) token;
  173.  
  174.  
  175.  
  176. // Recursive descent parser functions.
  177. // Don't touch these.
  178.  
  179. - (int) _item:(const char*)string:(int)pos;
  180. - (int) _itemList:(const char*)string:(int)pos;
  181. - (int) _variableList:(const char*) string:(int)pos;
  182. - (int) _globalForm:(const char*)string:(int)pos;
  183. - (int) _argumentList:(const char*) string:(int)pos:
  184.         (COWSArgumentList*) arguments;
  185. - (int) _localVariableList:(const char*) string:(int)pos: 
  186.         (HashTable*) arguments;
  187. - (int) _functionForm:(const char*)string:(int)pos;
  188. - (int) _programList:(const char*)string:(int)pos;
  189. - (int) _program:(const char*)string:(int)pos;
  190.  
  191.  
  192. // Recursive descent parser terminal functions.
  193. // Don't touch these.
  194.  
  195. - (int) _symbolName:(const char*)string:(COWSStringNode*)s:(int)pos;
  196. - (int) _atom:(const char*)string:(int)pos;
  197. - (int) _beginKeyword:(const char*)string:(int)pos;
  198. - (int) _functionKeyword:(const char*)string:(int)pos;
  199. - (int) _variableKeyword:(const char*)string:(int)pos;
  200. - (int) _closeParen:(const char*)string:(int)pos;
  201. - (int) _openParen:(const char*)string:(int)pos;
  202.  
  203.  
  204.  
  205. // Interpreter functions
  206. // Don't touch these.
  207.  
  208. - _executeProgram:(COWSArgumentList*) arguments:(COWSStringNode*) symbol;
  209. - _performFunction;
  210. - _performInternalFunction:
  211.         (COWSArgumentList*) arguments:(COWSStringNode*) symbol;
  212. - _performLibraryFunction:
  213.         (COWSArgumentList*) arguments:(COWSStringNode*) symbol;
  214. - _completeFunction;
  215. - _evaluateVariable:(COWSStringNode*) symbol;
  216. - _doKeywords;
  217. - _go;                // called by the timed entry 
  218. - _readEval;        // called by _go <repeats> number of times
  219.  
  220.  
  221. // Interpreter Keyword functions
  222. // Don't touch these.
  223.  
  224. - _startSet;
  225. - _doSet;
  226. - _finishSet;
  227. - _startIf;
  228. - _doIf;
  229. - _finishIf;
  230. - _startWhile;
  231. - _doWhile;
  232. - _finishWhile;
  233. - _startFor;
  234. - _doFor;
  235. - _finishFor;
  236.  
  237.  
  238. // Interpreter assistance functions
  239. // Don't touch these.
  240.  
  241. - (int) _skip;        // skips one value if there is one.  Returns error if not.
  242. - _makeTruth:(COWSStringNode*) string;        // makes a truth a node string
  243. - _makeNumber:(COWSStringNode*) string;        // makes a number a proper number.
  244.                                             // bad numbers may wind up
  245.                                             // different than expected or 0!
  246. - _strip:(COWSStringNode*) string;            // strips strings of their "s
  247. - (int) _error:(int) this_error:
  248.     (const char*) this_function:
  249.     (int) this_position: 
  250.     (const char*) this_string;                // reports an error.
  251.  
  252. @end