You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lua新手求教:如何为Hello-World函数构建抽象语法树(AST)

Building an AST for Your Lua Hello-World Function

Hey there! I totally get how ASTs can feel overwhelming when you're starting out—let's break this down step by step so you can see exactly how your code maps to a tree structure. First, let's recap your code for reference:

function foo()
    value = 10
    num = "to the"
    if value > 2 then
        print("Hello World")
    end
    return value
end
print(foo())

Core Idea of ASTs

Think of an AST as a structured "blueprint" of your code, where every piece of syntax (like a function definition, assignment, or if statement) becomes a node in the tree. Each node has a type (e.g., FunctionDefinition, Assignment) and may have child nodes that represent its parts.

Step-by-Step AST Breakdown

Let's start from the top level and drill down into each part:

1. Top-Level Program Node

This is the root of your AST, and it contains two child nodes:

  • A FunctionDefinition node for foo()
  • A CallExpression node for print(foo())

2. FunctionDefinition Node (for foo)

This node represents your entire function. It has three key parts:

  • Name: foo (a Identifier node)
  • Parameters: An empty list (since foo takes no arguments)
  • Body: A Block node containing all the statements inside foo()

The Block node has these child nodes (in order):

  • Assignment node 1: value = 10
  • Assignment node 2: num = "to the"
  • IfStatement node: The conditional check and its body
  • ReturnStatement node: return value

3. Assignment Nodes

Each assignment is a node with two children:

  • Left-hand side: An Identifier node (the variable name: value or num)
  • Right-hand side: A Literal node (the value being assigned: 10 is a NumericLiteral, "to the" is a StringLiteral)

Example for value = 10:

Assignment
├─ Identifier: "value"
└─ NumericLiteral: 10

4. IfStatement Node

This node has two main parts:

  • Condition: A BinaryExpression node representing value > 2
    • Operator: >
    • Left operand: Identifier node (value)
    • Right operand: NumericLiteral node (2)
  • Body: A Block node containing one CallExpression node (print("Hello World"))

The CallExpression for print("Hello World") has:

  • Callee: Identifier node (print)
  • Arguments: A list with one StringLiteral node ("Hello World")

5. ReturnStatement Node

This node has one child: an Identifier node (value), representing the value being returned.


6. Top-Level CallExpression Node (print(foo()))

This is the final line of your code. It's a CallExpression with:

  • Callee: Identifier node (print)
  • Arguments: A list containing another CallExpression node (calling foo()):
    • Callee: Identifier node (foo)
    • Arguments: Empty list

Visualizing the Tree Structure

If you were to draw this out, it would look something like this (simplified):

Program
├─ FunctionDefinition
│  ├─ Identifier: "foo"
│  ├─ Parameters: []
│  └─ Block
│     ├─ Assignment
│     │  ├─ Identifier: "value"
│     │  └─ NumericLiteral: 10
│     ├─ Assignment
│     │  ├─ Identifier: "num"
│     │  └─ StringLiteral: "to the"
│     ├─ IfStatement
│     │  ├─ BinaryExpression (>)
│     │  │  ├─ Identifier: "value"
│     │  │  └─ NumericLiteral: 2
│     │  └─ Block
│     │     └─ CallExpression
│     │        ├─ Identifier: "print"
│     │        └─ Arguments: [StringLiteral: "Hello World"]
│     └─ ReturnStatement
│        └─ Identifier: "value"
└─ CallExpression
   ├─ Identifier: "print"
   └─ Arguments:
      └─ CallExpression
         ├─ Identifier: "foo"
         └─ Arguments: []

Key Takeaway

Every line (and even parts of lines) of your code maps to a specific node type. Once you start matching syntax to node types, building ASTs becomes much more intuitive. Try picking apart smaller pieces of code first, then combine them into a full tree—you've got this!

内容的提问来源于stack exchange,提问作者verrrilog

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:50:42