Class: TCFlow::Function

Inherits:
Object show all
Defined in:
lib/tecsflow/classes.rb

Constant Summary collapse

@@Function_list =
@name

Symbol

@param_var::{ Symbol=>true } @local_var::{ Symbol=>true } @global_var::{ Symbol=>[n_access, n_write] } @call_func::{ Symbol=>Function }

@b_ref

Bool : true if referenced

@b_printed

Bool : true if printed already

@locale::[file_name, lineno, col]

{}
@@param_list =

String(name) => Function | Integer(0)

nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale) ⇒ Function

Returns a new instance of Function.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tecsflow/classes.rb', line 54

def initialize locale
  @param_var = {}
  @local_var = {}
  @global_var = {}
  @call_func = {}
  @b_ref = false
  @b_printed = false
  @locale = locale

  @pre_name = [0, "", 0, ""]
  @pre_printed = false
  @pfunc = ""
#    @pp = ""

  # param_list は先に解釈されている
  if @@param_list
    @@param_list.each { |param|
      new_param param
    }
  end
end

Class Method Details

.all_printed?Boolean

Returns:

  • (Boolean)


233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/tecsflow/classes.rb', line 233

def self.all_printed? 
  # 参照されているが未印刷のものを探す
  b_all_printed = true
  @@Function_list.each{ |name,func|
    if func.instance_of? Function
    	if func.is_printed? == false
       b_all_printed = false
    	  break
    	# else
     # print "#{name} : printed\n"
    	end
    end
  }
  return b_all_printed
end

.dump_funclist(dump_file_name) ⇒ Object



379
380
381
382
383
# File 'lib/tecsflow/classes.rb', line 379

def self.dump_funclist dump_file_name
  p "Dump Funclist"
  mar = Marshal.dump @@Function_list
  File.write( dump_file_name, mar )
end

.list_all_functionsObject



391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/tecsflow/classes.rb', line 391

def self.list_all_functions
  #print "==== function list\n"
  funcs = @@Function_list.keys.sort{ |a,b| a.to_s <=> b.to_s }
  funcs.each { |name|
    if @@Function_list[name].instance_of? Function
      func = @@Function_list[name]
      func.print_func
    else
      #printf( "%-20s: %-s\n", name, "not defined" )
    end
  }
end

.load_funclist(dump_file_name) ⇒ Object



385
386
387
388
389
# File 'lib/tecsflow/classes.rb', line 385

def self.load_funclist dump_file_name
  mar_in = File.read( dump_file_name )
  @@Function_list = Marshal.load( mar_in )
  return @@Function_list
end

.param_list(param_list) ⇒ Object

関数が new される前に param_list が解釈されるため



89
90
91
# File 'lib/tecsflow/classes.rb', line 89

def self.param_list param_list
  @@param_list = param_list
end

ツリー表示



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/tecsflow/classes.rb', line 149

def self.print_all_functions

  @@Function_list.each{ |name, func|
    if func.instance_of? Function
  	  func.print_all_functions(0, "")
    end
  }

  if ! self.all_printed?
    # print "===== recursive functions\n"
    funcs.each {|name|
    	func = @@Function_list[name]
	if func.instance_of? Function
 func.print_all_functions(-1, "")
	end
    }
  end
end

サマリー表示



279
280
281
282
283
284
285
286
287
# File 'lib/tecsflow/classes.rb', line 279

def self.print_summarize
  self.update
  # print "==== summary mode (all functions called from top level function)\n"
  self.print_summarize0 false
  if ! self.all_printed?
    # print "===== recursive functions\n"
    self.print_summarize0 true
  end
end


289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/tecsflow/classes.rb', line 289

