scanner: This function builds the tokens by the content of the file. The tokens will be saved in list 'tokens'
(string)
| 60 | |
| 61 | |
| 62 | def scanner(string): |
| 63 | """ |
| 64 | scanner: This function builds the tokens by the content of the file. |
| 65 | The tokens will be saved in list 'tokens' |
| 66 | """ |
| 67 | global tokens |
| 68 | token = "" |
| 69 | state = 0 # init state |
| 70 | |
| 71 | for ch in string: |
| 72 | match state: |
| 73 | case 0: |
| 74 | match ch: |
| 75 | case "m": # catch mov-command |
| 76 | state = 1 |
| 77 | token += "m" |
| 78 | |
| 79 | case "e": # catch register |
| 80 | state = 4 |
| 81 | token += "e" |
| 82 | |
| 83 | case "1": # catch a number |
| 84 | if ch <= "9" or ch == "-": |
| 85 | state = 6 |
| 86 | token += ch |
| 87 | |
| 88 | case "0": # catch a number or hex-code |
| 89 | state = 17 |
| 90 | token += ch |
| 91 | |
| 92 | case "a": # catch add-command |
| 93 | state = 7 |
| 94 | token += ch |
| 95 | |
| 96 | case "s": # catch sub command |
| 97 | state = 10 |
| 98 | token += ch |
| 99 | |
| 100 | case "i": # capture int command |
| 101 | state = 14 |
| 102 | token += ch |
| 103 | |
| 104 | case "p": # capture push or pop command |
| 105 | state = 19 |
| 106 | token += ch |
| 107 | |
| 108 | case "l": # capture label |
| 109 | state = 25 |
| 110 | token += ch |
| 111 | |
| 112 | case "j": # capture jmp command |
| 113 | state = 26 |
| 114 | token += ch |
| 115 | |
| 116 | case "c": # catch cmp-command |
| 117 | state = 29 |
| 118 | token += ch |
| 119 |
no test coverage detected