home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / guile-ii.src / guile-ii / guile-src / ctax / ctax.doc < prev    next >
Encoding:
Text File  |  1995-08-14  |  8.6 KB  |  388 lines

  1.  
  2. Ctax programs are a series of commands, each a definition or a statement.
  3.  
  4.     command -> definition
  5.     command -> statement
  6.  
  7. For example:
  8.  
  9.     definitions:
  10.  
  11.         scm x;
  12.  
  13.         scm
  14.         avg (a, b)
  15.         {
  16.           return (a + b) / 2;
  17.         }
  18.  
  19.  
  20.     statements:
  21.  
  22.         1 + 2;
  23.         avg (4, 9);
  24.  
  25.  
  26.  
  27. The interpreter reads commands interactively, evaluates them, and 
  28. prints the result.  For example:
  29.  
  30.     ctax> scm avg (a, b) { return (a + b) / 2; }
  31.     avg
  32.     ctax> avg (0, 10);
  33.     5
  34.  
  35. When loading a source file, ctax reads commands one at a time and evaluates 
  36. them as read, but does not print the result.
  37.  
  38.  
  39.  
  40. Definition commands are used to create variables and perhaps define
  41. a value for them.
  42.  
  43.  
  44.    1.    definition -> 'scm' symbol_list ';'
  45.    2.    definition -> 'scm' <id> '=' initializer ';'
  46.    3.    definition -> 'scm' <id> '(' parameter_list ')' opt_doc opt_interaction body
  47.    4.    definition -> 'struct' <id> opt_superclass '{' field_list '}' ';'
  48.     symbol_list -> <id>
  49.     symbol_list -> symbol_list ',' <id>
  50.  
  51.  
  52. Examples and explanations:
  53.  
  54.     1.  scm a, b, c;
  55.         Declares new variables, initially set to 0.
  56.         Note that "scm" is the "univeral type" -- all
  57.          values can be stored in a variable of type "scm".
  58.  
  59.         At the moment, procedures, parameters and variables
  60.          can *only* be of type "scm", though in the future this
  61.         restriction will be relaxed and optional type-checking
  62.         provided.
  63.  
  64.     2.  scm a = 1 + x;
  65.         Declare and initialize a single variable.
  66.  
  67.  
  68.     3.  scm
  69.         avg (a, b)
  70.         {
  71.            scm t;
  72.  
  73.            t = a + b;
  74.            return t / 2;
  75.         }
  76.  
  77.         Declare a new procedure.  Note that the body of the procedure
  78.            has a section of local declarations followed by a section of
  79.         statements.
  80.  
  81.     4.  struct user_record
  82.         {
  83.           scm name;
  84.           scm uid;
  85.           scm homedir;
  86.           scm passwd;
  87.           scm shell;
  88.         };
  89.  
  90.         Declare a new structure type.  Note that currently all fields must be
  91.         of type 'scm', the universal type, but this restriction will be 
  92.         relaxed in the future.
  93.  
  94.  
  95. Definitions such as the above can occur either globally, at the top
  96. level, or inside of a block, before any statements.
  97.  
  98.  
  99.  
  100. Here are structure declarations in detail.
  101.  
  102.     definition -> 'struct' <id> opt_superclass '{' field_list '}' ';'
  103.     opt_superclass -> ':' <id>
  104.     opt_superclass ->        /* empty */
  105.     field_list -> <id> ';'
  106.     field_list -> 'scm' <id> ';'
  107.     field_list -> field_list <id> ';'
  108.     field_list -> field_list 'scm' <id> ';'
  109.     field_list ->        /* empty */
  110.  
  111.  
  112. Examples:
  113.  
  114.     struct point
  115.     {
  116.       x;
  117.       y;
  118.     };
  119.  
  120.     struct point_3 : point
  121.     { 
  122.       z;
  123.     };
  124.  
  125.     struct verbose_point
  126.     {
  127.        scm x;
  128.        scm y;
  129.        scm z;
  130.     };
  131.  
  132. Note that, at present, unlike C, you can not delcare variables and define
  133. structure types in the same command.  There is no syntax for fields which
  134. are arrays or sub-structures.  There aren't unions.  All of these restrictions
  135. are likely to be relaxed in the future.
  136.  
  137. The fundamental operation on structures is field access:
  138.  
  139.     some_point->x;
  140.  
  141. Field access is polymorphic -- it works on any structure with a field of
  142. the right name.  Thus with:
  143.  
  144.     struct big
  145.     {
  146.        scm x;
  147.        scm y;
  148.     };
  149.  
  150.     struct little
  151.     {
  152.        scm y;
  153.        scm x;
  154.     };
  155.  
  156.     scm
  157.     get_x (a_struct)
  158.     {
  159.         return a_struct->x;
  160.     }
  161.  
  162. get_x can be called on structs of both type "struct big" and type
  163. "struct little".
  164.  
  165. In the current implementation, when get_x is called, it caches the location
  166. of field x.  So, if get_x is called again on the same structure type (or
  167. on any structure type with "x" in the same position), field access is pretty
  168. fast.  If get_x is called on a new type, there is a short linear search to find
  169. the field.
  170.  
  171. In the future, ctax will permit programmers to declare variable to be
  172. of specific types.  That will permit both optional static type
  173. checking and very fast structure access.
  174.  
  175.  
  176.  
  177. Here are procedure definitions in detail.
  178.  
  179.     definition -> 'scm' <id> '(' parameter_list ')' body
  180.     parameter_list -> <id>
  181.     parameter_list -> 'scm' <id>
  182.     parameter_list -> parameter_list ',' <id>
  183.     parameter_list -> parameter_list ',' 'scm' <id>
  184.     parameter_list ->        /* empty */
  185.     body -> '{' local_definition_list statement_list '}'
  186.     local_definition_list -> definition
  187.     local_definition_list -> local_definition_list definition
  188.     local_definition_list ->        /* empty */
  189.     statement_list -> statement
  190.     statement_list -> statement_list statement
  191.  
  192. Several examples have already been shown, for example:
  193.  
  194.     scm
  195.     avg (scm a, scm b)
  196.     {
  197.       scm c;
  198.  
  199.       c = a + b;
  200.         return c / 2;
  201.     }
  202.  
  203. It is mandatory to declare a return type, and currenly the only
  204. return type permitted is the universal type, "scm".
  205. It is optional to declare types of parameters, currently the
  206. only type permitted is "scm".
  207.  
  208. Local declarations must preceed statements and have the same syntax
  209. as top-level declarations.
  210.  
  211. Variable scoping is lexical.  That is, a variable is looked for first
  212. in the innermost local declarations, then moving out through local
  213. scopes through parameters and finally global variables.
  214.  
  215.  
  216.  
  217. Statements in detail:
  218.  
  219.     statement -> expression ';'
  220.     statement -> 'if' '(' expression ')' statement opt_else
  221.     statement -> 'while' '(' expression ')' statement
  222.     statement -> 'for' '(' exp1 ';' exp1 ';' exp1 ')' statement
  223.     statement -> 'do' statement 'while' '(' expression ')' ';'
  224.     statement -> '{' local_definition_list statement_list '}'
  225.     statement -> 'return' opt_expression ';'
  226.     statement -> 'break' ';'
  227.     statement -> 'continue' ';'
  228.     opt_expression -> expression
  229.     opt_expression ->        /* empty */
  230.     opt_else -> 'else' statement
  231.     opt_else ->        /* empty */
  232.  
  233. These are all just what you would expect from C, except that the
  234. expression syntax has some differences.
  235.  
  236.  
  237. Expressions are similar to C, but with some ommissions and extensions.
  238.  
  239. Expression syntax that overlaps with C:
  240.  
  241.     expression -> exp1
  242.     exp1 -> exp
  243.     exp1 -> exp1 ',' exp
  244.     exp -> '-' exp
  245.     exp -> '!' exp
  246.     exp -> '~' exp
  247.     exp -> '(' exp1 ')'
  248.     exp -> exp '(' arg_list ')'
  249.     opt_params -> '(' arg_list ')'
  250.     opt_params ->        /* empty */
  251.     exp -> exp '->' <id>
  252.     exp -> exp '[' exp ']'
  253.     exp -> exp '=' exp
  254.     exp -> exp '*' exp
  255.     exp -> exp '/' exp
  256.     exp -> exp '%' exp
  257.     exp -> exp '+' exp
  258.     exp -> exp '-' exp
  259.     exp -> exp '<<' exp
  260.     exp -> exp '>>' exp
  261.     exp -> exp '>>?' exp
  262.     exp -> exp '==' exp
  263.     exp -> exp '!=' exp
  264.     exp -> exp '<=' exp
  265.     exp -> exp '>=' exp
  266.     exp -> exp '<' exp
  267.     exp -> exp '>' exp
  268.     exp -> exp '&' exp
  269.     exp -> exp '^' exp
  270.     exp -> exp '|' exp
  271.     exp -> exp '&&' exp
  272.     exp -> exp '||' exp
  273.     exp -> exp '?' exp ':' exp
  274.     exp -> <number>
  275.     exp -> <char>
  276.     exp -> <string>
  277.     exp -> <id>
  278.     arg_list -> exp
  279.     arg_list -> arg_list ',' exp
  280.     arg_list ->        /* empty */
  281.     
  282.  
  283. Some operators come with a corresponding generic form.  This version
  284. of the operator can be extended by your program to apply to any type
  285. in arbitrary ways.  (On the other hand, there is a performance penalty
  286. for using these hyper-general operators.)
  287.  
  288. The overloading mechanism is not yet documented.
  289.  
  290.     exp -> '!:' exp
  291.     exp -> '~:' exp
  292.     exp -> exp '->:' <id>
  293.     exp -> exp '[:' exp ']:'
  294.     exp -> exp '=:' exp
  295.     exp -> exp '*:' exp
  296.     exp -> exp '/:' exp
  297.     exp -> exp '%:' exp
  298.     exp -> exp '+:' exp
  299.     exp -> exp '-:' exp
  300.     exp -> exp '<<:' exp
  301.     exp -> exp '==:' exp
  302.     exp -> exp '!=:' exp
  303.     exp -> exp '<=:' exp
  304.     exp -> exp '>=:' exp
  305.     exp -> exp '<:' exp
  306.     exp -> exp '>:' exp
  307.     exp -> exp '&:' exp
  308.     exp -> exp '^:' exp
  309.     exp -> exp '|:' exp
  310.     exp -> exp '&&:' exp
  311.     exp -> exp '||:' exp
  312.  
  313.  
  314.  
  315. Expressions have some extensions that are not much like C
  316. at all.
  317.  
  318.  
  319.     exp -> '@:' <id>
  320.  
  321.         example:    @:fnord
  322.  
  323.         This is a way to write a "keyword" (c.f. scm.info).
  324.         A keyword is simply a self-evaluating symbolic value.
  325.  
  326.     exp -> '@'<string>
  327.  
  328.         example:    @"(+ 1 2)"
  329.  
  330.         This is a way to write an arbitrary, readable, Scheme
  331.         value.  The example expression is a three element list (not
  332.         the number 3).
  333.  
  334.     exp -> '@' '(' arg_list ')'
  335.  
  336.         example:    @( x0 + vx, y0 + vy, z0 + vz)
  337.  
  338.         This constructs a list.  The example constructs
  339.         a three element list.
  340.  
  341.     exp -> '@' '[' arg_list ']'
  342.  
  343.         example:    @[ x0 + vx, y0 + vy, z0 + vz]
  344.  
  345.         This constructs a vector (an array).  The example constructs
  346.         a three element vector.
  347.  
  348.     exp -> '@bit[' arg_list ']'
  349.     exp -> '@uing[' arg_list ']'
  350.     exp -> '@int[' arg_list ']'
  351.     exp -> '@float[' arg_list ']'
  352.     exp -> '@double[' arg_list ']'
  353.     exp -> '@complex[' arg_list ']'
  354.  
  355.         example:    @int[ x0 + vx, y0 + vy, z0 + vz]
  356.  
  357.         These construct uniform arrays of various sorts (cf scm.info).
  358.  
  359.     exp -> '@\(' param_list ')' body
  360.  
  361.         example:    @\(a, b) { return a + b; }
  362.  
  363.         This constructs an anonymous procedure.
  364.  
  365.         
  366.     exp -> 'new' 'struct' <id> opt_params
  367.  
  368.         example:     p = new struct point (3, 4);
  369.                 p2 = new struct point;
  370.  
  371.         Allocate a new structure.
  372.  
  373.  
  374.     exp -> '(' ctax_struct_lx ctax_id_lx ')'
  375.  
  376.         example:    p_type = (struct point);
  377.  
  378.         Return a type object for a structure type.
  379.  
  380.  
  381.     exp -> '(' ctax_struct_lx ctax_id_lx opt_superclass '{' field_list '}' ')'
  382.  
  383.  
  384.         example:    p_type = (struct point { scm x; scm y; );
  385.  
  386.         Return a new anonymous type object for a structure type.
  387.  
  388.