Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Expression Compiler
- // Author: Yevano
- #define T_PLUS 0
- #define T_DASH 1
- #define T_ASTERISK 2
- #define T_FSLASH 3
- #define T_LPAREN 4
- #define T_RPAREN 5
- #define T_NUMBER 6
- //////// TOKENIZER ////////
- // Returns pointer to an array of tokens (ebx);
- // Parameters:
- // eax: pointer to code string
- tokenize:
- tokens: alloc 3000; // Allocate 4k for tokens (tokens are 3 bytes each (type, strindex1, strindex2), so max of 2k tokens)
- // Preserve registers
- push R0; // R0 points to characters in the code string
- push R1; // R1 points to current token
- push R2; // Misc
- mov R0, eax; // Point to the code string
- t_loop:
- // Check which kind of token to add
- cmp #R0, '+';
- jne check_dash;
- mov #R1, T_PLUS;
- add #R1, 3; // Ready for next token
- check_dash:
- cmp #R0, '-';
- jne check_asterisk;
- mov #R1, T_DASH;
- add #R1, 3; // Ready for next token
- check_asterisk:
- cmp #R0, '*';
- jne check_fslash;
- mov #R1, T_ASTERISK;
- add #R1, 3; // Ready for next token
- check_fslash:
- cmp #R0, '/';
- jne check_lparen;
- mov #R1, T_FSLASH;
- add #R1, 3; // Ready for next token
- check_lparen:
- cmp #R0, '(';
- jne check_rparen;
- mov #R1, T_LPAREN;
- add #R1, 3; // Ready for next token
- check_rparen:
- cmp #R0, ')';
- jne check_number;
- mov #R1, T_RPAREN;
- add #R1, 3; // Ready for next token
- check_number:
- mov R2, R0; // R2 gets incremented to last index
- push eax; // Preserve eax before calling char_is_digit
- mov eax, #R2; // Set the char parameter to the current char
- call char_is_digit; // Call char_is_digit :)
- pop eax; // Restore eax
- cmp ebx, 0; // Jump to done if not a digit
- je check_number_loop_done; // Finish the loop if no more digits
- inc R2; // Increment R2
- jmp check_number; // Loop again
- check_number_loop_done:
- cmp R2, R0; // This means R2 wasn't incremented, so no digits.
- je error; // Error, we can't tokenize dis shit
- mov #R1, T_NUMBER; // Set token type
- inc R1;
- mov #R1, R0; // Start index
- inc R1;
- mov #R1, R2; // End index
- inc R1; // Ready for next token
- check_number_done:
- cmp #R0, 0; // Continue tokenizing until we get a null character
- jne t_loop;
- pop R2; // Restore used registers
- pop R1;
- pop R0;
- mov ebx, tokens; // Return the tokens in ebx
- ret;
- error:
- pop R2; // Restore used registers
- pop R1;
- pop R0;
- mov ebx, 0; // Return null
- // Returns 1 if current given char is a digit (ebx);
- // Paramaters:
- // eax: char
- char_is_digit:
- cmp eax, '0';
- jl char_is_digit_false;
- cmp eax, '9';
- jg char_is_digit_false;
- mov ebx, 1;
- ret;
- char_is_digit_false: mov ebx, 0;
- ret;
- //////// END TOKENIZER ////////
Add Comment
Please, Sign In to add comment