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

Hello

Hello What I expect from my Design and Architecture course is to learn all the concepts shown and understand them fully, and I'm very excited to learn the programming language, Ruby. I have different hobbies such as sports like climbing and boulder, riding bicycle, basketball. Also I love nature, I really like to go camping, grow plants, and I like animals. Other hobbies I really like is electronics, like building and learning about music synths, MIDI Controllers, and other stuff related. Music and art have been a very important part of my life, i play the guitar and other instruments, I like to paint. Here is a following list of the books that I really like: The teachings of Don Juan - Carlos Castaneda Siddharta - Herman Hesse Demian - Herman Hesse Brave New World - Aldous Huxley Batallas en el desierto - José Emilio Pacheco Dos Crímenes - Jorge Ibarguengoitia Aura - Carlos Fuentes 1984 - George Orwell Animal Farm - George Orwell Pulp - Charles Bukowski Hol...

Software Architecture

For developing a very efficient system, it has to me modulated, in a way each module has its own functions and communicates with other modules through the whole system. In order to maintain the efficiency of this system is by analyzing every module at its own way. Seeing each and every module as a black box, that receives information from other modules to perform a very complex task, the way of looking to all modules as a black box is in point of view from seeing the whole complete system. But like a microscope, by observing each module will give the information of that special feature each modules performs. By mentioning modules it also comes the term component, in a well designed system, it has to have a balance between modules and component, trying to assure low coupling and high cohesion. Depending on the systems problem complexity, it as a certain type of architecture, such as, layered architecture, pipe and filter architecture, client server architecture, component based architec...

Moon Machines EP3: Navigation Computer

It is very interesting the process behind vast projects such as everything that involve getting a Rocket to the Moon. Is the continuous testing and researching into new technologies and ways to figure and solve all problems presented in the way, what really catches my attention is the whole hard work into the modules that conforms the complete project. As shown in the documentary, it is visible that a vast team of physicists and engineers can create incredible things. The development of new technologies is a new portal of discovery, because being new, its potential is very unknown. Seeing al the progress they did , in a very analog way and the techniques of storing data are the very roots of the technology we have today. Thinking about history, I ask myself , how could it be all the possible approaches in order to complete this task/project. All the key minds that manage to give and solve, the solutions in a very short period of time, show how the improvisation is made, be...