Ruby and the Interpreter Pattern
Language Design and Implementation using Ruby
and the Interpreter Pattern
The S-expression Interpreter Framework (SIF) is a tool for teaching language design and implementation which has been used to aid at programming languages course at Tecnológico de Monterrey, Campus Estado de México. It was written in Ruby to make students learn an object-oriented dynamic language, while learning language design and implementation concepts(1).
But, SIF?
S-Expressions were selected as the framework's source language notation of the Lisp family languages because they offer(1):
The S-expression Interpreter Framework (SIF) is a tool for teaching language design and implementation which has been used to aid at programming languages course at Tecnológico de Monterrey, Campus Estado de México. It was written in Ruby to make students learn an object-oriented dynamic language, while learning language design and implementation concepts(1).
But, SIF?
S-Expressions were selected as the framework's source language notation of the Lisp family languages because they offer(1):
- Both code and data can be easily represented. Adding a new language construct is pretty straightforward.
- The number of program elements is minimal, thus a parser is relatively easy to write.
- The arity of operators doesn’t need to be fixed, so, for example, the + operator can be defined to accept any number of arguments.
- There are no problems regarding operator precedence or associativity, because every operator has its own pair of parentheses.
The interpreter Pattern is a behavoral pattern (1) where a program is portrayed as a tree in which every construct or element is represented by a specific kind of node. Every node responds to a special operation called interpret, responsible for performing the expected behavior associated with the corresponding language element. The interpret Operation receives a context as a parameter.
What is Ruby?
Ruby is an interpreted, dynamically typed programming language with several simplicities on the construction of language interpreters (1):
- Built-in regular expressions, required to build a lexical analyzer or scanner.
- Garbage collection, which allows the interpreted languages to also benefit from automatic memory management.
- Built-in hashes can be used as symbol tables, while primitive symbol data values can be used as efficient hash keys.
- Open classes, which simplify using predefined Ruby classes to represent the interpreter’s basic data types, since new methods can always be added to these classes if needed.
- First class continuations, which permit the implementation of special control-flow instructions such as arbitrary loop breaks and exception handlers.
Everything in Ruby is an object, including: strings, numbers,
arrays (lists), hashes (maps or dictionaries), symbols, procedures
(closures), and even classes. Ruby has some naming conventions(1):
- Local variables, method parameters, and method names should all start with a lowercase letter or with an underscore.
- Global variables are prefixed with a dollar sign ($).
- Instance variables begin with an “at” sign (@).
- Class variables start with two “at'” signs (@@).
- Class names, module names, and constants should start with an uppercase letter.
SIF only supports integers,
symbols, lists, and procedures. Which are evaluated as follows (1):
- A number evaluates to itself.
- A symbol is considered to be a variable reference, so it evaluates to the value that it’s bound to. If the variable is unbound, a runtime error is raised.
- An empty list evaluates to itself
- A non-empty list is considered a procedure application. The first element of the list must evaluate to a “callable” object. An object is callable if it contains a method named call (this is true also for standard Ruby Proc objects). A runtime error is raised if no callable object is found at the beginning of the list. The remainder of the list elements are evaluated and sent as arguments to the procedure being called. A runtime error is also raised if the number of actual and formal parameters doesn’t match.
Speaking of the Programming Languages course the S-expression interpreter Framework (SIF) takes an important place in the aid of students allowing them to learn such an increasingly popular object-oriented dynamic language as Ruby which has the proper structure and function to fit the purpose already said, such is the importance of learning Ruby that going through the course without it would make a huge impact.
References:
1.-http://34.212.143.74/publicaciones/sif.pdf
Comentarios
Publicar un comentario