-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_example.py
More file actions
96 lines (82 loc) · 2.99 KB
/
parser_example.py
File metadata and controls
96 lines (82 loc) · 2.99 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
import json
import tokenizer
def parse_tokens(tokens):
index = 0
ast = []
def parse_expression():
nonlocal index
expression = []
while index < len(tokens):
token_type, token_value = tokens[index]
if token_type == 'KEYWORD' and token_value in ('print', 'read'):
# Parse function call
func_name = token_value
index += 2 # Skip '('
args = []
while tokens[index] != ('SYMBOL', ')'):
args.append(parse_expression())
# if tokens[index] == ('SYMBOL', ','):
# index += 1 # Skip ','
index += 1 # Skip ')'
expression.append({
"type": "FunctionCall",
"name": func_name,
"arguments": args
})
elif token_type in ('INT', 'FLOAT', 'STRING', 'BOOL'):
# Parse literals
expression.append({
"type": token_type,
"value": token_value
})
index += 1
elif token_type == 'IDENTIFIER':
# Parse variable or assignment
ident = token_value
if index + 1 < len(tokens) and tokens[index + 1] == ('ASSIGN', '='):
index += 2 # Skip '='
value = parse_expression()
expression.append({
"type": "VariableDeclaration",
"name": ident,
"value": value
})
else:
expression.append({
"type": "Identifier",
"name": ident
})
index += 1
elif token_type == 'OPERATOR':
# Parse operators
operator = token_value
index += 1
right = parse_expression()
expression = {
"type": "BinaryExpression",
"operator": operator,
"left": expression,
"right": right
}
elif token_type == 'SYMBOL' and token_value in (';', ')', '}'):
break
elif token_type == 'SYMBOL' and token_value in ('(', '{'):
index += 1
expression = parse_expression()
index += 1 # Skip ')'
else:
index += 1
print(index, len(tokens))
return expression[0] if len(expression) == 1 else expression
while index < len(tokens):
node = parse_expression()
# Append node to ast
if node:
print(node)
ast.append(node)
# Skip semicolon
if index < len(tokens) and tokens[index] == ('SYMBOL', ';'):
index += 1
ast_final = ast
return ast_final
# Example usage with your token list: