format method

int format(
  1. ClipParam param,
  2. bool strToVal
)

Implementation

int format( ClipParam param, bool strToVal ){
	int ret;

	if( _top != null ){
		if( _top!._code == ClipGlobal.codeSe ){
			return _formatSe( param, strToVal );
		} else if( _top!._code == ClipGlobal.codeStatement ){
			switch( _top!._token ){
			case ClipGlobal.statStart:
			case ClipGlobal.statEnd:
			case ClipGlobal.statEndInc:
			case ClipGlobal.statEndDec:
			case ClipGlobal.statEndEq:
			case ClipGlobal.statEndEqInc:
			case ClipGlobal.statEndEqDec:
			case ClipGlobal.statCont:
			case ClipGlobal.statContinue2:
			case ClipGlobal.statBreak2:
			case ClipGlobal.statReturn2:
			case ClipGlobal.statReturn3:
				return _formatSe( param, strToVal );
			case ClipGlobal.statDo:
			case ClipGlobal.statEndWhile:
			case ClipGlobal.statNext:
			case ClipGlobal.statEndFunc:
			case ClipGlobal.statElse:
			case ClipGlobal.statEndIf:
			case ClipGlobal.statDefault:
			case ClipGlobal.statEndSwi:
			case ClipGlobal.statBreakSwi:
			case ClipGlobal.statContinue:
			case ClipGlobal.statBreak:
				if( _top!._next != null ){
					return ClipGlobal.procWarnDeadToken;
				}
				return ClipGlobal.noErr;
			}
		}
	}

	// 演算子の優先順位に従って括弧を付ける
	if( (ret = _format( _top, param, strToVal )) != ClipGlobal.noErr ){
		return ret;
	}

	// 括弧を整える
	int i = 0;
	ClipTokenData? cur = _top;
	while( cur != null ){
		switch( cur._code ){
		case ClipGlobal.codeTop:
			i++;
			break;
		case ClipGlobal.codeEnd:
			i--;
			for( ; i < 0; i++ ){
				_ins( _top, param, String.fromCharCode( ClipGlobal.codeTop ), 1, strToVal );
			}
			break;
		}
		cur = cur._next;
	}
	for( ; i > 0; i-- ){
		add( param, String.fromCharCode( ClipGlobal.codeEnd ), 1, strToVal );
	}

	return ClipGlobal.noErr;
}