sábado, 15 de octubre de 2016

Ruby and the Interpreter Pattern



Today, I will talk about the article Language Design and Implementation using Ruby and the Interpreter Pattern by Ariel Ortiz. The paper describes in detail the S-expression Interpreter Framework (SIF), which aims to implement this design pattern and show some programming styles.

Although the relationship of the paper's title with the Compiler Design course may seem distant (because not even Ruby is used in the subject as a programming language), I can say that if we remember a little of what Saumya Debray said about compilers is that we have to consider them as mere translators, so although they are not built in the traditional way, the interpreter pattern basically performs the same function.

The central idea of the framework is based on the following:
For any special form being defined, identify its syntax and semantics.
The syntax must be checked in the initialize method,
while the semantics must be implemented in the interpret method.

Within the phases of a compiler we have the Lexical Analysis in which through the use of a scanner the tokens are obtained and these represent a stream of characters converted into meaningful lexemes, then the Syntax Analysis uses a parser to check if the expression made by the tokens is syntactically correct and the Semantic phase checks whether the parse tree constructed follows the rules of language. On the other hand, the Interpreter Pattern provides a way to evaluate language grammar or expression. And so as we can see the relationship they have both.

I found it quite interesting using the framework as a simple way to translate simple expressions with a specific context. It is possible to treat expressions with a very particular syntax as Clojure using Ruby. We have the possibility to extend the functionality of the framework and design languages with more complex functionality that was seen in the examples.

Finally, I think that the notion of compilers extends to many areas such as the Software Architecture, plus they are seen as translators or interpreters greatly facilitates their understanding.

References

Ortiz, A. (2008). Language Design and Implementation using Ruby and the Interpreter Pattern. Webcem. Retrieved 16 October 2016, from http://webcem01.cem.itesm.mx:8005/publicaciones/sif.pdf

jueves, 6 de octubre de 2016

Compile-Time Metaprogramming



In this new post for my blog I will talk about the podcast I just heard which is titled Compile-Time Metaprogramming and Laurence Tratt was the special guest. He is a programmer and researcher of programming languages, so that explains the general idea of the topic using Converge.

Unlike the topics I discussed in the last blog posts, DSL (domain specific language) is not new to me, because in the course of Software Design and Architecture we did a programming practice using this design pattern. What I understand is that focuses on language design and allows you to adapt the syntax used to solve problems of any kind.

The main topic of the podcast is Compile-time metaprogramming. This feature allow the user of a programming language a mechanism to Interact with the compiler. Also it related to the creation of a "domain-specific language" which is a way to improve the programming language and perform a specific task. Lisp, Clojure and many other languages have something called a macro, which allows the compiler to be extended by user code.

Reading a little bit about description of macro-like functionality in Converge. Compile-Time Meta-Programming (CTMP) is described as the most powerful cousin of macros, because unlike traditional macros, Converge works at the abstract syntax tree level.

In conclusion, I believe that if macros are already considered as a highly powerful tool because they modify and adapt the language to our convenience, then to manipulate the abstract syntax tree, is working on a deeper level of the compiler, where it is created a temporary module that is compiled and then injected into what is running at the time. From what I understand, the abstract syntax tree is not only used to check the proper use of language elements but is built at compile time.

References

Guide to compile-time metaprogramming. (n.d) Converge. Retrieved 6 October 2016, from http://convergepl.org/documentation/2.0/ctmp/