inline static void do_signal(yap_signals sig USES_REGS) { LOCK(LOCAL_SignalLock); if (!LOCAL_InterruptsDisabled) { CreepFlag = Unsigned(LCL0); if (sig != YAP_CREEP_SIGNAL) EventFlag = Unsigned(LCL0); } LOCAL_ActiveSignals |= sig; UNLOCK(LOCAL_SignalLock); }
void Brush_ConstructSphere(Brush& brush, const AABB& bounds, std::size_t sides, const char* shader, const TextureProjection& projection) { if(sides < c_brushSphere_minSides) { globalErrorStream() << c_brushSphere_name << ": sides " << Unsigned(sides) << ": too few sides, minimum is " << Unsigned(c_brushSphere_minSides) << "\n"; return; } if(sides > c_brushSphere_maxSides) { globalErrorStream() << c_brushSphere_name << ": sides " << Unsigned(sides) << ": too many sides, maximum is " << Unsigned(c_brushSphere_maxSides) << "\n"; return; } brush.undoSave(); brush.clear(); brush.reserve(sides*sides); float radius = max_extent(bounds.extents); const Vector3& mid = bounds.origin; Vector3 planepts[3]; double dt = 2 * c_pi / sides; double dp = c_pi / sides; for(std::size_t i=0; i < sides; i++) { for(std::size_t j=0;j < sides-1; j++) { double t = i * dt; double p = float(j * dp - c_pi / 2); planepts[0] = vector3_added(mid, vector3_scaled(vector3_for_spherical(t, p), radius)); planepts[1] = vector3_added(mid, vector3_scaled(vector3_for_spherical(t, p + dp), radius)); planepts[2] = vector3_added(mid, vector3_scaled(vector3_for_spherical(t + dt, p + dp), radius)); brush.addPlane(planepts[0], planepts[1], planepts[2], shader, projection); } } { double p = (sides - 1) * dp - c_pi / 2; for(std::size_t i = 0; i < sides; i++) { double t = i * dt; planepts[0] = vector3_added(mid, vector3_scaled(vector3_for_spherical(t, p), radius)); planepts[1] = vector3_added(mid, vector3_scaled(vector3_for_spherical(t + dt, p + dp), radius)); planepts[2] = vector3_added(mid, vector3_scaled(vector3_for_spherical(t + dt, p), radius)); brush.addPlane(planepts[0], planepts[1], planepts[2], shader, projection); } } }
void Brush_ConstructCone(Brush& brush, const AABB& bounds, std::size_t sides, const char* shader, const TextureProjection& projection) { if(sides < c_brushCone_minSides) { globalErrorStream() << c_brushCone_name << ": sides " << Unsigned(sides) << ": too few sides, minimum is " << Unsigned(c_brushCone_minSides) << "\n"; return; } if(sides > c_brushCone_maxSides) { globalErrorStream() << c_brushCone_name << ": sides " << Unsigned(sides) << ": too many sides, maximum is " << Unsigned(c_brushCone_maxSides) << "\n"; return; } brush.undoSave(); brush.clear(); brush.reserve(sides+1); Vector3 mins(vector3_subtracted(bounds.origin, bounds.extents)); Vector3 maxs(vector3_added(bounds.origin, bounds.extents)); float radius = max_extent(bounds.extents); const Vector3& mid = bounds.origin; Vector3 planepts[3]; planepts[0][0] = mins[0];planepts[0][1] = mins[1];planepts[0][2] = mins[2]; planepts[1][0] = maxs[0];planepts[1][1] = mins[1];planepts[1][2] = mins[2]; planepts[2][0] = maxs[0];planepts[2][1] = maxs[1];planepts[2][2] = mins[2]; brush.addPlane(planepts[0], planepts[1], planepts[2], shader, projection); for (std::size_t i=0 ; i<sides ; ++i) { double sv = sin (i*3.14159265*2/sides); double cv = cos (i*3.14159265*2/sides); planepts[0][0] = static_cast<float>(floor(mid[0]+radius*cv+0.5)); planepts[0][1] = static_cast<float>(floor(mid[1]+radius*sv+0.5)); planepts[0][2] = mins[2]; planepts[1][0] = mid[0]; planepts[1][1] = mid[1]; planepts[1][2] = maxs[2]; planepts[2][0] = static_cast<float>(floor(planepts[0][0] - radius * sv + 0.5)); planepts[2][1] = static_cast<float>(floor(planepts[0][1] + radius * cv + 0.5)); planepts[2][2] = maxs[2]; brush.addPlane(planepts[0], planepts[1], planepts[2], shader, projection); } }
inline void Toeplitz( DistMatrix<T,U,V>& A, Int m, Int n, const std::vector<T>& a ) { #ifndef RELEASE CallStackEntry entry("Toeplitz"); #endif const Int length = m+n-1; if( a.size() != Unsigned(length) ) LogicError("a was the wrong size"); A.ResizeTo( m, n ); const Int localHeight = A.LocalHeight(); const Int localWidth = A.LocalWidth(); const Int colShift = A.ColShift(); const Int rowShift = A.RowShift(); const Int colStride = A.ColStride(); const Int rowStride = A.RowStride(); for( Int jLoc=0; jLoc<localWidth; ++jLoc ) { const Int j = rowShift + jLoc*rowStride; for( Int iLoc=0; iLoc<localHeight; ++iLoc ) { const Int i = colShift + iLoc*colStride; A.SetLocal( iLoc, jLoc, a[i-j+(n-1)] ); } } }
void LoadCommandMap( const char* path ){ StringOutputStream strINI( 256 ); strINI << path << "shortcuts.ini"; FILE* f = fopen( strINI.c_str(), "r" ); if ( f != 0 ) { fclose( f ); globalOutputStream() << "loading custom shortcuts list from " << makeQuoted( strINI.c_str() ) << "\n"; Version version = version_parse( COMMANDS_VERSION ); Version dataVersion = { 0, 0 }; { char value[1024]; if ( read_var( strINI.c_str(), "Version", "number", value ) ) { dataVersion = version_parse( value ); } } if ( version_compatible( version, dataVersion ) ) { globalOutputStream() << "commands import: data version " << dataVersion << " is compatible with code version " << version << "\n"; ReadCommandMap visitor( strINI.c_str() ); GlobalShortcuts_foreach( visitor ); globalOutputStream() << "parsed " << Unsigned( visitor.count() ) << " custom shortcuts\n"; } else { globalOutputStream() << "commands import: data version " << dataVersion << " is not compatible with code version " << version << "\n"; } } else { globalOutputStream() << "failed to load custom shortcuts from " << makeQuoted( strINI.c_str() ) << "\n"; } }
static void FreeBlock(BlockHeader *b) { BlockHeader *p; YAP_SEG_SIZE *sp; /* { static long long int vsc_free_ops; vsc_free_ops++; BlockHeader *q = FreeBlocks; while (q) q = q->b_next_size; }*/ /* sanity check */ sp = &(b->b_size) + (b->b_size & ~InUseFlag); if (!(b->b_size & InUseFlag) || *sp != b->b_size) { #if !SHORT_INTS fprintf(stderr, "%% YAP INTERNAL ERROR: sanity check failed in FreeBlock %p %x %x\n", b, b->b_size, Unsigned(*sp)); #else fprintf(stderr, "%% YAP INTERNAL ERROR: sanity check failed in FreeBlock %p %lx %lx\n", b, b->b_size, *sp); #endif return; } b->b_size &= ~InUseFlag; LOCK(FreeBlocksLock); /* check if we can collapse with other blocsks */ /* check previous */ sp = &(b->b_size) - 1; if (!(*sp & InUseFlag)) { /* previous block is free */ p = (BlockHeader *) (sp - *sp); RemoveFromFreeList(p); p->b_size += b->b_size + 1; b = p; } /* check following */ sp = &(b->b_size) + b->b_size + 1; if (!(*sp & InUseFlag)) { /* following block is free */ p = (BlockHeader *) sp; RemoveFromFreeList(p); b->b_size += p->b_size + 1; } LOCK(HeapTopLock); if (sp == (YAP_SEG_SIZE *)HeapTop) { LOCK(HeapUsedLock); HeapUsed -= (b->b_size + 1) * sizeof(YAP_SEG_SIZE); UNLOCK(HeapUsedLock); HeapTop = (ADDR)b; *((YAP_SEG_SIZE *) HeapTop) = InUseFlag; } else { /* insert on list of free blocks */ AddToFreeList(b); } UNLOCK(HeapTopLock); UNLOCK(FreeBlocksLock); }
void Toeplitz( AbstractDistMatrix<S>& A, Int m, Int n, const vector<T>& a ) { EL_DEBUG_CSE const Int length = m+n-1; if( a.size() != Unsigned(length) ) LogicError("a was the wrong size"); A.Resize( m, n ); auto toeplitzFill = [&]( Int i, Int j ) { return a[i-j+(n-1)]; }; IndexDependentFill( A, function<S(Int,Int)>(toeplitzFill) ); }
void Brush_ConstructPrefab(Brush& brush, EBrushPrefab type, const AABB& bounds, std::size_t sides, const char* shader, const TextureProjection& projection) { switch(type) { case eBrushCuboid: { UndoableCommand undo("brushCuboid"); Brush_ConstructCuboid(brush, bounds, shader, projection); } break; case eBrushPrism: { int axis = GetViewAxis(); StringOutputStream command; command << c_brushPrism_name << " -sides " << Unsigned(sides) << " -axis " << axis; UndoableCommand undo(command.c_str()); Brush_ConstructPrism(brush, bounds, sides, axis, shader, projection); } break; case eBrushCone: { StringOutputStream command; command << c_brushCone_name << " -sides " << Unsigned(sides); UndoableCommand undo(command.c_str()); Brush_ConstructCone(brush, bounds, sides, shader, projection); } break; case eBrushSphere: { StringOutputStream command; command << c_brushSphere_name << " -sides " << Unsigned(sides); UndoableCommand undo(command.c_str()); Brush_ConstructSphere(brush, bounds, sides, shader, projection); } break; } }
static void reset_trail(tr_fr_ptr tr_top, tr_fr_ptr trp) { register CELL aux_cell; /* unbinding variables */ while (tr_top != trp) { aux_cell = TrailTerm(--trp); /* check for global or local variables */ if (IsVarTerm(aux_cell)) { /* clean up the trail when we backtrack */ /* shouldn't this test always succeed? */ if (Unsigned((Int)(aux_cell)-(Int)(H_FZ)) > Unsigned((Int)(B_FZ)-(Int)(H_FZ))) { RESET_VARIABLE(STACK_TO_SBA(aux_cell)); } else { RESET_VARIABLE(aux_cell); } } else if (IsPairTerm(aux_cell)) { /* avoid frozen segments */ if ((ADDR) RepPair(aux_cell) > HeapTop) { trp = (tr_fr_ptr) RepPair(aux_cell); } #ifdef MULTI_ASSIGNMENT_VARIABLES } else { CELL *aux_ptr = RepAppl(aux_cell); trp--; if (Unsigned((Int)(aux_ptr)-(Int)(H_FZ)) > Unsigned((Int)(B_FZ)-(Int)(H_FZ))) { *STACK_TO_SBA(aux_ptr) = TrailTerm(trp); } else { *aux_ptr = TrailTerm(trp); } #endif /* MULTI_ASSIGNMENT_VARIABLES */ } } }
inline void Toeplitz( Matrix<T>& A, Int m, Int n, const std::vector<T>& a ) { #ifndef RELEASE CallStackEntry entry("Toeplitz"); #endif const Int length = m+n-1; if( a.size() != Unsigned(length) ) LogicError("a was the wrong size"); A.ResizeTo( m, n ); for( Int j=0; j<n; ++j ) for( Int i=0; i<m; ++i ) A.Set( i, j, a[i-j+(n-1)] ); }
void write_stack_trace(PCONTEXT pContext, TextOutputStream& outputStream) { HANDLE m_hProcess = GetCurrentProcess(); DWORD dwMachineType = 0; CONTEXT context = *pContext; // Could use SymSetOptions here to add the SYMOPT_DEFERRED_LOADS flag if ( !SymInitialize( m_hProcess, (PSTR)environment_get_app_path(), TRUE ) ) { return; } STACKFRAME sf; memset( &sf, 0, sizeof(sf) ); #ifdef _M_IX86 // Initialize the STACKFRAME structure for the first call. This is only // necessary for Intel CPUs, and isn't mentioned in the documentation. sf.AddrPC.Offset = context.Eip; sf.AddrPC.Mode = AddrModeFlat; sf.AddrStack.Offset = context.Esp; sf.AddrStack.Mode = AddrModeFlat; sf.AddrFrame.Offset = context.Ebp; sf.AddrFrame.Mode = AddrModeFlat; dwMachineType = IMAGE_FILE_MACHINE_I386; #endif while ( 1 ) { // Get the next stack frame if ( ! StackWalk( dwMachineType, m_hProcess, GetCurrentThread(), &sf, &context, 0, SymFunctionTableAccess, SymGetModuleBase, 0 ) ) break; if ( 0 == sf.AddrFrame.Offset ) // Basic sanity check to make sure break; // the frame is OK. Bail if not. // Get the name of the function for this stack frame entry BYTE symbolBuffer[ sizeof(SYMBOL_INFO) + MAX_SYM_NAME ]; PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)symbolBuffer; pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO); pSymbol->MaxNameLen = MAX_SYM_NAME; DWORD64 symDisplacement = 0; // Displacement of the input address, // relative to the start of the symbol IMAGEHLP_MODULE module = { sizeof(IMAGEHLP_MODULE) }; if(SymGetModuleInfo(m_hProcess, sf.AddrPC.Offset, &module)) { outputStream << module.ModuleName << "!"; if ( SymFromAddr(m_hProcess, sf.AddrPC.Offset, &symDisplacement, pSymbol)) { char undecoratedName[MAX_SYM_NAME]; UnDecorateSymbolName(pSymbol->Name, undecoratedName, MAX_SYM_NAME, UNDNAME_COMPLETE); outputStream << undecoratedName; outputStream << "("; // Use SymSetContext to get just the locals/params for this frame IMAGEHLP_STACK_FRAME imagehlpStackFrame; imagehlpStackFrame.InstructionOffset = sf.AddrPC.Offset; SymSetContext( m_hProcess, &imagehlpStackFrame, 0 ); // Enumerate the locals/parameters EnumerateSymbolsContext context(sf, outputStream); SymEnumSymbols( m_hProcess, 0, 0, EnumerateSymbolsCallback, &context ); outputStream << ")"; outputStream << " + " << Offset(reinterpret_cast<void*>(symDisplacement)); // Get the source line for this stack frame entry IMAGEHLP_LINE lineInfo = { sizeof(IMAGEHLP_LINE) }; DWORD dwLineDisplacement; if ( SymGetLineFromAddr( m_hProcess, sf.AddrPC.Offset, &dwLineDisplacement, &lineInfo ) ) { outputStream << " " << lineInfo.FileName << " line " << Unsigned(lineInfo.LineNumber); } } else { outputStream << Address(reinterpret_cast<void*>(sf.AddrPC.Offset)); } } outputStream << "\n"; } SymCleanup(m_hProcess); return; }
static Term eval0(Int fi) { CACHE_REGS arith0_op fop = fi; switch (fop) { case op_pi: { RFLOAT(PI); } case op_e: { RFLOAT(M_E); } case op_epsilon: { RFLOAT(DBL_EPSILON); } case op_inf: { #ifdef _MSC_VER /* Microsoft's Visual C++ Compiler */ Yap_ArithError(TYPE_ERROR_EVALUABLE, TermNil, "evaluating infinity"); P = (yamop *)FAILCODE; RERROR(); #else if (isoLanguageFlag()) {/* iso */ Yap_ArithError(TYPE_ERROR_EVALUABLE, TermNil, "evaluating infinity"); P = (yamop *)FAILCODE; RERROR(); } else { RFLOAT(INFINITY); } #endif } case op_nan: { #ifdef _MSC_VER /* Microsoft's Visual C++ Compi<ler */ Yap_ArithError(TYPE_ERROR_EVALUABLE, TermNil, "evaluating infinity"); RERROR(); #else if (isoLanguageFlag()) {/* iso */ Yap_ArithError(TYPE_ERROR_EVALUABLE, TermNil, "evaluating not-a-number"); RERROR(); } else { RFLOAT(NAN); } #endif } case op_random: { RFLOAT(Yap_random()); } case op_cputime: { RFLOAT((Float)Yap_cputime()/1000.0); } case op_heapused: /// - heapused /// Heap (data-base) space used, in bytes. /// RINT(HeapUsed); case op_localsp: /// - local /// Local stack in use, in bytes /// #if YAPOR_SBA RINT((Int)ASP); #else RINT(LCL0 - ASP); #endif case op_b: /// - $b /// current choicepoint /// #if YAPOR_SBA RINT((Int)B); #else RINT(LCL0 - (CELL *)B); #endif case op_env: /// - $env /// Environment /// #if YAPOR_SBA RINT((Int)YENV); #else RINT(LCL0 - YENV); #endif case op_tr: /// - $tr /// Trail in use /// #if YAPOR_SBA RINT(TR); #else RINT(((CELL *)TR)-LCL0); #endif case op_stackfree: /// - $free_stack /// /// Not-a-number according to the IEEE Floating-Point standard. Note that evaluating this term will generate a domain error in the `iso` language mode. RINT(Unsigned(ASP) - Unsigned(HR)); case op_globalsp: /// - global /// Global stack in use, in bytes. /// #if YAPOR_SBA RINT((Int)HR); #else RINT(HR - H0); #endif } /// end of switch RERROR(); }
/* * LoadForeign(ofiles,libs,proc_name,init_proc) dynamically loads foreign * code files and libraries and locates an initialization routine */ static Int LoadForeign(StringList ofiles, StringList libs, char *proc_name, YapInitProc *init_proc) { char command[2*MAXPATHLEN]; char o_files[1024]; /* list of objects we want to load */ char l_files[1024]; /* list of libraries we want to load */ char tmp_buff[32] = "/tmp/YAP_TMP_XXXXXX"; /* used for mktemp */ char *tfile; /* name of temporary file */ int fildes; /* temp file descriptor */ struct aouthdr sysHeader; struct filehdr fileHeader; struct scnhdr sectionHeader[MAXSECTIONS]; struct exec header; /* header for loaded file */ unsigned long loadImageSize, firstloadImSz; /* size of image we will load */ char *FCodeBase; /* where we load foreign code */ /* * put in a string the names of the files you want to load and of any * libraries you want to use */ /* files first */ *o_files = '\0'; { StringList tmp = ofiles; while(tmp != NULL) { strcat(o_files," "); strcat(o_files,tmp->s); tmp = tmp->next; } } /* same_trick for libraries */ *l_files = '\0'; { StringList tmp = libs; while(tmp != NULL) { strcat(l_files," "); strcat(l_files,tmp->s); tmp = tmp->next; } } /* next, create a temp file to serve as loader output */ tfile = mktemp(tmp_buff); /* prepare the magic */ if (strlen(o_files) + strlen(l_files) + strlen(proc_name) + strlen(YapExecutable) > 2*MAXPATHLEN) { strcpy(Yap_ErrorSay, " too many parameters in load_foreign/3 "); return LOAD_FAILLED; } sprintf(command, "/usr/bin/ld -N -A %s -o %s %s %s -lc", YapExecutable, tfile, o_files, l_files); /* now, do the magic */ if (system(command) != 0) { unlink(tfile); strcpy(Yap_ErrorSay," ld returned error status in load_foreign_files "); return LOAD_FAILLED; } /* now check the music has played */ if ((fildes = open(tfile, O_RDONLY)) < 0) { strcpy(Yap_ErrorSay," unable to open temp file in load_foreign_files "); return LOAD_FAILLED; } /* it did, get the mice */ /* first, get the header */ read(fildes, (char *) &fileHeader, sizeof(fileHeader)); read(fildes, (char *) &sysHeader, sizeof(sysHeader)); { int i; for (i = 0; i < fileHeader.f_nscns; i++) read(fildes, (char *) §ionHeader[i], sizeof(*sectionHeader)); } close(fildes); /* get the full size of what we need to load */ loadImageSize = sysHeader.tsize + sysHeader.dsize + sysHeader.bsize; #ifdef mips /* add an extra page in mips machines */ loadImageSize += 4095 + 16; #else /* add 16 just to play it safe */ loadImageSize += 16; #endif /* keep this copy */ firstloadImSz = loadImageSize; /* now fetch the space we need */ if (!(FCodeBase = Yap_AllocCodeSpace((int) loadImageSize)) #ifdef pyr || activate_code(ForeignCodeBase, u1) #endif /* pyr */ ) { strcpy(Yap_ErrorSay," unable to allocate space for external code "); return LOAD_FAILLED; } #ifdef mips FCodeBase = (char *) (Unsigned(FCodeBase + PAGESIZE - 1) & ~(PAGESIZE - 1)); #endif /* now, a new incantation to load the new foreign code */ #ifdef convex /* No -N flag in the Convex loader */ /* -T option does not want MallocBase bit set */ sprintf(command, "ld -x -A %s -T %lx -o %s -u %s %s %s -lc", ostabf, ((unsigned long) (((unsigned long) (ForeignCodeBase)) & ((unsigned long) (~Yap_HeapBase)) ) ), tfile, entry_point, o_files, l_files); #else #ifdef mips sprintf(command, "ld -systype bsd43 -N -A %s -T %lx -o %s -u %s %s %s -lc", ostabf, (unsigned long) ForeignCodeBase, tfile, entry_point, o_files, l_files); #else sprintf(command, "ld -N -A %s -T %lx -o %s -e %s -u _%s %s -lc", ostabf, (unsigned long) ForeignCodeBase, tfile, entry_point, o_files, l_files); #endif /* mips */ #endif /* convex */ /* and do it */ if (system(command) != 0) { unlink(tfile); strcpy(Yap_ErrorSay," ld returned error status in load_foreign_files "); return LOAD_FAILLED; } if ((fildes = open(tfile, O_RDONLY)) < 0) { strcpy(Yap_ErrorSay," unable to open temp file in load_foreign_files "); return LOAD_FAILLED; } read(fildes, (char *) &fileHeader, sizeof(fileHeader)); read(fildes, (char *) &sysHeader, sizeof(sysHeader)); { int i; for (i = 0; i < fileHeader.f_nscns; i++) read(fildes, (char *) §ionHeader[i], sizeof(*sectionHeader)); } loadImageSize = sysHeader.tsize + sysHeader.dsize + sysHeader.bsize; if (firstloadImSz < loadImageSize) { strcpy(Yap_ErrorSay," miscalculation in load_foreign/3 "); return LOAD_FAILLED; } /* now search for our init function */ { char entry_fun[256]; struct nlist func_info[2]; #if defined(mips) || defined(I386) char NAME1[128], NAME2[128]; func_info[0].n_name = NAME1; func_info[1].n_name = NAME2; #endif /* COFF */ sprintf(entry_fun, "_%s", proc_name); func_info[0].n_name = entry_fun; func_info[1].n_name = NULL; if (nlist(tfile, func_info) == -1) { strcpy(Yap_ErrorSay," in nlist(3) "); return LOAD_FAILLED; } if (func_info[0].n_type == 0) { strcpy(Yap_ErrorSay," in nlist(3) "); return LOAD_FAILLED; } *init_proc = (YapInitProc)(func_info[0].n_value); } /* ok, we got our init point */ /* now read our text */ lseek(fildes, (long)(N_TXTOFF(header)), 0); { unsigned int u1 = header.a_text + header.a_data; read(fildes, (char *) FCodeBase, u1); /* zero the BSS segment */ while (u1 < loadImageSize) FCodeBase[u1++] = 0; } close(fildes); unlink(tfile); return LOAD_SUCCEEDED; }
void MRU_updateWidget(std::size_t index, const char *filename) { EscapedMnemonic mnemonic(64); mnemonic << Unsigned(index + 1) << "- " << filename; gtk_label_set_text_with_mnemonic(GTK_LABEL(gtk_bin_get_child(GTK_BIN(MRU_items[index]))), mnemonic.c_str()); }
inline static Int compare(Term t1, Term t2) /* compare terms t1 and t2 */ { if (t1 == t2) return 0; if (IsVarTerm(t1)) { if (IsVarTerm(t2)) return Signed(t1) - Signed(t2); return -1; } else if (IsVarTerm(t2)) { /* get rid of variables */ return 1; } if (IsAtomOrIntTerm(t1)) { if (IsAtomTerm(t1)) { if (IsAtomTerm(t2)) return cmp_atoms(AtomOfTerm(t1), AtomOfTerm(t2)); if (IsPrimitiveTerm(t2)) return 1; if (IsStringTerm(t2)) return 1; return -1; } else { if (IsIntTerm(t2)) { return IntOfTerm(t1) - IntOfTerm(t2); } if (IsApplTerm(t2)) { Functor fun2 = FunctorOfTerm(t2); switch ((CELL)fun2) { case double_e: return 1; case long_int_e: return IntOfTerm(t1) - LongIntOfTerm(t2); #ifdef USE_GMP case big_int_e: return Yap_gmp_tcmp_int_big(IntOfTerm(t1), t2); #endif case db_ref_e: return 1; case string_e: return -1; } } return -1; } } else if (IsPairTerm(t1)) { if (IsApplTerm(t2)) { Functor f = FunctorOfTerm(t2); if (IsExtensionFunctor(f)) return 1; else { if (f != FunctorDot) return strcmp(".", RepAtom(NameOfFunctor(f))->StrOfAE); else { return compare_complex(RepPair(t1) - 1, RepPair(t1) + 1, RepAppl(t2)); } } } if (IsPairTerm(t2)) { return ( compare_complex(RepPair(t1) - 1, RepPair(t1) + 1, RepPair(t2) - 1)); } else return 1; } else { /* compound term */ Functor fun1 = FunctorOfTerm(t1); if (IsExtensionFunctor(fun1)) { /* float, long, big, dbref */ switch ((CELL)fun1) { case double_e: { if (IsFloatTerm(t2)) return (rfloat(FloatOfTerm(t1) - FloatOfTerm(t2))); if (IsRefTerm(t2)) return 1; return -1; } case long_int_e: { if (IsIntTerm(t2)) return LongIntOfTerm(t1) - IntOfTerm(t2); if (IsFloatTerm(t2)) { return 1; } if (IsLongIntTerm(t2)) return LongIntOfTerm(t1) - LongIntOfTerm(t2); #ifdef USE_GMP if (IsBigIntTerm(t2)) { return Yap_gmp_tcmp_int_big(LongIntOfTerm(t1), t2); } #endif if (IsRefTerm(t2)) return 1; return -1; } #ifdef USE_GMP case big_int_e: { if (IsIntTerm(t2)) return Yap_gmp_tcmp_big_int(t1, IntOfTerm(t2)); if (IsFloatTerm(t2)) { return 1; } if (IsLongIntTerm(t2)) return Yap_gmp_tcmp_big_int(t1, LongIntOfTerm(t2)); if (IsBigIntTerm(t2)) { return Yap_gmp_tcmp_big_big(t1, t2); } if (IsRefTerm(t2)) return 1; return -1; } #endif case string_e: { if (IsApplTerm(t2)) { Functor fun2 = FunctorOfTerm(t2); switch ((CELL)fun2) { case double_e: return 1; case long_int_e: return 1; #ifdef USE_GMP case big_int_e: return 1; #endif case db_ref_e: return 1; case string_e: return strcmp((char *)StringOfTerm(t1), (char *)StringOfTerm(t2)); } return -1; } return -1; } case db_ref_e: if (IsRefTerm(t2)) return Unsigned(RefOfTerm(t2)) - Unsigned(RefOfTerm(t1)); return -1; } } if (!IsApplTerm(t2)) { if (IsPairTerm(t2)) { Int out; Functor f = FunctorOfTerm(t1); if (!(out = ArityOfFunctor(f)) - 2) out = strcmp((char *)RepAtom(NameOfFunctor(f))->StrOfAE, "."); return out; } return 1; } else { Functor fun2 = FunctorOfTerm(t2); Int r; if (IsExtensionFunctor(fun2)) { return 1; } r = ArityOfFunctor(fun1) - ArityOfFunctor(fun2); if (r) return r; r = cmp_atoms(NameOfFunctor(fun1), NameOfFunctor(fun2)); if (r) return r; else return (compare_complex(RepAppl(t1), RepAppl(t1) + ArityOfFunctor(fun1), RepAppl(t2))); } } }
static Int compare_complex(register CELL *pt0, register CELL *pt0_end, register CELL *pt1) { CACHE_REGS register CELL **to_visit = (CELL **)HR; register Int out = 0; loop: while (pt0 < pt0_end) { register CELL d0, d1; ++pt0; ++pt1; d0 = Derefa(pt0); d1 = Derefa(pt1); if (IsVarTerm(d0)) { if (IsVarTerm(d1)) { out = Signed(d0) - Signed(d1); if (out) goto done; } else { out = -1; goto done; } } else if (IsVarTerm(d1)) { out = 1; goto done; } else { if (d0 == d1) continue; else if (IsAtomTerm(d0)) { if (IsAtomTerm(d1)) out = cmp_atoms(AtomOfTerm(d0), AtomOfTerm(d1)); else if (IsPrimitiveTerm(d1)) out = 1; else out = -1; /* I know out must be != 0 */ goto done; } else if (IsIntTerm(d0)) { if (IsIntTerm(d1)) out = IntOfTerm(d0) - IntOfTerm(d1); else if (IsFloatTerm(d1)) { out = 1; } else if (IsLongIntTerm(d1)) { out = IntOfTerm(d0) - LongIntOfTerm(d1); #ifdef USE_GMP } else if (IsBigIntTerm(d1)) { out = Yap_gmp_tcmp_int_big(IntOfTerm(d0), d1); #endif } else if (IsRefTerm(d1)) out = 1; else out = -1; if (out != 0) goto done; } else if (IsFloatTerm(d0)) { if (IsFloatTerm(d1)) { out = rfloat(FloatOfTerm(d0) - FloatOfTerm(d1)); } else if (IsRefTerm(d1)) { out = 1; } else { out = -1; } if (out != 0) goto done; } else if (IsStringTerm(d0)) { if (IsStringTerm(d1)) { out = strcmp((char *)StringOfTerm(d0), (char *)StringOfTerm(d1)); } else if (IsIntTerm(d1)) out = 1; else if (IsFloatTerm(d1)) { out = 1; } else if (IsLongIntTerm(d1)) { out = 1; #ifdef USE_GMP } else if (IsBigIntTerm(d1)) { out = 1; #endif } else if (IsRefTerm(d1)) { out = 1; } else { out = -1; } if (out != 0) goto done; } else if (IsLongIntTerm(d0)) { if (IsIntTerm(d1)) out = LongIntOfTerm(d0) - IntOfTerm(d1); else if (IsFloatTerm(d1)) { out = 1; } else if (IsLongIntTerm(d1)) { out = LongIntOfTerm(d0) - LongIntOfTerm(d1); #ifdef USE_GMP } else if (IsBigIntTerm(d1)) { out = Yap_gmp_tcmp_int_big(LongIntOfTerm(d0), d1); #endif } else if (IsRefTerm(d1)) { out = 1; } else { out = -1; } if (out != 0) goto done; } #ifdef USE_GMP else if (IsBigIntTerm(d0)) { if (IsIntTerm(d1)) { out = Yap_gmp_tcmp_big_int(d0, IntOfTerm(d1)); } else if (IsFloatTerm(d1)) { out = 1; } else if (IsLongIntTerm(d1)) { out = Yap_gmp_tcmp_big_int(d0, LongIntOfTerm(d1)); } else if (IsBigIntTerm(d1)) { out = Yap_gmp_tcmp_big_big(d0, d1); } else if (IsRefTerm(d1)) out = 1; else out = -1; if (out != 0) goto done; } #endif else if (IsPairTerm(d0)) { if (!IsPairTerm(d1)) { if (IsApplTerm(d1)) { Functor f = FunctorOfTerm(d1); if (IsExtensionFunctor(f)) out = 1; else if (!(out = 2 - ArityOfFunctor(f))) out = strcmp(".", (char *)RepAtom(NameOfFunctor(f))->StrOfAE); } else out = 1; goto done; } #ifdef RATIONAL_TREES to_visit[0] = pt0; to_visit[1] = pt0_end; to_visit[2] = pt1; to_visit[3] = (CELL *)*pt0; to_visit += 4; *pt0 = d1; #else /* store the terms to visit */ if (pt0 < pt0_end) { to_visit[0] = pt0; to_visit[1] = pt0_end; to_visit[2] = pt1; to_visit += 3; } #endif pt0 = RepPair(d0) - 1; pt0_end = RepPair(d0) + 1; pt1 = RepPair(d1) - 1; continue; } else if (IsRefTerm(d0)) { if (IsRefTerm(d1)) out = Unsigned(RefOfTerm(d1)) - Unsigned(RefOfTerm(d0)); else out = -1; goto done; } else if (IsApplTerm(d0)) { register Functor f; register CELL *ap2, *ap3; if (!IsApplTerm(d1)) { out = 1; goto done; } else { /* store the terms to visit */ Functor f2; ap2 = RepAppl(d0); ap3 = RepAppl(d1); f = (Functor)(*ap2); if (IsExtensionFunctor(f)) { out = 1; goto done; } f2 = (Functor)(*ap3); if (IsExtensionFunctor(f2)) { out = -1; goto done; } /* compare functors */ if (f != (Functor)*ap3) { if (!(out = ArityOfFunctor(f) - ArityOfFunctor(f2))) out = cmp_atoms(NameOfFunctor(f), NameOfFunctor(f2)); goto done; } #ifdef RATIONAL_TREES to_visit[0] = pt0; to_visit[1] = pt0_end; to_visit[2] = pt1; to_visit[3] = (CELL *)*pt0; to_visit += 4; *pt0 = d1; #else /* store the terms to visit */ if (pt0 < pt0_end) { to_visit[0] = pt0; to_visit[1] = pt0_end; to_visit[2] = pt1; to_visit += 3; } #endif d0 = ArityOfFunctor(f); pt0 = ap2; pt0_end = ap2 + d0; pt1 = ap3; continue; } } } } /* Do we still have compound terms to visit */ if (to_visit > (CELL **)HR) { #ifdef RATIONAL_TREES to_visit -= 4; pt0 = to_visit[0]; pt0_end = to_visit[1]; pt1 = to_visit[2]; *pt0 = (CELL)to_visit[3]; #else to_visit -= 3; pt0 = to_visit[0]; pt0_end = to_visit[1]; pt1 = to_visit[2]; #endif goto loop; } done: /* failure */ #ifdef RATIONAL_TREES while (to_visit > (CELL **)HR) { to_visit -= 4; pt0 = to_visit[0]; pt0_end = to_visit[1]; pt1 = to_visit[2]; *pt0 = (CELL)to_visit[3]; } #endif return (out); }
void PlugInMenu_Add( GtkMenu* plugin_menu, IPlugIn* pPlugIn ){ GtkWidget *menu, *item, *parent, *subMenu; const char *menuText, *menuCommand; WidgetStack menuStack; parent = gtk_menu_item_new_with_label( pPlugIn->getMenuName() ); gtk_widget_show( parent ); gtk_container_add( GTK_CONTAINER( plugin_menu ), parent ); std::size_t nCount = pPlugIn->getCommandCount(); if ( nCount > 0 ) { menu = gtk_menu_new(); if ( g_Layout_enableDetachableMenus.m_value ) { menu_tearoff( GTK_MENU( menu ) ); } while ( nCount > 0 ) { menuText = pPlugIn->getCommandTitle( --nCount ); menuCommand = pPlugIn->getCommand( nCount ); if ( menuText != 0 && strlen( menuText ) > 0 ) { if ( !strcmp( menuText, "-" ) ) { item = gtk_menu_item_new(); gtk_widget_set_sensitive( item, FALSE ); } else if ( !strcmp( menuText, ">" ) ) { menuText = pPlugIn->getCommandTitle( --nCount ); menuCommand = pPlugIn->getCommand( nCount ); if ( !strcmp( menuText, "-" ) || !strcmp( menuText, ">" ) || !strcmp( menuText, "<" ) ) { globalErrorStream() << pPlugIn->getMenuName() << " Invalid title (" << menuText << ") for submenu.\n"; continue; } item = gtk_menu_item_new_with_label( menuText ); gtk_widget_show( item ); gtk_container_add( GTK_CONTAINER( menu ), item ); subMenu = gtk_menu_new(); gtk_menu_item_set_submenu( GTK_MENU_ITEM( item ), subMenu ); menuStack.push( menu ); menu = subMenu; continue; } else if ( !strcmp( menuText, "<" ) ) { if ( !menuStack.empty() ) { menu = menuStack.top(); menuStack.pop(); } else { globalErrorStream() << pPlugIn->getMenuName() << ": Attempt to end non-existent submenu ignored.\n"; } continue; } else { item = gtk_menu_item_new_with_label( menuText ); g_object_set_data( G_OBJECT( item ),"command", const_cast<gpointer>( static_cast<const void*>( menuCommand ) ) ); g_signal_connect( G_OBJECT( item ), "activate", G_CALLBACK( plugin_activated ), gint_to_pointer( m_nNextPlugInID ) ); } gtk_widget_show( item ); gtk_container_add( GTK_CONTAINER( menu ), item ); pPlugIn->addMenuID( m_nNextPlugInID++ ); } } if ( !menuStack.empty() ) { std::size_t size = menuStack.size(); if ( size != 0 ) { globalErrorStream() << pPlugIn->getMenuName() << " mismatched > <. " << Unsigned( size ) << " submenu(s) not closed.\n"; } for ( std::size_t i = 0; i < ( size - 1 ); i++ ) { menuStack.pop(); } menu = menuStack.top(); menuStack.pop(); } gtk_menu_item_set_submenu( GTK_MENU_ITEM( parent ), menu ); } }