Skip to content

Commit 61fc5b0

Browse files
Sebastian Gutschefingolfin
authored andcommitted
libgap: add GAP_(Can)AssignGlobalVariable
1 parent 84cd55e commit 61fc5b0

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/libgap-api.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ Obj GAP_ValueGlobalVariable(const char * name)
9595
}
9696
}
9797

98+
int GAP_CanAssignGlobalVariable(const char * name)
99+
{
100+
UInt gvar = GVarName(name);
101+
return !(IsReadOnlyGVar(gvar) || IsConstantGVar(gvar));
102+
}
103+
104+
void GAP_AssignGlobalVariable(const char * name, Obj value)
105+
{
106+
UInt gvar = GVarName(name);
107+
AssGVar(gvar, value);
108+
}
98109

99110
////
100111
//// arithmetic

src/libgap-api.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void GAP_Initialize(int argc,
163163
//// program evaluation and execution
164164
////
165165

166-
// Evaluate a string of GAP commands
166+
// Evaluate a string of GAP commands.
167167
//
168168
// To see an example of how to use this function see tst/testlibgap/basic.c
169169
//
@@ -175,11 +175,18 @@ Obj GAP_EvalString(const char * cmd);
175175
//// variables
176176
////
177177

178-
// Combines GVarName and ValGVar. For a given string, it returns the value
179-
// of the gvar with name <name>, or NULL if the global variable is not
180-
// defined.
178+
// Returns the value of the global GAP variable with name <name>, or NULL if
179+
// no global variable with this this name is defined.
181180
Obj GAP_ValueGlobalVariable(const char * name);
182181

182+
// Checks if assigning to the global GAP variable <name> is possible, by
183+
// verifying that <name> is not the name of a read-only or constant variable.
184+
int GAP_CanAssignGlobalVariable(const char * name);
185+
186+
// Assign <value> to the global GAP variable <name>. If <name> is the name of
187+
// a readonly or constant variable, an error is raised.
188+
void GAP_AssignGlobalVariable(const char * name, Obj value);
189+
183190

184191
////
185192
//// arithmetic
@@ -278,7 +285,7 @@ int GAP_IsLargeInt(Obj obj);
278285
// `integer.c`). The absolute value of <size> determines the number of limbs.
279286
// If <size> is zero, then `INTOBJ_INT(0)` is returned. Otherwise, the sign
280287
// of the returned integer object is determined by the sign of <size>.
281-
// //
288+
//
282289
// Note that GAP automatically reduces and normalized the integer object,
283290
// i.e., it will discard any leading zeros; and if the integer fits into a
284291
// small integer, it will be returned as such.

tst/testlibgap/api.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,24 @@ void operations(void)
138138
void globalvars(void)
139139
{
140140
Obj a;
141+
int x;
141142

142143
a = GAP_ValueGlobalVariable("yaddayaddayadda");
143144
assert(a == 0);
144145

145146
// Hopefully this always exists.
146147
a = GAP_ValueGlobalVariable("GAPInfo");
147148
assert(GAP_IsRecord(a) != 0);
149+
150+
x = GAP_CanAssignGlobalVariable("GAPInfo");
151+
assert(x == 0);
152+
153+
x = GAP_CanAssignGlobalVariable("GAPInfo_copy");
154+
assert(x != 0);
155+
156+
GAP_AssignGlobalVariable("GAPInfo_copy", a);
157+
a = GAP_ValueGlobalVariable("GAPInfo_copy");
158+
assert(a != 0);
148159
}
149160

150161
int main(int argc, char ** argv)

0 commit comments

Comments
 (0)