#lexer #interpreter #compiler #c #parser #fibonacci-sequence

应用 mini-c-parser

从头开始的最小C语言词法分析器、解析器和虚拟执行器

11个版本 (6个重大更新)

0.12.2 2024年7月8日
0.12.1 2024年7月7日
0.11.0 2024年7月6日
0.9.1 2024年6月18日
0.1.4 2024年6月10日

#29编程语言

Download history 442/week @ 2024-06-09 221/week @ 2024-06-16 9/week @ 2024-06-23 182/week @ 2024-06-30 298/week @ 2024-07-07 12/week @ 2024-07-14 72/week @ 2024-07-28

每月72次下载

MIT许可证

660KB
9K SLoC

Rust编写的C语言词法分析器、解析器和虚拟执行器

Rust编写的C语言词法分析器、解析器和虚拟执行器,从头开始。

不支持语法

  • #包含
  • #预处理指令
  • 联合
  • 枚举
  • 类型限定符(volatilerestrictstaticextern

特性

  • 标记化器(词法分析器)
  • 预处理器
  • 解析器(AST构建器)
  • 代码生成器
  • 虚拟机(指令执行器)

工作原理

阶段1:词法分析

将原始源代码标记化成一个标记列表。此阶段将删除C/C++注释,将空白序列合并成一个标记。换行符将被保留作为一个标记,以便后续阶段使用。如果源代码不以换行符结尾,它将自动添加一个换行符标记。

阶段2:行分析

分析每行的标记并生成一个包含分析结果的Line列表。此阶段将提取预处理器指令和宏定义。

阶段3:预处理

通过扩展宏和删除预处理器指令来预处理源代码。

阶段4:构建抽象语法树

从预处理后的标记流中构建一个抽象语法树(AST)。AST将被用于生成虚拟指令。

阶段5:生成指令

从AST生成一系列虚拟指令。这些指令将在软件级别执行。

阶段6:执行指令

执行生成的虚拟指令。虚拟机将模拟代码的执行。

注意

此过程不会生成二进制或汇编代码。相反,它将生成一系列虚拟指令(virtualmachine/instruction),这些指令将在软件级别执行。

如何运行

cargo run

要执行程序,请将C代码传递给stdin。完成后,按^D结束输入。程序将进行标记化、解析、生成指令并执行代码。

示例C代码(仅包含已实现的功能)位于samples/目录中。尝试使用以下命令运行它们:cat samples/sample.c | cargo run

示例

/// samples/sample.c

// declaration of function fibonacci sequence
int fibonacci(int);

#define MY_MACRO_FUNC(x, y) y + x

#if MY_MACRO_FUNC(1, 2) == 3

// main function
int main()
{
  print_str("Hello, World!"); // built in function 'print_str'
  int var = 10;
  int* ptr = &var;
  *ptr = MY_MACRO_FUNC(40, 60);
  print(ptr, *ptr, var); // built in function 'print'

  print(MY_MACRO_FUNC(10, 20));

  // print fibonacci sequence
  print_str("Fibonacci sequence:");
  int i;
  for (i = 1; i <= 10; i++)
  {
    print(i, fibonacci(i));
  }

  return 0;
}

// definition of function fibonacci sequence using recursion
int fibonacci(int n)
{
  if (n <= 2)
    return 1;
  else
    return fibonacci(n - 1) + fibonacci(n - 2);
}

#else

THIS WILL BE IGNORED

#endif

将C代码传递给stdin

cat samples/sample.c | cargo run

结果将如下

Enter your code (and ^D for EOF):
================================================================================
===============================Phase1: Tokenizing===============================
================================================================================
LINE | ---------------------------------Result----------------------------------
   0: 
   1: Identifier("int") Whitespace Identifier("fibonacci") LeftParen Identifier("int") RightParen SemiColon 
   2: 
   3: PreprocessorDefine Whitespace Identifier("MY_MACRO_FUNC") LeftParen Identifier("x") Comma Whitespace Identifier("y") RightParen Whitespace Identifier("y") Whitespace Plus Whitespace Identifier("x") 
   4: 
   5: PreprocessorIf Whitespace Identifier("MY_MACRO_FUNC") LeftParen ConstantInteger(1) Comma Whitespace ConstantInteger(2) RightParen Whitespace EqOp Whitespace ConstantInteger(3) 
   6: 
   7: 
   8: Identifier("int") Whitespace Identifier("main") LeftParen RightParen 
   9: LeftBrace 
  10: Whitespace Identifier("print_str") LeftParen StringLiteral("Hello, World!") RightParen SemiColon Whitespace 
  11: Whitespace Identifier("int") Whitespace Identifier("var") Whitespace Equal Whitespace ConstantInteger(10) SemiColon 
  12: Whitespace Identifier("int") Star Whitespace Identifier("ptr") Whitespace Equal Whitespace Ampersand Identifier("var") SemiColon 
  13: Whitespace Star Identifier("ptr") Whitespace Equal Whitespace Identifier("MY_MACRO_FUNC") LeftParen ConstantInteger(40) Comma Whitespace ConstantInteger(60) RightParen SemiColon 
  14: Whitespace Identifier("print") LeftParen Identifier("ptr") Comma Whitespace Star Identifier("ptr") Comma Whitespace Identifier("var") RightParen SemiColon Whitespace 
  15: 
  16: Whitespace Identifier("print") LeftParen Identifier("MY_MACRO_FUNC") LeftParen ConstantInteger(10) Comma Whitespace ConstantInteger(20) RightParen RightParen SemiColon 
  17: 
  18: Whitespace 
  19: Whitespace Identifier("print_str") LeftParen StringLiteral("Fibonacci sequence:") RightParen SemiColon 
  20: Whitespace Identifier("int") Whitespace Identifier("i") SemiColon 
  21: Whitespace Identifier("for") Whitespace LeftParen Identifier("i") Whitespace Equal Whitespace ConstantInteger(1) SemiColon Whitespace Identifier("i") Whitespace LeOp Whitespace ConstantInteger(10) SemiColon Whitespace Identifier("i") IncOp RightParen 
  22: Whitespace LeftBrace 
  23: Whitespace Identifier("print") LeftParen Identifier("i") Comma Whitespace Identifier("fibonacci") LeftParen Identifier("i") RightParen RightParen SemiColon 
  24: Whitespace RightBrace 
  25: 
  26: Whitespace Identifier("return") Whitespace ConstantInteger(0) SemiColon 
  27: RightBrace 
  28: 
  29: 
  30: Identifier("int") Whitespace Identifier("fibonacci") LeftParen Identifier("int") Whitespace Identifier("n") RightParen 
  31: LeftBrace 
  32: Whitespace Identifier("if") Whitespace LeftParen Identifier("n") Whitespace LeOp Whitespace ConstantInteger(2) RightParen 
  33: Whitespace Identifier("return") Whitespace ConstantInteger(1) SemiColon 
  34: Whitespace Identifier("else") 
  35: Whitespace Identifier("return") Whitespace Identifier("fibonacci") LeftParen Identifier("n") Whitespace Minus Whitespace ConstantInteger(1) RightParen Whitespace Plus Whitespace Identifier("fibonacci") LeftParen Identifier("n") Whitespace Minus Whitespace ConstantInteger(2) RightParen SemiColon 
  36: RightBrace 
  37: 
  38: PreprocessorElse 
  39: 
  40: Identifier("THIS") Whitespace Identifier("WILL") Whitespace Identifier("BE") Whitespace Identifier("IGNORED") 
  41: 
  42: PreprocessorEndIf 
================================================================================
=============================Phase2: Line Analysis==============================
================================================================================
LINE | ---------------------------------Result----------------------------------
   0: RawTokens { tokens: [Identifier("int"), Identifier("fibonacci"), LeftParen, Identifier("int"), RightParen, SemiColon] }
   1: DefineFunction { name: "MY_MACRO_FUNC", param_count: 2, replacement: [PreprocessorPlaceholder(1), Plus, PreprocessorPlaceholder(0)] }
   2: If { expression_tokens: [Identifier("MY_MACRO_FUNC"), LeftParen, ConstantInteger(1), Comma, ConstantInteger(2), RightParen, EqOp, ConstantInteger(3)] }
   3: RawTokens { tokens: [Identifier("int"), Identifier("main"), LeftParen, RightParen] }
   4: RawTokens { tokens: [LeftBrace] }
   5: RawTokens { tokens: [Identifier("print_str"), LeftParen, StringLiteral("Hello, World!"), RightParen, SemiColon] }
   6: RawTokens { tokens: [Identifier("int"), Identifier("var"), Equal, ConstantInteger(10), SemiColon] }
   7: RawTokens { tokens: [Identifier("int"), Star, Identifier("ptr"), Equal, Ampersand, Identifier("var"), SemiColon] }
   8: RawTokens { tokens: [Star, Identifier("ptr"), Equal, Identifier("MY_MACRO_FUNC"), LeftParen, ConstantInteger(40), Comma, ConstantInteger(60), RightParen, SemiColon] }
   9: RawTokens { tokens: [Identifier("print"), LeftParen, Identifier("ptr"), Comma, Star, Identifier("ptr"), Comma, Identifier("var"), RightParen, SemiColon] }
  10: RawTokens { tokens: [Identifier("print"), LeftParen, Identifier("MY_MACRO_FUNC"), LeftParen, ConstantInteger(10), Comma, ConstantInteger(20), RightParen, RightParen, SemiColon] }
  11: RawTokens { tokens: [Identifier("print_str"), LeftParen, StringLiteral("Fibonacci sequence:"), RightParen, SemiColon] }
  12: RawTokens { tokens: [Identifier("int"), Identifier("i"), SemiColon] }
  13: RawTokens { tokens: [Identifier("for"), LeftParen, Identifier("i"), Equal, ConstantInteger(1), SemiColon, Identifier("i"), LeOp, ConstantInteger(10), SemiColon, Identifier("i"), IncOp, RightParen] }
  14: RawTokens { tokens: [LeftBrace] }
  15: RawTokens { tokens: [Identifier("print"), LeftParen, Identifier("i"), Comma, Identifier("fibonacci"), LeftParen, Identifier("i"), RightParen, RightParen, SemiColon] }
  16: RawTokens { tokens: [RightBrace] }
  17: RawTokens { tokens: [Identifier("return"), ConstantInteger(0), SemiColon] }
  18: RawTokens { tokens: [RightBrace] }
  19: RawTokens { tokens: [Identifier("int"), Identifier("fibonacci"), LeftParen, Identifier("int"), Identifier("n"), RightParen] }
  20: RawTokens { tokens: [LeftBrace] }
  21: RawTokens { tokens: [Identifier("if"), LeftParen, Identifier("n"), LeOp, ConstantInteger(2), RightParen] }
  22: RawTokens { tokens: [Identifier("return"), ConstantInteger(1), SemiColon] }
  23: RawTokens { tokens: [Identifier("else")] }
  24: RawTokens { tokens: [Identifier("return"), Identifier("fibonacci"), LeftParen, Identifier("n"), Minus, ConstantInteger(1), RightParen, Plus, Identifier("fibonacci"), LeftParen, Identifier("n"), Minus, ConstantInteger(2), RightParen, SemiColon] }
  25: RawTokens { tokens: [RightBrace] }
  26: Else
  27: RawTokens { tokens: [Identifier("THIS"), Identifier("WILL"), Identifier("BE"), Identifier("IGNORED")] }
  28: EndIf
================================================================================
=============================Phase3: Preprocessing==============================
================================================================================
LINE | ---------------------------------Result----------------------------------
   0: [Int, Identifier("fibonacci"), LeftParen, Int, RightParen, SemiColon]
   1: [Int, Identifier("main"), LeftParen, RightParen]
   2: [LeftBrace]
   3: [Identifier("print_str"), LeftParen, StringLiteral("Hello, World!"), RightParen, SemiColon]
   4: [Int, Identifier("var"), Equal, ConstantInteger(10), SemiColon]
   5: [Int, Star, Identifier("ptr"), Equal, Ampersand, Identifier("var"), SemiColon]
   6: [Star, Identifier("ptr"), Equal, ConstantInteger(60), Plus, ConstantInteger(40), SemiColon]
   7: [Identifier("print"), LeftParen, Identifier("ptr"), Comma, Star, Identifier("ptr"), Comma, Identifier("var"), RightParen, SemiColon]
   8: [Identifier("print"), LeftParen, ConstantInteger(20), Plus, ConstantInteger(10), RightParen, SemiColon]
   9: [Identifier("print_str"), LeftParen, StringLiteral("Fibonacci sequence:"), RightParen, SemiColon]
  10: [Int, Identifier("i"), SemiColon]
  11: [For, LeftParen, Identifier("i"), Equal, ConstantInteger(1), SemiColon, Identifier("i"), LeOp, ConstantInteger(10), SemiColon, Identifier("i"), IncOp, RightParen]
  12: [LeftBrace]
  13: [Identifier("print"), LeftParen, Identifier("i"), Comma, Identifier("fibonacci"), LeftParen, Identifier("i"), RightParen, RightParen, SemiColon]
  14: [RightBrace]
  15: [Return, ConstantInteger(0), SemiColon]
  16: [RightBrace]
  17: [Int, Identifier("fibonacci"), LeftParen, Int, Identifier("n"), RightParen]
  18: [LeftBrace]
  19: [If, LeftParen, Identifier("n"), LeOp, ConstantInteger(2), RightParen]
  20: [Return, ConstantInteger(1), SemiColon]
  21: [Else]
  22: [Return, Identifier("fibonacci"), LeftParen, Identifier("n"), Minus, ConstantInteger(1), RightParen, Plus, Identifier("fibonacci"), LeftParen, Identifier("n"), Minus, ConstantInteger(2), RightParen, SemiColon]
  23: [RightBrace]
================================================================================
======================Phase4: Building AbstractSyntaxTree=======================
================================================================================
ASTs: 
TranslationUnit {
    statements: [
        FunctionDeclaration {
            return_type: Int32,
            name: "fibonacci",
            params: [
                Int32,
            ],
        },
        FunctionDefinitionStatement {
            return_type: Int32,
            name: "main",
            params: [],
            body: CompoundStatement {
                statements: [
                    ExpressionStatement {
                        expression: PostParen {
                            src: PrimaryIdentifier {
                                name: "print_str",
                            },
                            args: [
                                StringLiteral {
                                    value: "Hello, World!",
                                },
                            ],
                        },
                    },
                    DeclarationStatement {
                        vars: [
                            (
                                "var",
                                Int32,
                                Some(
                                    ConstantInteger {
                                        value: 10,
                                    },
                                ),
                            ),
                        ],
                    },
                    DeclarationStatement {
                        vars: [
                            (
                                "ptr",
                                Pointer(
                                    Int32,
                                ),
                                Some(
                                    UnaryExpression {
                                        op: AddressOf,
                                        src: PrimaryIdentifier {
                                            name: "var",
                                        },
                                    },
                                ),
                            ),
                        ],
                    },
                    ExpressionStatement {
                        expression: AssignExpression {
                            lhs: UnaryExpression {
                                op: Dereference,
                                src: PrimaryIdentifier {
                                    name: "ptr",
                                },
                            },
                            rhs: AdditiveExpression {
                                op: Add,
                                lhs: ConstantInteger {
                                    value: 60,
                                },
                                rhs: ConstantInteger {
                                    value: 40,
                                },
                            },
                        },
                    },
                    ExpressionStatement {
                        expression: PostParen {
                            src: PrimaryIdentifier {
                                name: "print",
                            },
                            args: [
                                PrimaryIdentifier {
                                    name: "ptr",
                                },
                                UnaryExpression {
                                    op: Dereference,
                                    src: PrimaryIdentifier {
                                        name: "ptr",
                                    },
                                },
                                PrimaryIdentifier {
                                    name: "var",
                                },
                            ],
                        },
                    },
                    ExpressionStatement {
                        expression: PostParen {
                            src: PrimaryIdentifier {
                                name: "print",
                            },
                            args: [
                                AdditiveExpression {
                                    op: Add,
                                    lhs: ConstantInteger {
                                        value: 20,
                                    },
                                    rhs: ConstantInteger {
                                        value: 10,
                                    },
                                },
                            ],
                        },
                    },
                    ExpressionStatement {
                        expression: PostParen {
                            src: PrimaryIdentifier {
                                name: "print_str",
                            },
                            args: [
                                StringLiteral {
                                    value: "Fibonacci sequence:",
                                },
                            ],
                        },
                    },
                    DeclarationStatement {
                        vars: [
                            (
                                "i",
                                Int32,
                                None,
                            ),
                        ],
                    },
                    ForStatement {
                        init: AssignExpression {
                            lhs: PrimaryIdentifier {
                                name: "i",
                            },
                            rhs: ConstantInteger {
                                value: 1,
                            },
                        },
                        cond: ComparisonExpression {
                            op: LessThanOrEqual,
                            lhs: PrimaryIdentifier {
                                name: "i",
                            },
                            rhs: ConstantInteger {
                                value: 10,
                            },
                        },
                        next: Some(
                            PostIncrement {
                                src: PrimaryIdentifier {
                                    name: "i",
                                },
                            },
                        ),
                        statement: CompoundStatement {
                            statements: [
                                ExpressionStatement {
                                    expression: PostParen {
                                        src: PrimaryIdentifier {
                                            name: "print",
                                        },
                                        args: [
                                            PrimaryIdentifier {
                                                name: "i",
                                            },
                                            PostParen {
                                                src: PrimaryIdentifier {
                                                    name: "fibonacci",
                                                },
                                                args: [
                                                    PrimaryIdentifier {
                                                        name: "i",
                                                    },
                                                ],
                                            },
                                        ],
                                    },
                                },
                            ],
                        },
                    },
                    ReturnStatement {
                        expr: Some(
                            ConstantInteger {
                                value: 0,
                            },
                        ),
                    },
                ],
            },
        },
        FunctionDefinitionStatement {
            return_type: Int32,
            name: "fibonacci",
            params: [
                (
                    Some(
                        "n",
                    ),
                    Int32,
                ),
            ],
            body: CompoundStatement {
                statements: [
                    IfStatement {
                        cond: ComparisonExpression {
                            op: LessThanOrEqual,
                            lhs: PrimaryIdentifier {
                                name: "n",
                            },
                            rhs: ConstantInteger {
                                value: 2,
                            },
                        },
                        then_statement: ReturnStatement {
                            expr: Some(
                                ConstantInteger {
                                    value: 1,
                                },
                            ),
                        },
                        else_statement: Some(
                            ReturnStatement {
                                expr: Some(
                                    AdditiveExpression {
                                        op: Add,
                                        lhs: PostParen {
                                            src: PrimaryIdentifier {
                                                name: "fibonacci",
                                            },
                                            args: [
                                                AdditiveExpression {
                                                    op: Sub,
                                                    lhs: PrimaryIdentifier {
                                                        name: "n",
                                                    },
                                                    rhs: ConstantInteger {
                                                        value: 1,
                                                    },
                                                },
                                            ],
                                        },
                                        rhs: PostParen {
                                            src: PrimaryIdentifier {
                                                name: "fibonacci",
                                            },
                                            args: [
                                                AdditiveExpression {
                                                    op: Sub,
                                                    lhs: PrimaryIdentifier {
                                                        name: "n",
                                                    },
                                                    rhs: ConstantInteger {
                                                        value: 2,
                                                    },
                                                },
                                            ],
                                        },
                                    },
                                ),
                            },
                        ),
                    },
                ],
            },
        },
    ],
}
================================================================================
========================Phase5: Generating Instructions=========================
================================================================================
ADDR | ---------------------------------Result----------------------------------
Instructions: 
   0: DefineLabel { label: "main" }
   1: PushStack { operand: Register(5) }
   2: MoveRegister { operand_from: Register(6), operand_to: Register(5) }
   3: MoveRegister { operand_from: Value(UInt64(0)), operand_to: Register(0) }
   4: PrintStr { str: Register(0) }
   5: MoveRegister { operand_from: Value(Int32(10)), operand_to: Register(0) }
   6: Assign { lhs_type: Int32, lhs: Register(1), rhs: Register(0) }
   7: PushStack { operand: Register(1) }
   8: MoveRegister { operand_from: Register(5), operand_to: Register(0) }
   9: AddAssign { lhs: Register(0), rhs: Value(Int64(0)) }
  10: Assign { lhs_type: Pointer(Int32), lhs: Register(1), rhs: Register(0) }
  11: PushStack { operand: Register(1) }
  12: MoveRegister { operand_from: Value(Int32(40)), operand_to: Register(0) }
  13: PushStack { operand: Register(0) }
  14: MoveRegister { operand_from: Value(Int32(60)), operand_to: Register(0) }
  15: Assign { lhs_type: UInt32, lhs: Register(0), rhs: Register(0) }
  16: PopStack { operand: Register(1) }
  17: AddAssign { lhs: Register(0), rhs: Register(1) }
  18: PushStack { operand: Register(0) }
  19: MoveRegister { operand_from: Register(5), operand_to: Register(0) }
  20: AddAssign { lhs: Register(0), rhs: Value(Int64(1)) }
  21: MoveRegister { operand_from: Derefed(0, 0), operand_to: Register(0) }
  22: PopStack { operand: Register(1) }
  23: Assign { lhs_type: Int32, lhs: Derefed(0, 0), rhs: Register(1) }
  24: MoveRegister { operand_from: Register(5), operand_to: Register(0) }
  25: AddAssign { lhs: Register(0), rhs: Value(Int64(0)) }
  26: PushStack { operand: Derefed(0, 0) }
  27: MoveRegister { operand_from: Register(5), operand_to: Register(0) }
  28: AddAssign { lhs: Register(0), rhs: Value(Int64(1)) }
  29: MoveRegister { operand_from: Derefed(0, 0), operand_to: Register(0) }
  30: PushStack { operand: Derefed(0, 0) }
  31: MoveRegister { operand_from: Register(5), operand_to: Register(0) }
  32: AddAssign { lhs: Register(0), rhs: Value(Int64(1)) }
  33: PushStack { operand: Derefed(0, 0) }
  34: PushStack { operand: Value(UInt64(3)) }
  35: Print
  36: MoveRegister { operand_from: Value(Int32(10)), operand_to: Register(0) }
  37: PushStack { operand: Register(0) }
  38: MoveRegister { operand_from: Value(Int32(20)), operand_to: Register(0) }
  39: Assign { lhs_type: UInt32, lhs: Register(0), rhs: Register(0) }
  40: PopStack { operand: Register(1) }
  41: AddAssign { lhs: Register(0), rhs: Register(1) }
  42: PushStack { operand: Register(0) }
  43: PushStack { operand: Value(UInt64(1)) }
  44: Print
  45: MoveRegister { operand_from: Value(UInt64(14)), operand_to: Register(0) }
  46: PrintStr { str: Register(0) }
  47: PushStack { operand: Value(Int32(0)) }
  48: MoveRegister { operand_from: Value(Int32(1)), operand_to: Register(0) }
  49: PushStack { operand: Register(0) }
  50: MoveRegister { operand_from: Register(5), operand_to: Register(0) }
  51: AddAssign { lhs: Register(0), rhs: Value(Int64(2)) }
  52: PopStack { operand: Register(1) }
  53: Assign { lhs_type: Int32, lhs: Derefed(0, 0), rhs: Register(1) }
  54: DefineLabel { label: ".__L0__" }
  55: MoveRegister { operand_from: Register(5), operand_to: Register(0) }
  56: AddAssign { lhs: Register(0), rhs: Value(Int64(2)) }
  57: PushStack { operand: Derefed(0, 0) }
  58: MoveRegister { operand_from: Value(Int32(10)), operand_to: Register(0) }
  59: PopStack { operand: Register(1) }
  60: LessThan { lhs: Register(0), rhs: Register(1), to: Register(0) }
  61: LogicalNot { operand: Register(0) }
  62: JumpZero { label: ".__L1__", operand_cond: Register(0) }
  63: MoveRegister { operand_from: Register(5), operand_to: Register(0) }
  64: AddAssign { lhs: Register(0), rhs: Value(Int64(2)) }
  65: PushStack { operand: Derefed(0, 0) }
  66: Call { label: "fibonacci" }
  67: SubAssign { lhs: Register(6), rhs: Value(UInt64(1)) }
  68: PushStack { operand: Register(0) }
  69: MoveRegister { operand_from: Register(5), operand_to: Register(0) }
  70: AddAssign { lhs: Register(0), rhs: Value(Int64(2)) }
  71: PushStack { operand: Derefed(0, 0) }
  72: PushStack { operand: Value(UInt64(2)) }
  73: Print
  74: DefineLabel { label: ".__L2__" }
  75: MoveRegister { operand_from: Register(5), operand_to: Register(0) }
  76: AddAssign { lhs: Register(0), rhs: Value(Int64(2)) }
  77: MoveRegister { operand_from: Register(0), operand_to: Register(1) }
  78: MoveRegister { operand_from: Derefed(1, 0), operand_to: Register(0) }
  79: AddAssign { lhs: Derefed(1, 0), rhs: Value(UInt8(1)) }
  80: Jump { label: ".__L0__" }
  81: DefineLabel { label: ".__L1__" }
  82: MoveRegister { operand_from: Value(Int32(0)), operand_to: Register(0) }
  83: Return
  84: Panic { message: "Function main must return a Int32 value" }
  85: DefineLabel { label: "fibonacci" }
  86: PushStack { operand: Register(5) }
  87: MoveRegister { operand_from: Register(6), operand_to: Register(5) }
  88: MoveRegister { operand_from: Register(5), operand_to: Register(0) }
  89: AddAssign { lhs: Register(0), rhs: Value(Int64(-3)) }
  90: PushStack { operand: Derefed(0, 0) }
  91: MoveRegister { operand_from: Value(Int32(2)), operand_to: Register(0) }
  92: PopStack { operand: Register(1) }
  93: LessThan { lhs: Register(0), rhs: Register(1), to: Register(0) }
  94: LogicalNot { operand: Register(0) }
  95: JumpZero { label: ".__L3__", operand_cond: Register(0) }
  96: MoveRegister { operand_from: Value(Int32(1)), operand_to: Register(0) }
  97: Return
  98: Jump { label: ".__L4__" }
  99: DefineLabel { label: ".__L3__" }
 100: MoveRegister { operand_from: Value(Int32(2)), operand_to: Register(0) }
 101: PushStack { operand: Register(0) }
 102: MoveRegister { operand_from: Register(5), operand_to: Register(0) }
 103: AddAssign { lhs: Register(0), rhs: Value(Int64(-3)) }
 104: Assign { lhs_type: UInt32, lhs: Register(0), rhs: Derefed(0, 0) }
 105: PopStack { operand: Register(1) }
 106: SubAssign { lhs: Register(0), rhs: Register(1) }
 107: PushStack { operand: Register(0) }
 108: Call { label: "fibonacci" }
 109: SubAssign { lhs: Register(6), rhs: Value(UInt64(1)) }
 110: PushStack { operand: Register(0) }
 111: MoveRegister { operand_from: Value(Int32(1)), operand_to: Register(0) }
 112: PushStack { operand: Register(0) }
 113: MoveRegister { operand_from: Register(5), operand_to: Register(0) }
 114: AddAssign { lhs: Register(0), rhs: Value(Int64(-3)) }
 115: Assign { lhs_type: UInt32, lhs: Register(0), rhs: Derefed(0, 0) }
 116: PopStack { operand: Register(1) }
 117: SubAssign { lhs: Register(0), rhs: Register(1) }
 118: PushStack { operand: Register(0) }
 119: Call { label: "fibonacci" }
 120: SubAssign { lhs: Register(6), rhs: Value(UInt64(1)) }
 121: Assign { lhs_type: UInt32, lhs: Register(0), rhs: Register(0) }
 122: PopStack { operand: Register(1) }
 123: AddAssign { lhs: Register(0), rhs: Register(1) }
 124: Return
 125: DefineLabel { label: ".__L4__" }
 126: Panic { message: "Function fibonacci must return a Int32 value" }
  --------------------------------Start Address---------------------------------
 127: Call { label: "main" }
================================================================================
=========================Phase6: Executing Instructions=========================
================================================================================
"Hello, World!"
Print: UInt64(36), Int32(100), Int32(100), 
Print: UInt32(30), 
"Fibonacci sequence:"
Print: Int32(1), Int32(1), 
Print: Int32(2), Int32(1), 
Print: Int32(3), UInt32(2), 
Print: Int32(4), UInt32(3), 
Print: Int32(5), UInt32(5), 
Print: Int32(6), UInt32(8), 
Print: Int32(7), UInt32(13), 
Print: Int32(8), UInt32(21), 
Print: Int32(9), UInt32(34), 
Print: Int32(10), UInt32(55), 

可视化的AST将如下

AST

TranslationUnit
├── DeclarationStatement
│   └── Vars
│       └── (Some("fibonacci"), Function(Int32, [Int32]), None)
├── FunctionDefinitionStatement: main
│   ├── ReturnType: Int32
│   ├── Name: main
│   ├── Params: []
│   └── Body: CompoundStatement
│       ├── ExpressionStatement
│       │   └── PostParen
│       │       ├── Src: PrimaryIdentifier(print_str)
│       │       └── Args: [StringLiteral("Hello, World!")]
│       ├── DeclarationStatement
│       │   └── Vars
│       │       └── (Some("var"), Int32, ConstantInteger(10))
│       ├── DeclarationStatement
│       │   └── Vars
│       │       └── (Some("ptr"), Pointer(Int32), UnaryExpression(AddressOf, PrimaryIdentifier(var)))
│       ├── ExpressionStatement
│       │   └── AssignExpression
│       │       ├── Op: Assign
│       │       ├── Lhs: UnaryExpression(Dereference, PrimaryIdentifier(ptr))
│       │       └── Rhs: ConstantInteger(100)
│       ├── ExpressionStatement
│       │   └── PostParen
│       │       ├── Src: PrimaryIdentifier(print)
│       │       └── Args: [PrimaryIdentifier(ptr), UnaryExpression(Dereference, PrimaryIdentifier(ptr)), PrimaryIdentifier(var)]
│       ├── ExpressionStatement
│       │   └── PostParen
│       │       ├── Src: PrimaryIdentifier(print_str)
│       │       └── Args: [StringLiteral("Fibonacci sequence:")]
│       ├── DeclarationStatement
│       │   └── Vars
│       │       └── (Some("i"), Int32, None)
│       ├── ForStatement
│       │   ├── Init: AssignExpression
│       │   │   ├── Op: Assign
│       │   │   ├── Lhs: PrimaryIdentifier(i)
│       │   │   └── Rhs: ConstantInteger(1)
│       │   ├── Cond: ComparisonExpression
│       │   │   ├── Op: LessThanOrEqual
│       │   │   ├── Lhs: PrimaryIdentifier(i)
│       │   │   └── Rhs: ConstantInteger(10)
│       │   ├── Next: PostIncrement(PrimaryIdentifier(i))
│       │   └── Statement: CompoundStatement
│       │       └── ExpressionStatement
│       │           └── PostParen
│       │               ├── Src: PrimaryIdentifier(print)
│       │               └── Args: [PrimaryIdentifier(i), PostParen(PrimaryIdentifier(fibonacci), [PrimaryIdentifier(i)])]
│       └── ReturnStatement
│           └── Expr: ConstantInteger(0)
├── FunctionDefinitionStatement: fibonacci
│   ├── ReturnType: Int32
│   ├── Name: fibonacci
│   ├── Params: [(Some("n"), Int32)]
│   └── Body: CompoundStatement
│       └── IfStatement
│           ├── Cond: ComparisonExpression
│           │   ├── Op: LessThanOrEqual
│           │   ├── Lhs: PrimaryIdentifier(n)
│           │   └── Rhs: ConstantInteger(2)
│           ├── Then: ReturnStatement
│           │   └── Expr: ConstantInteger(1)
│           └── Else: ReturnStatement
│               └── Expr: AdditiveExpression
│                   ├── Op: Add
│                   ├── Lhs: PostParen(PrimaryIdentifier(fibonacci), [AdditiveExpression(Sub, PrimaryIdentifier(n), ConstantInteger(1))])
│                   └── Rhs: PostParen(PrimaryIdentifier(fibonacci), [AdditiveExpression(Sub, PrimaryIdentifier(n), ConstantInteger(2))])

依赖项