Guest User

Untitled

a guest
Oct 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Expression Compiler
  2. // Author: Yevano
  3.  
  4. #define T_PLUS     0
  5. #define T_DASH     1
  6. #define T_ASTERISK 2
  7. #define T_FSLASH   3
  8. #define T_LPAREN   4
  9. #define T_RPAREN   5
  10. #define T_NUMBER   6
  11.  
  12. //////// TOKENIZER ////////
  13.  
  14. // Returns pointer to an array of tokens (ebx);
  15. // Parameters:
  16. //     eax: pointer to code string
  17. tokenize:
  18.     tokens: alloc 3000;  // Allocate 4k for tokens (tokens are 3 bytes each (type, strindex1, strindex2), so max of 2k tokens)
  19.  
  20.     // Preserve registers
  21.     push R0;             // R0 points to characters in the code string
  22.     push R1;             // R1 points to current token
  23.     push R2;             // Misc
  24.    
  25.     mov R0, eax;         // Point to the code string
  26.    
  27.     t_loop:
  28.         // Check which kind of token to add
  29.             cmp #R0, '+';
  30.             jne check_dash;
  31.             mov #R1, T_PLUS;
  32.             add #R1, 3;                // Ready for next token
  33.         check_dash:
  34.             cmp #R0, '-';
  35.             jne check_asterisk;
  36.             mov #R1, T_DASH;
  37.             add #R1, 3;                // Ready for next token
  38.         check_asterisk:
  39.             cmp #R0, '*';
  40.             jne check_fslash;
  41.             mov #R1, T_ASTERISK;
  42.             add #R1, 3;                // Ready for next token
  43.         check_fslash:
  44.             cmp #R0, '/';
  45.             jne check_lparen;
  46.             mov #R1, T_FSLASH;
  47.             add #R1, 3;                // Ready for next token
  48.         check_lparen:
  49.             cmp #R0, '(';
  50.             jne check_rparen;
  51.             mov #R1, T_LPAREN;
  52.             add #R1, 3;                // Ready for next token
  53.         check_rparen:
  54.             cmp #R0, ')';
  55.             jne check_number;
  56.             mov #R1, T_RPAREN;
  57.             add #R1, 3;                // Ready for next token
  58.         check_number:
  59.             mov R2, R0;                // R2 gets incremented to last index
  60.             push eax;                  // Preserve eax before calling char_is_digit
  61.             mov eax, #R2;              // Set the char parameter to the current char
  62.             call char_is_digit;        // Call char_is_digit :)
  63.             pop eax;                   // Restore eax
  64.             cmp ebx, 0;                // Jump to done if not a digit
  65.             je check_number_loop_done; // Finish the loop if no more digits
  66.             inc R2;                    // Increment R2
  67.             jmp check_number;          // Loop again
  68.            
  69.             check_number_loop_done:
  70.             cmp R2, R0;                // This means R2 wasn't incremented, so no digits.
  71.             je error;                  // Error, we can't tokenize dis shit
  72.             mov #R1, T_NUMBER;         // Set token type
  73.             inc R1;
  74.             mov #R1, R0;               // Start index
  75.             inc R1;
  76.             mov #R1, R2;               // End index
  77.             inc R1;                    // Ready for next token
  78.         check_number_done:
  79.        
  80.         cmp #R0, 0;       // Continue tokenizing until we get a null character
  81.         jne t_loop;
  82.    
  83.     pop R2;              // Restore used registers
  84.     pop R1;
  85.     pop R0;
  86.     mov ebx, tokens;     // Return the tokens in ebx
  87.     ret;
  88.    
  89.     error:
  90.     pop R2;              // Restore used registers
  91.     pop R1;
  92.     pop R0;
  93.     mov ebx, 0;          // Return null
  94.  
  95. // Returns 1 if current given char is a digit (ebx);
  96. // Paramaters:
  97. //     eax: char
  98. char_is_digit:
  99.     cmp eax, '0';
  100.     jl char_is_digit_false;
  101.     cmp eax, '9';
  102.     jg char_is_digit_false;
  103.     mov ebx, 1;
  104.     ret;
  105.     char_is_digit_false: mov ebx, 0;
  106.     ret;
  107.  
  108. //////// END TOKENIZER ////////
Add Comment
Please, Sign In to add comment