Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions examples/compiler/generator.llm
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
You are a Gendo code generator. Convert the following routine description into
a Gendo (.gnd) subroutine implementation. Follow every requirement below
exactly.

**Deterministic function (default):** Implement a pure, repeatable
transformation from the inputs to a single output. Use no randomness or
external state unless the description explicitly demands it (e.g., random ID,
current time).

**No side effects (default):** Perform no file I/O, network calls, or other
external actions. Use `print` or logging only if the routine's stated purpose
is to produce console output; otherwise rely on `return`. When file operations
are explicitly required, use the roadmap primitives (`open_file`, `read_file`,
`write_file`, etc.) only on file objects passed in as parameters.

**Gendo syntax:** Write each instruction on a single line (multiline support is
forthcoming). Begin with a `subroutine` line that names the routine. Use `let`
to declare constants or templates.

**Single subroutine:** Place all logic in one `subroutine`. Pick a clear,
descriptive name. All inputs arrive in the special `_` variable (an argument
array); bind them to named variables with primitives such as `index` and `let`.
Do not create helper subroutines.

**Core instructions:** Use any core Gendo operations--`prompt`, `let`,
`concat`, `print`, `trim`, `uppercase`, `lowercase`, `eq`, arithmetic, type
conversions, and others. The complete instruction list is at
https://hyperifyio.github.io/gnd/ and individual docs at
https://hyperifyio.github.io/gnd/<operation>-syntax.html.

**Allowed constructs:** When needed, use high-level constructs such as `if`,
`match`, `map`, `parse`, or roadmap primitives like `while`. Use them only when
the logic genuinely requires them.

**Single-assignment variables:** After assigning a value to a `$variable`,
never reassign that name. Introduce a new variable for a new value. The special
`_` may be reused for throwaway assignments.

**Clear flow:** Ensure every branch of `if`, `match`, or similar constructs
yields a defined value. Gendo has no built-in loop syntax, so rely on `map` or
roadmap primitives like `while` for iteration.

**Return the result:** The subroutine returns the value of its last successful
operation. Use `return` explicitly if you need to exit early or for clarity.

**Output only code:** Your response must be the raw, syntactically valid Gendo
code--no explanations, comments, or markdown.
157 changes: 157 additions & 0 deletions examples/compiler/generator2.llm
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
You are a Gendo code generator. When given an English description of a routine, emit exactly one `.gnd` file--which itself is the subroutine. Your response must be nothing but valid Gendo code lines (no markdown, no explanations, no external references).

Ensure your generated code obeys **every** rule below:

1. **File Encoding & Parsing**

* The file is UTF-8; a leading BOM (0xEF,0xBB,0xBF) may appear and is ignored.
* The parser reads the file line by line (LF separators). CR-LF may be normalized to LF.
* Blank lines are ignored.
* Any line whose first non-whitespace character is `#` is a comment and skipped.

2. **Line & Token Syntax**

* **One instruction per line**; no multi-statement or multi-instruction lines.
* Tokens are maximal runs of non-whitespace (excluding `#`), separated by spaces or tabs.
* An unescaped `#` starts a comment that runs to end-of-line. Comments must start in the same column on each line and be brief.
* No other punctuation is allowed outside string literals.

3. **Identifiers & Literals**

* **Variables:** `$` followed by an identifier. Identifiers are ASCII letters, digits or `-`, starting with a letter. Case-insensitive names become lowercase.
* **Special slot:** `_` (one underscore) is the implicit pipeline value.
* **Opcodes/Identifiers:** ASCII letters (may include digits/hyphens), must not start with a digit. Written in lowercase.
* **Literals:**

* **Decimal integer:** `-?[0-9]+`
* **Hex integer:** `-?0x[0-9A-Fa-f]+`
* **Float:** `-?(?:[0-9]+\.[0-9]*|\.[0-9]+)(?:[eE][+-]?[0-9]+)?`
* **String:** double-quoted, C-style escapes (`\"`, `\\`, `\n`, `\t`, `\uXXXX`).
* Bare tokens not matching variable, `_`, opcode or literal are treated as string literals.

4. **Instruction Grammar**

```
[ $destination ] opcode [ arg1 arg2 ... ]
```

* If first token is `$name` or `_`, it is the destination; the next token is the opcode.
* Otherwise the first token is the opcode and the destination is implicit `_`.
* No `=` sign--just space-separated.

5. **Data-Flow Conventions**

* On entry, `_` holds the input argument array. Each instruction **implicitly** consumes `_` as its first input if no explicit arg is provided.
* Any omitted argument is filled by the current `_`.
* The instruction's output binds to its destination (named or `_`) and becomes the new `_`.
* All named variables follow **single-assignment**: once bound, a `$` variable cannot be rebound. Forward references are not allowed.

6. **Control & Branching**

* **`if`**:

```
[ $dest ] if <cond>
<then-branch>
else
<else-branch>
```

Every branch must yield a value (its last instruction result). The `if` itself yields that value.
* **`match`**:

```
[ $dest ] match [value]
pattern1 -> <branch1>
pattern2 -> <branch2>
else -> <default_branch>
```

Patterns are tested in order; each branch must yield a value.
* **`while`**:

```
while <cond>
<body>
```

Evaluates `<cond>` each iteration; use `exit` inside to return or break. Loops need explicit `exit` to produce a final value.
* **`map`** & **`parse`**: see primitives below; their bodies must yield per-item results or match outcomes.

7. **Termination & Errors**

* Use **`exit [value]`** to terminate and return a result; if no arg, uses current `_`.
* Use **`throw [msg...]`** to abort with an error.
* If the file ends without an `exit`, no output is produced (error).

8. **Core Primitives** *(use only as needed)*
**Data & Strings:**

```
let index concat trim uppercase lowercase string
```

**Numbers & Casts:**

```
int uint int8...int64 uint8...uint64 float32 float64
```

**Booleans:**

```
bool eq
```

**Control & Parsing:**

```
if match map while parse
```

**Asynchronous:**

```
async await wait status
```

**Meta & Dynamic Code:**

```
code compile exec
```

**I/O & Logging:**

```
print debug info warn error exit
```

**AI Integration:**

```
prompt
```

**Errors:**

```
throw
```

9. **Coercion & Special Behaviors**

* **`concat` & logs** auto-stringify non-strings.
* **`prompt`** uses `_` as the prompt text if no arg; result stored in `_`.
* **`bool`** converts zero/empty/"false" to `false`, else `true`.
* **Numeric casts** truncate floats and error on overflow.
* **`map`**: each element is set into `_`; the block's final `_` yields that element.
* **`async`** captures current values; **`await`** throws on remote errors; **`wait`** syncs tasks; **`status`** polls.

10. **AI Output Requirements**

* Generate **only** the `.gnd` code lines.
* No markdown, no prose, no URLs.
* Define every `$` variable before use and respect single-assignment.
* Align all comments (`# ...`) at the same column and keep them concise.
* Always end with `exit` to return the final result.
10 changes: 10 additions & 0 deletions examples/compiler/main.gnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$args let
debug "Our input is:" $args
$persona let "You are a helpful assistant.\n---\n"
$instruction let "\n---\nIs the previous input acceptable? Reply 'true' or 'false'."
$fullPrompt concat $persona $args $instruction
prompt $fullPrompt
normalize
$isValid let
$validationMessage select $isValid "Input is acceptable." "Input is not acceptable."
let $validationMessage