[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 2605
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
636 Value::matchNumber($value) || Value::isFormula($value) || Value::isValue($value);
637 if (Value::isFormula($value)) {$type = $value->typeRef} 635 if (Value::isFormula($value)) {$type = $value->typeRef}
638 else {($value,$type) = Value::getValueType($self,$value)} 636 else {($value,$type) = Value::getValueType($self,$value)}
639 $self->Error("Variable '$x' should be of type $variables->{$x}{type}{name}") 637 $self->Error("Variable '$x' should be of type $variables->{$x}{type}{name}")
640 unless Parser::Item::typeMatch($type,$variables->{$x}{type}); 638 unless Parser::Item::typeMatch($type,$variables->{$x}{type});
641 $self->{values}{$x} = $value; 639 $self->{values}{$x} = $value;
642 } 640 }
641}
642
643
644##################################################
645##################################################
646#
647# Convert a student answer to a formula, with error trapping.
648# If the result is undef, there was an error (message is in Context()->{error} object)
649#
650
651sub Formula {
652 my $f = shift;
653 eval {Value::Formula->new($f)};
654}
655
656#
657# Evaluate a formula, with error trapping.
658# If the result is undef, there was an error (message is in Context()->{error} object)
659# If the result was a real, make it a fuzzy one.
660#
661sub Evaluate {
662 my $f = shift;
663 return unless defined($f);
664 my $v = eval {$f->eval(@_)};
665 $v = Value::Real->new($v) if defined($v) && $f->isRealNumber;
666 return $v;
643} 667}
644 668
645 669
646################################################## 670##################################################
647################################################## 671##################################################
664use Value::Formula; 688use Value::Formula;
665use Parser::Context; 689use Parser::Context;
666use Parser::Context::Default; 690use Parser::Context::Default;
667 691
668# use Parser::Differentiation; 692# use Parser::Differentiation;
693
694###########################################################################
695
696use vars qw($installed);
697$Parser::installed = 1;
669 698
670########################################################################### 699###########################################################################
671########################################################################### 700###########################################################################
672# 701#
673# To Do: 702# To Do:

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9