编译单个V源文件生成ast.File,编译一组源文件,生成[ ]ast.File

    模块

    模块定义

    1. module main
    2. fn main() {
    3. println('from main')
    4. }
    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. }

    常量,结构体,接口,类型等一级元素生成C代码后的名称规则是:”模块名__名称”,用双下划线区隔

    结构体的方法等二级元素生成C代码后的名称规则是:”模块名 _ 类名 方法名”,用单下划线区隔

    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. "mod": "os",
    13. "alias": "os",
    14. "syms": [],
    15. "pos": {
    16. "line_nr": 2,
    17. "pos": 20,
    18. "len": 2
    19. }
    20. }

    常量

    常量定义

    枚举定义

    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. }
    10. pub struct EnumDecl {
    11. pub:
    12. is_pub bool
    13. is_flag bool // true when the enum has [flag] tag
    14. is_multi_allowed bool
    15. comments []Comment // enum Abc { /* comments */ ... }
    16. fields []EnumField
    17. attrs []table.Attr
    18. pos token.Position
    19. }
    20. //AST实例
    21. {
    22. "ast_type": "EnumDecl",
    23. "name": "main.Color",
    24. "is_pub": false,
    25. "is_flag": false,
    26. "is_multi_allowed": false,
    27. "pos": {
    28. "line_nr": 9,
    29. "pos": 59,
    30. "len": 10
    31. },
    32. "fields": [{
    33. "ast_type": "EnumField",
    34. "name": "white",
    35. "has_expr": true,
    36. "expr": {
    37. "ast_type": "IntegerLiteral",
    38. "val": "2",
    39. "pos": {
    40. "line_nr": 10,
    41. "pos": 81,
    42. "len": 1
    43. }
    44. },
    45. "pos": {
    46. "line_nr": 10,
    47. "pos": 73,
    48. "len": 5
    49. },
    50. "comments": []
    51. }, {
    52. "ast_type": "EnumField",
    53. "name": "black",
    54. "has_expr": false,
    55. "expr": "",
    56. "pos": {
    57. "line_nr": 11,
    58. "pos": 84,
    59. "len": 5
    60. },
    61. "comments": []
    62. }, {
    63. "ast_type": "EnumField",
    64. "name": "blue",
    65. "has_expr": false,
    66. "expr": "",
    67. "line_nr": 12,
    68. "pos": 91,
    69. "len": 4
    70. },
    71. "comments": []
    72. }],
    73. "comments": [],
    74. "attrs": []
    75. }
    1. //C代码
    2. main__Color_white = 2, // 2
    3. main__Color_black, // 2+1
    4. main__Color_blue, // 2+2
    5. } 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. 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. }
    8. {
    9. "ast_type": "FnTypeDecl",
    10. "name": "main.MyFn",
    11. "is_pub": false,
    12. "typ": "main.MyFn",
    13. "pos": {
    14. "line_nr": 24,
    15. "pos": 171,
    16. "len": 9
    17. }
    18. }
    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. }
    8. {
    9. "ast_type": "SumTypeDecl",
    10. "name": "MySumtype",
    11. "is_pub": false,
    12. "sub_types": ["f32", "int", "string"],
    13. "pos": {
    14. "line_nr": 26,
    15. "pos": 204,
    16. "len": 14
    17. }

    接口定义

    泛型

    泛型函数

    泛型结构体