コンパイラ作成(55) バグ修正
バグ
いくつかテストをしてたらバグが見つかったよ。
// セミコロンがない int main() { sub() sub(); } int sub() { printf("sub\n"); return 0; }
modify_el
ここを修正。
# elの変形 # [2, +, 3, *, 5] # =>[2, +, [3, *, 5]] def modify_el(el, opl, assoc = :l_to_r) mel = [] tel = [] tel << el.shift loop do if el == [] then break end if el[0].kind_of?(Array) then perror "expected operator or ';'" end if el[0].kind != TK::SYMBOL then perror "expected operator or ';'" end if opl.include? el[0].str then tel << el.shift tel << el.shift else if tel.size == 1 then mel << tel[0] else mel << tel end tel = [] mel << el.shift tel << el.shift end end if assoc == :r_to_l then tel = rtol_el tel end if tel.size == 1 then mel << tel[0] else mel << tel end return mel end
チェックが甘かったよ。演算子が来るべきところで違うもの来ちゃったら、ちゃんとコンパイルエラーにするようにした。修正は二行追加しただけ。
動作テスト
どうかな。
~/myc$ myc err22.myc err22.myc:5:10 error: expected operator or ';' ~/myc$
直ったよ。他にもセミコロンが抜けてるケースをテストして今日は終了。