File ProgrammingModel.html    Author McKeeman    Copyright © 2007    index

XCOM Programming Model

Overview

The xcom compiler does not use the MATLAB class system (but it could have). Instead it uses nested functions and function handles to provide object capabilities.

Enumerations

A class of named constants, similar to a C enum, can be built from a cell array of field names. By default the base value is 1. An optional second argument is provided for other bases (such as 0). For example

    e = enum({'zero', 'one', 'two', 'three'}, 0);
    x = e.one;    % assign 1 to x.
  

This facility is used, for instance, to name grammar rules and reserved words in the parser.

      rw    = enum(cfg.reserved);             % set up tables
      rule  = enum(cfg.ruleNames);
    

Objects

The combination of nested functions and function handles has been used to build an OO compiler, with an object for each stage of the compilation process. The general model is to call a MATLAB function (the constructor) which instantiates an object. The result of the constructor is a struct of values and function handles giving access to the state of the constructed object. An example follows:


  function obj = objtest()       % ctor
    
    % variables in the persistent frame accessed by the handles
    x = zeros(1,0, 'int32');
    C = 2007;
    
    obj = public();              % return the public interface
    
    return;   % end of constructor
    
    % ---- nested functions ----
    function set(i, v)           % change the i-th value of x
      x(i) = v;
    end
    
    function v = get(i)          % latest value of the i-th value of x
      v = x(i);
    end
    
    function a = getx()          % latest value of the whole array
      a = x;
    end
    
    % ---- public interface ----
    function o = public()
      o     = struct;            % to pass the public interface
      o.set = @set;              % function handles for "methods"
      o.get = @get;
      o.x   = @getx;             % o.x = x would mean the initial value of x
      o.C   = C;                 % no @ needed because C is not a function
    end
  end
  

The local variables of the constructor function are preserved in the call frame of the constructor which remains live via the return value after the return from the constructor. The values at construction-time can be made available in the returned struct. Values computed later need to be accessed via function handles, which are also in the struct. For example:


    >> x = objtest();
    >> second = x.get(2)
    second = 0
    >> x.set(2,113);
    >> second = x.get(2)
    second = 113
    >> date = x.C
    date = 2007
  

Passing one object to the constructor of a second object is the equivalent of extending the first object. All of the public methods and fields of the first object are available for use in the extended object.

Frequent Testing

Since MATLAB provides instant turnaround, the work pattern of do-a-little, test-a-little is feasible. For that reason xcom comes with an extensive set of unit tests which can and should be run frequently. Any new code should get a corresponding entry in the unit tests. This is all the more important since MATLAB provides very little compile-time redundancy, meaning that most errors are run-time errors.