Tuesday, April 16, 2013

Program Embedding

My assignment for Programming Languages this week was to embed a programming language inside an existing programming to make it more extensible. We began with the Unix script program and embedded Lua inside of it so that we could use it to complete other actions. the Unix script program is something which records a log of the terminal session while running. In other words, at the end of the session you can read a file which shows all of the actions entered by the user and the output from the terminal.

In general, you can imagine the script file as follows (figures taken from CS131 assignment instructions):


The internal behavior of script can be visualized as:


Finally, we embed a language within script to add functionality:


To complete this process, we had to substantially modify the C source code for script and make it so that we could call functions written in Lua from C and vice versa. The final part of the homework assignment was to do something interesting with the new Lua embedding. One of the things that could be done was to change the way in which information is displayed on the screen. This is what my partner and I decided to do:


For every string that was supposed to be displayed to the user, we went through character by character and added the ansi commands required to change the color and then displayed the newly altered string to the user in the terminal. As you can see in this picture, we began by running the newly compile lscript. All of the text in white is the original color before the embedded code starts running. Then, once the code starts running, everything displayed and everything the user types becomes RAINBOW! As an example, I used cat on the rubric for this assignment so the terminal displayed the rubric in gorgeous colors.