/**
* 运算符状态
*
* @param char // 状态字符
* @param subStateIndex // 后继状态数组的索引
* @param subStateCount // 后继状态个数
* @param index // 运算符索引
* @param state // 状态
* @param symbol // 状态
*/
case class OperatorState(char:Char,subStateIndex:Int,subStateCount:Int,index:Int,state:Int,symbol:String){
}
object OperatorStateMachineConfig{
val state0:List[OperatorState]=List[OperatorState](
OperatorState('-',1,1,0,0,"-"),
OperatorState('+',0,2,1,0,"+"),
OperatorState('=',0,0,2,0,"=")
)
val state1:List[OperatorState]=List[OperatorState](
OperatorState('+',0,1,3,1,"++"), // ++
OperatorState('=',0,0,4,1,"+="), // +=
OperatorState('=',0,0,5,1,"-=") // -=
)
val state2:List[OperatorState]=List[OperatorState](
OperatorState('+',0,0,6,2,"+++") // +++
)
}
def matchState(operators:Array[Char]):OperatorState={
def matchOperator(operators:Array[Char],opIndex:Int,group:Int,index:Int,stateCount:Int,operatorState: OperatorState):OperatorState={
var result=operatorState
val states=group match{
case 0 => OperatorStateMachineConfig.state0
case 1 => OperatorStateMachineConfig.state1
case 2 => OperatorStateMachineConfig.state2
}
for(i <- index until (index+stateCount) if opIndex<operators.size){
val state=states(i)
val operator=operators(opIndex)
operator match{
case eqState if operator==state.char =>{
state.subStateCount match{
case 0 =>result= state
case _ =>result= matchOperator(operators,opIndex+1,group+1,state.subStateIndex,state.subStateCount,operatorState)
}
}
case _ =>{} // do nothing
}
}
result
}
matchOperator(operators,0,0,0,OperatorStateMachineConfig.state0.size,null)
}
def main(args: Array[String]) {
val command="+++"
val operators=command.toCharArray
var states=List[OperatorState]()
states=states :+ matchState(operators)
println(states)
}
}
网友评论