macro.rb

Path: macro.rb
Last Update: Sun Feb 05 17:07:57 JST 2006

Fortran90 code generator as macro

Authors:Yasuhiro MORIKAWA
Version:$Id: macro.rb,v 1.6 2006/02/05 08:07:57 morikawa Exp $
Tag Name:$Name: gt4f90io-20070628 $
Copyright:Copyright (C) GFD Dennou Club, 2005. 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 のコードを記述できることが期待されます.

Methods

Public Instance methods

Output (:), or (:,:) or … in array definition

Usage

   array_colon(num)

 [JA]

配列定義の際の (:), (:,:), … を出力する関数

使い方

   array_colon(num)

 num に正の整数を代入する. 0 ならば何も出力せず, 1 ならば (:),
 2 ならば (:,:), ... と出力する.

[Source]

     # File macro.rb, line 208
208: def array_colon(num)
209:   return unless num
210:   int = num.to_i
211:   return if int < 1
212:   return "(:)" if int < 2
213:   int -= 1
214:   body = "(:"
215:   int.times{
216:     body << ",:"
217:   }
218:   body << ")"
219:   return body
220: end

Function as "for" for macro

Usage

   foreach(string-1, first, last, string-2)

 [JA]

マクロ的に利用できる for 関数

使い方

   forloop(string-1, first, last, string-2)

 first, last には整数を代入する.
 string-2 を last - first 回文だけ反復して返す.
 その際, string-2 内で string-1 と同じ文字列を数値に置換する.
 置換される数値は first から last へと 1 つづつ増加する数値である.

[Source]

     # File macro.rb, line 176
176: def foreach(str, *words)
177:   return if words.size < 2
178:   body = "#{words[words.size - 1]}"
179:   words.pop
180:   rbody = ""
181:   repeated = nil
182:   words.each{ |word|
183:     rbody << "\n" if repeated
184:     rbody << body.sub(/^\n/, '').gsub(/#{str}/, word.to_s).chomp
185:     repeated = true
186:   }
187:   return rbody.chomp
188: end

An imitation of the forloop function in m4-doc.

Usage

   forloop(string-1, first, last, string-2)

 [JA]

m4-doc で紹介される forloop 関数を模した関数

使い方

   forloop(str, first, last, body)

 first, last には整数を代入する.
 body を last - first 回文だけ反復して返す.
 その際, body  内で str と同じ文字列を数値に置換する.
 置換される数値は first から last へと 1 つづつ増加する数値である.

[Source]

     # File macro.rb, line 145
145: def forloop(str, first, last, body)
146:   rbody = ""
147:   repeated = nil
148:   for i in first..last
149:     rbody << "\n" if repeated
150:     rbody << body.sub(/^\n/, '').gsub(/#{str}/, i.to_s).chomp
151:     repeated = true
152:   end
153:   return rbody.chomp
154: end

An imitation of the ifelse function of m4.

Usage

   ifelse(string-1, string-2, equal, [not-equal])
   ifelse(string-1, string-2, equal, [string-3, string-4, equal, ... [not-equal]])

 [JA]

m4 の ifelse 関数を模した関数

使い方

    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 が返る. 等しくない場合には
 さらに後ろの引数に制御を渡す.

[Source]

     # File macro.rb, line 84
 84: def ifelse(*all)
 85:   entire = Array.new
 86:   count = -1
 87:   one_set = Array.new(3)
 88:   body = ""
 89:   # 正規化 (?)
 90:   # 3 の倍数 + 2 の場合, 最後の1つは捨てる
 91:   if all.size.modulo(3) == 2 then
 92:     all.pop
 93:   end
 94:   # 3 の倍数 + 1 の場合, 最後の1つは別途持っておく
 95:   lastitem = Array.new
 96:   if all.size.modulo(3) == 1 then
 97:     lastitem << all.pop
 98:   end
 99:   # データの整理
100:   all.each{ |item|
101:     count += 1
102:     one_set[count.modulo(3)] = item
103:     if count.modulo(3) == 2 then
104:       entire << one_set.clone
105:       one_set.clear
106:       next
107:     end
108:   }
109:   if !lastitem.empty? then
110:     entire << lastitem
111:   end
112:   entire.each{ |set|
113:     if set.size == 1 then
114:       body << set[0].sub(/^\n/, '').chomp
115:       break
116:     end
117:     if set[0] == set[1] then
118:       body << set[2].sub(/^\n/, '').chomp
119:       break
120:     end
121:   }
122:   return body.chomp
123: end

Local variable section in emacs

[Source]

    # File macro.rb, line 48
48: def rb2f90_emacs_readonly
49:   mess = "!Local Variables:\n"
50:   mess << "!mode: f90\n!buffer-read-only: t\n!End:\n"
51:   return mess
52: end

Header Comment

[Source]

    # File macro.rb, line 24
24: def rb2f90_header_comment
25:   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"
26: 
27:   return mess
28: end

[Validate]