-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathast.h
More file actions
821 lines (662 loc) · 17 KB
/
ast.h
File metadata and controls
821 lines (662 loc) · 17 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/**
The One Programming Language
File: ast/ast.h
_ _
/ \ |\ | |_ Max Base
\_/ | \| |_ Copyright 2021; One Language Contributors
**/
#ifndef _ONE_AST_AST_H_
#define _ONE_AST_AST_H_
#include <inttypes.h>
#include "../builtins/array.h"
#include "../parser/lexer/token.h"
typedef enum _token_type TokenType;
typedef struct _location Location;
/*
package main
package test
package firstName.subName
*/
typedef struct
{
char* name;
} AstPackage;
// typedefs
typedef struct _ast_file AstFile;
typedef struct _ast_import_declaration AstImportDeclaration;
typedef struct _ast_import_name AstImportName;
typedef struct _ast_import_symbol AstImportSymbol;
typedef struct _ast_block_declaration AstBlockDeclaration;
typedef enum _ast_block_type AstBlockType;
typedef struct _ast_function_declaration AstFunctionDeclaration;
typedef enum _ast_attribute_type AstAttributeType;
typedef struct _ast_attribute_declaration AstAttributeDeclaration;
typedef struct _ast_param AstParam;
typedef struct _ast_data AstData;
typedef struct _ast_data_item AstDataItem;
typedef struct _ast_struct_declaration AstStructDeclaration;
typedef struct _ast_struct_field AstStructField;
typedef struct _ast_enum_declaration AstEnumDeclaration;
typedef struct _ast_enum_field AstEnumField;
typedef struct _ast_expr_declaration AstExprDeclaration;
typedef enum _ast_operator_type AstOperatorType;
typedef enum _ast_expr_type AstExprType;
typedef struct _ast_type_declaration AstTypeDeclaration;
typedef enum _ast_type_declaration_type AstTypeDeclarationType;
typedef struct _ast_type_function_declaration AstTypeFunctionDeclaration;
typedef struct _ast_type_alias_declaration AstTypeAliasDeclaration;
typedef struct _ast_type_sum_declaration AstTypeSumDeclaration;
typedef struct _ast_type_sum_item AstTypeSumItem;
typedef struct _ast_statement_declaration AstStatementDeclaration;
typedef enum _ast_statement_type AstStatementType;
typedef struct _ast_statement_if AstStatementIf;
typedef struct _ast_statement_for AstStatementFor;
typedef enum _ast_statement_for_type AstStatementForType;
typedef struct _ast_statement_for_c AstStatementForC;
typedef struct _ast_statement_for_each AstStatementForEach;
typedef struct _ast_statement_for_map AstStatementForMap;
typedef struct _ast_statement_for_loop AstStatementForLoop;
typedef struct _ast_statement_match AstStatementMatch;
typedef struct _ast_statement_ret AstStatementRet;
typedef struct _ast_statement_assign AstStatementAssign;
typedef struct _ast_statement_assign_const AstStatementAssignConst;
typedef struct _ast_statement_const_item AstStatementAssignConstItem;
typedef struct _ast_statement_variable AstStatementVariable;
typedef struct _ast_statement_expr AstStatementExpr;
// Array
typedef Array AstBlockDeclarationArray; // AstBlockDeclaration
typedef Array AstFunctionDeclarationArray; // AstFunctionDeclaration
typedef Array AstStructDeclarationArray; // AstStructDeclaration
typedef Array AstEnumDeclarationArray; // AstEnumDeclaration
typedef Array AstTypeDeclarationArray; // AstTypeDeclaration
typedef Array AstStatementDeclarationArray; // AstStatementDeclaration
typedef Array AstImportSymbolArray; // AstImportSymbol
typedef Array AstImportNameArray; // AstImportName
typedef Array AstImportDeclarationArray; // AstImportDeclaration
// typedef Array AstBlockDeclarationArray; // AstBlockDeclaration
typedef Array StringArray; // char
typedef Array AstParamArray; // AstParam
typedef Array AstAttributeDeclarationArray; // AstAttributeDeclaration
typedef Array AstDataItemArray; // AstDataItem
typedef Array AstStructFieldArray; // AstStructField
typedef Array AstEnumFieldArray; // AstEnumField
typedef Array AstTypeSumItemArray; // AstTypeSumItem
typedef Array AstStatementAssignConstItemArray; // AstStatementAssignConstItem
typedef struct _ast_file
{
AstPackage* module;
AstImportDeclarationArray* imports; // AstImportDeclaration
AstBlockDeclarationArray* blocks; // AstBlockDeclaration
const char* path;
const char* path_base;
} AstFile;
/*
import math // math.sin(), math.cos()
import math { sin } // sin()
import math { sin, cos } // sin(), cos()
import parentLib.childLib // childLib.functionName()
import parentLib.childLib as pc // pc.functionName()
import parentLib {childLib.functionName as a_fn} // a_fn()
import file // file.create()
import file { create } // create()
import file { create as cr } // cr()
*/
typedef struct _ast_import_declaration
{
// IMPORT ****.****.*** { *** } AS ***
// ^ pos
// ^ pos_names
// ^ pos_symbols
// ^ pos_alias
Location pos;
Location pos_names;
Location pos_symbols;
Location pos_alias;
AstImportNameArray* names; // AstImportName
AstImportSymbolArray* symbols; // AstImportSymbol
char* alias;
} AstImportDeclaration;
typedef struct _ast_import_name
{
Location pos;
const char* name;
} AstImportName;
typedef struct _ast_import_symbol
{
Location pos_names;
Location pos_alias;
bool has_alias;
AstImportNameArray* names; // char*
char* alias;
} AstImportSymbol;
typedef enum _ast_block_type
{
AST_BLOCK_FUNCTION,
AST_BLOCK_STRUCT,
AST_BLOCK_ENUM,
AST_BLOCK_TYPE,
AST_BLOCK_STATEMENT,
} AstBlockType;
/*
i8, u8
i16, u16
i32, u32
i64, u64
i128, u128
f32, f64
bool
char, string
pubName.publicTypeName
*/
typedef struct _ast_data
{
// &Node<T>
// ^ pos_address
// ^ pos_name
// ^ pos_generic
// Node<T>
// ^ pos_address or maybe nil
// ^ pos_name
// ^ pos_generic
Location pos;
Location pos_address;
Location pos_name;
Location pos_generic;
bool has_address;
bool has_generic;
AstDataItemArray* names; // AstDataItem
StringArray* generics; // char*
} AstData;
typedef struct _ast_data_item
{
Location pos;
char* name; // char*
} AstDataItem;
typedef struct _ast_param
{
// fn (mut t MyTime) century() int {}
// mut t MyTime
// ^ pos_mut
// ^ pos_name
// ^ pos_type
Location pos;
Location pos_mut;
Location pos_name;
Location pos_type;
bool is_mut;
char* name;
AstData* type;
} AstParam;
typedef struct _ast_function_declaration
{
// [main] FN ( mut name type ) fn_name () type { }
// ^ pos_attribute
// ^ pos_function
// ^ pos_receiver
// ^ pos_name
// ^ pos_param
// ^ pos_return
// ^ pos_body
Location pos;
Location pos_attribute;
Location pos_function;
Location pos_receiver;
Location pos_name;
Location pos_param;
Location pos_return;
Location pos_body;
bool is_main;
bool is_noreturn;
bool is_public;
bool is_depracted;
bool has_return;
char* name;
AstParam* receiver;
AstParamArray* params; // AstParam
AstBlockDeclaration* statements;
AstAttributeDeclarationArray* attributes; // AstAttributeDeclaration
} AstFunctionDeclaration;
typedef enum _ast_attribute_type
{
AST_ATTRIBUTE_IDENTIFIER,
AST_ATTRIBUTE_KEY,
} AstAttributeType;
typedef struct _ast_attribute_declaration
{
Location pos;
Location pos_name;
Location pos_value;
AstAttributeType type;
bool has_value;
char* name;
char* value;
} AstAttributeDeclaration;
typedef struct _ast_struct_declaration
{
Location pos;
Location pos_attribute;
Location pos_name;
Location pos_body;
char* name;
// Array global; // AstStructField
// Array pub_mut; // AstStructField
// Array pub; // AstStructField
// Array mut; // AstStructField
// Array imut; // AstStructField
AstStructFieldArray* fields; // AstStructField
AstAttributeDeclarationArray* attributes; // AstAttributeDeclaration
} AstStructDeclaration;
typedef struct _ast_enum_declaration
{
Location pos;
Location pos_attribute;
Location pos_name;
Location pos_body;
char* name;
AstEnumFieldArray* fields; // AstEnumField
AstAttributeDeclarationArray* attributes; // AstAttributeDeclaration
} AstEnumDeclaration;
typedef enum _ast_type_declaration_type
{
AST_TYPE_SUM,
AST_TYPE_ALIAS,
AST_TYPE_FUNCTION,
} AstTypeDeclarationType;
typedef struct _ast_type_function_declaration
{
Location pos;
Location pos_public;
Location pos_name;
Location pos_type;
bool is_public;
char* name;
AstData* data; // TODO: AstData can store a function type?
} AstTypeFunctionDeclaration;
typedef struct _ast_type_alias_declaration
{
Location pos;
Location pos_public;
Location pos_name;
Location pos_type;
bool is_public;
char* name;
AstData* data;
} AstTypeAliasDeclaration;
typedef struct _ast_type_sum_declaration
{
Location pos;
Location pos_public;
Location pos_names;
bool is_public;
char* name;
AstTypeSumItemArray* data; // AstTypeSumItem
} AstTypeSumDeclaration;
/*
struct Point {
x int
y int
}
struct Line {
p1 Point
p2 Point
}
type ObjectSumType = Line | Point
struct Moon {}
struct Mars {}
struct Venus {}
type World = Mars | Moon | Venus
type Filter = fn (string) string
*/
typedef struct _ast_type_declaration
{
Location pos;
Location pos_public;
Location pos_name;
AstTypeDeclarationType type;
bool is_public;
char* name;
union
{
AstTypeFunctionDeclaration* function;
AstTypeAliasDeclaration* alias;
AstTypeSumDeclaration* sum;
} value;
} AstTypeDeclaration;
typedef enum _ast_operator_type
{
AST_OPERATOR_NONE, // emit data
AST_OPERATOR_PLUS, // -
AST_OPERATOR_PLUS_PLUS, // --
AST_OPERATOR_MINUS, // -
AST_OPERATOR_MINUS_MINUS, // --
AST_OPERATOR_STAR, // *
AST_OPERATOR_STAR_STAR, // **
AST_OPERATOR_MOD, // %
AST_OPERATOR_SLASH, // /
AST_OPERATOR_SLASH_INT, // //
AST_OPERATOR_SHIFT_LEFT, // >>
AST_OPERATOR_SHIFT_RIGHT, // <<
AST_OPERATOR_BIT_AND, // &
AST_OPERATOR_BIT_OR, // |
AST_OPERATOR_BIT_XOR, // ^
AST_OPERATOR_BIT_NOT, // ~
AST_OPERATOR_AND, // &&
AST_OPERATOR_OR, // ||
AST_OPERATOR_NOT, // !
AST_OPERATOR_NOT_EQUAL, // !=
// AST_OPERATOR_NOT_EQUAL_EQUAL, // !==
AST_OPERATOR_EQUAL, // =
AST_OPERATOR_EQUAL_ASSIGN, // :=
AST_OPERATOR_EQUAL_EQUAL, // ==
// AST_OPERATOR_EQUAL_EQUAL, // ===
// Relational Operators
AST_OPERATOR_GREATER, // >
AST_OPERATOR_GREATER_EQUAL, // >=
AST_OPERATOR_LESS, // <
AST_OPERATOR_LESS_EQUAL, // <=
AST_OPERATOR_DOT, // parent.sub
AST_OPERATOR_DOT_DOT, // [1..4]
AST_OPERATOR_DOT_DOT_DOT, // {...objects}
// Assignment Operators
AST_OPERATOR_EQUAL_PLUS, // +=
AST_OPERATOR_EQUAL_MINUS, // -=
AST_OPERATOR_EQUAL_STAR, // *=
AST_OPERATOR_EQUAL_STAR_STAR, // **=
AST_OPERATOR_EQUAL_SLASH, // /=
AST_OPERATOR_EQUAL_MOD, // %=
AST_OPERATOR_EQUAL_BIT_AND, // &=
AST_OPERATOR_EQUAL_BIT_OR, // |=
AST_OPERATOR_EQUAL_BIT_NOT, // ^=
AST_OPERATOR_EQUAL_SHIFT_LEFT, // >>=
AST_OPERATOR_EQUAL_SHIFT_RIGHT, // <<=
} AstOperatorType;
typedef enum _ast_expr_type
{
AST_EXPRESSION_TYPE_I8,
AST_EXPRESSION_TYPE_U8,
AST_EXPRESSION_TYPE_I16,
AST_EXPRESSION_TYPE_U16,
AST_EXPRESSION_TYPE_I32,
AST_EXPRESSION_TYPE_U32,
AST_EXPRESSION_TYPE_I64,
AST_EXPRESSION_TYPE_U64,
AST_EXPRESSION_TYPE_I128,
AST_EXPRESSION_TYPE_U128,
AST_EXPRESSION_TYPE_F32,
AST_EXPRESSION_TYPE_F64,
AST_EXPRESSION_TYPE_BOOL,
AST_EXPRESSION_TYPE_STRING,
AST_EXPRESSION_TYPE_CHAR,
} AstExprType;
typedef struct _ast_expr_declaration
{
Location pos;
Location pos_op;
Location pos_left;
Location pos_right;
Location pos_value;
AstExprType type;
AstOperatorType op;
AstExprDeclaration* left;
AstExprDeclaration* right;
union
{
char vi8;
unsigned char vu8;
short vi16;
unsigned short vu16;
int vi32;
unsigned int vu32;
int64_t vi64;
uint64_t vu64;
float vf32;
double vf64;
bool vbool;
char* vstring;
char* vchar;
} value;
} AstExprDeclaration;
typedef struct _ast_statement_for_c
{
// for i:=i;
Location pos;
Location pos_init;
Location pos_clauses;
Location pos_action;
Location pos_body;
bool has_init;
bool has_clauses;
bool has_action;
AstExprDeclaration* init;
AstExprDeclaration* clauses;
AstExprDeclaration* action;
AstBlockDeclaration* body;
} AstStatementForC;
typedef struct _ast_statement_for_each
{
// names := ['Max', 'Ali', 'Javad', 'John']
// for i, name in names {}
// mut numbers := [0, 1, 2]
// for mut num in numbers {
// num++
// }
Location pos;
// TODO
} AstStatementForEach;
typedef struct _ast_statement_for_map
{
Location pos;
// TODO
} AstStatementForMap;
typedef struct _ast_statement_for_loop
{
// mut sum := 0
// mut i := 0
// for i <= 100 {
// sum += i
// i++
// }
Location pos;
// TODO
} AstStatementForLoop;
typedef struct _ast_statement_match
{
// match ... { }
Location pos;
// TODO
} AstStatementMatch;
typedef struct _ast_statement_ret
{
// return <expr>
// ^ pos
// ^pos_expr
// return
// ^ pos
Location pos;
Location pos_expr;
bool has_expr;
AstExprDeclaration* expr;
} AstStatementRet;
typedef struct _ast_statement_assign
{
// const <name> := <value>
Location pos;
Location pos_name;
Location pos_op;
Location pos_value;
char* name;
AstExprDeclaration* value;
} AstStatementAssign;
typedef struct _ast_statement_assign_const
{
// const (
// pi = 3.14
// world = '世界'
// )
// const <name> = <value>
Location pos;
Location pos_open;
Location pos_close;
bool is_multi;
AstStatementAssignConstItemArray* fields; // AstStatementAssignConstItem
// Array name; // char*
// Array value; // AstExprDeclaration
} AstStatementAssignConst;
typedef struct _ast_statement_const_item
{
Location pos_name;
Location pos_op;
Location pos_value;
char* name;
AstExprDeclaration* value;
} AstStatementAssignConstItem;
typedef struct _ast_statement_variable
{
// varName = 5
// varName += 7
// varStructName.fieldName = 7
// varStructName.fieldName *= 7
Location pos;
Location pos_left;
Location pos_op;
Location pos_right;
// Assignment Operators
AstOperatorType op;
AstExprDeclaration* left;
AstExprDeclaration* right;
} AstStatementVariable;
typedef struct _ast_statement_expr
{
// <expr>
// ^ pos
Location pos;
AstExprDeclaration* expr;
} AstStatementExpr;
typedef enum _ast_statement_type
{
AST_STATEMENT_IF,
AST_STATEMENT_FOR,
AST_STATEMENT_MATCH,
AST_STATEMENT_RET,
AST_STATEMENT_ASSIGN,
AST_STATEMENT_ASSIGN_CONST,
AST_STATEMENT_VARIABLE,
AST_STATEMENT_EXPRESSION,
} AstStatementType;
typedef struct _ast_statement_if
{
// if <expr> {} else ...
Location pos;
Location pos_expr;
Location pos_body;
Location pos_else;
bool has_else;
AstExprDeclaration* expr;
AstBlockDeclaration* body;
AstStatementIf* otherwise;
} AstStatementIf;
typedef enum _ast_statement_for_type
{
AST_STATEMENT_FOR_C,
AST_STATEMENT_FOR_EACH,
AST_STATEMENT_FOR_MAP,
AST_STATEMENT_FOR_LOOP,
} AstStatementForType;
typedef struct _ast_statement_for
{
Location pos;
AstStatementForType type;
union
{
AstStatementForC* c;
AstStatementForEach* each;
AstStatementForMap* map;
AstStatementForLoop* loop;
} value;
} AstStatementFor;
typedef struct _ast_statement_declaration
{
Location pos;
AstStatementType type;
union
{
AstStatementIf* clauses;
AstStatementFor* foreach;
AstStatementMatch* match;
AstStatementRet* ret;
AstStatementAssign* assign;
AstStatementAssignConst* assign_const;
AstStatementVariable* variable;
AstStatementExpr* expr;
} value;
} AstStatementDeclaration;
typedef struct _ast_block_item
{
Location pos;
AstBlockType type;
union
{
AstFunctionDeclaration* function;
AstStructDeclaration* structure;
AstEnumDeclaration* enumerate;
AstTypeDeclaration* type;
AstStatementDeclaration* statement;
} value;
} AstBlockItem;
typedef struct _ast_block_declaration
{
Location pos;
AstFunctionDeclarationArray* functions;
AstStructDeclarationArray* structures;
AstEnumDeclarationArray* enumerates;
AstTypeDeclarationArray* types;
AstStatementDeclarationArray* statements;
} AstBlockDeclaration;
typedef struct _ast_struct_field
{
Location pos;
Location pos_attribute;
Location pos_type;
Location pos_name;
Location pos_value;
bool is_mut;
bool is_public;
bool is_global;
bool has_default;
AstExprDeclaration* value;
AstData* type;
char* name;
AstAttributeDeclarationArray* attributes; // AstAttributeDeclaration
} AstStructField;
typedef struct _ast_enum_field
{
Location pos;
Location pos_attribute;
Location pos_name;
Location pos_value;
bool has_default;
AstExprDeclaration* value;
char* name;
AstAttributeDeclarationArray* attributes; // AstAttributeDeclaration
} AstEnumField;
typedef struct _ast_type_sum_item
{
Location pos;
AstData* data;
} AstTypeSumItem;
// char* ast_statement_name(AstStatementType type);
// char* ast_value_name(AstValueType type);
void ast_init(const char* input_file, const char* data, Token** tokens, AstFile* ast);
void ast_trace_ident(FILE* file_out);
void ast_trace_ident_next();
void ast_trace_ident_prev();
void ast_trace_blocks(FILE* file_out, AstBlockDeclarationArray* blocks);
void ast_trace_block(FILE* file_out, AstBlockDeclaration* block);
void ast_trace_imports(FILE* file_out, AstImportDeclarationArray* imports);
void ast_trace_import(FILE* file_out, AstImportDeclaration* import);
void ast_trace_import_symbols(FILE* file_out, AstImportSymbolArray* symbols);
void ast_trace_import_symbol(FILE* file_out, AstImportSymbol* symbol);
void ast_trace_import_names(FILE* file_out, AstImportNameArray* names);
void ast_trace(FILE* file_out, AstFile* ast);
void ast_free();
#endif // _ONE_AST_AST_H_