Ver Mensaje Individual
  #1
Antiguo 23 de febrero de 2017, 09:41
Kuk
Administrador
Última Actividad 19.09.2026 20:11
Posts Posts: 2.500
Likes enviados Enviados: 1037
Likes recibidos Recibidos: 1207
Predeterminado Interpretador
0 Inactivo Inactivo

Citación del post de Eslopes
Cobol does not have the ability to evaluate expressions dynamically, i.e. defining an expression in a string and having it evaluated during the runtime. OO Cobol is a whole different story. There is no eval command, but we can create our own way to evaluate things using the Interpreter Pattern!
That sounds great, but what is an evalute command anyways? Good question. Language elements stored in a string are nothing, but a string. An eval command take that string and interpret its content, processing the elements accordling. That means power, the ability to define code blocks and have it processed when needed. The Interpreter Pattern allows you to define your own grammar, and process it in order to do some useful task.

The Interpreter Pattern allows much more than simple arithmetic expressions evaluation, thought this is exactly what we are going to show here. The classes in this solutions are:
  • IExpression: Interface that declares an operation
  • Context: Global information used by the expression
  • TerminalExpression: Represent elements in the grammar that do no to get replace, such as symbols
  • NonTerminalExpression: Represents elements that will be replaced during the evaluation such as variables or even rules
  • ComputeFormula: Client class that also contains the parser used to break expression's elements.

The ComputeFormula class deals with a lot of concepts not covered in this article, but I recommend you to understand that class in order to get yourself used to things like stacks, collections, string handling and much more.

OO Cobol code

Everything starts with an Interface definition. IExpression declares the contract to which classes must adhere in order to participate in this solution.

Código COBOL:
  1.  INTERFACE-ID. IExpression as "InterpreterPattern.IExpression".
  2.    environment division.
  3.      configuration section.
  4.        repository.
  5.          class  ClassContext as "InterpreterPattern.Context".
  6.          
  7.    procedure division.
  8.    
  9.      method-id. EvaluateExpression as "Evaluate".
  10.        
  11.         data division.
  12.           linkage section.
  13.          
  14.           01  inContext   object reference ClassContext.
  15.           01  outValue    usage comp-2.
  16.          
  17.          procedure division using inContext returning outValue.
  18.       end method EvaluateExpression.
  19.  END INTERFACE IExpression.      

The following classes implements the IExpression interface: AddExpression.cob, SubtractExpression.cob, , MultiplyExpression.cob, DivideExpression.cob, and PowerExpression.cob. These are also the classes that contains the actual calculation performed over operands.

Below the code for PowerExpression code:

Código COBOL:
  1.  CLASS-ID. PowerExpression AS "InterpreterPattern.PowerExpression"
  2.            inherits ClassNonTerminalExpression.
  3.    environment division.
  4.      configuration section.
  5.        repository.
  6.          class ClassContext               as "InterpreterPattern.Context"
  7.          class SystemString               as "System.String"
  8.          class ClassNonTerminalExpression as "InterpreterPattern.NonTerminalExpression"
  9.          
  10.           interface IExpression             as "InterpreterPattern.IExpression".
  11.          
  12.    *> Instance's data and methods
  13.     object.
  14.       data division.
  15.         working-storage section.
  16.         copy "types.book".
  17.        
  18.         01  variable  object reference SystemString private.
  19.        
  20.       procedure division.
  21.      
  22.         method-id. NEW.
  23.            data division.
  24.              linkage section.
  25.              01   inLeftExpression    object reference IExpression.
  26.              01   inRightExpression   object reference IExpression.
  27.              
  28.            procedure division using inLeftExpression, inRightExpression.
  29.              
  30.              invoke super "NEW" using inLeftExpression, inRightExpression.
  31.          
  32.         end method NEW.
  33.        
  34.         method-id. EvaluateExpresion as "Evaluate" override.
  35.           data division.
  36.             working-storage section.
  37.            
  38.             01 anExpression   object reference IExpression.
  39.            
  40.             linkage section.
  41.            
  42.             01 inContext  object reference ClassContext.
  43.             01 outValue   type SystemDouble.
  44.          
  45.           procedure division using inContext returning outValue.
  46.          
  47.             invoke super "GetLeftNode" returning anExpression
  48.             move  anExpression::"Evaluate"(inContext)  to  outValue
  49.             invoke super "GetRightNode" returning anExpression
  50.            
  51.             *> This is the real "PowerExpression"
  52.              compute outValue = outValue ** anExpression::"Evaluate"(inContext)
  53.                
  54.         end method EvaluateExpresion.
  55.     end object.
  56.   END CLASS PowerExpression.

Other operations' classes such as AddExpression or MultiplyExpression have pretty much basically the same code structure.

The TerminalExpression class that defines symbols behavior:

