
how to write a simple interpreter in java that can execute simple commandes like adding two integers and sysout a result and calling java classes and executing the methodes in that class .

Well although there was nobody found to answer this tricky question I myself answered it :
The first class (main class):
package bb;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class main {
static Object o;
static int j=0;
static Object []ss=new Object[10];
static Object []dd=new Object[10];
static HashMap<String , Object> map= new HashMap<String, Object>();
public static void main(String[] args) throws Exception {
Scanner s=new Scanner(System.in);
System.out.println("Please Enter File Name !");
File x=new File(s.nextLine());
Scanner sc=new Scanner(x);
do{
String h=sc.nextLine();
if(h.contains("=")){
String[]hh=h.split(" ");
if(hh[2].equals("=")){
//Double� d� =� new� Double(14.7);
Class c=Class.forName(hh[0]);
if(h.contains("new")==true){
String co=hh[4].substring(hh[4].indexOf("(")+1, hh[4].length()-2);
String[] input=co.split(",");
Class []cInput=Getinput(input);
Object o=GetObject(c,cInput,ss);
map.put(hh[1], o);
}else{
//Long� l� =� d.LongValue();
if(map.containsKey(hh[3].substring(0, hh[3].indexOf(".")))==false){
MethodesFeilds mf=new MethodesFeilds (hh[3].substring(0,hh[3].length()-1),map);
Object o=mf.main();
map.put(hh[1], o);
}else{
Object o=map.get(hh[3].substring(0, hh[3].indexOf(".")));
Object o2=ObjectMethod(o,o.getClass(),hh[3].substring(hh[3].indexOf(".")+1,hh[3].length()-1));
map.put(hh[1], o2);
}
}
}else if(hh[1].equals("=")){
// d=new boolean(false);
Class c=map.get(hh[0]).getClass();
map.remove(hh[0]);
if(h.contains("new")==true){
String co=hh[3].substring(hh[3].indexOf("(")+1, hh[3].length()-2);
String[] input=co.split(",");
Class []cInput=Getinput(input);
Object o=GetObject(c,cInput,ss);
map.put(hh[0], o);
}else{
//� l� =� d.LongValue();
if(map.containsKey(hh[2].substring(0, hh[2].indexOf(".")))==false){
MethodesFeilds mf=new MethodesFeilds (hh[2].substring(0,hh[2].length()-1),map);
Object o=mf.main();
map.put(hh[0], o);
}else{
Object o=map.get(hh[2].substring(0, hh[2].indexOf(".")));
Object o2=ObjectMethod(o,o.getClass(),hh[2].substring(hh[2].indexOf(".")+1,hh[2].length()-1));
map.put(hh[0], o2);
}
}
}
}else{
MethodesFeilds x1=new MethodesFeilds(h.substring(0,h.length()-1),map);
x1.main();
}
}while(sc.hasNext()==true);
}
public static Class[] Getinput( String[] input) {
if(input.length==1&&input[0].equals("")){
Class[]o2=new Class[0];
return o2;}
else{Class[]o=new Class[input.length];
for (int i = 0; i < input.length; i++) {
if( map.containsKey(input[i]) ){
ss[i]=map.get(input[i]);
if(ss[i].getClass().equals(Long.class))
o[i]=long.class;
else if(ss[i].getClass().equals(Double.class)){
o[i]=double.class;
}
else if (ss[i].getClass().equals(String.class)) {
o[i]=String.class;
}else if((ss[i].toString().charAt(0)==('f')||ss[i].toString().charAt(0)==('t'))) {
o[i]=boolean.class;
}else{
o[i]=int.class;
}
}
else if(input[i].contains(".")){
o[i]=double.class;
ss[i]=Double.parseDouble(input[i] );
}
else if (input[i].charAt(0)==('"')||input[i].contains("'")) {
o[i]=String.class;
ss[i]=input[i].substring(1,input[i].length()-1);
}else if((input[i].charAt(0)==('f')||input[i].charAt(0)==('t'))) {
o[i]=boolean.class;
ss[i]=Boolean.parseBoolean (input[i] );
}else{
o[i]=int.class;
ss[i]=Integer.parseInt(input[i] );
}
}
return o;}
}
public static Object GetObject(Class c, Class[] cInput,Object[]x) throws Exception {
if(cInput.length==0){
Object o=c.newInstance();
return o;
}else if(cInput.length==1){
Constructor cons =c.getConstructor(cInput[0]);
Object o=cons.newInstance(x[0]);
return o;
}else if(cInput.length==2){
Constructor cons =c.getConstructor(cInput[0],cInput[1]);
Object o=cons.newInstance(x[0],x[1]);
return o;
}else if(cInput.length==3){
Constructor cons =c.getConstructor(cInput[0],cInput[1],cInput[2]);
Object o=cons.newInstance(x[0],x[1],x[2]);
return o;
}
return null;
}
public static Object ObjectMethod(Object o2, Class c, String input) throws Exception {
String a=input.substring(0, input.indexOf("("));
String b=input.substring(input.indexOf("(")+1, input.indexOf(")"));
String[]x=a.split("\\.");
while(input.equals("")==false){
int i=x.length-1;
while(i>=0){
if(input.contains("(")&&i==0){
String[] Minput=b.split(",");
Class []cInput=Getinput(Minput);
if(cInput.length==0){
Method m= c.getDeclaredMethod(x[x.length-1], null);
o=m.invoke(o2, null);
}else if(cInput.length==1){
Method m= c.getDeclaredMethod(x[x.length-1], cInput[0]);
o=m.invoke(o2, dd[0]);
}else if(cInput.length==2){
Method m= c.getDeclaredMethod(x[x.length-1], cInput[0],cInput[1]);
o=m.invoke(o2, dd[0],dd[1]);
}else if(cInput.length==3){
Method m= c.getClass().getDeclaredMethod(x[x.length-1], cInput[0],cInput[1],cInput[2]);
o=m.invoke(o2, dd[0],dd[1],dd[2]);
}
}else{
Field f=c.getDeclaredField(x[j]);
o =f.get(o2);
}
i--;
j++;
c=o.getClass();
}
input=input.substring( input.indexOf(")")+1, input.length());
if(input.equals("")==false) {
a=input.substring(0, input.indexOf("("));
x=a.split("\\.");
b=input.substring(input.indexOf("(")+1, input.indexOf(")"));}
}
return o;
}
}
The second class(Methodes Fields):
package bb;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
public class MethodesFeilds {
static Object []ss=new Object[10];
String input;
HashMap<String,Object>map=new HashMap<String,Object>();
Object o=null;
int j=3;
public MethodesFeilds(String x, HashMap<String,Object> map2){
map=map2;
input=x;
}
public Object main() throws Exception {
if(input.contains("java.lang.")){
String a=input.substring(0, input.indexOf("("));
String b=input.substring(input.indexOf("(")+1, input.indexOf(")"));
String[]x=a.split("\\.");
Class c=Class.forName("java.lang."+x[2]);
int i=x.length-4;
while(input.equals("")==false){
while(i>=0){
if((b.equals("")==false)&&i==0){
String[] Minput=b.split(",");
Class []cInput=Getinput(Minput);
if(cInput.length==0){
Method m= c.getDeclaredMethod(x[x.length-1], null);
o=m.invoke(o, null);
if(o==null)break;
}else if(cInput.length==1){
Method m= c.getDeclaredMethod(x[x.length-1], cInput[0]);
o=m.invoke(o, ss[0]);
if(o==null)break;
}else if(cInput.length==2){
Method m= c.getDeclaredMethod(x[x.length-1], cInput[0],cInput[1]);
o=m.invoke(o, ss[0],ss[1]);
if(o==null)break;
}else if(cInput.length==3){
Method m= c.getClass().getDeclaredMethod(x[x.length-1], cInput[0],cInput[1],cInput[2]);
o=m.invoke(o, ss[0],ss[1],ss[2]);
if(o==null)break;
}
}else{
Field f=c.getDeclaredField(x[j]);
o=f.get(o);
}
i--;
j++;
c=o.getClass();
}input=input.substring( input.indexOf(")")+1, input.length());
if(input.equals("")==false) {
a=input.substring(0, input.indexOf("("));
x=a.split("\\.");
b=input.substring(input.indexOf("(")+1, input.indexOf(")"));}
}
}else
{
String a=input.substring(0, input.indexOf("("));
String b=input.substring(input.indexOf("(")+1, input.indexOf(")"));
String[]x=a.split("\\.");
Class c=Class.forName(x[0]);
j=1;
int i=x.length-2;
while(input.equals("")==false){
while(i>=0){
if(input.contains("(")&&i==0){
String[] Minput=b.split(",");
Class []cInput=Getinput(Minput);
if(cInput.length==0){
Method m= c.getDeclaredMethod(x[x.length-1], null);
o=m.invoke(o, null);
if(o==null)break;
}else if(cInput.length==1){
Method m= c.getDeclaredMethod(x[x.length-1], cInput[0]);
o=m.invoke(o, ss[0]);
if(o==null)break;
}else if(cInput.length==2){
Method m= c.getDeclaredMethod(x[x.length-1], cInput[0],cInput[1]);
o=m.invoke(o, ss[0],ss[1]);
if(o==null)break;
}else if(cInput.length==3){
Method m= c.getClass().getDeclaredMethod(x[x.length-1], cInput[0],cInput[1],cInput[2]);
o=m.invoke(o, ss[0],ss[1],ss[2]);
if(o==null)break;
}
}else{
Field f=c.getDeclaredField(x[j]);
Object o=f.get(null);
}
i--;
j++;
c=o.getClass();
}input=input.substring( input.indexOf(")")+1, input.length());
if(input.equals("")==false) {
a=input.substring(0, input.indexOf("("));
x=a.split("\\.");
b=input.substring(input.indexOf("(")+1, input.indexOf(")"));
}
}
}
return o;
}
public Class[] Getinput( String[] input) {
if(input.length==1&&input[0].equals("")){
Class[]o2=new Class[0];
return o2;}
else{
Class[]o=new Class[input.length];
for (int i = 0; i < input.length; i++) {
if( map.containsKey(input[i]) ){
ss[i]=map.get(input[i]);
if(ss[i].getClass().equals(Long.class))
o[i]=long.class;
else if(ss[i].getClass().equals(Double.class)){
o[i]=double.class;
}
else if (ss[i].getClass().equals(String.class)) {
o[i]=String.class;
}else if((ss[i].toString().charAt(0)==('f')||ss[i].toString().charAt(0)==('t'))) {
o[i]=boolean.class;
}else{
o[i]=int.class;
}
}
else if(input[i].contains(".")){
o[i]=double.class;
ss[i]=Double.parseDouble(input[i] );
}
else if (input[i].charAt(0)==('"')||input[i].contains("'")) {
o[i]=String.class;
ss[i]=input[i].substring(1,input[i].length()-1);
}else if((input[i].charAt(0)==('f')||input[i].charAt(0)==('t'))) {
o[i]=boolean.class;
ss[i]=Boolean.parseBoolean (input[i] );
}else{
o[i]=int.class;
ss[i]=Integer.parseInt(input[i] );
}
}
return o;
}}
}
for more information contact me with my mail:

