2025-09-19 22:27:59 +09:00
# AST JSON v0 (Macro Expansion)
Status: Draft. This document specifies a minimal JSON schema for representing Nyash AST to enable macro expansion by external processes (e.g., PyVM-based MacroBox).
Top-level
- Object with `kind` discriminator.
- Nested nodes referenced inline; no IDs.
- Span is omitted in v0 (unknown). Future versions may include `span` with file/line/col.
2025-09-19 22:47:12 +09:00
Kinds (subset for Phase 2+)
2025-09-19 22:27:59 +09:00
- Program: { kind: "Program", statements: [Node] }
2025-09-19 22:47:12 +09:00
- Loop: { kind: "Loop", condition: Node, body: [Node] }
2025-09-19 22:27:59 +09:00
- Print: { kind: "Print", expression: Node }
- Return: { kind: "Return", value: Node|null }
2025-09-19 22:47:12 +09:00
- Break: { kind: "Break" }
- Continue: { kind: "Continue" }
2025-09-19 22:27:59 +09:00
- Assignment: { kind: "Assignment", target: Node, value: Node }
- If: { kind: "If", condition: Node, then: [Node], else: [Node]|null }
- FunctionDeclaration: { kind: "FunctionDeclaration", name: string, params: [string], body: [Node], static: bool, override: bool }
- Variable: { kind: "Variable", name: string }
- Literal: { kind: "Literal", value: LiteralValue }
- BinaryOp: { kind: "BinaryOp", op: string, left: Node, right: Node }
- UnaryOp: { kind: "UnaryOp", op: string, operand: Node }
- MethodCall: { kind: "MethodCall", object: Node, method: string, arguments: [Node] }
- FunctionCall: { kind: "FunctionCall", name: string, arguments: [Node] }
- Array: { kind: "Array", elements: [Node] }
- Map: { kind: "Map", entries: [{k: string, v: Node}] }
2025-09-19 22:52:22 +09:00
- Local: { kind: "Local", variables: [string], inits: [Node|null] }
2025-09-19 22:27:59 +09:00
LiteralValue
- { type: "string", value: string }
- { type: "int", value: integer }
- { type: "float", value: number }
- { type: "bool", value: boolean }
- { type: "null" }
- { type: "void" }
Unary operators
- "-" for Minus, "not" for Not
Binary operators
- "+", "-", "*", "/", "%", "& ", "|", "^", "<< ", ">>", "==", "!=", "< ", ">", "< =", ">=", "&& ", "||"
Notes
- The schema is intentionally minimal; it covers nodes needed for Phase 2 samples.
- Future: add `span` , `attrs` , typed annotations as needed.
## Example
Source:
```
print("x")
```
Expanded AST JSON v0:
```
{"kind":"Program","statements":[{"kind":"Print","expression":{"kind":"Literal","value":{"type":"string","value":"x"}}}]}
```