Skip to content

Commit 05f8b84

Browse files
committed
Make AST comments executable
1 parent d3628cf commit 05f8b84

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

src/pdl/pdl-schema.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@
13491349
},
13501350
"CodeBlock": {
13511351
"additionalProperties": false,
1352-
"description": "Execute a piece of code.\n\nExample:\n```PDL\n- def: N\n lang: python\n code: |\n import random\n # (In PDL, set `result` to the output you wish for your code block.)\n result = random.randint(1, 20)\n```",
1352+
"description": "Execute a piece of code.\n\nExample:\n```PDL\nlang: python\ncode: |\n import random\n # (In PDL, set `result` to the output you wish for your code block.)\n result = random.randint(1, 20)\n```",
13531353
"properties": {
13541354
"description": {
13551355
"anyOf": [
@@ -4318,7 +4318,7 @@
43184318
},
43194319
"IfBlock": {
43204320
"additionalProperties": false,
4321-
"description": "Conditional control structure.\n\nExample:\n```PDL\n- if: ${ eval == 'no' }\n then:\n text:\n - read:\n message: \"Why not?\\n\"\n```",
4321+
"description": "Conditional control structure.\n\nExample:\n```PDL\ndefs:\n answer:\n read:\n message: \"Enter a number? \"\nif: ${ (answer | int) == 42 }\nthen: You won!\n```",
43224322
"properties": {
43234323
"description": {
43244324
"anyOf": [
@@ -7186,7 +7186,7 @@
71867186
},
71877187
"MatchBlock": {
71887188
"additionalProperties": false,
7189-
"description": "Match control structure.",
7189+
"description": "Match control structure.\n\nExample:\n```PDL\ndefs:\n answer:\n read:\n message: \"Enter a number? \"\nmatch: ${ (answer | int) }\nwith:\n- case: 42\n then: You won!\n- case:\n any:\n def: x\n if: ${ x > 42 }\n then: Too high\n- then: Too low",
71907190
"properties": {
71917191
"description": {
71927192
"anyOf": [

src/pdl/pdl_ast.py

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,8 @@ class CodeBlock(BaseCodeBlock):
466466
467467
Example:
468468
```PDL
469-
- def: N
470-
lang: python
471-
code: |
469+
lang: python
470+
code: |
472471
import random
473472
# (In PDL, set `result` to the output you wish for your code block.)
474473
result = random.randint(1, 20)
@@ -600,11 +599,12 @@ class IfBlock(StructuredBlock):
600599
601600
Example:
602601
```PDL
603-
- if: ${ eval == 'no' }
604-
then:
605-
text:
606-
- read:
607-
message: "Why not?\\n"
602+
defs:
603+
answer:
604+
read:
605+
message: "Enter a number? "
606+
if: ${ (answer | int) == 42 }
607+
then: You won!
608608
```
609609
"""
610610

@@ -642,7 +642,25 @@ class MatchCase(BaseModel):
642642

643643

644644
class MatchBlock(StructuredBlock):
645-
"""Match control structure."""
645+
"""Match control structure.
646+
647+
Example:
648+
```PDL
649+
defs:
650+
answer:
651+
read:
652+
message: "Enter a number? "
653+
match: ${ (answer | int) }
654+
with:
655+
- case: 42
656+
then: You won!
657+
- case:
658+
any:
659+
def: x
660+
if: ${ x > 42 }
661+
then: Too high
662+
- then: Too low
663+
"""
646664

647665
kind: Literal[BlockKind.MATCH] = BlockKind.MATCH
648666
match_: ExpressionType[Any] = Field(alias="match")
@@ -735,7 +753,20 @@ class RepeatBlock(StructuredBlock):
735753

736754

737755
class ReadBlock(LeafBlock):
738-
"""Read from a file or standard input."""
756+
"""Read from a file or standard input.
757+
758+
Example. Read from the standard input with a prompt starting with `> `.
759+
```PDL
760+
read:
761+
message: "> "
762+
```
763+
764+
Example. Read the file `./data.yaml` in the same directory of the PDL file containing the block and parse it into YAML.
765+
```PDL
766+
read: ./data.yaml
767+
parser: yaml
768+
```
769+
"""
739770

740771
kind: Literal[BlockKind.READ] = BlockKind.READ
741772
read: ExpressionType[str] | None

0 commit comments

Comments
 (0)