Design Patterns in ActionScript-Interpreter

In web programming, we often use regular expression for validating the e-mail address or phone number. Regular expression is a powerful tool in validating the specific format field. However, the interpretation of regular expression is not an easy job.

In the GoF’s design patterns, there is a corresponding pattern named Interpreter.

 

Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

–By GOF BOOK

Maybe, from the intent, you’ll know that you won’t touch this pattern in your future career Design Patterns in ActionScript-Interpreter That’s what I think.

To illustrate this pattern, I’ll show you a demo for calculating an arithmetic expression. And the expression will only support addition and subtraction, so the expression may looks like “11 + 2 + 3 - 4 – 5 + 8”.

And the grammar we define as follows:

GeneralExpression => AddExpression | SubExpression | NumberExpression

AddExpression => GeneralExpression + NumberExpression

SubExpression => GeneralExpression - NumberExpression

NumberExpression => 0 | ([1-9][0-9]*)

Note: “|” means or.

Now, the expression “11 + 2 + 3 - 4 – 5 + 8”’s grammar tree can be describe as follows.

Design Patterns in ActionScript-Interpreter

As you see, we can calculate the expression by parsing the expression from top to down. Now, we need to design the classes.

The class diagram is as follows.

Design Patterns in ActionScript-Interpreter

When we get an expression, it’ll be passed to the GeneralExpression, then the interpret method will be called. The source code is as follows.

  1. public function interpret () : int  
  2. {  
  3. if ( exp . lastIndexOf ( " + " ) > exp . lastIndexOf ( " - " ))  
  4. return   new AddExpression ( exp ) . interpret () ;
  5. else   if ( exp . lastIndexOf ( " + " ) < exp . lastIndexOf ( " - " ))  
  6. return   new SubExpression ( exp ) . interpret () ;
  7. else  
  8. return   new NumberExpression ( exp ) . interpret () ;
  9. }

And the interpret method of SubExpression or AddExpression maybe called, and here is the source code of interpret method in SubExpression.

  1. public function interpret () : int  
  2. {  
  3. var   index : int = exp . lastIndexOf ( SUB ) ;
  4. var   generalExp : String = exp . substr ( 0 , index ) ;
  5. var   numberExp : String = exp . substr ( index + 1 , exp . length ) ;
  6.  
  7. return   new GeneralExpression ( generalExp ) . interpret ()  
  8. - new   NumberExpression ( numberExp ) . interpret () ;
  9. }

In the interpret method of each class will parsing the expression from top to down, then calculate the value and returns it.

As you see, this pattern is, eh, all about the complier Design Patterns in ActionScript-Interpreter I don’t like this pattern, because it’s uneasy to implement when the grammar is not so simple. Maybe it’s all because I haven’t learned the complier principle well.Design Patterns in ActionScript-InterpreterDownload Full Project

Enjoy!