Python で S式(整数の四則演算だけ)を字句解析する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from enum import Enum | |
from functools import wraps | |
def assert_simgle_char(f): | |
@wraps(f) | |
def inner(c: str): | |
if len(c) != 1: | |
raise ValueError(c) | |
return f(c) | |
return inner | |
@assert_simgle_char | |
def is_space(c): | |
return c in '\s\n\t\r ' | |
@assert_simgle_char | |
def is_numeric(c): | |
return c in '0123456789' | |
def tokenize(code: str) -> list[str]: | |
tokens = [] | |
i = 0 | |
while (True): | |
c = code[i] | |
if c in '()': | |
tokens.append(c) | |
i += 1 | |
elif c in '+-*/': | |
tokens.append(c) | |
i += 1 | |
elif is_numeric(c): | |
buf = '' | |
while (True): | |
if is_numeric(code[i]): | |
buf += code[i] | |
i += 1 | |
continue | |
tokens.append(buf) | |
break | |
elif is_space(c): | |
i += 1 | |
if len(code) <= i: | |
break | |
return tokens | |
if __name__ == '__main__': | |
ret = tokenize('(+ 1 23)') | |
print(ret) | |
ret = tokenize('(* (+ 100 234) 987))') | |
print(ret) |
Author
hassaku63
commented
Nov 6, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment