I need a mini "logic interpreter"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Baron Samedi

    I need a mini "logic interpreter"

    I want to extend the PHP based blog system which I use.

    We have static templates for the various blog pages and I would like to
    introduce some logic to them.

    That is, I want to have some sort of structure, like

    [[if <condition>]]
    [[else]]
    [[endif]]

    or [[for 0, 3]] // do 4 time
    [[endfor]]

    but I am bogged down at the very first hurdle, which is just evaluating
    something like '2 == 2.".

    The text from the templates will be seen as a string, of course, but
    when I try to evaluate it as a bool,
    echo 'Evaluated condition: "' . (bool) $text . '"<br>';

    it always evaluates to true, even if my condition is '2 == 3'.

    Obviously I am missing something very basic about evaluation and maybe
    casting.

    Can anyone enlighten me, please?

  • Alan Little

    #2
    Re: I need a mini &quot;logic interpreter&quo t;

    Carved in mystic runes upon the very living rock, the last words of Baron
    Samedi of comp.lang.php make plain:
    [color=blue]
    > I want to extend the PHP based blog system which I use.
    >
    > We have static templates for the various blog pages and I would like to
    > introduce some logic to them.
    >
    > That is, I want to have some sort of structure, like
    >
    > [[if <condition>]]
    > [[else]]
    > [[endif]]
    >
    > or [[for 0, 3]] // do 4 time
    > [[endfor]]
    >
    > but I am bogged down at the very first hurdle, which is just evaluating
    > something like '2 == 2.".
    >
    > The text from the templates will be seen as a string, of course, but
    > when I try to evaluate it as a bool,
    > echo 'Evaluated condition: "' . (bool) $text . '"<br>';
    >
    > it always evaluates to true, even if my condition is '2 == 3'.[/color]

    Strings and non-zero numbers evaluate to TRUE. You might consider the
    eval() function.

    eval('$conditio n = ($text)? true : false;');

    if ($condtion) {
    }

    The usual caveats about not trusting visitor input apply.

    I have a template processor I've been developing, that might work for
    you. It's kind of a work-in-progress, but I've used it in production
    environments. Email me if you'd like a copy.

    --
    Alan Little
    Phorm PHP Form Processor

    Comment

    • Baron Samedi

      #3
      Re: I need a mini &quot;logic interpreter&quo t;

      $text = '2==2';

      eval('$conditio n = ($text)? true : false;');

      if ($condtion)
      echo '<hr>True<hr>' ;
      else
      echo '<hr>False<hr>' ;


      --------------------------------------------------------------------------------
      False
      --------------------------------------------------------------------------------

      Comment

      • Alan Little

        #4
        Re: I need a mini &quot;logic interpreter&quo t;

        Carved in mystic runes upon the very living rock, the last words of
        Baron Samedi of comp.lang.php make plain:
        [color=blue]
        > $text = '2==2';
        >
        > eval('$conditio n = ($text)? true : false;');
        >
        > if ($condtion)
        > echo '<hr>True<hr>' ;
        > else
        > echo '<hr>False<hr>' ;
        >
        >
        > -----------------------------------------------------------------------
        > False
        > -----------------------------------------------------------------------[/color]

        1) Variables in single quotes do not get evaluated. That expression
        needs to be in double quotes, and then you need to escape the $
        for $condition

        2) You have a typo. You have $condition in your eval, and $condtion
        in your if().

        --
        Alan Little
        Phorm PHP Form Processor

        Comment

        • Baron Samedi

          #5
          Re: I need a mini &quot;logic interpreter&quo t;

          1) oops, I didn't know that about the single quotes. Thanks.

          What did you mean by "escape the $ for $condition " ?

          2) thanks for that.

          it is still not working ("2==3" gicves true) , so I guess that I need
          to "escape the $ in $condition".

          Thanks for your help.

          Comment

          • Rik

            #6
            Re: I need a mini &quot;logic interpreter&quo t;

            Baron Samedi wrote:[color=blue]
            > 1) oops, I didn't know that about the single quotes. Thanks.
            >
            > What did you mean by "escape the $ for $condition " ?[/color]

            In:
            eval("$conditio n = ($text)? true : false;");

            PHP will try to search for the variable "condition" to put in the eval
            statement.

            Escaping it like:
            eval("\$conditi on = ($text)? true : false;");

            will let PHP know it has to treat this part as text, so it will be evalled
            as "$condition " and not the value of the (prehaps existing) variable
            $condition. If it tries to replace it with the value of the variables
            "condition" , there is off course no way $condition gets set in this
            statement.

            Grtz,
            --
            Rik Wasmus


            Comment

            • Jerry Stuckle

              #7
              Re: I need a mini &quot;logic interpreter&quo t;

              Rik wrote:[color=blue]
              > Baron Samedi wrote:
              >[color=green]
              >>1) oops, I didn't know that about the single quotes. Thanks.
              >>
              >>What did you mean by "escape the $ for $condition " ?[/color]
              >
              >
              > In:
              > eval("$conditio n = ($text)? true : false;");
              >
              > PHP will try to search for the variable "condition" to put in the eval
              > statement.
              >
              > Escaping it like:
              > eval("\$conditi on = ($text)? true : false;");
              >
              > will let PHP know it has to treat this part as text, so it will be evalled
              > as "$condition " and not the value of the (prehaps existing) variable
              > $condition. If it tries to replace it with the value of the variables
              > "condition" , there is off course no way $condition gets set in this
              > statement.
              >
              > Grtz,[/color]

              How about:

              $condition = (eval ($text)) ? true : false;

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • Alan Little

                #8
                Re: I need a mini &quot;logic interpreter&quo t;

                Carved in mystic runes upon the very living rock, the last words of
                Jerry Stuckle of comp.lang.php make plain:
                [color=blue]
                > Rik wrote:[color=green]
                >> Baron Samedi wrote:
                >>[color=darkred]
                >>>1) oops, I didn't know that about the single quotes. Thanks.
                >>>
                >>>What did you mean by "escape the $ for $condition " ?[/color]
                >>
                >> In:
                >> eval("$conditio n = ($text)? true : false;");
                >>
                >> PHP will try to search for the variable "condition" to put in the
                >> eval statement.
                >>
                >> Escaping it like:
                >> eval("\$conditi on = ($text)? true : false;");
                >>
                >> will let PHP know it has to treat this part as text[/color]
                >
                > How about:
                >
                > $condition = (eval ($text)) ? true : false;[/color]

                That gives a parse error in the eval, as 2==3 is not a syntactically
                valid PHP statement.

                --
                Alan Little
                Phorm PHP Form Processor

                Comment

                • Baron Samedi

                  #9
                  Re: I need a mini &quot;logic interpreter&quo t;

                  Isn't it a condition? Do I have to wrap it in brackets? Nope, taht
                  doesn't work.

                  Of course "(2==3")" is just an example. The problem is taht I don't
                  knwo what users will code ther. Could be anything, so long as it is a
                  condition.

                  Can you help me out with a code example? I would be very grateful.

                  Comment

                  • Baron Samedi

                    #10
                    Re: I need a mini &quot;logic interpreter&quo t;

                    $text = "(2==3)";

                    eval('\$conditi on = ($text)? true : false;');


                    Warning: Unexpected character in input: '\' (ASCII=92) state=1 in
                    C:\graham\blog\ extensions\_sni ppet_dev\~scp.p hp(127) : eval()'d code on
                    line 1

                    Sorry, I still don't get it to work.

                    Comment

                    • Alan Little

                      #11
                      Re: I need a mini &quot;logic interpreter&quo t;

                      Carved in mystic runes upon the very living rock, the last words of Baron
                      Samedi of comp.lang.php make plain:
                      [color=blue]
                      > $text = "(2==3)";[/color]

                      No parentheses.
                      [color=blue]
                      > eval('\$conditi on = ($text)? true : false;');[/color]

                      Double qoutes, man. Double quotes.
                      [color=blue]
                      > Warning: Unexpected character in input: '\' (ASCII=92) state=1 in
                      > C:\graham\blog\ extensions\_sni ppet_dev\~scp.p hp(127) : eval()'d code on
                      > line 1
                      >
                      > Sorry, I still don't get it to work.[/color]


                      --
                      Alan Little
                      Phorm PHP Form Processor

                      Comment

                      • Rik

                        #12
                        Re: I need a mini &quot;logic interpreter&quo t;

                        Baron Samedi wrote:[color=blue]
                        > $text = "(2==3)";
                        >
                        > eval('\$conditi on = ($text)? true : false;');
                        >
                        >
                        > Warning: Unexpected character in input: '\' (ASCII=92) state=1 in
                        > C:\graham\blog\ extensions\_sni ppet_dev\~scp.p hp(127) : eval()'d code
                        > on line 1[/color]

                        eval("\$conditi on = ($text)? true : false;"); or
                        eval('$conditio n = ('.$text.')? true : false;');

                        Grtz,
                        --
                        Rik Wasmus


                        Comment

                        • Rik

                          #13
                          Re: I need a mini &quot;logic interpreter&quo t;

                          Alan Little wrote:[color=blue]
                          > Carved in mystic runes upon the very living rock, the last words of
                          > Baron Samedi of comp.lang.php make plain:
                          >[color=green]
                          >> $text = "(2==3)";[/color]
                          >
                          > No parentheses.
                          >[color=green]
                          >> eval('\$conditi on = ($text)? true : false;');[/color]
                          >
                          > Double qoutes, man. Double quotes.[/color]

                          Yup,
                          eval("\$conditi on = ($text)? true : false;");
                          or
                          eval('$conditio n = ('.$text.')? true : false;');

                          For quoting-education:


                          Grtz,
                          --
                          Rik Wasmus


                          Comment

                          • the DtTvB

                            #14
                            Re: I need a mini &quot;logic interpreter&quo t;

                            I made one

                            Source file: http://dttvb.yi.org/tparser.phps
                            There are still some problems about nesting. But it works quite well.

                            -- TESTS --
                            echo dtsparse('[[if 2==2]]this condition is true[[else]]this condition
                            is false[[endif]]');
                            // Outputs this condition is true
                            echo dtsparse('[[if 3==2]]this condition is true[[else]]this condition
                            is false[[endif]]');
                            // Outputs this condition is false
                            echo dtsparse('[[if 3>2]]this condition is true[[else]]this condition
                            is false[[endif]]');
                            // Outputs this condition is true
                            echo dtsparse('befor e if[[if 3==3]], this condition is true[[endif]],
                            after if');
                            // Outputs before if, this condition is true, after if
                            echo dtsparse('befor e if[[if 3==2]], this condition is true[[endif]],
                            after if');
                            // Outputs before if, after if
                            echo dtsparse('[[for 5,10]]aa [[endfor]]');
                            // Outputs aa aa aa aa aa aa
                            echo dtsparse('[[for 5 to 10]][[index]],[[endfor]]finish');
                            // Outputs 5,6,7,8,9,10,fi nish

                            Is that what you want? Sorry if it's not.

                            Comment

                            • Baron Samedi

                              #15
                              Re: I need a mini &quot;logic interpreter&quo t;

                              This looks ecxcellent!! Is it GPL? If s, can you please mail me the
                              code & test script?

                              Thank you very much for your help!

                              Comment

                              Working...