-
Notifications
You must be signed in to change notification settings - Fork 0
/
pflfp_token.py
156 lines (144 loc) · 4.3 KB
/
pflfp_token.py
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
from enum import Enum
from typing import List
class T(Enum):
STRING = 0
INTEGER = 1
FLOAT = 2
BOOL = 3
LABEL = 4
IDENT = 5
IDENTASSIGN = 6
PLUS = 20
MINUS = 21
STAR = 22
SLASH = 23
HAT = 24
EXP = 25
EQ = 26
DEF = 27
DOUBLEQUOTES = 28
GREATER = 29
LESSER = 30
QUESTION = 60
LCURLY = 61
RCURLY = 62
BLOCK = 63
TYPE = 98
PRINT = 99
FUNCTION = 100
DUP = 101
DROP = 102
COMMENT = 997
ILEGAL = 998
EOF = 999
class Token:
def __init__(self, type: T, literal: str, pos: int) -> None:
self.type = type
self.literal = literal
self.pos = int
def __str__(self) -> str:
return "(" + self.literal + " " + look_up[self.type] + ")"
look_up = {
T.BOOL: "bool",
T.STRING: "string",
T.INTEGER: "integer",
T.IDENT: "identifier",
T.IDENTASSIGN: "assignment",
T.LABEL: "label",
T.FUNCTION: "function",
T.DUP: "dup",
T.DROP: "drop",
T.PLUS: "operator",
T.MINUS: "operator",
T.GREATER: "operator",
T.LESSER: "operator",
T.STAR: "operator",
T.SLASH: "operator",
T.DOUBLEQUOTES: "\"",
T.EOF: "eof",
T.ILEGAL: "ilegal",
T.EQ: "operator",
T.DEF: "def",
T.PRINT: "print",
T.TYPE: "type",
T.COMMENT: "comment",
T.BLOCK: "operator"
}
# LEXING INLINE FUNCT ONS
is_label = lambda x: x[0] == ":" and x[1:].isalpha()
is_ws = lambda x: x == " " or x == "\n" or x == "\t" or x == "\r"
def read_ident(source: str, index: int):
i = index
begin = i
while len(source) > i and source[i].isalpha() and not is_ws(source[i]):
i += 1
end = i
return (source[begin:end], i)
def read_string(source: str, index: int):
i = index
begin = i
while source[i] != "\"":
i += 1
end = i
return (source[begin:end], i)
def read_integer(source: str, index: int):
i = index
begin = i
while source[i].isdigit():
i += 1
end = i
return (source[begin:end], i)
def tokenize(source: str) -> List:
size = len(source)
i = 0
tokens = []
while 1:
if i >= size-1:
tokens.append(Token(T.EOF, 'eof', i))
break
match source[i]:
case ' ' | '\t' | '\n':
pass
case '+': tokens.append(Token(T.PLUS, '+', i))
case '-': tokens.append(Token(T.MINUS, '-', i))
case '*': tokens.append(Token(T.STAR, '*', i))
case '/': tokens.append(Token(T.SLASH, '/', i))
case '=':
if source[i+1] != None and source[i+1].isalpha():
i += 1
ident, i = read_ident(source, i)
tokens.append(Token(T.IDENTASSIGN, ident, i))
else: tokens.append(Token(T.EQ, '=', i))
case '>': tokens.append(Token(T.GREATER, '>', i))
case '<': tokens.append(Token(T.LESSER, '<', i))
case 'T' | 'F': tokens.append(Token(T.BOOL, source[i], i))
case '#':
while source[i] != '\n':
i += 1
case '|':
tokens.append(Token(T.BLOCK, source[i], i))
i += 1
case '"':
i += 1
string, i = read_string(source, i)
tokens.append(Token(T.STRING, string, i))
case ':':
i += 1
label_ident, i = read_ident(source, i)
tokens.append(Token(T.LABEL, label_ident, i))
case _:
if source[i].isdigit():
aux, i = read_integer(source, i)
tokens.append(Token(T.INTEGER, aux, i))
elif source[i].isalpha():
aux, i = read_ident(source, i)
if aux == "print": tokens.append(Token(T.PRINT, aux, i))
elif aux == "type": tokens.append(Token(T.TYPE, aux, i))
elif aux == "dup": tokens.append(Token(T.DUP, aux, i))
elif aux == "drop": tokens.append(Token(T.DROP, aux, i))
elif aux == "def": tokens.append(Token(T.DEF, aux, i))
else: tokens.append(Token(T.IDENT, aux, i))
else:
tokens.append(Token(T.ILEGAL, "\0", i))
i += 1
return tokens