Runtime Values

Values were implemented using the boost::variant template class. This class essentially allowed values to be defined in a style more reminiscent of O'Caml's variant types, albeit much more cumbersome. In general, there are two methods of operating on a boost::variant derivative. The primary method used in the VM implementation was the visitor pattern. A class inheriting from boost::static_visitor<T> defines the () operator on each relevant class. For instance, one would define a to_string method as such:

class to_string_visitor : boost::static_visitor<string>
{
public:
  string operator(int i) { return string(itoa(i)); }
  string operator(double d) { return string(ftoa(f)); }
  string operator(ListRef lr)
  {
    return boost::apply_visitor(to_string_visitor(), lr->get_car()) + 
	     to_string_visitor()(lr->get_cdr());
  }
  ...
};

The above code also demonstrates using a visitor, via a call to boost::apply_visitor. This method is again similar to O'Caml's pattern matching, albeit much more verbose and less transparent.

The second method is using the boost::get<T> method, which returns a pointer to the appropriate value iff the variable stored is actually of the appropriate type. This is less recommended and in general less useful, since defining any method on all varietes of values would require very tedious case analysis.



Mason Smith 2008-06-11