MCPcopy
hub / github.com/fluentpython/example-code-2e / read_from_tokens

Function read_from_tokens

18-with-match/lispy/original/lis.py:68–82  ·  view source on GitHub ↗

Read an expression from a sequence of tokens.

(tokens)

Source from the content-addressed store, hash-verified

66 return s.replace('(',' ( ').replace(')',' ) ').split()
67
68def read_from_tokens(tokens):
69 "Read an expression from a sequence of tokens."
70 if len(tokens) == 0:
71 raise SyntaxError('unexpected EOF while reading')
72 token = tokens.pop(0)
73 if '(' == token:
74 L = []
75 while tokens[0] != ')':
76 L.append(read_from_tokens(tokens))
77 tokens.pop(0) # pop off ')'
78 return L
79 elif ')' == token:
80 raise SyntaxError('unexpected )')
81 else:
82 return atom(token)
83
84def atom(token):
85 "Numbers become numbers; every other token is a symbol."

Callers 1

parseFunction · 0.70

Calls 3

popMethod · 0.80
atomFunction · 0.70
appendMethod · 0.45

Tested by

no test coverage detected