Código COBOL:
  1.  CLASS-ID. TerminalExpression AS "InterpreterPattern.TerminalExpression".
  2.    environment division.
  3.      configuration section.
  4.        repository.
  5.          class ClassContext       as "InterpreterPattern.Context"
  6.          class SystemCollectionHashTable as "System.Collections.Hashtable"
  7.          class SystemString       as "System.String"
  8.          
  9.          interface IExpression       as "InterpreterPattern.IExpression".
  10.          
  11.    *> Instance's data and methods
  12.     object. implements IExpression.
  13.       data division.
  14.         working-storage section.
  15.         copy "types.book".
  16.        
  17.         01  variable  object reference SystemString private.
  18.        
  19.       procedure division.
  20.      
  21.         method-id. NEW.
  22.            data division.
  23.              linkage section.
  24.              01   inName    object reference SystemString.
  25.              
  26.            procedure division using inName.
  27.            
  28.              set   variable     to  inName
  29.          
  30.         end method NEW.
  31.        
  32.         method-id. EvaluateExpression as "Evaluate".
  33.           data division.
  34.             linkage section.
  35.            
  36.             01 inContext  object reference ClassContext.
  37.             01 outValue   type SystemDouble.
  38.          
  39.           procedure division using inContext returning outValue.
  40.          
  41.             move inContext::"GetValue"(variable)   to  outValue
  42.                
  43.         end method EvaluateExpression.
  44.     end object.
  45.   END CLASS TerminalExpression.

The NonTerminalExpression class that defines rules:

Código COBOL:
  1.  CLASS-ID. NonTerminalExpression AS "InterpreterPattern.NonTerminalExpression".
  2.    environment division.
  3.      configuration section.
  4.        repository.
  5.          class ClassContext       as "InterpreterPattern.Context"
  6.          class SystemCollectionHashTable as "System.Collections.Hashtable"
  7.          class SystemString       as "System.String"
  8.          
  9.          interface IExpression       as "InterpreterPattern.IExpression".
  10.          
  11.    *> Instance's data and methods
  12.     object. implements IExpression.
  13.       data division.
  14.         working-storage section.
  15.         copy "types.book".
  16.      
  17.         01  leftNode    object reference IExpression private.      
  18.         01  rightNode   object reference IExpression private.      
  19.        
  20.       procedure division.
  21.      
  22.         method-id. NEW.
  23.            data division.
  24.              linkage section.
  25.              01   inLeftExpression  object reference IExpression.
  26.              01   inRightExpression object reference IExpression.
  27.            procedure division using inLeftExpression inRightExpression.
  28.              
  29.              invoke self "SetLeftNode"  using inLeftExpression
  30.              invoke self "SetRightNode"   using inRightExpression
  31.            
  32.         end method NEW.
  33.        
  34.          method-id. SetLeftNode.
  35.           data division.
  36.             linkage section.
  37.            
  38.             01 inNode   object reference IExpression.
  39.          
  40.           procedure division using inNode.
  41.          
  42.             set  leftNode     to  inNode
  43.                
  44.         end method SetLeftNode.  
  45.          method-id. SetRightNode.
  46.           data division.
  47.             linkage section.
  48.            
  49.             01 inNode   object reference IExpression.
  50.          
  51.           procedure division using inNode.
  52.          
  53.             set  rightNode     to  inNode
  54.              
  55.         end method SetRightNode.  
  56.          method-id. GetLeftNode.
  57.           data division.
  58.             linkage section.
  59.            
  60.             01 outNode   object reference IExpression.
  61.          
  62.           procedure division returning outNode.
  63.          
  64.             set  outNode      to  leftNode
  65.                
  66.         end method GetLeftNode.  
  67.          method-id. GetRightNode.
  68.           data division.
  69.             linkage section.
  70.            
  71.             01 outNode   object reference IExpression.
  72.          
  73.           procedure division returning outNode.
  74.          
  75.             set  outNode       to  rightNode
  76.                
  77.         end method GetRightNode.  
  78.         method-id. EvaluateExpression as "Evaluate".
  79.           data division.
  80.             linkage section.
  81.            
  82.             01 inContext  object reference ClassContext.
  83.             01 outValue   type SystemDouble.
  84.          
  85.           procedure division using inContext returning outValue.
  86.          
  87.             *> Implemented because NetCobol.Net does not supports Abstract classes
  88.                
  89.         end method EvaluateExpression.
  90.     end object.
  91.   END CLASS NonTerminalExpression.
Imágenes Adjuntas
 



NORMAS DEL FORO - para garantizar el buen funcionamiento del Foro.
¿Te han ayudado? NO TE OLVIDES de darle a
¿Quieres dirigirte a alguien en tu post? Notifícale haciendo clic en su Nick
Kuk is offline   Responder Con Cita