Skip to main content

Preserving Lexical Scoping When Dynamically Embedding Languages

  • Conference paper
  • First Online:
Programming Languages (SBLP 2015)

Part of the book series: Lecture Notes in Computer Science ((LNPSE,volume 9325))

Included in the following conference series:

  • 454 Accesses

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.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Subscribe and save

Springer+ Basic
$34.99 /Month
  • Get 10 units per month
  • Download Article/Chapter or eBook
  • 1 Unit = 1 Article or 1 Chapter
  • Cancel anytime
Subscribe now

Buy Now

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Similar content being viewed by others

Notes

  1. 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. 2.

    The semantics of Lua Core in [4] is based on an environment \(\varSigma =(\varGamma ,S,F)\) where F is specific to Terra functions. In our presentation, we removed F. Rules reused from [4] were adapted accordingly.

References

  1. 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)

    Google Scholar 

  2. 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)

    Chapter  Google Scholar 

  3. 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)

    Chapter  Google Scholar 

  4. 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)

    Google Scholar 

  5. Fleutot, F.: Metalua: static meta-programming for Lua. https://github.com/fab13n/metalua (2007). Accessed February 2015

  6. Fleutot, F., Tratt, L.: Contrasting compile-time meta-programming in Metalua and converge. In: Proceedings of the Workshop on Dynamic Languages and Applications (2007)

    Google Scholar 

  7. Fowler, M.: Domain Specific Languages, 1st edn. Addison-Wesley Professional, Boston (2010)

    Google Scholar 

  8. Ierusalimschy, R.: Programming in Lua, 2nd edn. Lua.Org (2006)

    Google Scholar 

  9. 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)

    Google Scholar 

  10. Maidl, A.M.: Lua-parser: a Lua 5.3 parser written with LPeg. https://github.com/andremm/lua-parser (2013). Accessed April 2015

  11. McCarthy, J.: Recursive functions of symbolic expressions and their computation by machine, part I. Commun. ACM 3(4), 184–195 (1960)

    Article  MATH  Google Scholar 

  12. Microsoft: LINQ. https://msdn.microsoft.com/en-us/library/bb397926.aspx (2013). Accessed April 2015

  13. Muhammad, H.: LuaDec: a decompiler for the Lua language. http://luadec.luaforge.net/ (2006). Accessed April 2015

  14. Taha, W.: Multi-stage programming: its theory and applications. Ph.D thesis, Oregon Graduate Institute of Science and Technology (1999)

    Google Scholar 

  15. 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)

    Chapter  Google Scholar 

  16. 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)

    Chapter  Google Scholar 

  17. 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)

    Google Scholar 

Download references

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Félix Ribeiro .

Editor information

Editors and Affiliations

Rights and permissions

Reprints 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)

Publish with us

Policies and ethics