Abstract
There are various situations in which one may want to embed source code from one language into another, for example when combining relational query languages with application code or when performing staged meta-programming. Typically, one will want to transfer data between these languages. We propose an approach in which the embedded code shares variables with the host language, preserving lexical scoping rules even after the code is converted into an intermediate representation. We demonstrate this approach through a module for meta-programming using Lua as both embedded and host languages. Our technique supports dynamically generated code, requires no special annotation of functions to be translated and is implemented as a library, requiring no source pre-processing or changes to the host language execution environment.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Similar content being viewed by others
Notes
- 1.
Note that in C# parlance, lambda expression is a more general term that can refer to both single-expression anonymous functions called expression lambdas and multi-statement functions called statement lambdas. Conversion to expression trees is only supported for expression lambdas.
- 2.
References
Bawden, A.: Quasiquotation in Lisp. In: Danvy, O. (ed.) Proceedings of the ACM SIGPLAN Workshop on Partial Evaluation and Semantics-Based Program Manipulation (PEPM 1999). Number NS-99-1 in BRICS Note Series, pp. 4–12, San Antonio, Texas (1999)
Calcagno, C., Taha, W., Huang, L., Leroy, X.: Implementing multi-stage languages using ASTs, gensym, and reflection. In: Pfenning, F., Macko, M. (eds.) GPCE 2003. LNCS, vol. 2830, pp. 57–76. Springer, Heidelberg (2003)
Czarnecki, K., O’Donnell, J.T., Striegnitz, J., Taha, W.: DSL implementation in MetaOCaml, template haskell, and C++. In: Lengauer, C., Batory, D., Blum, A., Odersky, M. (eds.) Domain-Specific Program Generation. LNCS, vol. 3016, pp. 51–72. Springer, Heidelberg (2004)
DeVito, Z., Hegarty, J., Aiken, A., Hanrahan, P., Vitek, J.: Terra: a multi-stage language for high-performance computing. In: Proceedings of the 34th ACM SIGPLAN Conference on Programming Language Design and Implementation, PLDI 2013, pp. 105–116, New York, NY, USA, ACM (2013)
Fleutot, F.: Metalua: static meta-programming for Lua. https://github.com/fab13n/metalua (2007). Accessed February 2015
Fleutot, F., Tratt, L.: Contrasting compile-time meta-programming in Metalua and converge. In: Proceedings of the Workshop on Dynamic Languages and Applications (2007)
Fowler, M.: Domain Specific Languages, 1st edn. Addison-Wesley Professional, Boston (2010)
Ierusalimschy, R.: Programming in Lua, 2nd edn. Lua.Org (2006)
Kameyama, Y., Kiselyov, O., Shan, C.C.: Closing the stage: from staged code to typed closures. In: Proceedings of the 2008 ACM SIGPLAN Symposium on Partial Evaluation and Semantics-based Program Manipulation, PEPM 2008, pp. 147–157, New York, NY, USA, ACM (2008)
Maidl, A.M.: Lua-parser: a Lua 5.3 parser written with LPeg. https://github.com/andremm/lua-parser (2013). Accessed April 2015
McCarthy, J.: Recursive functions of symbolic expressions and their computation by machine, part I. Commun. ACM 3(4), 184–195 (1960)
Microsoft: LINQ. https://msdn.microsoft.com/en-us/library/bb397926.aspx (2013). Accessed April 2015
Muhammad, H.: LuaDec: a decompiler for the Lua language. http://luadec.luaforge.net/ (2006). Accessed April 2015
Taha, W.: Multi-stage programming: its theory and applications. Ph.D thesis, Oregon Graduate Institute of Science and Technology (1999)
Taha, W.: A gentle introduction to multi-stage programming. In: Lengauer, C., Batory, D., Blum, A., Odersky, M. (eds.) Domain-Specific Program Generation. LNCS, vol. 3016, pp. 30–50. Springer, Heidelberg (2004)
Taha, W.: A gentle introduction to multi-stage programming, part II. In: Lämmel, R., Visser, J., Saraiva, J. (eds.) Generative and Transformational Techniques in Software Engineering II. LNCS, vol. 5235, pp. 260–290. Springer, Heidelberg (2008)
Westbrook, E., Ricken, M., Inoue, J., Yao, Y., Abdelatif, T., Taha, W.: Mint: java multi-stage programming using weak separability. In: Proceedings of the 2010 ACM SIGPLAN Conference on Programming Language Design and Implementation, PLDI 2010, pp. 400–411, New York, NY, USA, ACM (2010)
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
Rights and permissions
Copyright information
© 2015 Springer International Publishing Switzerland
About this paper
Cite this paper
Ribeiro, F., Muhammad, H., Maidl, A.M., Ierusalimschy, R. (2015). Preserving Lexical Scoping When Dynamically Embedding Languages. In: Pardo, A., Swierstra, S. (eds) Programming Languages. SBLP 2015. Lecture Notes in Computer Science(), vol 9325. Springer, Cham. https://doi.org/10.1007/978-3-319-24012-1_3
Download citation
DOI: https://doi.org/10.1007/978-3-319-24012-1_3
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-319-24011-4
Online ISBN: 978-3-319-24012-1
eBook Packages: Computer ScienceComputer Science (R0)