8
I am developing an android application where it will be possible to write a java code and run... something similar to the visualg.
then I did something different than most tutorials on compilers and pseudocodes would say... where a machine code written in Assembly would have to be generated, or maybe just a java bytecode...
in pc :(text --> intermediate code --> machine code --> Executes)
on android:(*. java --> java.class --> classes.Dex --> ART interprets -->Runs)
but as the creation of such a compiler is too complex I decided to make an interpreter at runtime, and what it does is compile the text into "micro-operations", such as Sum, Subtraction, Multiplication, Println, etc...
then he does the following:
(text --> creates micro-generations --> performs each micro-generation)
the structure of the interpreter is similar to this:
now I wanted to ask you if there’s a way to optimize this code,
doing some tests with the emulator, I saw that it is possible to run up to 79 million micro-operations in 1 second, which is about 12 times slower than a real 1.0 gigaherts processor.
but wanted to find out if the way I put it all together is really something new or if there are interpreted languages that work in a similar way to this (should consume a lot of memory RAM)
any suggestions on how to improve/optimize this project?
example of one of the executable classes (all others are equal only changes the type and the operator)
public final class SomarDoubles extends Executavel {
final WDouble a;
final WDouble b;
final WDouble c;
public SomarDoubles(WDouble a, WDouble b,WDouble c)
{
this.a = a;
this.b = b;
this.c = c;
}
@Override
final public void Executar() {
c.valor = a.valor+b.valor;
}
}
example of a type class (same thing, only changes the value contained):
public final class WByte extends Valor {
byte valor = 0;
public WByte(byte v) {
this.valor = v;
}
// métodos de casting:
@Override
final public short Short() {
return valor;
}
@Override
final public char Char() {
return (char)valor;
}
// etc...
}
and the code when executing (the compilation is done before and sends the array and instructions)
public class Executar extends Thread{
private final Executavel[] instrucoes;
private final int length;
public static int i=0;
public Executar(Executavel[] instrucoes)
{
super();
this.instrucoes = instrucoes;
length = instrucoes.length;
}
@Override
final public void run()
{
i=0;
long delay = System.nanoTime();
long k=0;
try {
while(i<length)
{
instrucoes[i++].Executar();
k++;
}
} catch (Exception e) {
e.printStackTrace();
}
long agora= System.nanoTime();
double tempo = (agora-delay);
System.out.println("emulado : executou "+k+" instrucoes em "+tempo/1000000.0d+" milisegundos ("+tempo/1000000000.0d+" segundos), o que d� \n"
+ " aproximadamente "+tempo/k+" nanosegundos por instrucao");
}
}
Thanks for responding to me so quickly and with multiple sources. will certainly interpret is slower, but will be used with "moderation", after all will run in an android application with just learning purpose, if the code takes 0.4 nanoseconds or 60 nanoseconds to sum 2 integers does not make much difference...
– Erick Weil
if you want to know how I interpret the notation of the Reverse Polish Notation (does not use parentheses) converter: http://andreinc.net/2010/10/05/converting-infix-to-rpn-shunting-yard-algorithm/ resolver: http://www.java2s.com/Code/Java/Collections-Data-Structure/Reversepolishnotation.htm I just adapted and expanded these two algorithms so that they could support most programming operators ( =, <= ,>= , ==, *=, /=, etc... ) and instead of calculating save the operation as an object of one of the child types of Executable with the values a,b, and c, which are added to the array
– Erick Weil