summaryrefslogtreecommitdiff
path: root/tnslc/parse/ast.tnsl
blob: cdd0ae26bca33d99d309641f5731d644aff41ba1 (plain)
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

uint16 NTYPE_MODULE = 0
uint16 NTYPE_EXPORT = 1
uint16 NTYPE_STRUCT = 2
uint16 NTYPE_ID = 3
uint16 NTYPE_BIN_OP = 4
uint16 NTYPE_PRE_OP = 5
uint16 NTYPE_POST_OP = 6
uint16 NTYPE_FUNCTION = 7
uint16 NTYPE_METHOD = 8


struct Node {
	uint16 _type,
	~uint8 data,
	utils.Vector sub
}

/; method Node
	/; init (uint16 _type, ~uint8 data)
		self.data = data
		self._type = _type

		Node n
		self.sub.init(len n)
	;/

	/; end
		_delete(self.data)
		
		/; loop (uint i = 0; i < self.sub.count) [i++]
			~Node cur = self.sub.get(i)
			cur`.end()
		;/
		self.sub.end()
	;/
;/

/; _ast_block (~utils.File fin, ~Node mod, Token first)
;/

/; _ast_module (~utils.File fin, ~Node mod, Token first)
	Node out

	uint16 t = NTYPE_MODULE
	/; if (first.eq("export\0") == true)
		Token tmp = produce_next_token(fin, first)
		first.end()
		first = tmp
		t = NTYPE_EXPORT
	;/

	Token tmp = produce_next_token(fin, first)
	first.end()
	out.init(t, tmp.data)

	tmp = produce_next_token(fin, tmp)
	/; loop (tmp.eq(";/\0") !== true)
		/; if (tmp.eq("import\0"))
			first = produce_next_token(fin, tmp)
			tmp.end()
			tmp = first

			~uint8 path = utils.unquote_str(tmp.data)
			utils.File _import = fin`.relative(path)
			_delete(path)
			path = _import.path.to_cstr('/')
			
			_printf("Importing ./\0")
			_printf(path)
			_printf("\n\0")
			_delete(path)

			_ast_file(~_import, mod)
			_import.end()

		;; else
			first = produce_next_token(fin, tmp)
			tmp.end()
			tmp = first
		;/
	;/

	mod`.sub.push(~out)
;/

/; _ast_file (~utils.File fin, ~Node mod)
	fin`.open()
	
	Token tmp = produce_first_token(fin)
	Token first
	/; loop (tmp._type !== TTYPE_ERR)
		/; if (tmp.eq("import\0"))
			first = produce_next_token(fin, tmp)
			tmp.end()
			tmp = first

			~uint8 path = utils.unquote_str(tmp.data)
			utils.File _import = fin`.relative(path)
			_delete(path)
			path = _import.path.to_cstr('/')
			
			_printf("Importing ./\0")
			_printf(path)
			_printf("\n\0")
			_delete(path)

			_ast_file(~_import, mod)
			_import.end()
		;; else
			first = produce_next_token(fin, tmp)
			tmp.end()
			tmp = first
		;/
	;/

	fin`.close()
;/

/; generate_ast (~utils.File fin) [Node]
	Node out

	utils.Vector v
	v.init(1)

	out.init(NTYPE_MODULE, v.as_cstr())

	_ast_file(fin, ~out)

	return out
;/

#
# Print out the AST from a specific node
#

/; print_node_type (~Node n)
	/; if (n`._type == NTYPE_MODULE)
		_printf("Mod\0")
	;; else if (n`._type == NTYPE_EXPORT)
		_printf("Exported Module\0")
	;; else if (n`._type == NTYPE_STRUCT)
		_printf("Struct\0")
	;; else if (n`._type == NTYPE_ID)
		_printf("ID\0")
	;; else if (n`._type == NTYPE_BIN_OP)
		_printf("Binary OP\0")
	;; else if (n`._type == NTYPE_PRE_OP)
		_printf("Pre OP\0")
	;; else if (n`._type == NTYPE_POST_OP)
		_printf("Post OP\0")
	;; else if (n`._type == NTYPE_FUNCTION)
		_printf("Function\0")
	;/
;/

/; print_node_head (~Node n)
	_printf("{ NODE_TYPE: \0")
	print_node_type(n)
	_printf(", DATA: \0")
	_printf(n`.data)
	_printf("\n\0")
;/

/; print_ast_rec(~Node n, uint depth)
	/; loop (int i = 0; i < depth) [i++]
		_printf("  \0")
	;/
	print_node_head(n)

	/; loop (uint i = 0; i < n`.sub.count) [i++]
		~Node s = n`.sub.get(i)
		print_ast_rec(s, depth + 1)
	;/
	
	/; loop (int i = 0; i < depth) [i++]
		_printf("  \0")
	;/
	_printf("}\n\0")
;/

/; print_ast (~Node n)
	print_ast_rec(n, 0)
;/