Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Mata function saving conventions and best practices


From   "David Elliott" <[email protected]>
To   [email protected]
Subject   Re: st: Mata function saving conventions and best practices
Date   Wed, 20 Jun 2007 16:54:04 -0300

Bill, that is a lot longer than a "no."

Bill has quite rightly connected the dots between my two recent
questions.  I was writing a -mo_compile_ and -mlib_compile- routine
and wanted them to work from standard *.mata source files.

Below is a beta version 1.0.0 of -mo_compile- that will run a number
of mata source files and then save the compiled object code to *.mo
files.  I was able to get around the not being able to capture
function names in mata memory ( question asked in thread:
http://www.stata.com/statalist/archive/2007-06/msg00660.html ) with an
ugly kludge of logging a -mata describe- and then picking the
functions out of the text.  The advantage of being able to compile
like this is that one could process a long file list, or have multiple
functions defined in a single source, and still be able to save each
of the functions without the labour of identifying them individually.

In many ways, compiling to a library is easier because once can use a
*() wildcard.

I'd appreciate it if some folks could test the program on different
platforms as well as relate to me any errors they throw. (as always,
check for obvious wrapped lines - the mata portion is pretty immune to
the effect of wraps but ado code is not) I haven't built in any
checking of whether the source files actually exist, or whether a mata
session has already defined functions.  Be aware that the program
issues a -mata: mata clear- that will wipe out any previously defined
mata objects.

Cheers,

DCE


----------mo_compile.ado--------
program define mo_compile
version 9.0

*! version 1.0.0  2007.06.18
*! Compiles and saves object code of a mata source file(s) (*.mata)
*! by David C. Elliott
*! syntax examples:
*! mo_compile ,s(c:\ado\func1.mata c:\ado\func2.mata) dir(PERSONAL) replace
*! mo_compile ,s(c:\ado\manyfunc.mata)

syntax  ,Source(str) [DIR(str) REPLACE]

mata: mata clear
tokenize `source'
local i=1

while "``i''" !="" {
   run `"``i++''"'
   }

tempfile f_log
capture quietly log using `f_log', text replace
mata: mata describe
quietly log close
mata: get_functionnames("`f_log'")
di _n "{txt:Compiled functions:}"

foreach  j of local f_list {
   quietly mata: mata mosave `j', dir("`dir'") `replace'
   di "{res:    `j'}"
   }

end

mata:
void get_functionnames(string scalar log_name)
   {
   string scalar log_line, obj_line, f_list
   string colvector log
   log = cat(log_name)
   for (i=3; i<rows(log)-2; i++) {
       log_line = strreverse(log[i,1])
       obj_name = strreverse(
           substr(log_line,1,(strpos(log_line," ")-1)))
       if (strmatch(obj_name,"*()")) {
           f_list = f_list + " " + obj_name
           }
       }
   st_local("f_list",f_list)
   }
end
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index