V/AST/C代码对照
V语言编译器的基本思路:V代码=>AST语法树=>C代码/js代码/…
源文件
编译单个V源文件生成ast.File,编译一组源文件,生成[ ]ast.File
1. //AST代码
2. pub struct File {
3. pub:
4. path string
5. mod Module
6. global_scope &Scope
7. pub mut:
8. scope &Scope
9. stmts []Stmt //整个源文件的AST都在这里
10. imports []Import
11. errors []errors.Error
12. warnings []errors.Warning
13. }
模块
模块定义
1. //V代码
2. module main
3. fn main() {
4. println('from main')
5. }
1. //AST代码
2. pub struct Module {
3. pub:
4. name string
5. path string
6. expr Expr
7. pos token.Position
8. is_skipped bool // module main can be skipped in single file programs
9. }
10. //AST实例
11. {
12. "ast_type": "Module",
13. "name": "main",
14. "path": "",
15. "expr": "",
16. "is_skipped": false,
17. "pos": {
18. "line_nr": 0,
19. "pos": 0,
20. "len": 19
21. }
22. }
V模块,生成C代码后,只是对应元素名称的前缀,毕竟C语言中没有模块的概念
常量,结构体,接口,类型等一级元素生成C代码后的名称规则是:”模块名__名称”,用双下划线区隔
比如mymodule模块中的add()函数生成C代码后的名称为:mymodule__add()
结构体的方法等二级元素生成C代码后的名称规则是:”模块名 _ 类名 方法名”,用单下划线区隔
比如mymodule模块中的Color结构体的str()方法生成C代码后的名称为:mymodule__Color_str()
1. //C代码
2. static void main__main();
3. static void main__main() {
4. println(tos_lit("from main"));
5. }
导入模块
1. //V代码
2. import os
1. //AST代码
2. pub struct Import {
3. pub:
4. pos token.Position
5. mod string
6. alias string
7. pub mut:
8. syms []ImportSymbol
9. }
10. //AST实例
11. {
12. "ast_type": "Import",
13. "mod": "os",
14. "alias": "os",
15. "syms": [],
16. "pos": {
17. "line_nr": 2,
18. "pos": 20,
19. "len": 2
20. }
21. }
常量
常量定义
枚举
枚举定义
1. //V代码
2. enum Color {
3. white = 2
4. black
5. blue
6. }
1. //AST代码
2. pub struct EnumField {
3. pub:
4. name string
5. pos token.Position
6. comments []Comment
7. expr Expr
8. has_expr bool
9. }
11. pub struct EnumDecl {
12. pub:
13. name string
14. is_pub bool
15. is_flag bool // true when the enum has [flag] tag
16. is_multi_allowed bool
17. comments []Comment // enum Abc { /* comments */ ... }
18. fields []EnumField
19. attrs []table.Attr
20. pos token.Position
21. }
22. //AST实例
23. {
24. "ast_type": "EnumDecl",
25. "name": "main.Color",
26. "is_pub": false,
27. "is_flag": false,
28. "is_multi_allowed": false,
29. "pos": {
30. "line_nr": 9,
31. "pos": 59,
32. "len": 10
33. },
34. "fields": [{
35. "ast_type": "EnumField",
36. "name": "white",
37. "has_expr": true,
38. "expr": {
39. "ast_type": "IntegerLiteral",
40. "val": "2",
41. "pos": {
42. "line_nr": 10,
43. "pos": 81,
44. "len": 1
45. }
46. },
47. "pos": {
48. "line_nr": 10,
49. "pos": 73,
50. "len": 5
51. },
52. "comments": []
53. }, {
54. "ast_type": "EnumField",
55. "name": "black",
56. "has_expr": false,
57. "expr": "",
58. "pos": {
59. "line_nr": 11,
60. "pos": 84,
61. "len": 5
62. },
63. "comments": []
64. }, {
65. "ast_type": "EnumField",
66. "name": "blue",
67. "has_expr": false,
68. "expr": "",
69. "pos": {
70. "line_nr": 12,
71. "pos": 91,
72. "len": 4
73. },
74. "comments": []
75. }],
76. "comments": [],
77. "attrs": []
78. }
1. //C代码
2. typedef enum {
3. main__Color_white = 2, // 2
4. main__Color_black, // 2+1
5. main__Color_blue, // 2+2
6. } main__Color;
内置类型
数组
字典
函数
函数定义
函数调用
函数多返回值
流程控制
条件
匹配
循环
结构体
结构体定义
结构体初始化
结构体方法
1. //V代码
类型
类型别名
1. //V代码
2. type Myint = int
1. //AST代码
2. pub struct AliasTypeDecl {
3. pub:
4. name string
5. is_pub bool
6. parent_type table.Type
7. pos token.Position
8. }
9. //AST实例
10. {
11. "ast_type": "AliasTypeDecl",
12. "name": "Myint",
13. "is_pub": false,
14. "parent_type": "int",
15. "pos": {
16. "line_nr": 20,
17. "pos": 131,
18. "len": 10
19. }
20. }
1. //C代码
2. typedef int main__Myint;
函数类型
1. type MyFn = fn ( int, int) int
1. pub struct FnTypeDecl {
2. pub:
3. name string
4. is_pub bool
5. typ table.Type
6. pos token.Position
7. }
9. {
10. "ast_type": "FnTypeDecl",
11. "name": "main.MyFn",
12. "is_pub": false,
13. "typ": "main.MyFn",
14. "pos": {
15. "line_nr": 24,
16. "pos": 171,
17. "len": 9
18. }
19. }
1. typedef int (*main__MyFn)(int,int);
联合类型
1. type MySumtype = f32 | int | string
1. pub struct SumTypeDecl {
2. pub:
3. name string
4. is_pub bool
5. sub_types []table.Type
6. pos token.Position
7. }
9. {
10. "ast_type": "SumTypeDecl",
11. "name": "MySumtype",
12. "is_pub": false,
13. "sub_types": ["f32", "int", "string"],
14. "pos": {
15. "line_nr": 26,
16. "pos": 204,
17. "len": 14
18. }
19. }
1. typedef struct {
2. void* _object;
3. int typ;
4. } main__MySumtype;