def self.print_summarize0 b_unprinted
  funcs = @@Function_list.keys.sort{ |a,b| a.to_s <=> b.to_s }
  funcs.each {|name|
    func = @@Function_list[name]
    if func.instance_of? Function
	if ! b_unprinted
 next if ! func.is_top?
	else
 next if func.is_printed?
	end

	@@ref_function_list = {}
	@@ref_global_var_list = {}
	func.summarize
	#print name
	locale = func.get_locale
	#printf( "()   <%s:%d>\n", locale[0], locale[1] )
	if @@ref_function_list.length > 0 
 #print "  Calling Function Summary:\n"
 ref_funcs = @@ref_function_list.keys.sort{ |a,b| a.to_s <=> b.to_s }
 ref_funcs.each { |name|
   function = @@ref_function_list[name]
   #print "    "
   #print name
   if function.instance_of? Function
     locale = function.get_locale
     #printf( "()   <%s:%d>\n", locale[0], locale[1] )
   else
     #print "()    not defined\n"
   end
 }
	end
	if @@ref_global_var_list.length > 0
 #print "  Global Vars Summary:\n"
 vars = @@ref_global_var_list.keys.sort{ |a,b| a.to_s <=> b.to_s }
 vars.each{ |name|
   count_array = @@ref_global_var_list[name]
   #print "    "
   #print name
   #printf( "   [R:%dW:%d]\n", count_array[0]-count_array[1], count_array[1] )
 }
	end
    end
  }
end

.updateObject

@call_func を update する

完全になるように更新する サマリー表示の場合には必要だが、ツリー表示の場合、処理の途中で更新するので不要 summarize の中でやると分かりにくくなるので、独立させた



254
255
256
257
258
259
260
261
262
263
# File 'lib/tecsflow/classes.rb', line 254

def self.update
#  print "updating: "
  @@Function_list.each{ |name,func|
    # print name, ", "
    if func.instance_of? Function
    	func.update
    end
  }
#  print "\n"
end

Instance Method Details

#get_call_funcsObject

Funciton#get_call_funcs ハッシュを返す (呼出し関数名=>Function)



229
230
231
# File 'lib/tecsflow/classes.rb', line 229

def get_call_funcs
  @call_func
end

#get_localeObject



375
376
377
# File 'lib/tecsflow/classes.rb', line 375

def get_locale
  @locale
end

#get_nameObject



417
418
419
# File 'lib/tecsflow/classes.rb', line 417

def get_name
  @name
end

#is_local_var?(var) ⇒ Boolean

Returns:

  • (Boolean)


357
358
359
360
361
362
363
364
365
# File 'lib/tecsflow/classes.rb', line 357

def is_local_var? var
 #    p "#{var}, #{@local_var[ var ]}, #{@param_var[ var ]}"
##ここで@local_var[var]が真にならない
  if @local_var[ var ] || @param_var[ var ]
    return true #local or param
  else
    return false #global
  end
end

#is_printed?Boolean

Returns:

  • (Boolean)


371
372
373
# File 'lib/tecsflow/classes.rb', line 371

def is_printed?
  @b_printed
end

#is_top?Boolean

Returns:

  • (Boolean)


367
368
369
# File 'lib/tecsflow/classes.rb', line 367

def is_top?
  @b_ref == false
end

#new_local(local) ⇒ Object



98
99
100
101
# File 'lib/tecsflow/classes.rb', line 98

def new_local local
  # print "new_local: #{local}\n"
  @local_var[ local ] = local
end

#new_param(param) ⇒ Object



93
94
95
96
# File 'lib/tecsflow/classes.rb', line 93

def new_param param
  # print "new_param: #{param}\n"
  @param_var[ param ] = param
end

ツリー表示する

level

Integer: 0: top_level のものを表示する, -1: 未表示のものを表示する

level = 0, -1 の場合、レベルに一致した関数だけが表示される それ以外の場合、再帰的にツリー表示が行われる



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/tecsflow/classes.rb', line 172

