After getting pretty far along with To model or not to model, I've decided that making the application general enough to meet the requirements requires a change to a parsed-grammar approach. I've rewritten almost everything as a Parse::RecDescent grammar.

Almost everything works correctly.

In one case, however, I get strange results back from the @item array, like this:
19|ifthenelse|>>Matched action<< (return value: []) | 19|ifthenelse|>>Matched production: [if elsif else | | |endif]<< | 19|ifthenelse|>>Matched rule<< (return value: | | |[ARRAY(0x870cb08)]) | 19|ifthenelse|(consumed: []) |
from source text:
if stw!=0 then stw else 1.40 * 1e-6 endif
and the following (simplified) grammar:
expr: ifthenelse { $return = main::evaluate($main::c_v, @item); } ifthenelse: if elsif(s?) else(?) endif { $return = 'NIL'; for (my $i = 1; $i < @item; $i++) { print " iftest: $i '" . $item[$i] . "'\n"; if ($item[$i] ne 'NIL') { $return = $item[$i]; last; } } if ($return eq 'NIL') { die "IF statement '@item' not resolvable! $!\n"; } } if: /if/i expr /then/i expr { print 'i2: ' . $item[2] ."\n"; if($item[2]) { $return = $item[4]; } else { $return = 'NIL'; + } } else: /else/i expr { $return = $item[2]; } elsif: /elsif/i expr /then/i expr { if ($item[2]) { $return = $item[4] } else { $return = 'NIL' +} } endif: /endif/i { $return = 'NIL'; }
and main 'engine' subroutine:
sub evaluate { my $contvar = shift; shift; my $estr = join(' ', @_); my @whatsleft = @_; print " evaluating '$estr'\n"; $estr =~ s/ARRAY\x28[^\x28\x29]+\x29//g; $estr = trim($estr); if ($estr !~ /^[\s\.\/\x28\x290-9eE*+-]+$/) { my $parser = SpiceGrammar->new() or die "Bad SpiceGrammar: $!\n"; print " evaluating variable\n"; defined(my $value = $parser->vardef($estr)) or die "Bad input: {$estr} $!\n"; if ($value eq '') { $value = 0; print " substituting\n"; } return $value; } else { my $value = eval($estr); $vartable{$contvar} = $value; return $value; } }
WHen the ELSE clause matched, it (apparently) saved the result properly:
21| expr |>>Matched action<< (return value: | | |[1.4e-06]) | 21| expr |>>Matched production: [number operator| | |expr]<< | 21| expr |>>Matched rule<< (return value: [1.4e-| | |06]) | 21| expr |(consumed: [ 1.40 * 1e-6]) | 20| else |>>Matched subrule: [expr]<< (return | | |value: [1.4e-06] | 20| else |Trying action | 20| else |>>Matched action<< (return value: | | |[1.4e-06]) | 20| else |>>Matched production: [/else/i expr]<<| 20| else |>>Matched rule<< (return value: [1.4e-| | |06]) | 20| else |(consumed: [ else 1.40 * 1e-6]) | 19|ifthenelse|>>Matched repeated subrule: [else]<< | | |(1 times)
but I can't see where it's going south and returning an array (or the string expression of an array). In other similar cases (outside the ifthenelse-decoder) sub-expression results are returned properly.

TIA for pointers or assistance! :D

In reply to To make the Model WORK by samizdat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.