# Basic arithmetic operations for a programming language

I have written a set of functions to compute basic mathematical operations for my own Java-based programming language. First I tokenize input from the source code file and store it in a String array. For the mathematics, I simply read through the array looking for an operator (+, -, /, *) and if I find one I call a operation function. The language needs to be able to automatically handle double/int mathematics without additional casting or indication so obviously this complicates the functions. However, my question is, are these functions really the simplest and most efficient way to do basic math operations?: (Note, I have 4 just like this, one for each basic operation, just copy/pasted the same code with minor changes. That feels repetitive. Isn't there a better way to do this also along with the shortening of each function?)

### Example:

5.32 + 7 returns 12.32

### My Number Parser:

NumReturn numberParser(int cIndex) {
NumReturn nri;
NumReturn nrd;
try {
nri = new NumReturn(Integer.parseInt(Lexer.token[cIndex]), cIndex++, 'i');
System.out.println(nri.value + " ");
return nri;
} catch (NumberFormatException intExcep) {
}
try {
nrd = new NumReturn(Double.parseDouble((Lexer.token[cIndex])), cIndex++, 'd');
System.out.println(nrd.dvalue + " ");
return nrd;
} catch (NumberFormatException doubExcep) {
// doubExcep.printStackTrace();
}
return null;
}


My addition function: (Could this somehow be shortened or be made more efficient?)

NumReturn add(int x) {
if (numberParser(x) != null) {
NumReturn a1 = numberParser(x);
if (Lexer.token[x + 1].equals("+")) {
if (numberParser(x + 2) != null) {
NumReturn a2 = numberParser(x + 2);
if (a1.type == 'd' && a2.type == 'd') {
System.out.println(a1.dvalue + a2.dvalue);
return new NumReturn(a1.dvalue + a2.dvalue, a2.pointerLocation, 'd');
} else if (a1.type == 'i' && a2.type == 'd') {
a1.dvalue = (double) a1.value;
System.out.println(a1.dvalue + a2.dvalue);
return new NumReturn((double) a1.value + a2.dvalue, a2.pointerLocation, 'd');
}

else if (a1.type == 'd' && a2.type == 'i') {
a2.dvalue = (double) a2.value;
System.out.println(a1.dvalue + a2.dvalue);
return new NumReturn(a1.dvalue + (double) a2.value, a2.pointerLocation, 'd');
}

else {
System.out.println(a1.value + a2.value);
return new NumReturn(a1.value + a2.value, a2.pointerLocation, 'i');
}

}

else if (Lexer.token[x + 2].equals("+")) {
System.out.println(a1.value + 1);
System.out.println(a1.dvalue + 1);
if (a1.type == 'i')
return new NumReturn(a1.value++,a1.pointerLocation + 3, 'i');
else
return new NumReturn(a1.dvalue++,a1.pointerLocation + 3, 'd');

}
}
}
return null;

}


### NumReturn Class Type:

 package jsmash;
public class NumReturn {
int value;
double dvalue;
int pointerLocation;
char type;
NumReturn(int value, int pointerLocation, char type) {
this.value = value;
this.pointerLocation = pointerLocation;
this.type = type;
}
NumReturn(double dvalue, int pointerLocation, char type) {
this.dvalue = dvalue;
this.pointerLocation = pointerLocation;
this.type = type;
}

}


By making your calculation methods return a different type than they accept, you rob yourself the possibility to chain their calls: a.add(b).add(c)`