summaryrefslogtreecommitdiff
path: root/ipl/procs/calls.icn
blob: 6ebb8a1a129fc9c67f773acec285d6d111ad9072 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
############################################################################
#
#	File:     calls.icn
#
#	Subject:  Procedures for calls as objects
#
#	Author:   Ralph E. Griswold
#
#	Date:     March 25, 2002
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  These procedures deal with procedure invocations that are encapsulated
#  in records.
#
############################################################################
#
#  Links:  ivalue, procname
#
############################################################################

invocable all

link ivalue
link procname

record call(proc, args)

#
#  Invoke a procedure with a argument list from a call record.

procedure invoke(call)

   suspend call.proc ! call.args

end


#
#  Produce a string images of a call

procedure call_image(call)
   local args

   args := ""

   every args ||:= !call.args || ", "

   return procname(call.proc) || "(" || args[1:-2] || ")"

end

  
#  Make a call record from a string that looks like an invocation.
#  What the arguments can be is limited to the capabilities of ivalue.

procedure make_call(s)
   local arg, args, result

   s ? {
      result := call(proc(tab(upto('(')))) | fail
      move(1)
      result.args := make_args(tab(-1))
      }

   return result

end

#  Make an argument list from a comma-separated string

procedure make_args(s)
   local args, arg

   args := []

   s ? {
      while arg := tab(upto(',') | 0) do {
         put(args, ivalue(arg)) | fail
         move(1) | break
         }
      }

   return args

end

#  Produce a string of Icon code to construct a call record.

procedure call_code(s)
   local code, arg, result

   s ? {
      result := "call(" || tab(upto('(')) || ", [" | fail
      move(1)
      while arg := tab(upto(',)')) do {
         result ||:= ivalue(arg) || ", " | fail
         move(1) | break
         }
      }

   return result[1:-2] || "])"

end

#  Write a table of calls to a file.  The file format is
#
#	name=proc:arg1,arg2,arg3, ... argn,
#
#  where name is the name associated with the call, proc is the
#  procedure, and arg1, arg2, arg3, ... argn are the arguments.
#  Note the trailing comma.

procedure write_calltable(T, p, f)
   local name

   every name := key(T) do {
      writes(f, name, "=")
      writes(f, procname(p), ":")
      every writes(f, image(!T[name]), ",")
      }

   write(f)

   return

end
   
#  read a call table file into a table

procedure read_calltable(f)
   local T, line, p, args

   T := table()

   every line := read(f) do
      line ? {
         name := tab(upto('="')) | fail
         move(1)
         p := tab(upto(':')) | fail
         move(1)
         args := []
         while put(args, ivalue(tab(upto(',')))) do
             move(1)
         T[name] := call(proc(p), args) | fail
         }

   return T

end