<p>If there are any language designers out there (or people simply in the know), I'm curious about the methodology behind creating standard libraries for interpreted languages. Specifically, what seems to be the best approach? Defining standard functions/methods in the interpreted language, or performing the processing of those calls in the compiled language in which the interpreter is written?</p>

<p>What got me to thinking about this was the SO question about a stripslashes()-like function in Python. My first thought was "why not define your own and just call it when you need it", but it raised the question: is it preferable, for such a function, to let the interpreted language handle that overhead, or would it be better to write an extension and leverage the compiled language behind the interpreter?</p>

[{'answer_id': 13605, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 4, 'selected': True, 'text': '<p>The line between "interpreted" and "compiled" languages is really fuzzy these days. For example, the first thing Python does when it sees source code is compile it into a bytecode representation, essentially the same as what Java does when compiling class files. This is what *.pyc files contain. Then, the python runtime executes the bytecode without referring to the original source. Traditionally, a purely interpreted language would refer to the source code continuously when executing the program.</p>\n\n<p>When building a language, it is a good approach to build a solid foundation on which you can implement the higher level functions. If you\'ve got a solid, fast string handling system, then the language designer can (and should) implement something like stripslashes() outside the base runtime. This is done for at least a few reasons:</p>\n\n<ul>\n<li>The language designer can show that the language is flexible enough to handle that kind of task.</li>\n<li>The language designer actually writes real code in the language, which has tests and therefore shows that the foundation is solid.</li>\n<li>Other people can more easily read, borrow, and even change the higher level function without having to be able to build or even understand the language core.</li>\n</ul>\n\n<p>Just because a language like Python compiles to bytecode and executes that doesn\'t mean it is slow. There\'s no reason why somebody couldn\'t write a Just-In-Time (JIT) compiler for Python, along the lines of what Java and .NET already do, to further increase the performance. In fact, IronPython compiles Python directly to .NET bytecode, which is then run using the .NET system including the JIT.</p>\n\n<p>To answer your question directly, the only time a language designer would implement a function in the language behind the runtime (eg. C in the case of Python) would be to maximise the performance of that function. This is why modules such as the regular expression parser are written in C rather than native Python. On the other hand, a module like getopt.py is implemented in pure Python because it can all be done there and there\'s no benefit to using the corresponding C library.</p>\n'}, {'answer_id': 13627, 'author': 'Curt Hagenlocher', 'author_id': 533, 'author_profile': 'https://Stackoverflow.com/users/533', 'pm_score': 2, 'selected': False, 'text': '<p>There\'s also an increasing trend of reimplementing languages that are traditionally considered "interpreted" onto a platform like the JVM or CLR -- and then allowing easy access to "native" code for interoperability.  So from Jython and JRuby, you can easily access Java code, and from IronPython and IronRuby, you can easily access .NET code.</p>\n\n<p>In cases like these, the ability to "leverage the compiled language behind the interpreter" could be described as the primary motivator for the new implementation.</p>\n'}, {'answer_id': 13632, 'author': 'Judge Maygarden', 'author_id': 1491, 'author_profile': 'https://Stackoverflow.com/users/1491', 'pm_score': 1, 'selected': False, 'text': '<p>As long as you are using a portable API for the compiled code base like the <a href="http://en.wikipedia.org/wiki/C_standard_library" rel="nofollow noreferrer">ANSI C standard library</a> or <a href="http://en.wikipedia.org/wiki/Standard_Template_Library" rel="nofollow noreferrer">STL</a> in C++, then taking advantage of those functions would keep you from reinventing the wheel and likely provide a smaller, faster interpreter. <a href="http://www.lua.org/" rel="nofollow noreferrer">Lua</a> takes this approach and it is definitely small and fast as compared to many others.</p>\n'}, {'answer_id': 86866, 'author': 'akauppi', 'author_id': 14455, 'author_profile': 'https://Stackoverflow.com/users/14455', 'pm_score': 2, 'selected': False, 'text': '<p>See the \'Papers\' section at <a href="http://www.lua.org/docs.html" rel="nofollow noreferrer">www.lua.org</a>.</p>\n\n<p>Especially <a href="http://www.tecgraf.puc-rio.br/~lhf/ftp/doc/jucs05.pdf" rel="nofollow noreferrer">The Implementation of Lua 5.0</a></p>\n\n<p>Lua defines all standard functions in the underlying (ANSI C) code. I believe this is mostly for performance reasons. Recently, i.e. the \'string.*\' functions got an alternative implementation in pure Lua, which may prove vital for subprojects where Lua is run on top of .NET or Java runtime (where C code cannot be used).</p>\n'}]
['https://Stackoverflow.com/questions/13586', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1344/']