@global library_defined; /* Prototype implementation of a library system for l1. To define a library create a new file and include the following lines: @export(s1,s2,...) where s1,s2,... are the symbols you with exported from this library @import( args... ) where the elements of args may be either * a bare identifier, i.e. @import( lib1 ) in which case all symbols exported by lib1 are imported into the given file * a pair such as: (f1, lib1.f2) in which case the symbol f2 in lib1 will be visible in the current file as f1 Multiple import and export statements are allowed, however, they must exist outside of any complex statements. So, for instance, a library file "utils.cqct" might contain the following: @export( strcmp ); @define strcmp(a,b) { ... } The library file functions could then be used by another library @import( (mystrcmp, utils.strcmp) ); mystrcmp("a","b"); //calls the strcmp function in utils or they can be used in non-library cqct code with the @with_import macro: @with_import( (mystrcmp, utils.strcmp) ) { x = mystrcmp("a","b"); //calls the strcmp function in utils } */ if (library_defined == nil) { @global __library_exports; library_defined = 1; //__library_exports is a map from library identifies (cids) to a map from //exported symbols (also cids) to the value of those exported symbols //defined by that library. //This is not meant for general use. if (__library_exports == nil) //might not have been set in a macro __library_exports = [:]; //bare import statements are the sign of a problem -- let the user know @defstx @import(args...) { error("@import statements not allowed outside of libraries"); return #[]; } //by default, ignore export statments (XXX should this cause a warning?) @defstx @export(args...) { return #[]; } //this macro adds the provided path to the loadpath at macro expansion time. @defstx @add_loadpath(path) { @local p,lp; p = compile(path)(); lp = loadpath(); append(lp,p); setloadpath(lp); return #`{ nil; }; } //this macro allows some code to be run with bindings to some imported symbols @defstx @with_imports(imports...) body { @local new_body, new_locals, has_dot; if (__library_exports == nil) __library_exports = [:]; @defloc pathto(f) { @local i,paths,len; paths = loadpath(); len = length(paths); for(i=0;i