Path: | ../script/rb2f90/lib-rb2f90-macro.rb |
Last Update: | Wed Mar 25 17:17:36 +0900 2009 |
Authors: | Yasuhiro MORIKAWA |
Version: | $Id: lib-rb2f90-macro.rb,v 1.1 2009-03-25 08:17:36 morikawa Exp $ |
Tag Name: | $Name: gtool5-20101228-1 $ |
Copyright: | Copyright (C) GFD Dennou Club, 2005-2009. All rights reserved. |
License: | See ../COPYRIGHT |
These functions are used to generate f90 code files from Ruby code files. They are expected to help ruby code approximates f90 code for as long as possible.
[JAPANESE]
これらの関数は Ruby で記述されたファイルから F90 ファイルを生成 するための関数です. これらの関数により, できるだけ F90 コードに 近い形で Ruby のコードを記述できることが期待されます.
array_colon(num) [JA]
array_colon(num) num に正の整数を代入する. 0 ならば何も出力せず, 1 ならば (:), 2 ならば (:,:), ... と出力する.
# File ../script/rb2f90/lib-rb2f90-macro.rb, line 209 209: def array_colon(num) 210: return unless num 211: int = num.to_i 212: return if int < 1 213: return "(:)" if int < 2 214: int -= 1 215: body = "(:" 216: int.times{ 217: body << ",:" 218: } 219: body << ")" 220: return body 221: end
foreach(string-1, first, last, string-2) [JA]
forloop(string-1, first, last, string-2) first, last には整数を代入する. string-2 を last - first 回文だけ反復して返す. その際, string-2 内で string-1 と同じ文字列を数値に置換する. 置換される数値は first から last へと 1 つづつ増加する数値である.
# File ../script/rb2f90/lib-rb2f90-macro.rb, line 177 177: def foreach(str, *words) 178: return if words.size < 2 179: body = "#{words[words.size - 1]}" 180: words.pop 181: rbody = "" 182: repeated = nil 183: words.each{ |word| 184: rbody << "\n" if repeated 185: rbody << body.sub(/^\n/, '').gsub(/#{str}/, word.to_s).chomp 186: repeated = true 187: } 188: return rbody.chomp 189: end
forloop(string-1, first, last, string-2) [JA]
forloop(str, first, last, body) first, last には整数を代入する. body を last - first 回文だけ反復して返す. その際, body 内で str と同じ文字列を数値に置換する. 置換される数値は first から last へと 1 つづつ増加する数値である.
# File ../script/rb2f90/lib-rb2f90-macro.rb, line 146 146: def forloop(str, first, last, body) 147: rbody = "" 148: repeated = nil 149: for i in first..last 150: rbody << "\n" if repeated 151: rbody << body.sub(/^\n/, '').gsub(/#{str}/, i.to_s).chomp 152: repeated = true 153: end 154: return rbody.chomp 155: end
ifelse(string-1, string-2, equal, [not-equal]) ifelse(string-1, string-2, equal, [string-3, string-4, equal, ... [not-equal]]) [JA]
ifelse(string-1, string-2, equal, [not-equal]) ifelse(string-1, string-2, equal, [string-3, string-4, equal, ... [not-equal]]) string-1 と string-2 が等しい場合, equal が返る. 等しくない場合には not-equal が返る. not-equal が指定されない場合には何も返らない. 後ろにさらに条件分岐を付け加えることも可能である. 3 つ目の例では, string-3 と 4 が指定されている. string-1 と 2 が異なる場合, 次に string-3 と string-4 が比較され, 等しい場合にはその後ろの equal が返る. 等しくない場合には さらに後ろの引数に制御を渡す.
# File ../script/rb2f90/lib-rb2f90-macro.rb, line 85 85: def ifelse(*all) 86: entire = Array.new 87: count = -1 88: one_set = Array.new(3) 89: body = "" 90: # 正規化 (?) 91: # 3 の倍数 + 2 の場合, 最後の1つは捨てる 92: if all.size.modulo(3) == 2 then 93: all.pop 94: end 95: # 3 の倍数 + 1 の場合, 最後の1つは別途持っておく 96: lastitem = Array.new 97: if all.size.modulo(3) == 1 then 98: lastitem << all.pop 99: end 100: # データの整理 101: all.each{ |item| 102: count += 1 103: one_set[count.modulo(3)] = item 104: if count.modulo(3) == 2 then 105: entire << one_set.clone 106: one_set.clear 107: next 108: end 109: } 110: if !lastitem.empty? then 111: entire << lastitem 112: end 113: entire.each{ |set| 114: if set.size == 1 then 115: body << set[0].sub(/^\n/, '').chomp 116: break 117: end 118: if set[0] == set[1] then 119: body << set[2].sub(/^\n/, '').chomp 120: break 121: end 122: } 123: return body.chomp 124: end
# File ../script/rb2f90/lib-rb2f90-macro.rb, line 49 49: def rb2f90_emacs_readonly 50: mess = "!Local Variables:\n" 51: mess << "!mode: f90\n!buffer-read-only: t\n!End:\n" 52: return mess 53: end
# File ../script/rb2f90/lib-rb2f90-macro.rb, line 25 25: def rb2f90_header_comment 26: mess = "! *** Caution!! ***\n!\n! This file is generated from \"\#{File.basename($0.to_s)}\" by Ruby \#{RUBY_VERSION}.\n! Please do not edit this file directly.\n!\n! [JAPANESE]\n!\n! \242\250\242\250\242\250 \303\355\260\325!!! \242\250\242\250\242\250\n!\n! \244\263\244\316\245\325\245\241\245\244\245\353\244\317 \"\#{File.basename($0.to_s)}\" \244\253\244\351 Ruby \#{RUBY_VERSION}\n! \244\313\244\350\244\303\244\306\274\253\306\260\300\270\300\256\244\265\244\354\244\277\245\325\245\241\245\244\245\353\244\307\244\271.\n! \244\263\244\316\245\325\245\241\245\244\245\353\244\362\304\276\300\334\312\324\275\270\244\267\244\336\244\273\244\363\244\350\244\246\244\252\264\352\244\244\303\327\244\267\244\336\244\271.\n!\n" 27: 28: return mess 29: end