staticunsignedcharinputLocation=0;// Current index in text input
staticdoublecalc(constcharinput[CALC_BUFFER_SIZE+1])// Finds value of input char array, relatively small and fast I think
{
charinputToken[CALC_BUFFER_SIZE+1];// Input buffer, used when a single token (generally a number) takes up more
unsignedcharinputTokenLocation=0,inputLocation=0;// Keep track of indices
structTokentokens[CALC_BUFFER_SIZE+1];// Input, converted to tokens, one extra large to accomodate for possible negative sign then open parenthesis as first character
unsignedchartokenCount=0;// Keep track of index
booldashAsMinus=false;// Kind of a hacky solution to determining whether to treat a dash as a minus sign or a negative sign
// sscanf(inputToken, "%lf", &tokens[tokenCount].raw.num); // I would like to use sscanf here, but the small version of stdio.h on the chip doesn't allow sscanf or its sister functions to be used to process floats
tokens[tokenCount].raw.num=atof(inputToken);
tokens[tokenCount].isNum=true;
for(unsignedchari=0;i<inputTokenLocation+1;i++)
{
inputToken[i]='\0';
}
inputTokenLocation=0;
tokenCount++;
dashAsMinus=true;
continue;
}
/* inputTokenLocation == 0 */
tokens[tokenCount].isNum=false;
tokens[tokenCount].raw.op.c=input[inputLocation];
tokens[tokenCount].raw.op.priority=0;
tokens[tokenCount].raw.op.ltr=true;
dashAsMinus=false;
switch(input[inputLocation])
{
caseCALC_CHAR_BEG:
break;
caseCALC_CHAR_END:
dashAsMinus=true;
break;
caseCALC_CHAR_ADD:
tokens[tokenCount].raw.op.priority=CALC_PRIO_ADD;
break;
caseCALC_CHAR_SUB:
tokens[tokenCount].raw.op.priority=CALC_PRIO_SUB;
break;
caseCALC_CHAR_MUL:
tokens[tokenCount].raw.op.priority=CALC_PRIO_MUL;
break;
caseCALC_CHAR_DIV:
tokens[tokenCount].raw.op.priority=CALC_PRIO_DIV;
break;
caseCALC_CHAR_EXP:
tokens[tokenCount].raw.op.priority=CALC_PRIO_EXP;
tokens[tokenCount].raw.op.ltr=false;
break;
caseCALC_CHAR_SIN:
caseCALC_CHAR_COS:
caseCALC_CHAR_TAN:
caseCALC_CHAR_ASN:
caseCALC_CHAR_ACS:
caseCALC_CHAR_ATN:
caseCALC_CHAR_LGE:
caseCALC_CHAR_LOG:
caseCALC_CHAR_SQT:
break;
caseCALC_CHAR_EUL:
tokens[tokenCount].isNum=true;
tokens[tokenCount].raw.num=CALC_VALU_EUL;
dashAsMinus=true;
break;
caseCALC_CHAR_PI:
tokens[tokenCount].isNum=true;
tokens[tokenCount].raw.num=CALC_VALU_PI;
dashAsMinus=true;
break;
case'\0':
tokenCount--;
inputLocation=CALC_BUFFER_SIZE;
break;
default:
tokenCount--;
break;
}
tokenCount++;
inputLocation++;
}
structTokenoutput[CALC_BUFFER_SIZE+1];// Final output tokens before evaluation
structTokenopstack[CALC_BUFFER_SIZE+1];// Stack of operators
unsignedcharoutputLocation=0,opstackLocation=0;// Keep track of indices
unsignedcharnumBrackets=0;// The number of parenthesis
staticchartext[CALC_BUFFER_SIZE+1];// Used to store input and then output when ready to print
staticcharbackspaceText[CALC_BUFFER_SIZE+1];// Pretty dumb waste of memory because only backspace characters, used with send_string to backspace and remove input