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 :) 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.

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 :) 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.DownloadDownload Full Project

Enjoy!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值