From: Emerson on
Hi,

I've create a new design pattern example using OO Cobol that I would
like to share with you. This new example shows how to create an
arithmetic expression evaluator, so the application using it can
evaluate a formula such as:

((a + b) ^(c / d)) * e

or

a + b + c * (d - e)

There some limitations due the way the parser was built:

.. Only one letter variables are supported
.. There is no numeric literal support. All values must be hold in
variables
.. Variables are case sensitive (a <> A)

Here is an example of how to use it:

IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN AS "ConsoleApplication1.Main".
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
REPOSITORY.
class ClassComputeFormula as
"InterpreterPattern.ComputeFormula"
class ClassContext as
"InterpreterPattern.Context".

DATA DIVISION.
WORKING-STORAGE SECTION.
01 computeFormula object reference ClassComputeFormula.
01 context object reference ClassContext.
01 result comp-2.
01 displayResult pic s9(09)v99.

PROCEDURE DIVISION.
invoke ClassComputeFormula "NEW" returning computeFormula
invoke ClassContext "NEW" returning context

invoke computeFormula "SetExpression" using "((a ^ b) + c)
* d"

invoke context "CreateVariable" using "a", 10
invoke context "CreateVariable" using "b", 2
invoke context "CreateVariable" using "c", 2
invoke context "CreateVariable" using "d", 8.5

invoke computeFormula "SetContext" using context
invoke computeFormula "Evaluate" returning result

move result to displayResult

display displayResult

END PROGRAM MAIN.


The OO Cobol code is an improved version of Partha Kuchana Java
Calculator sample : http://www.java2s.com/Code/Java/Design-Pattern/InterpreterPatternCalculator.htm

The article explaning the pattern implementation is under development
at www.100coolthings.net and should be available soon (I'm upgrading
the entire site infra), but the source code is available already at
http://www.codeplex.com/cobolRocks (Source section) .
From: HeyBub on
Emerson wrote:
> Hi,
>
> I've create a new design pattern example using OO Cobol that I would
> like to share with you. This new example shows how to create an
> arithmetic expression evaluator, so the application using it can
> evaluate a formula such as:
>
> ((a + b) ^(c / d)) * e
>
> or
>
> a + b + c * (d - e)
>
> There some limitations due the way the parser was built:
>
> . Only one letter variables are supported
> . There is no numeric literal support. All values must be hold in
> variables
> . Variables are case sensitive (a <> A)
>
> Here is an example of how to use it:
>
> IDENTIFICATION DIVISION.
> PROGRAM-ID. MAIN AS "ConsoleApplication1.Main".
> ENVIRONMENT DIVISION.
> CONFIGURATION SECTION.
> SPECIAL-NAMES.
> REPOSITORY.
> class ClassComputeFormula as
> "InterpreterPattern.ComputeFormula"
> class ClassContext as
> "InterpreterPattern.Context".
>
> DATA DIVISION.
> WORKING-STORAGE SECTION.
> 01 computeFormula object reference ClassComputeFormula.
> 01 context object reference ClassContext.
> 01 result comp-2.
> 01 displayResult pic s9(09)v99.
>
> PROCEDURE DIVISION.
> invoke ClassComputeFormula "NEW" returning computeFormula
> invoke ClassContext "NEW" returning context
>
> invoke computeFormula "SetExpression" using "((a ^ b) + c)
> * d"
>
> invoke context "CreateVariable" using "a", 10
> invoke context "CreateVariable" using "b", 2
> invoke context "CreateVariable" using "c", 2
> invoke context "CreateVariable" using "d", 8.5
>
> invoke computeFormula "SetContext" using context
> invoke computeFormula "Evaluate" returning result
>
> move result to displayResult
>
> display displayResult
>
> END PROGRAM MAIN.
>
>
> The OO Cobol code is an improved version of Partha Kuchana Java
> Calculator sample :
> http://www.java2s.com/Code/Java/Design-Pattern/InterpreterPatternCalculator.htm
>
> The article explaning the pattern implementation is under development
> at www.100coolthings.net and should be available soon (I'm upgrading
> the entire site infra), but the source code is available already at
> http://www.codeplex.com/cobolRocks (Source section) .