def print_all_functions (level)

  # TOP レベルは参照されていないものだけとする
  return if level == 0 && @b_ref == true

  # @b_ref だが未印刷のもの(循環参照されている)
  if level == -1
    return if @b_printed
    level = 0
  end

  indent = "    " * level

  if @b_printed
    # 印刷済みは再帰的に印刷しない
    print indent
    print @name
    print " : printed\n"
    return
  end
  @b_printed = true

  #変数一覧印字 ##########
  if @global_var.length > 0 
    #print indent
    #print "  Global Vars: \n"
    @global_var.each { |name, count_array|
	#print indent
	#print "    "
	#print name
	#printf( "   [R:%dW:%d]\n", count_array[0]-count_array[1], count_array[1] )
    }
  end

  #関数一覧印字 #############
  if @call_func.length > 0 
    print indent
    print "  Calling Function: \n"
    cfuncs = @call_func.keys.sort{ |a,b| a.to_s <=> b.to_s } #名前順ソート
    # cfuncs = @call_func.keys #関数呼び出し順  hash の順序は保障されていない【Matz版は保障されるらしい)
    cfuncs.each{ |name|
     func = @call_func[ name ]
     if func.instance_of? Function
        print indent, "    ", name, "\n"
       func.print_all_functions(level+1) #順番変更するとrecursive functionになる?
    	else
       print indent
       print "    " ##
       print name
       print "() : not defined\n"
    	end
    }
  end
end


408
409
410
411
412
413
414
415
# File 'lib/tecsflow/classes.rb', line 408

def print_func
  #if @ref_func
  #  ref_func = @ref_func.get_name
  #else
    ref_func = ""
  #end
  #printf( "%-20s: %-30s line: %d   %s\n", @name, @locale[0], @locale[1], ref_func )
end

#ref_func(func_name) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/tecsflow/classes.rb', line 128

def ref_func func_name
  func_name = func_name.to_sym
#p func_name ###150216 この時点では表記順
  if @@Function_list[func_name].instance_of? Function
    function = @@Function_list[func_name]
    function.referenced self
    @call_func[ func_name ] = function
    # print "call func: #{func_name} : defined\n" 
  else
    @@Function_list[func_name] = 0
    @call_func[ func_name ] = 0
    # print "call func: #{func_name} : not defined\n"
  end
end

#ref_var(var, b_write = false) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tecsflow/classes.rb', line 103

def ref_var var, b_write = false
#p var
#p @local_var
#p @global_var
  if @local_var[ var ]
    # print "ref local: #{var}\n"
  elsif @param_var[ var ]
    # print "ref param: #{var}\n"
  else
    # print "ref global: #{var}\n"

    #変数のread/write回数をカウント ################
    count_array = @global_var[ var ]
    if count_array == nil
    	count_array = [ 0, 0 ]
     @global_var[ var ] = count_array
    end
    if b_write
     count_array[1] += 1
    else
     count_array[0] += 1
    end
  end
end

#referenced(ref_func) ⇒ Object



143
144
145
146
# File 'lib/tecsflow/classes.rb', line 143

def referenced ref_func
  @b_ref = true
  @ref_func = ref_func
end

#set_name(name) ⇒ Object

関数をパースする最後で名前が確定



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/tecsflow/classes.rb', line 76

def set_name name    # 関数をパースする最後で名前が確定
#    print "^^^ function: #{name}\n"
  @name = name.to_sym
  if @@Function_list[@name] == 0
    @b_ref = true
  end
  @@Function_list[@name] = self
  @@param_list = nil
#p @name ###150216
#    print "test:", @name, "\n"
end

#set_printedObject



404
405
406
# File 'lib/tecsflow/classes.rb', line 404

def set_printed
  @b_printed = true
end

#summarizeObject



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/tecsflow/classes.rb', line 335

def summarize
  @global_var.each { |name, count_array|
    count_array2 = @@ref_global_var_list[name]
    if count_array2
     count_array2[0] += count_array[0]
     count_array2[1] += count_array[1]
    else
    	@@ref_global_var_list[name] = count_array.clone
    end
  }
  @call_func.each { |name, function|
    next if @@ref_function_list[name]     # カウント済み
    @@ref_function_list[name] = function
    if function.instance_of? Function
     function.summarize
    end
  }

  @b_printed = true
end

#updateObject



265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/tecsflow/classes.rb', line 265

def update
  @call_func.each { |name, func|
    if func.instance_of? Function   # 関数名重複の場合、前のものを忘れない方が、よりよい(恐らく)
    	next
    end
    func = @@Function_list[name]
    if func.instance_of? Function
    	@call_func[name]=func
     func.referenced self
    end
  }
end