Skip to content

Commit

Permalink
libgap: add GAP_(Can)AssignGlobalVariable
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasguts authored and fingolfin committed May 10, 2019
1 parent 84cd55e commit 5fec327
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/libgap-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ Obj GAP_ValueGlobalVariable(const char * name)
}
}

int GAP_CanAssignGlobalVariable(const char * name)
{
UInt gvar = GVarName(name);
return !(IsReadOnlyGVar(gvar) || IsConstantGVar(gvar));
}

void GAP_AssignGlobalVariable(const char * name, Obj value)
{
UInt gvar = GVarName(name);
AssGVar(gvar, value);
}

////
//// arithmetic
Expand Down
17 changes: 12 additions & 5 deletions src/libgap-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void GAP_Initialize(int argc,
//// program evaluation and execution
////

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

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

// Checks if assigning to the global GAP variable <name> is possible, by
// verifying that <name> is not the name of a read-only or constant variable.
int GAP_CanAssignGlobalVariable(const char * name);

// Assign <value> to the global GAP variable <name>. If <name> is the name of
// a readonly or constant variable, an error is raised.
void GAP_AssignGlobalVariable(const char * name, Obj value);


////
//// arithmetic
Expand Down Expand Up @@ -278,7 +285,7 @@ int GAP_IsLargeInt(Obj obj);
// `integer.c`). The absolute value of <size> determines the number of limbs.
// If <size> is zero, then `INTOBJ_INT(0)` is returned. Otherwise, the sign
// of the returned integer object is determined by the sign of <size>.
// //
//
// Note that GAP automatically reduces and normalized the integer object,
// i.e., it will discard any leading zeros; and if the integer fits into a
// small integer, it will be returned as such.
Expand Down
11 changes: 11 additions & 0 deletions tst/testlibgap/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,24 @@ void operations(void)
void globalvars(void)
{
Obj a;
int x;

a = GAP_ValueGlobalVariable("yaddayaddayadda");
assert(a == 0);

// Hopefully this always exists.
a = GAP_ValueGlobalVariable("GAPInfo");
assert(GAP_IsRecord(a) != 0);

x = GAP_IsNameOfWritableGlobalVariable("GAPInfo");
assert(x == 0);

x = GAP_IsNameOfWritableGlobalVariable("GAPInfo_copy");
assert(x != 0);

GAP_AssignGlobalVariable("GAPInfo_copy", a);
a = GAP_ValueGlobalVariable("GAPInfo_copy");
assert(a != 0);
}

int main(int argc, char ** argv)
Expand Down

0 comments on commit 5fec327

Please sign in to comment.