Thanks for the excellent example.

Here's another rendering using non-OO COBOL

IDENTIFICATION DIVISION.
PROGRAM-ID. FIDDLE.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 MyFormula pic x(200) Global.
01 Result comp-2 Global.

PROCEDURE DIVISION.

Call "MakeFormula" USING MyFormula.
Call "GET"
Call "MULL"
Call "PUT"
EXIT PROGRAM.


From: Alistair Maclean on
On May 23, 1:42 am, "HeyBub" <hey...(a)NOSPAMgmail.com> wrote:
> Emerson wrote:
> > Hi,
>
> > I've create a new design pattern example using OO Cobol that I would
> > like to share with you. This new example shows how to create an
> > arithmetic expression evaluator, so the application using it can
> > evaluate a formula such as:
>
> > ((a + b) ^(c / d)) * e
>
> > or
>
> > a + b + c * (d - e)
>
> > There some limitations due the way the parser was built:
>
> > . Only one letter variables are supported
> > . There is no numeric literal support. All values must be hold in
> > variables
> > . Variables are case sensitive (a <> A)
>
> > Here is an example of how to use it:
>
> >       IDENTIFICATION DIVISION.
> >       PROGRAM-ID. MAIN AS "ConsoleApplication1.Main".
> >       ENVIRONMENT DIVISION.
> >       CONFIGURATION SECTION.
> >       SPECIAL-NAMES.
> >       REPOSITORY.
> >           class ClassComputeFormula   as
> > "InterpreterPattern.ComputeFormula"
> >           class ClassContext          as
> > "InterpreterPattern.Context".
>
> >       DATA DIVISION.
> >       WORKING-STORAGE SECTION.
> >       01  computeFormula    object reference ClassComputeFormula.
> >       01  context           object reference ClassContext.
> >       01  result            comp-2.
> >       01  displayResult     pic s9(09)v99.
>
> >       PROCEDURE DIVISION.
> >           invoke  ClassComputeFormula "NEW" returning computeFormula
> >           invoke  ClassContext "NEW" returning context
>
> >           invoke  computeFormula "SetExpression" using "((a ^ b) + c)
> > * d"
>
> >           invoke  context "CreateVariable" using "a", 10
> >           invoke  context "CreateVariable" using "b", 2
> >           invoke  context "CreateVariable" using "c", 2
> >           invoke  context "CreateVariable" using "d", 8.5
>
> >           invoke  computeFormula "SetContext" using context
> >           invoke  computeFormula "Evaluate" returning result
>
> >           move    result                 to  displayResult
>
> >           display displayResult
>
> >       END PROGRAM MAIN.
>
> > The OO Cobol code is an improved version of Partha Kuchana Java
> > Calculator sample :
> >http://www.java2s.com/Code/Java/Design-Pattern/InterpreterPatternCalc...
>
> > The article explaning the pattern implementation is under development
> > atwww.100coolthings.netand should be available soon (I'm upgrading
> > the entire site infra), but the source code is available already at
> >http://www.codeplex.com/cobolRocks(Source section) .
>
> Thanks for the excellent example.
>
> Here's another rendering using non-OO COBOL
>
>        IDENTIFICATION DIVISION.
>        PROGRAM-ID. FIDDLE.
>        ENVIRONMENT DIVISION.
>        CONFIGURATION SECTION.
>
>        DATA DIVISION.
>        WORKING-STORAGE SECTION.
>         01  MyFormula                 pic x(200) Global.
>        01  Result            comp-2 Global.
>
>        PROCEDURE DIVISION.
>
>        Call "MakeFormula" USING MyFormula.
>        Call "GET"
>        Call "MULL"
>        Call "PUT"
>        EXIT PROGRAM.- Hide quoted text -
>
> - Show quoted text -

You won't get very far in programming umless you learn to obfuscate
your code.