Advertisement
Guest User

Untitled

a guest
Feb 10th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. func TokeniseExpression(string expr) {
  2.     numcache = ""
  3.     exprTokens = []
  4.  
  5.     for char in expr.Replace(" ", "") {
  6.         if precedences.ContainsKey(char) {
  7.             // is an operator
  8.             if len(numcache) > 0 {
  9.                 // numcache has a number in it
  10.                 exprTokens += ('number', numcache)
  11.                 // add token type 'number'
  12.             }
  13.             exprTokens += ('operator', char)
  14.             // char is an operator type, add token
  15.         } else if char is a digit {
  16.             //1, 2, 3, etc.
  17.             numcache += char
  18.             // add to numcache
  19.         }
  20.     }
  21.  
  22.     if len(numcache) > 0 then exprTokens += ('number', numcache)
  23.     // this is for the number at the end of the expression, which
  24.     //     hasn't been 'popped' off it yet
  25.  
  26.     return exprTokens
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement