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

Diff of /trunk/pg/lib/Parser.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 2579 Revision 2611
56 push(@{$tokens},['op',$5,$p0,$p1,$space]) if (defined($5)); 56 push(@{$tokens},['op',$5,$p0,$p1,$space]) if (defined($5));
57 push(@{$tokens},['open',$6,$p0,$p1,$space]) if (defined($6)); 57 push(@{$tokens},['open',$6,$p0,$p1,$space]) if (defined($6));
58 push(@{$tokens},['close',$7,$p0,$p1,$space]) if (defined($7)); 58 push(@{$tokens},['close',$7,$p0,$p1,$space]) if (defined($7));
59 push(@{$tokens},['var',$8,$p0,$p1,$space]) if (defined($8)); 59 push(@{$tokens},['var',$8,$p0,$p1,$space]) if (defined($8));
60 } else { 60 } else {
61 push(@{$tokens},['error',substr($string,$p0,3),$p0]); 61 push(@{$tokens},['error',substr($string,$p0,1),$p0,$p0+1]);
62 $self->{error} = 1; 62 $self->{error} = 1;
63 last; 63 last;
64 } 64 }
65 $space = ($string =~ m/\G\s+/gc); 65 $space = ($string =~ m/\G\s+/gc);
66 } 66 }
89 /num/ and do {$self->Num($ref->[1]); last}; 89 /num/ and do {$self->Num($ref->[1]); last};
90 /const/ and do {$self->Const($ref->[1]); last}; 90 /const/ and do {$self->Const($ref->[1]); last};
91 /var/ and do {$self->Var($ref->[1]); last}; 91 /var/ and do {$self->Var($ref->[1]); last};
92 /fn/ and do {$self->Fn($ref->[1]); last}; 92 /fn/ and do {$self->Fn($ref->[1]); last};
93 /str/ and do {$self->Str($ref->[1]); last}; 93 /str/ and do {$self->Str($ref->[1]); last};
94 /error/ and do {$self->Error("Unexpected characters '$ref->[1]'",$ref); last}; 94 /error/ and do {$self->Error("Unexpected character '$ref->[1]'",$ref); last};
95 } 95 }
96 return if ($self->{error}); 96 return if ($self->{error});
97 } 97 }
98 $self->Close('start'); return if ($self->{error}); 98 $self->Close('start'); return if ($self->{error});
99 $self->{tree} = $self->{stack}->[0]->{value}; 99 $self->{tree} = $self->{stack}->[0]->{value};
629 my $self = shift; my ($value,$type); 629 my $self = shift; my ($value,$type);
630 my $variables = $self->{context}{variables}; 630 my $variables = $self->{context}{variables};
631 $self->{values} = {@_}; 631 $self->{values} = {@_};
632 foreach my $x (keys %{$self->{values}}) { 632 foreach my $x (keys %{$self->{values}}) {
633 $self->Error("Undeclared variable '$x'") unless defined $variables->{$x}; 633 $self->Error("Undeclared variable '$x'") unless defined $variables->{$x};
634 $value = $self->{values}{$x}; 634 $value = Value::makeValue($self->{values}{$x});
635 $value = Value::Formula->new($value) unless 635 $value = Value::Formula->new($value) unless
636 Value::matchNumber($value) || Value::isFormula($value) || Value::isValue($value); 636 Value::isFormula($value) || Value::isValue($value);
637 if (Value::isFormula($value)) {$type = $value->typeRef} 637 if (Value::isFormula($value)) {$type = $value->typeRef}
638 else {($value,$type) = Value::getValueType($self,$value)} 638 else {($value,$type) = Value::getValueType($self,$value)}
639 $self->Error("Variable '$x' should be of type $variables->{$x}{type}{name}") 639 $self->Error("Variable '$x' should be of type $variables->{$x}{type}{name}")
640 unless Parser::Item::typeMatch($type,$variables->{$x}{type}); 640 unless Parser::Item::typeMatch($type,$variables->{$x}{type});
641 $self->{values}{$x} = $value; 641 $self->{values}{$x} = $value;
642 } 642 }
643}
644
645
646##################################################
647##################################################
648#
649# Convert a student answer to a formula, with error trapping.
650# If the result is undef, there was an error (message is in Context()->{error} object)
651#
652
653sub Formula {
654 my $f = shift;
655 eval {Value::Formula->new($f)};
656}
657
658#
659# Evaluate a formula, with error trapping.
660# If the result is undef, there was an error (message is in Context()->{error} object)
661# If the result was a real, make it a fuzzy one.
662#
663sub Evaluate {
664 my $f = shift;
665 return unless defined($f);
666 my $v = eval {$f->eval(@_)};
667 $v = Value::makeValue($v) if defined($v);
668 return $v;
643} 669}
644 670
645 671
646################################################## 672##################################################
647################################################## 673##################################################
664use Value::Formula; 690use Value::Formula;
665use Parser::Context; 691use Parser::Context;
666use Parser::Context::Default; 692use Parser::Context::Default;
667 693
668# use Parser::Differentiation; 694# use Parser::Differentiation;
695
696###########################################################################
697
698use vars qw($installed);
699$Parser::installed = 1;
669 700
670########################################################################### 701###########################################################################
671########################################################################### 702###########################################################################
672# 703#
673# To Do: 704# To Do:

Legend:
Removed from v.2579  
changed lines
  Added in v.2611

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9