Infinity Calculator implementation for Assignment 2 in Computer Organization.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

79 lines
1.6 KiB

#include "token.h"
// The first string is where the tokenizer reads from, while the second is set
// when the underlying data needs to be parsed more specifically, like for
// numbers.
const char *input_str = NULL;
const char *token_str = NULL;
int tokenize() {
token_str = NULL;
// No text means no tokens.
if (!input_str) {
return TOK_NULL;
}
while (1) {
switch(*input_str) {
case ' ':
case '\t':
case '\n':
// Discard whitespace.
input_str++;
break;
case '0':
// Check if hex number.
if ((*(input_str+1) == 'X' || (*(input_str+1) == 'x')) && ISHEX(*(input_str+2))) {
// Yes, munch hex digits and return.
token_str = input_str + 2;
input_str += 3;
while(ISHEX(*input_str)) input_str++;
return TOK_NUMHEX;
}
// No, this is a decimal number.
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
// Decimal number. Munch digits.
token_str = input_str;
while(ISDIGIT(*input_str)) input_str++;
return TOK_NUMDEC;
// Operators and parenthesises.
case '+':
input_str++;
return TOK_PLUS;
case '-':
input_str++;
return TOK_MINUS;
case '*':
input_str++;
return TOK_STAR;
case '/':
input_str++;
return TOK_FSLASH;
case '^':
input_str++;
return TOK_CARROT;
case '(':
input_str++;
return TOK_LPAREN;
case ')':
input_str++;
return TOK_RPAREN;
// If there is no more data, return the null token.
case '\0':
return TOK_NULL;
// All other cases return an error token and don't advance the pointer.
default:
token_str = input_str;
return TOK_ERR;
}
}
}