File: record.gi

package info (click to toggle)
gap 4.15.1-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 110,212 kB
  • sloc: ansic: 97,261; xml: 48,343; cpp: 13,946; sh: 4,900; perl: 1,650; javascript: 255; makefile: 252; ruby: 9
file content (230 lines) | stat: -rw-r--r-- 6,979 bytes parent folder | download
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
226
227
228
229
230
#############################################################################
##
##  This file is part of GAP, a system for computational discrete algebra.
##
##  Copyright of GAP belongs to its developers, whose names are too numerous
##  to list here. Please refer to the COPYRIGHT file for details.
##
##  SPDX-License-Identifier: GPL-2.0-or-later
##

# returns names of components
InstallMethod( RecNames,
               "for a record in internal representation",
               [ IsRecord and IsInternalRep ],
               REC_NAMES );


InstallGlobalFunction( "NamesOfComponents",
function( obj )
    if IsComponentObjectRep( obj ) then
        return REC_NAMES_COMOBJ( obj );
    elif IsRecord( obj ) then
        return RecNames( obj );
    else
        Error( "<obj> must be a component object or a record" );
    fi;
end );

#############################################################################
##
#m  PrintObj( <record> )
##
##  The record <record> is printed by printing all its components.
##
InstallMethod( PrintObj,
    "record",
    [ IsRecord ],
## Changed to use sorted component names to make printing nicer to read and
## independent of session (i.e., the ordering in which component names were
## first used). Except for the sorting of components this does the same as
## the former (now removed) kernel function FuncPRINT_PREC_DEFAULT.
    function( record )
    local com, i, snam, nam, names;
    Print("\>\>rec(\n\>\>");
    com := false;

    names := Set(RecNames(record));

    for i in [1..Length(names)] do
        nam := names[i];
        if com then
            Print("\<\<,\n\>\>");
        else
            com := true;
        fi;
        SET_PRINT_OBJ_INDEX(RNamObj(nam));
        # easy if nam is integer or valid identifier:
        if ForAll(nam, x-> x in IdentifierLetters) and Size(nam) > 0 then
          Print(nam, "\< := \>");
        else
          # otherwise we use (...) syntax:
          snam := String(nam);
          Print("("); View(snam); Print(")\< := \>");
        fi;
        PrintObj(record.(nam));
    od;
    Print(" \<\<\<\<)");
end);

#############################################################################
##
#m  String( <record> )  . . . . . . . . . . . . . . . . . . . .  for a record
##
InstallMethod( String,
    "record",
    [ IsRecord ],
    function( record )
    local   str,  nam,  com;

    str := "rec( ";
    com := false;
    for nam in Set(RecNames( record )) do
      if com then
        Append( str, ", " );
      else
        com := true;
      fi;
      Append( str, nam );
      Append( str, " := " );
      if IsStringRep( record.( nam ) )
         or ( IsString( record.( nam ) )
              and not IsEmpty( record.( nam ) ) ) then
        Append( str, "\"" );
        Append( str, String( record.(nam) ) );
        Append( str, "\"" );
      else
        Append( str, String( record.(nam) ) );
      fi;
    od;
    Append( str, " )" );
    # should not be necessary if all methods for components are alright
    ConvertToStringRep( str );
    return str;
end );


#############################################################################
##
#m  ViewObj( <record> ) . . . . . . . . . . . . . . .  for a record (default)
##
InstallMethod( ViewObj,
    "record",
    [ IsRecord ],
    function( record )
    local nam, com, i, snam, names;
    Print("\>\>rec( \>\>");
    com := false;

    names := Set(RecNames(record));

    for i in [1..Length(names)] do
        nam := names[i];
        if com then
            Print("\<,\< \>\>");
        else
            com := true;
        fi;
        SET_PRINT_OBJ_INDEX(RNamObj(nam));
        # easy if nam is integer or valid identifier:
        if ForAll(nam, x-> x in IdentifierLetters) and Size(nam) > 0 then
          Print(nam, " := ");
        else
          # otherwise we use (...) syntax:
          snam := String(nam);
          Print("("); View(snam); Print(") := ");
        fi;
        ViewObj(record.(nam));
    od;
    Print(" \<\<\<\<)");
end);


# methods to catch error cases

InstallMethod( \.,
               "catch error",
               true,
               [IsObject,IsObject],
               0,
function(obj,nr)
    local msg;
    msg:=Concatenation("illegal access to record component `obj.",
                        NameRNam(nr),"'\n",
                        "of the object <obj>. (Objects by default do not have record components.\n",
                        "The error might be a relic from translated GAP3 code.)");
    Error(msg);
end);

InstallMethod( IsBound\.,
               "catch error",true,[IsObject,IsObject],0,
function(obj,nr)
    local msg;
    msg:=Concatenation("illegal access to record component `IsBound(obj.",
                        NameRNam(nr),")'\n",
                        "of the object <obj>. (Objects by default do not have record components.\n",
                        "The error might be a relic from translated GAP3 code.)");
    Error(msg);
end);

InstallMethod( Unbind\.,
               "catch error",
               true,
               [IsObject,IsObject],
               0,
function(obj,nr)
    local msg;
    msg:=Concatenation("illegal access to record component `Unbind(obj.",
                       NameRNam(nr),")'\n",
                       "of the object <obj>. (Objects by default do not have record components.\n",
                       "The error might be a relic from translated GAP3 code.)");
    Error(msg);
end);

InstallMethod( \.\:\=,
               "catch error",
               true,
               [IsObject,IsObject,IsObject],
               0,
function(obj,nr,elm)
local msg;
    msg:=Concatenation("illegal assignment to record component `obj.",
                       NameRNam(nr),"'\n",
                       "of the object <obj>. (Objects by default cannot have record components.\n",
                       "The error might be a relic from translated GAP3 code.)");
    Error(msg);
end);

#############################################################################
##
#F  SetNamesForFunctionsInRecord( <rec-name>[, <record> ][, <field-names>])
##
##  set the names of functions bound to components of a record.
##
InstallGlobalFunction(SetNamesForFunctionsInRecord,
function(arg)
    local recname, next, record, fields, field;
    if LENGTH(arg) = 0 or not IS_STRING(arg[1]) then
        Error("SetNamesForFunctionsInRecord: you must give a record name");
    fi;
    recname := arg[1];
    next := 2;
    if LENGTH(arg) >= next and IS_REC(arg[next]) then
        record := arg[2];
        next := 3;
    else
        record := VALUE_GLOBAL(recname);
    fi;
    if LENGTH(arg) >= next and IS_LIST(arg[next]) then
        fields := arg[next];
    else
        fields := REC_NAMES(record);
    fi;
    for field in fields do
        if IS_STRING(field) then
            if IsFunction(record.(field)) then
                SetNameFunction(record.(field), Concatenation(recname,".",field));
            fi;
        fi;
    od;
end);