赞
踩
ExpressRunner.execute()
实质分成两个步骤
//(1)将规则脚本编译成指令集:string -> InstructionSet
parseResult = this.parseInstructionSet(expressString);
//(2)指令集绑定上下文进行执行:InstructionSet + context ->Object
InstructionSetRunner.executeOuter(this,parseResult,this.loader,context, errorList,isTrace,false,aLog,false)
通过设置ExpressRunner.execute()
的isCache
入参,
编译得到的指令集就会以Map的形式,被缓存在ExpressRunner实例的内部对象里
//ExpressRunner.java
/**
* 解析一段文本,生成指令集合
*/
public InstructionSet parseInstructionSet(String text)
{
//(1)token分解
Word[] words = WordSplit.parse(this.nodeTypeManager.splitWord,express);
//(2)token解析
List<ExpressNode> tempList = this.transferWord2ExpressNode(rootExpressPackage,words,selfDefineClass,true);
//(3)匹配AST语法树
QLMatchResult result = QLPattern.findMatchStatement(this.nodeTypeManager, this.nodeTypeManager.findNodeType("PROGRAM").getPatternNode(), tempList,0);
result.getMatchs().get(0).buildExpressNodeTree();
ExpressNode root =(ExpressNode)result.getMatchs().get(0).getRef();
resetParent(root,null);
//(4)生成指令集合
InstructionSet result = createInstructionSet(root, "main");
}
sum=0;for(i=0;i<10;i=i+1){sum=sum+i;};return sum;
token分解:
使用分隔符号(空格、换行、位操作、四则运算、Boolean运算符号)将String
分解为Word[]:words = "sum",”=“,”0“,”;“,"for","(","i",......
token解析:
Word
的值判断类型:数字、字符串、boolean、关键字等ExpressNode
匹配AST语法树:
根据KeyWordDefine4Java.java
定义的推导文法,递归的构造出一棵AST(抽象语法树)
ExpressNode
包含父节点、子节点的List、type(Block、Break、Cast、CallFunction、If、In)KeyWordDefine4Java
中定义了如何split、有哪些关键词等等生成指令集合
InstructionoFactory
将ExpressNode
转换为InstructionSet
,InstructionSet
有不同的excute
方法1:LoadAttr:sum
2:LoadData 0
3:OP : = OPNUMBER[2]
4:openNewArea
5:clearDataStack
6:LoadAttr:i
7:LoadData 0
8:OP : = OPNUMBER[2]
9:clearDataStack
10:LoadAttr:i
11:LoadData 10
12:OP : < OPNUMBER[2]
13:GoToIf[false,isPop=true] +13
......
29:return [value]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。