in reply to Logic expression evaluation not working. What am I doing wrong?

An interesting way to do it is to use a full blown parser. Marpa is a relatively easy way to get the job done:

use strict; use warnings; use Marpa::R2; package DoStuff; my %vars; sub doAnd { my @params = @_; return $params[1] && $params[3]; } sub doOr { my @params = @_; return $params[1] || $params[3]; } sub doVarValue { my @params = @_; return $vars{$params[1]}; } sub doAssign { my @params = @_; $vars{$params[1]} = $params[3]; return; } sub doVal { my @params = @_; my $val = 'ARRAY' eq ref $params[1] ? $params[1][1] : $params[1]; return $val; } sub doNVal { my @params = @_; my $val = 'ARRAY' eq ref $params[2] ? $params[2][1] : $params[2]; return $val ? 0 : 1; } sub doResult { my @params = @_; return $params[-1]; } package main; my $syntax = <<'SYNTAX'; lexeme default = latm => 1 test ::= assignments expression action => doResult | expression action => doResult assignments ::= assignment assignments | assignment assignment ::= var '=' constant action => doAssign expression ::= expression '|' term action => doOr | term action => ::f +irst term ::= term '&' factor action => doAnd | factor action => ::first factor ::= negatable action => doVal | '!' negatable action => doNVal negatable ::= '(' expression ')' action => [values] | value action => +::first value ::= var action => doVarValue | constant action => ::first var ~ [A-Z] constant ~ [10] :discard ~ spaces spaces ~ [\s]+ SYNTAX my $grammar = Marpa::R2::Scanless::G->new({source => \$syntax}); for my $inputs ([0, 0, 0], [0, 0, 1], [0, 1, 1], [1, 0, 1]) { my $input = <<INPUT; C = $inputs->[0] Q = $inputs->[1] T = $inputs->[2] (!(C)&T)&!Q INPUT my $result = $grammar->parse(\$input, 'DoStuff'); printf "${input}Result: %d\n\n", $$result; }

Prints:

C = 0 Q = 0 T = 0 (!(C)&T)&!Q Result: 0 C = 0 Q = 0 T = 1 (!(C)&T)&!Q Result: 1 C = 0 Q = 1 T = 1 (!(C)&T)&!Q Result: 0 C = 1 Q = 0 T = 1 (!(C)&T)&!Q Result: 0
Premature optimization is the root of all job security

Replies are listed 'Best First'.
Re^2: Logic expression evaluation not working. What am I doing wrong?
by Anonymous Monk on Jan 05, 2016 at 08:22 UTC

    my %vars; ...

    Is that closure the only option?

      It's a package variable, not a closure. In a sense it's a global variable, but notionally private to the package. There are other ways it could be done, but for a "simple" example it is expedient and not totally terrible.

      Premature optimization is the root of all job security

        It's a package variable, not a closure. In a sense it's a global variable, but notionally private to the package. There are other ways it could be done, but for a "simple" example it is expedient and not totally terrible.

        actually it is a closure :) it may not look like it but it is, just like this is a closure, foo()/bar() close over $foo  { my $foo; sub foo { $foo++; } sub bar { $foo--;} }

        No, its not totally terrible, but I wanted to know these other ways because a global is inherently limited

        Does Marpa provide a stash DoStuff can access?