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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
|
/****************************************************************************
**
** 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
*/
// LibGAP API - API for using GAP as shared library.
#include <signal.h>
#include "libgap-api.h"
#include "libgap_intern.h"
#include "ariths.h"
#include "bool.h"
#include "calls.h"
#include "funcs.h"
#include "gap.h"
#include "gapstate.h"
#include "gasman.h"
#ifdef USE_GASMAN
#include "gasman_intern.h"
#endif
#include "gvars.h"
#include "integer.h"
#include "lists.h"
#include "macfloat.h"
#include "modules.h"
#include "opers.h"
#include "plist.h"
#include "precord.h"
#include "range.h"
#include "records.h"
#include "streams.h"
#include "stringobj.h"
#include <stdio.h>
static BOOL UsingLibGap = FALSE;
BOOL IsUsingLibGap(void)
{
return UsingLibGap;
}
//
// Setup and initialisation
//
void GAP_Initialize(int argc,
char ** argv,
GAP_CallbackFunc markBagsCallback,
GAP_CallbackFunc errorCallback,
int handleSignals)
{
UsingLibGap = TRUE;
InitializeGap(&argc, argc, (const char **)argv, handleSignals);
SetExtraMarkFuncBags(markBagsCallback);
STATE(JumpToCatchCallback) = errorCallback;
GAP_True = True;
GAP_False = False;
GAP_Fail = Fail;
}
////
//// Garbage collector interface
////
void GAP_MarkBag(Obj obj)
{
// For now pass a dummy 'ref' value to MarkBag. That's fine
// when using GASMAN or Boehm GC. It won't work with Julia GC
// but that's fine as GAP.jl doesn't need it.
MarkBag(obj, 0);
}
void GAP_CollectBags(BOOL full)
{
CollectBags(0, full);
}
////
//// program evaluation and execution
////
Obj GAP_EvalString(const char * cmd)
{
Obj instream;
Obj res;
Obj viewObjFunc, streamFunc;
streamFunc = GAP_ValueGlobalVariable("InputTextString");
viewObjFunc = GAP_ValueGlobalVariable("ViewObj");
instream = DoOperation1Args(streamFunc, MakeString(cmd));
res = READ_ALL_COMMANDS(instream, False, True, viewObjFunc);
return res;
}
////
//// variables
////
Obj GAP_ValueGlobalVariable(const char * name)
{
UInt gvar = GVarName(name);
GAP_ASSERT(gvar != 0);
return ValAutoGVar(gvar);
}
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
////
int GAP_EQ(Obj a, Obj b)
{
return EQ(a, b);
}
int GAP_LT(Obj a, Obj b)
{
return LT(a, b);
}
int GAP_IN(Obj a, Obj b)
{
return IN(a, b);
}
Obj GAP_SUM(Obj a, Obj b)
{
return SUM(a, b);
}
Obj GAP_DIFF(Obj a, Obj b)
{
return DIFF(a, b);
}
Obj GAP_PROD(Obj a, Obj b)
{
return PROD(a, b);
}
Obj GAP_QUO(Obj a, Obj b)
{
return QUO(a, b);
}
Obj GAP_LQUO(Obj a, Obj b)
{
return LQUO(a, b);
}
Obj GAP_POW(Obj a, Obj b)
{
return POW(a, b);
}
Obj GAP_COMM(Obj a, Obj b)
{
return COMM(a, b);
}
Obj GAP_MOD(Obj a, Obj b)
{
return MOD(a, b);
}
////
//// booleans
////
int GAP_IsBoolean(Obj obj)
{
return obj && TNUM_OBJ(obj) == T_BOOL;
}
Obj GAP_True;
Obj GAP_False;
Obj GAP_Fail;
////
//// functions and calls
////
int GAP_IsFunction(Obj obj)
{
return obj && IS_FUNC(obj);
}
Obj GAP_CallFuncList(Obj func, Obj args)
{
return CallFuncList(func, args);
}
Obj GAP_CallFuncArray(Obj func, UInt narg, Obj args[])
{
Obj result;
Obj list;
if (TNUM_OBJ(func) == T_FUNCTION) {
// call the function
switch (narg) {
case 0:
result = CALL_0ARGS(func);
break;
case 1:
result = CALL_1ARGS(func, args[0]);
break;
case 2:
result = CALL_2ARGS(func, args[0], args[1]);
break;
case 3:
result = CALL_3ARGS(func, args[0], args[1], args[2]);
break;
case 4:
result = CALL_4ARGS(func, args[0], args[1], args[2], args[3]);
break;
case 5:
result =
CALL_5ARGS(func, args[0], args[1], args[2], args[3], args[4]);
break;
case 6:
result = CALL_6ARGS(func, args[0], args[1], args[2], args[3],
args[4], args[5]);
break;
default:
list = NewPlistFromArray(args, narg);
result = CALL_XARGS(func, list);
}
}
else {
list = NewPlistFromArray(args, narg);
result = DoOperation2Args(CallFuncListOper, func, list);
}
return result;
}
Obj GAP_CallFunc0Args(Obj func)
{
Obj result;
if (TNUM_OBJ(func) == T_FUNCTION) {
result = CALL_0ARGS(func);
}
else {
Obj list = NewEmptyPlist();
result = DoOperation2Args(CallFuncListOper, func, list);
}
return result;
}
Obj GAP_CallFunc1Args(Obj func, Obj a1)
{
Obj result;
if (TNUM_OBJ(func) == T_FUNCTION) {
result = CALL_1ARGS(func, a1);
}
else {
Obj list = NewPlistFromArgs(a1);
result = DoOperation2Args(CallFuncListOper, func, list);
}
return result;
}
Obj GAP_CallFunc2Args(Obj func, Obj a1, Obj a2)
{
Obj result;
if (TNUM_OBJ(func) == T_FUNCTION) {
result = CALL_2ARGS(func, a1, a2);
}
else {
Obj list = NewPlistFromArgs(a1, a2);
result = DoOperation2Args(CallFuncListOper, func, list);
}
return result;
}
Obj GAP_CallFunc3Args(Obj func, Obj a1, Obj a2, Obj a3)
{
Obj result;
if (TNUM_OBJ(func) == T_FUNCTION) {
result = CALL_3ARGS(func, a1, a2, a3);
}
else {
Obj list = NewPlistFromArgs(a1, a2, a3);
result = DoOperation2Args(CallFuncListOper, func, list);
}
return result;
}
////
//// floats
////
int GAP_IsMacFloat(Obj obj)
{
return obj && IS_MACFLOAT(obj);
}
double GAP_ValueMacFloat(Obj obj)
{
if (!IS_MACFLOAT(obj)) {
ErrorMayQuit("<obj> is not a MacFloat", 0, 0);
}
return (double)VAL_MACFLOAT(obj);
}
Obj GAP_NewMacFloat(double x)
{
return NEW_MACFLOAT(x);
}
////
//// integers
////
int GAP_IsInt(Obj obj)
{
return obj && IS_INT(obj);
}
int GAP_IsSmallInt(Obj obj)
{
return obj && IS_INTOBJ(obj);
}
int GAP_IsLargeInt(Obj obj)
{
return obj && IS_LARGEINT(obj);
}
Obj GAP_MakeObjInt(const UInt * limbs, Int size)
{
return MakeObjInt(limbs, size);
}
Obj GAP_NewObjIntFromInt(Int val)
{
return ObjInt_Int(val);
}
Int GAP_ValueInt(Obj obj)
{
return Int_ObjInt(obj);
}
Int GAP_SizeInt(Obj obj)
{
RequireInt("GAP_SizeInt", obj);
if (obj == INTOBJ_INT(0))
return 0;
Int size = (IS_INTOBJ(obj) ? 1 : SIZE_INT(obj));
return IS_POS_INT(obj) ? size : -size;
}
const UInt * GAP_AddrInt(Obj obj)
{
if (obj && IS_LARGEINT(obj))
return CONST_ADDR_INT(obj);
else
return 0;
}
////
//// lists
////
int GAP_IsList(Obj obj)
{
return obj && IS_LIST(obj);
}
UInt GAP_LenList(Obj obj)
{
return LEN_LIST(obj);
}
void GAP_AssList(Obj list, UInt pos, Obj val)
{
if (val)
ASS_LIST(list, pos, val);
else
UNB_LIST(list, pos);
}
Obj GAP_ElmList(Obj list, UInt pos)
{
if (pos == 0)
return 0;
return ELM0_LIST(list, pos);
}
Obj GAP_NewPlist(Int capacity)
{
return NEW_PLIST(T_PLIST_EMPTY, capacity);
}
static BOOL fitsInIntObj(Int i)
{
return INT_INTOBJ_MIN <= i && i <= INT_INTOBJ_MAX;
}
Obj GAP_NewRange(Int len, Int low, Int inc)
{
if (!inc) return GAP_Fail;
if (!fitsInIntObj(len)) return GAP_Fail;
if (!fitsInIntObj(low)) return GAP_Fail;
if (!fitsInIntObj(inc)) return GAP_Fail;
Int high = low + (len - 1) * inc;
if (!fitsInIntObj(high)) return GAP_Fail;
return NEW_RANGE(len, low, inc);
}
////
//// matrix obj
////
static Obj IsMatrixOrMatrixObjFilt;
static Obj IsMatrixFilt;
static Obj IsMatrixObjFilt;
static Obj NrRowsAttr;
static Obj NrColsAttr;
// Returns 1 if <obj> is a GAP matrix or matrix obj, 0 if not.
int GAP_IsMatrixOrMatrixObj(Obj obj)
{
return obj && CALL_1ARGS(IsMatrixOrMatrixObjFilt, obj) == True;
}
// Returns 1 if <obj> is a GAP matrix, 0 if not.
int GAP_IsMatrix(Obj obj)
{
return obj && CALL_1ARGS(IsMatrixFilt, obj) == True;
}
// Returns 1 if <obj> is a GAP matrix obj, 0 if not.
int GAP_IsMatrixObj(Obj obj)
{
return obj && CALL_1ARGS(IsMatrixObjFilt, obj) == True;
}
// Returns the number of rows of the given GAP matrix obj.
// If <mat> is not a GAP matrix obj, an error may be raised.
UInt GAP_NrRows(Obj mat)
{
Obj nrows = CALL_1ARGS(NrRowsAttr, mat);
return UInt_ObjInt(nrows);
}
// Returns the number of columns of the given GAP matrix or matrix obj.
// If <mat> is not a GAP matrix or matrix obj, an error may be raised.
UInt GAP_NrCols(Obj mat)
{
Obj ncols = CALL_1ARGS(NrColsAttr, mat);
return UInt_ObjInt(ncols);
}
// Assign <val> at the <row>, <col> into the GAP matrix or matrix obj <mat>.
// If <val> is zero, then this unbinds the list entry.
// If <mat> is not a GAP matrix or matrix obj, an error may be raised.
void GAP_AssMat(Obj mat, UInt row, UInt col, Obj val)
{
Obj r = ObjInt_UInt(row);
Obj c = ObjInt_UInt(col);
ASS_MAT(mat, r, c, val);
}
// Returns the element at the <row>, <col> in the GAP matrix obj <mat>.
// Returns 0 if <row> or <col> are out of bounds, i.e., if either
// is zero, or larger than the number of rows respectively columns of <mat>.
// If <mat> is not a GAP matrix or matrix obj, an error may be raised.
Obj GAP_ElmMat(Obj mat, UInt row, UInt col)
{
Obj r = ObjInt_UInt(row);
Obj c = ObjInt_UInt(col);
return ELM_MAT(mat, r, c);
}
////
//// records
////
int GAP_IsRecord(Obj obj)
{
return obj && IS_REC(obj);
}
void GAP_AssRecord(Obj rec, Obj name, Obj val)
{
UInt rnam = RNamObj(name);
ASS_REC(rec, rnam, val);
}
Obj GAP_ElmRecord(Obj rec, Obj name)
{
UInt rnam = RNamObj(name);
if (ISB_REC(rec, rnam))
return ELM_REC(rec, rnam);
return 0;
}
Obj GAP_NewPrecord(Int capacity)
{
return NEW_PREC(capacity);
}
////
//// strings
////
int GAP_IsString(Obj obj)
{
return obj && IS_STRING_REP(obj);
}
UInt GAP_LenString(Obj obj)
{
return GET_LEN_STRING(obj);
}
Obj GAP_MakeString(const char * string)
{
return MakeString(string);
}
Obj GAP_MakeStringWithLen(const char * string, UInt len)
{
return MakeStringWithLen(string, len);
}
Obj GAP_MakeImmString(const char * string)
{
return MakeImmString(string);
}
char * GAP_CSTR_STRING(Obj string)
{
if (!IS_STRING_REP(string))
return 0;
return CSTR_STRING(string);
}
////
//// chars
////
int GAP_IsChar(Obj obj)
{
return obj && TNUM_OBJ(obj) == T_CHAR;
}
Int GAP_ValueOfChar(Obj obj)
{
if (TNUM_OBJ(obj) != T_CHAR) {
return -1;
}
return (Int)CHAR_VALUE(obj);
}
Obj GAP_CharWithValue(UChar obj)
{
return ObjsChar[obj];
}
jmp_buf * GAP_GetReadJmpError(void)
{
return &(STATE(ReadJmpError));
}
static volatile sig_atomic_t EnterStackCount = 0;
static volatile Int RecursionDepth;
// These are wrapped by the macros GAP_EnterStack() and GAP_LeaveStack()
// respectively.
void GAP_EnterStack_(void * StackTop)
{
if (EnterStackCount < 0) {
EnterStackCount = -EnterStackCount;
}
else {
if (EnterStackCount == 0) {
#ifdef USE_GASMAN
SetStackBottomBags(StackTop);
#endif
}
EnterStackCount++;
}
}
void GAP_LeaveStack_(void)
{
EnterStackCount--;
}
void GAP_EnterDebugMessage_(char * message, char * file, int line)
{
fprintf(stderr, "%s: %d; %s:%d\n", message, (int)EnterStackCount, file, line);
}
int GAP_Error_Prejmp_(const char * file, int line)
{
GAP_ENTER_DEBUG_MESSAGE("Error_Prejmp", file, line);
if (EnterStackCount > 0) {
return 1;
}
RecursionDepth = GetRecursionDepth();
return 0;
}
/* Helper function for GAP_Error_Postjmp_ (see libgap-api.h) which manipulates
* EnterStackCount in the (generally unlikely) case of returning from a
* longjmp
*/
void GAP_Error_Postjmp_Returning_(void)
{
/* This only should have been called from the outer-most
* GAP_EnterStack() call so make sure it resets the EnterStackCount;
* We set EnterStackCount to its negative which indicates to
* GAP_EnterStack that we just returned from a long jump and should
* reset EnterStackCount to its value at the return point rather than
* increment it again */
if (EnterStackCount > 0) {
EnterStackCount = -EnterStackCount;
}
SetRecursionDepth(RecursionDepth);
}
/****************************************************************************
**
*F InitKernel( <module> ) . . . . . . . . initialise kernel data structures
*/
static Int InitKernel(StructInitInfo * module)
{
InitFopyGVar("IsMatrixOrMatrixObj", &IsMatrixOrMatrixObjFilt);
InitFopyGVar("IsMatrix", &IsMatrixFilt);
InitFopyGVar("IsMatrixObj", &IsMatrixObjFilt);
InitFopyGVar("NrRows", &NrRowsAttr);
InitFopyGVar("NrCols", &NrColsAttr);
return 0;
}
/****************************************************************************
**
*F InitInfoLibGapApi() . . . . . . . . . . . . . . . table of init functions
*/
static StructInitInfo module = {
// init struct using C99 designated initializers; for a full list of
// fields, please refer to the definition of StructInitInfo
.type = MODULE_BUILTIN,
.name = "libgap",
.initKernel = InitKernel,
};
StructInitInfo * InitInfoLibGapApi ( void )
{
return &module;
}
|