-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpc.pyx
More file actions
247 lines (180 loc) · 7.51 KB
/
pc.pyx
File metadata and controls
247 lines (180 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# MIT License
# Copyright 2025 @asyncze (Michael Sjöberg)
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Tokenizer for PlayCode (.pyx)
# cython: language_level=3
cimport cython
cdef str WHITESPACE = "whitespace"
cdef str DEFAULT = "default"
cdef str KEYWORD = "keyword"
cdef str CLASS = "class"
cdef str NAME = "name"
cdef str PARAMETER = "parameter"
cdef str LAMBDA = "lambda"
cdef str STRING = "string"
cdef str NUMBER = "number"
cdef str OPERATOR = "operator"
cdef str COMMENT = "comment"
cdef str SPECIAL = "special"
cdef str TYPE = "type"
cdef str CONDITIONAL = "conditional"
cdef str BUILT_IN = "built_in"
# system colors
cdef str ERROR = "_error"
cdef str WARNING = "_warning"
cdef str SUCCESS = "_success"
KEYWORDS = { "if", "else", "while", "swap", "print" }
CONDITIONALS = { "True", "False" }
cdef inline bint _is_letter(str ch):
return ('a' <= ch <= 'z') or ('A' <= ch <= 'Z') or (ch == '_')
cdef inline bint _is_digit(str ch):
return '0' <= ch <= '9'
cdef inline bint _is_alnum_or_underscore(str ch):
return _is_letter(ch) or _is_digit(ch)
cdef inline int handle_whitespace(int current_char_index):
current_char_index += 1
return current_char_index
cdef int handle_dash(int current_char_index, str text, list tokens):
cdef int n = len(text)
cdef int start = current_char_index
cdef str lexeme
if current_char_index + 1 < n and text[current_char_index + 1] == '-':
lexeme = "--"
current_char_index += 2
while current_char_index < n and text[current_char_index] != '\n':
lexeme += text[current_char_index]
current_char_index += 1
tokens.append((COMMENT, start, lexeme))
return current_char_index
elif current_char_index + 1 < n and text[current_char_index + 1] == '>':
tokens.append((SPECIAL, start, "->"))
return current_char_index + 2
else:
tokens.append((OPERATOR, current_char_index, "-"))
return current_char_index + 1
cdef int handle_operator(int current_char_index, str text, list tokens):
tokens.append((OPERATOR, current_char_index, text[current_char_index]))
return current_char_index + 1
cdef int handle_tag(int current_char_index, str text, list tokens):
cdef int n = len(text)
cdef int start = current_char_index
cdef str lexeme = "@"
current_char_index += 1
while current_char_index < n and _is_letter(text[current_char_index]):
lexeme += text[current_char_index]
current_char_index += 1
tokens.append((LAMBDA, start, lexeme))
return current_char_index
cdef int handle_string(int current_char_index, str text, list tokens):
cdef int n = len(text)
cdef int start = current_char_index
cdef str lexeme = "\""
current_char_index += 1
while current_char_index < n and text[current_char_index] != '"':
lexeme += text[current_char_index]
current_char_index += 1
if current_char_index < n:
lexeme += '"'
current_char_index += 1
tokens.append((STRING, start, lexeme))
return current_char_index
cdef int handle_number(int current_char_index, str text, list tokens):
cdef int n = len(text)
cdef int start = current_char_index
cdef str lexeme = text[current_char_index]
current_char_index += 1
while current_char_index < n and (_is_digit(text[current_char_index]) or text[current_char_index] == '.'):
lexeme += text[current_char_index]
current_char_index += 1
tokens.append((NUMBER, start, lexeme))
return current_char_index
cdef int handle_identifier(int current_char_index, str text, list tokens):
cdef int n = len(text)
cdef int start = current_char_index
cdef str lexeme = text[current_char_index]
current_char_index += 1
while current_char_index < n and _is_alnum_or_underscore(text[current_char_index]):
lexeme += text[current_char_index]
current_char_index += 1
if lexeme in CONDITIONALS:
tokens.append((CONDITIONAL, start, lexeme))
elif lexeme in KEYWORDS:
tokens.append((KEYWORD, start, lexeme))
else:
tokens.append((DEFAULT, start, lexeme))
return current_char_index
@cython.cclass
class Lexer:
@property
def lexer_name(self):
return "PlayCode"
@property
def comment_char(self):
return ["--", None]
@property
def line_comment(self):
return "--"
def _is_class(self, line_text):
return False
def _is_function_name(self, line_text):
if not line_text.startswith("@"):
return False
return [("lambda", line_text)]
def _is_type_def(self, line_text):
return False
def tokenize(self, str text):
cdef int current_char_index = 0
cdef int n = len(text)
cdef str ch
cdef str next_ch
cdef list tokens = []
cdef bint new_line = True
while current_char_index < n:
ch = text[current_char_index]
next_ch = text[current_char_index + 1] if current_char_index + 1 < n else ""
# whitespace
if ch in (' ', '\t', '\r'):
current_char_index = handle_whitespace(current_char_index)
# newline
elif ch == '\n':
current_char_index = handle_whitespace(current_char_index)
new_line = True
continue
# '-' : comment / '->' / minus
elif ch == '-':
current_char_index = handle_dash(current_char_index, text, tokens)
# single-char operators
elif ch in ('=', '!', '+', '*', '/', '<', '>'):
current_char_index = handle_operator(current_char_index, text, tokens)
# '@' tag
elif ch == '@':
current_char_index = handle_tag(current_char_index, text, tokens)
# string literal
elif ch == '"':
current_char_index = handle_string(current_char_index, text, tokens)
# number
elif _is_digit(ch):
current_char_index = handle_number(current_char_index, text, tokens)
# identifier
elif _is_letter(ch):
current_char_index = handle_identifier(current_char_index, text, tokens)
# fallback
else:
tokens.append((DEFAULT, current_char_index, ch))
current_char_index += 1
new_line = False
return tokens