Ir al contenido principal

Metaprogramming

Create a program that writes programs, an interesting tool for optimizing time, generating automated code. I didn’t exactly know what metaprogramming was until today, while reading the article I remembered a program I wrote a while ago, called WAV shifter (https://github.com/k3ll3x/WAV-shifter/), the purpose of this code is to generate algorithmic music using the C language, an endless iterator, and bitwise operators. By looking at the code I see that there exists 3 source codes, play.sh, wavsh.c, wavsh.py; play.sh is a bash script that executes the compiled c file with an input audio file(that in a way is going to be modified), after that a wav file will be generated, and this is played by the aplay command. Note, this code only works in Unix systems, with pulseaudio installed. The Python file, wavsh.py, takes the source file wavsh.c and replaces a specific space on the source code, changing that with the input string given at the python program, that string could be a variable t modified with bitwise operators, such as t>>1; t<<&t; t|t; t^t; and so on. After that it takes the new generated code and compiles that with the GCC Compiler. Now the output audio file can be played.

The python script looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python
import subprocess
import signal
import sys

def signal_handler(sig, frame):
        print('You pressed Ctrl+C!')
        sys.exit(0)

def main():
 with open('wavsh.c', 'r') as myfile:
  data=myfile.read()#.replace('\n', '')
 input = raw_input('ncmd: ');
 ndata = data.replace("$",input)
 text_file = open("wwavsh.c", "w")
 text_file.write("%s" % ndata)
 text_file.close()
 ncmd = 'gcc wwavsh.c -lm'.split()
 rc = subprocess.call(ncmd);
 ncmd = 'rm wwavsh.c'.split()
 rc = subprocess.call(ncmd);
 ncmd = './play.sh'
 signal.signal(signal.SIGINT, signal_handler)
 rc = subprocess.call(ncmd.split());
 signal.pause()

 print('Press Ctrl+C')

main()

This application (WAV Shifter) is a very simple program, and very easy to use, the utilization from other programming languages in order to generate code (other programming language) is made, it is interesting this kind of used metaprogramming although I didn’t actually know it was that. Of course this is a simple example and is just a test project, but metaprogramming is very powerful and very useful.

References:

http://34.212.143.74/s201911/tc3049/metaprogramming.html
http://hilite.me/

Comentarios

Entradas más populares de este blog

Software Craftsmanship

Software Craftsmanship can be seen as a methodology in order to create good software, which has strong key elements that established the measures and techniques to create professional software development. It is the strong and complete philosophy to practice and helping others learn this craft, and as the Software Craftsmanship Manifesto ( http://manifesto.softwarecraftsmanship.org/ ) established some “rules” that completes this task. Such as the work on software that has to be performed to a well-crafted software, the response to change and the steadily adding value, the relationship between individuals and interactions and through the community of professionals developing software and finally customer collaboration with also productive partnership. Software Craftsmanship has a strong structure that can help the software industry in order to produce software quality, this philosophy can be applied in other topics. During the podcast with Bob Martin, or well known as “Uncle Bo...

Hidden Figures

Movies can become into an interesting resource to present certain stories and events that haven’t been known or not well known In the case of the movie, Hidden Figures , 3 main characters are being presented, which had an important role in history during the first steps of the Space Program in the United States, calculations had to be performed in order to get the right scenario for the rockets to l and. Of course dramatization is presented, it is Hollywood, but with the observation of the created cinematic context it is possible to get into the characters shoes and in a way experience their environment and actions, for that empathy grows, and the observers, see themselves involved into that movie scenario. Speaking of the movie content it is very noticeable the roles women had to endure in certain decades such as the 60’s, and in the sense of the movie, it is shown the context, scenario and roles, black women had to endure during this time. For that, the observer could had a good pe...

WarGames

How far technology can go? It is an often question everyone ask, including such philosophical questions about we humans and our creations, just like our Frankenstein Monster, our creation could bring chaos to ourselves. We have the power to create and the limit of our creation could be bypass by that same creation. This movie called War Games, bring this topic and it is an actual matter, and now even more because now a days technology had reached tremendous power and capacity that is very impressive, some tasks that we humans cannot do, or maybe as fast, are done by our own creation. Just like the arguments of the Android David in the movies Prometheus and Alien Covenant, David reached a conscious level that defined him to be as he was and committing every action during the movie. It is that self evolution that keeps growing and it hasn’t the same limits as human interpretation has. Another example is this classic movie 2001 A Space Odyssey, from S. Kubrik, HAL 9 000, who has e...