Well although there was nobody found to answer this tricky question I myself answered it :
The first class (main class):
package bb;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class main {
static Object o;
static int j=0;
static Object []ss=new Object[10];
static Object []dd=new Object[10];
static HashMap<String , Object> map= new HashMap<String, Object>();
public static void main(String[] args) throws Exception {
Scanner s=new Scanner(System.in);
System.out.println("Please Enter File Name !");
File x=new File(s.nextLine());
Scanner sc=new Scanner(x);
do{
String h=sc.nextLine();
if(h.contains("=")){
String[]hh=h.split(" ");
if(hh[2].equals("=")){
//Double� d� =� new� Double(14.7);
Class c=Class.forName(hh[0]);
if(h.contains("new")==true){
String co=hh[4].substring(hh[4].indexOf("(")+1, hh[4].length()-2);
String[] input=co.split(",");
Class []cInput=Getinput(input);
Object o=GetObject(c,cInput,ss);
map.put(hh[1], o);
}else{
//Long� l� =� d.LongValue();
if(map.containsKey(hh[3].substring(0, hh[3].indexOf(".")))==false){
MethodesFeilds mf=new MethodesFeilds (hh[3].substring(0,hh[3].length()-1),map);
Object o=mf.main();
map.put(hh[1], o);
}else{
Object o=map.get(hh[3].substring(0, hh[3].indexOf(".")));
Object o2=ObjectMethod(o,o.getClass(),hh[3].substring(hh[3].indexOf(".")+1,hh[3].length()-1));
map.put(hh[1], o2);
}
}
}else if(hh[1].equals("=")){
// d=new boolean(false);
Class c=map.get(hh[0]).getClass();
map.remove(hh[0]);
if(h.contains("new")==true){
String co=hh[3].substring(hh[3].indexOf("(")+1, hh[3].length()-2);
String[] input=co.split(",");
Class []cInput=Getinput(input);
Object o=GetObject(c,cInput,ss);
map.put(hh[0], o);
}else{
//� l� =� d.LongValue();
if(map.containsKey(hh[2].substring(0, hh[2].indexOf(".")))==false){
MethodesFeilds mf=new MethodesFeilds (hh[2].substring(0,hh[2].length()-1),map);
Object o=mf.main();
map.put(hh[0], o);
}else{
Object o=map.get(hh[2].substring(0, hh[2].indexOf(".")));
Object o2=ObjectMethod(o,o.getClass(),hh[2].substring(hh[2].indexOf(".")+1,hh[2].length()-1));
map.put(hh[0], o2);
}
}
}
}else{
MethodesFeilds x1=new MethodesFeilds(h.substring(0,h.length()-1),map);
x1.main();
}
}while(sc.hasNext()==true);
}
public static Class[] Getinput( String[] input) {
if(input.length==1&&input[0].equals("")){
Class[]o2=new Class[0];
return o2;}
else{Class[]o=new Class[input.length];
for (int i = 0; i < input.length; i++) {
if( map.containsKey(input[i]) ){
ss[i]=map.get(input[i]);
if(ss[i].getClass().equals(Long.class))
o[i]=long.class;
else if(ss[i].getClass().equals(Double.class)){
o[i]=double.class;
}
else if (ss[i].getClass().equals(String.class)) {
o[i]=String.class;
}else if((ss[i].toString().charAt(0)==('f')||ss[i].toString().charAt(0)==('t'))) {
o[i]=boolean.class;
}else{
o[i]=int.class;
}
}
else if(input[i].contains(".")){
o[i]=double.class;
ss[i]=Double.parseDouble(input[i] );
}
else if (input[i].charAt(0)==('"')||input[i].contains("'")) {
o[i]=String.class;
ss[i]=input[i].substring(1,input[i].length()-1);
}else if((input[i].charAt(0)==('f')||input[i].charAt(0)==('t'))) {
o[i]=boolean.class;
ss[i]=Boolean.parseBoolean (input[i] );
}else{
o[i]=int.class;
ss[i]=Integer.parseInt(input[i] );
}
}
return o;}
}
public static Object GetObject(Class c, Class[] cInput,Object[]x) throws Exception {
if(cInput.length==0){
Object o=c.newInstance();
return o;
}else if(cInput.length==1){
Constructor cons =c.getConstructor(cInput[0]);
Object o=cons.newInstance(x[0]);
return o;
}else if(cInput.length==2){
Constructor cons =c.getConstructor(cInput[0],cInput[1]);
Object o=cons.newInstance(x[0],x[1]);
return o;
}else if(cInput.length==3){
Constructor cons =c.getConstructor(cInput[0],cInput[1],cInput[2]);
Object o=cons.newInstance(x[0],x[1],x[2]);
return o;
}
return null;
}
public static Object ObjectMethod(Object o2, Class c, String input) throws Exception {
String a=input.substring(0, input.indexOf("("));
String b=input.substring(input.indexOf("(")+1, input.indexOf(")"));
String[]x=a.split("\\.");
while(input.equals("")==false){
int i=x.length-1;
while(i>=0){
if(input.contains("(")&&i==0){
String[] Minput=b.split(",");
Class []cInput=Getinput(Minput);
if(cInput.length==0){
Method m= c.getDeclaredMethod(x[x.length-1], null);
o=m.invoke(o2, null);
}else if(cInput.length==1){
Method m= c.getDeclaredMethod(x[x.length-1], cInput[0]);
o=m.invoke(o2, dd[0]);
}else if(cInput.length==2){
Method m= c.getDeclaredMethod(x[x.length-1], cInput[0],cInput[1]);
o=m.invoke(o2, dd[0],dd[1]);
}else if(cInput.length==3){
Method m= c.getClass().getDeclaredMethod(x[x.length-1], cInput[0],cInput[1],cInput[2]);
o=m.invoke(o2, dd[0],dd[1],dd[2]);
}
}else{
Field f=c.getDeclaredField(x[j]);
o =f.get(o2);
}
i--;
j++;
c=o.getClass();
}
input=input.substring( input.indexOf(")")+1, input.length());
if(input.equals("")==false) {
a=input.substring(0, input.indexOf("("));
x=a.split("\\.");
b=input.substring(input.indexOf("(")+1, input.indexOf(")"));}
}
return o;
}
}
The second class(Methodes Fields):
package bb;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
public class MethodesFeilds {
static Object []ss=new Object[10];
String input;
HashMap<String,Object>map=new HashMap<String,Object>();
Object o=null;
int j=3;
public MethodesFeilds(String x, HashMap<String,Object> map2){
map=map2;
input=x;
}
public Object main() throws Exception {
if(input.contains("java.lang.")){
String a=input.substring(0, input.indexOf("("));
String b=input.substring(input.indexOf("(")+1, input.indexOf(")"));
String[]x=a.split("\\.");
Class c=Class.forName("java.lang."+x[2]);
int i=x.length-4;
while(input.equals("")==false){
while(i>=0){
if((b.equals("")==false)&&i==0){
String[] Minput=b.split(",");
Class []cInput=Getinput(Minput);
if(cInput.length==0){
Method m= c.getDeclaredMethod(x[x.length-1], null);
o=m.invoke(o, null);
if(o==null)break;
}else if(cInput.length==1){
Method m= c.getDeclaredMethod(x[x.length-1], cInput[0]);
o=m.invoke(o, ss[0]);
if(o==null)break;
}else if(cInput.length==2){
Method m= c.getDeclaredMethod(x[x.length-1], cInput[0],cInput[1]);
o=m.invoke(o, ss[0],ss[1]);
if(o==null)break;
}else if(cInput.length==3){
Method m= c.getClass().getDeclaredMethod(x[x.length-1], cInput[0],cInput[1],cInput[2]);
o=m.invoke(o, ss[0],ss[1],ss[2]);
if(o==null)break;
}
}else{
Field f=c.getDeclaredField(x[j]);
o=f.get(o);
}
i--;
j++;
c=o.getClass();
}input=input.substring( input.indexOf(")")+1, input.length());
if(input.equals("")==false) {
a=input.substring(0, input.indexOf("("));
x=a.split("\\.");
b=input.substring(input.indexOf("(")+1, input.indexOf(")"));}
}
}else
{
String a=input.substring(0, input.indexOf("("));
String b=input.substring(input.indexOf("(")+1, input.indexOf(")"));
String[]x=a.split("\\.");
Class c=Class.forName(x[0]);
j=1;
int i=x.length-2;
while(input.equals("")==false){
while(i>=0){
if(input.contains("(")&&i==0){
String[] Minput=b.split(",");
Class []cInput=Getinput(Minput);
if(cInput.length==0){
Method m= c.getDeclaredMethod(x[x.length-1], null);
o=m.invoke(o, null);
if(o==null)break;
}else if(cInput.length==1){
Method m= c.getDeclaredMethod(x[x.length-1], cInput[0]);
o=m.invoke(o, ss[0]);
if(o==null)break;
}else if(cInput.length==2){
Method m= c.getDeclaredMethod(x[x.length-1], cInput[0],cInput[1]);
o=m.invoke(o, ss[0],ss[1]);
if(o==null)break;
}else if(cInput.length==3){
Method m= c.getClass().getDeclaredMethod(x[x.length-1], cInput[0],cInput[1],cInput[2]);
o=m.invoke(o, ss[0],ss[1],ss[2]);
if(o==null)break;
}
}else{
Field f=c.getDeclaredField(x[j]);
Object o=f.get(null);
}
i--;
j++;
c=o.getClass();
}input=input.substring( input.indexOf(")")+1, input.length());
if(input.equals("")==false) {
a=input.substring(0, input.indexOf("("));
x=a.split("\\.");
b=input.substring(input.indexOf("(")+1, input.indexOf(")"));
}
}
}
return o;
}
public Class[] Getinput( String[] input) {
if(input.length==1&&input[0].equals("")){
Class[]o2=new Class[0];
return o2;}
else{
Class[]o=new Class[input.length];
for (int i = 0; i < input.length; i++) {
if( map.containsKey(input[i]) ){
ss[i]=map.get(input[i]);
if(ss[i].getClass().equals(Long.class))
o[i]=long.class;
else if(ss[i].getClass().equals(Double.class)){
o[i]=double.class;
}
else if (ss[i].getClass().equals(String.class)) {
o[i]=String.class;
}else if((ss[i].toString().charAt(0)==('f')||ss[i].toString().charAt(0)==('t'))) {
o[i]=boolean.class;
}else{
o[i]=int.class;
}
}
else if(input[i].contains(".")){
o[i]=double.class;
ss[i]=Double.parseDouble(input[i] );
}
else if (input[i].charAt(0)==('"')||input[i].contains("'")) {
o[i]=String.class;
ss[i]=input[i].substring(1,input[i].length()-1);
}else if((input[i].charAt(0)==('f')||input[i].charAt(0)==('t'))) {
o[i]=boolean.class;
ss[i]=Boolean.parseBoolean (input[i] );
}else{
o[i]=int.class;
ss[i]=Integer.parseInt(input[i] );
}
}
return o;
}}
}
for more information contact me with my mail:
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.