我寫了用於測試的小型M4腳本(test.m4
):爲什麼評估ifelse宏的「錯誤」表達?
define(`test', `ifelse(`$#', `1', `$1', test(shift([email protected])))')
test(`arg1', `arg2')
與m4 test.m4 -t test -de
運行它。輸出是
m4trace: -1- test -> ifelse(`2', `1', `arg1', test(shift(`arg1',`arg2')))
m4trace: -2- test -> ifelse(`1', `1', `arg2', test(shift(`arg2')))
m4trace: -3- test -> ifelse(`1', `1', `', test(shift(`')))
m4trace: -4- test -> ifelse(`1', `1', `', test(shift(`')))
.
.
.
直到由於超出遞歸限制而中止執行。我想知道爲什麼這樣,因爲實際上1
和1
應該比較相等,並且if else
宏應評估爲`'
。
不過,我有創新的想法,把[not-equal]
到引號,所以宏是這樣的:
define(`test', `ifelse(`$#', `1', `$1', `test(shift([email protected]))')')
test(`arg1', `arg2')
就萬事大吉了,它的工作就像一個魅力(即arg2
是一起打印出來一個領先的換行符)。
輸出(具有相同的調用參數):
NL
m4trace: -1- test -> ifelse(`2', `1', `arg1', `test(shift(`arg1',`arg2'))')
m4trace: -1- test -> ifelse(`1', `1', `arg2', `test(shift(`arg2'))')
arg2
(NL
代表 「新行」)。
我的結論是:即使兩個比較的字符串實際上是相等的,但預處理器仍然評估[not-equal]
分支。
這是否有任何特定的用途?國際海事組織,這只是不直觀的。或者我錯過了什麼?
-t test
接通調試跟蹤用於在宏test
。 -de
將調用宏的定義添加到調試輸出中。