[system] / trunk / pg / lib / Value / Context.pm Repository:
ViewVC logotype

View of /trunk/pg/lib/Value/Context.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2606 - (download) (as text) (annotate)
Sat Aug 14 11:25:47 2004 UTC (15 years, 6 months ago) by dpvc
File size: 2717 byte(s)
Can now specify that the objects stringify themselfs in their TeX
forms rather than their answer-string forms.  This makes it easier to
use the objects in the text of a problem (without having to call ->TeX
explicitly each reference).

Use

       Context()->texStrings

to start outputting TeX strings, and

      Context()->normalStrings

to switch back.

    1 #########################################################################
    2 #
    3 #  The basic context (Parser::Context is a subclass of this)
    4 #
    5 
    6 package Value::Context;
    7 my $pkg = "Value::Context";
    8 use strict;
    9 
   10 #
   11 #  Create a new Context object and initialize its data lists
   12 #
   13 sub new {
   14   my $self = shift; my $class = ref($self) || $self;
   15   my $context = bless {
   16     flags => {},
   17     pattern => {
   18       number => '(?:\d+(?:\.\d*)?|\.\d+)(?:E[-+]?\d+)?',
   19       signedNumber => '[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:E[-+]?\d+)?',
   20     },
   21     format => {
   22       number => '%g',  # default format for Reals
   23     },
   24     error => {
   25       string => '',
   26       pos => undef,
   27       message => '',
   28       flag => 0,
   29     },
   30     data => {
   31       hashes => ['lists'],
   32       arrays => ['data'],
   33       values => ['flags','pattern','format'],
   34     },
   35   }, $class;
   36   my %data = (lists=>{},flags=>{},@_);
   37   $context->{_lists} = new Value::Context::Lists($context,%{$data{lists}});
   38   $context->{_flags} = new Value::Context::Flags($context,%{$data{flags}});
   39   $context->{_initialized} = 1;
   40   $context->update;
   41   return $context;
   42 }
   43 
   44 #
   45 #  Implemented in subclasses
   46 #
   47 sub update {}
   48 
   49 #
   50 #  Access to the data lists
   51 #
   52 sub lists     {(shift)->{_lists}}
   53 sub flags     {(shift)->{_flags}}
   54 sub flag      {(shift)->{_flags}->get(shift)}
   55 
   56 #
   57 #  Make a copy of a Context object
   58 #
   59 sub copy {
   60   my $self = shift;
   61   my $context = $self->new();
   62   $context->{_initialized} = 0;
   63   foreach my $data (@{$context->{data}{hashes}}) {
   64     $context->{$data} = {};
   65     foreach my $x (keys %{$self->{$data}}) {
   66       $context->{$data}{$x} = {%{$self->{$data}{$x}}};
   67     }
   68     $context->{"_$data"}->update;
   69   }
   70   foreach my $data (@{$context->{data}{arrays}}) {
   71     $context->{$data} = {};
   72     foreach my $x (keys %{$self->{$data}}) {
   73       $context->{$data}{$x} = [@{$self->{$data}{$x}}];
   74     }
   75   }
   76   foreach my $data (@{$context->{data}{values}}) {
   77     $context->{$data} = {%{$self->{$data}}};
   78   }
   79   $context->{_initialized} = 1;
   80   return $context;
   81 }
   82 
   83 #
   84 #  Make stringify produce TeX or regular strings
   85 #
   86 sub texStrings {shift->flags->set(StringifyAsTeX=>1)}
   87 sub normalStrings {shift->flags->set(StringifyAsTeX=>0)}
   88 
   89 #
   90 #  Clear error flags
   91 #
   92 sub clearError {
   93   my $error = (shift)->{error};
   94   $error->{string} = '';
   95   $error->{pos} = undef;
   96   $error->{message} = '';
   97   $error->{flag} = 0;
   98 }
   99 
  100 #
  101 #  Set the error flags
  102 #
  103 sub setError {
  104   my $error = (shift)->{error};
  105   $error->{message} = shift;
  106   $error->{string} = shift;
  107   $error->{pos} = shift;
  108   $error->{flag} = 1;
  109 }
  110 
  111 #########################################################################
  112 #
  113 #  Load the subclasses.
  114 #
  115 
  116 use Value::Context::Data;
  117 
  118 #########################################################################
  119 
  120 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9