Initial PROTEL 2026 toolchain import
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
# 5.5.2 UNIX Command Line and Shell Exit Status
|
||||
|
||||
**PROTEL Introductory Manual 2026** — Vincente D'Ingianni
|
||||
|
||||
---
|
||||
|
||||
## 5.5.2 UNIX `argc` / `argv` and `RETURN` as Exit Status
|
||||
|
||||
On macOS and UNIX/Linux hosts, a standalone program's **IS ENTRY** procedure may receive the host command-line argument count and vector. A **RETURN** from an ENTRY body declared **RETURNS integer** becomes the process exit status observed by the shell (`echo $?`).
|
||||
|
||||
### Entry declaration
|
||||
|
||||
The **IS ENTRY** forward declaration may use the standard UNIX parameter list:
|
||||
|
||||
```protel
|
||||
DCL Start PROC(argc integer, argv PTR TO PTR TO char) IS ENTRY;
|
||||
```
|
||||
|
||||
The procedure body must use the same parameter list. To propagate a status to the shell, declare **RETURNS integer** on the body:
|
||||
|
||||
```protel
|
||||
DCL Start PROC(argc integer, argv PTR TO PTR TO char) RETURNS integer IS
|
||||
BLOCK
|
||||
...
|
||||
RETURN status;
|
||||
ENDBLOCK;
|
||||
```
|
||||
|
||||
Programs without command-line arguments continue to use the classical form from §5.5.2:
|
||||
|
||||
```protel
|
||||
DCL Start PROC() IS ENTRY;
|
||||
DCL Start PROC() IS
|
||||
BLOCK
|
||||
...
|
||||
ENDBLOCK;
|
||||
```
|
||||
|
||||
### Generated host `main`
|
||||
|
||||
The C++ transpiler emits a POSIX `main` that forwards `argc` and `argv` and returns the ENTRY result:
|
||||
|
||||
```cpp
|
||||
int main(int argc, char** argv) {
|
||||
return protel_generated::ArgsExit::Start(argc, argv);
|
||||
}
|
||||
```
|
||||
|
||||
When the ENTRY body does not **RETURNS integer**, the wrapper calls the procedure and returns `0`.
|
||||
|
||||
### Example: `examples/intro_5_5_2_args_exit.protel`
|
||||
|
||||
This module prints each command-line argument (including the program name) and **RETURN**s `argc` as the exit status.
|
||||
|
||||
Build and run:
|
||||
|
||||
```bash
|
||||
./protel examples/intro_5_5_2_args_exit.protel -o build/ArgsExit --run -- one two
|
||||
echo $?
|
||||
```
|
||||
|
||||
Arguments after `--` are forwarded to the compiled program when using **`--run`**.
|
||||
|
||||
Expected output:
|
||||
|
||||
```
|
||||
build/ArgsExit
|
||||
one
|
||||
two
|
||||
```
|
||||
|
||||
Expected exit status: `3` (program name plus two arguments).
|
||||
|
||||
### I/O
|
||||
|
||||
Command-line demonstration uses **EXTERNAL** C linkage to the minimal POSIX runtime (`writeln` in `src/runtime/protel_io.c`). I/O is not **INTRINSIC** and the transpiler does not emit `#include <iostream>`.
|
||||
|
||||
### See also
|
||||
|
||||
- §7.0 — **PROTEL 2026 Development Tools** (`protel` driver, `--run`)
|
||||
- §7.0.5 — **Runnable `.protel` files (shebang)** — direct execution with `#!/usr/bin/env protel-run`
|
||||
- Reference Manual §13 — **EXPORT** / **EXTERNAL** (ENTRY vs library procedures)
|
||||
@@ -0,0 +1,139 @@
|
||||
# 7.0 PROTEL 2026 Development Tools
|
||||
|
||||
**PROTEL Introductory Manual 2026** — Vincente D'Ingianni
|
||||
|
||||
---
|
||||
|
||||
## 7.0 PROTEL 2026 Development Tools
|
||||
|
||||
The PROTEL 2026 toolchain ships with a small set of command-line and editor tools intended for macOS and UNIX/Linux hosts with minimal dependencies.
|
||||
|
||||
| Tool | Section | Summary |
|
||||
|------|---------|---------|
|
||||
| **`protel`** | §7.1 | Parser, transpiler, and compiler driver |
|
||||
| **`Pb`** | §7.0.1 | Source beautifier (keyword uppercasing) |
|
||||
| **`protel-mode`** | §7.2 | GNU Emacs major mode for `.protel`, `.pt`, `.ptl`, legacy `.P`, and PLS `.AA01`–`.ZZ99` files |
|
||||
|
||||
---
|
||||
|
||||
## 7.0.1 Pb — PROTEL Beautifier
|
||||
|
||||
`Pb` uppercases PROTEL reserved keywords for readable, manual-style source. It is a filter-style tool:
|
||||
|
||||
```bash
|
||||
Pb examples/Hello.protel build/Hello.pretty.protel
|
||||
Pb --bold < myprog.protel > myprog.pretty.protel
|
||||
```
|
||||
|
||||
Install with `make install` (marks `Pb` executable). See `Pb --help` for options.
|
||||
|
||||
In Emacs, run Pb on the current buffer with **`C-c C-f`** (`M-x protel-beautify`) — see Introductory Manual §7.2.
|
||||
|
||||
---
|
||||
|
||||
## 7.0.2 PLS edition modules (`.AA01` … `.ZZ99`)
|
||||
|
||||
Historical DMS/PLS modules use edition suffixes such as `vdi.aa01`. The `protel` driver **automatically enables classical preprocessing** for these names (keyword normalization, `$LI` / `$EJECT` linker directives stripped before parse).
|
||||
|
||||
```bash
|
||||
protel vdi.aa01 -c -o build/vdi.aa01.o
|
||||
make vdi
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7.0.3 Type errors and `examples/errors/`
|
||||
|
||||
The transpiler performs **strong typing** checks (Reference Manual §11.3). Invalid assignments — such as storing an **integer** into **char** or a **PACK** byte — are rejected before code generation. See `docs/reference-error-messages.md` and `examples/errors/`.
|
||||
|
||||
```bash
|
||||
./protel examples/errors/err_assign_integer_to_char.protel
|
||||
./protel examples/errors/ok_assign_char_literal.protel -o build/ok_char
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7.0.4 UNIX command line and exit status
|
||||
|
||||
Standalone programs may read **`argc`** and **`argv`** in the **IS ENTRY** procedure and **RETURN** an integer exit status to the shell. See Introductory Manual §5.5.2 (`docs/intro-5-5-2-entry-args-exit.md`) and `examples/intro_5_5_2_args_exit.protel`.
|
||||
|
||||
```bash
|
||||
./protel examples/intro_5_5_2_args_exit.protel -o build/ArgsExit --run -- one two
|
||||
echo $?
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7.0.5 Runnable `.protel` files (shebang)
|
||||
|
||||
A PROTEL source file may be executed directly from the shell when:
|
||||
|
||||
1. **Line 1** is a UNIX shebang (`#!...`) naming **`protel-run`** (or `protel --run` via `env -S`).
|
||||
2. The file is **executable** (`chmod +x`).
|
||||
3. The PROTEL project directory is on your **`PATH`** so `protel-run` (or `protel`) is found.
|
||||
|
||||
The shebang is **not** PROTEL syntax. The preprocessor removes it before parse; **`Pb`** preserves it unchanged when beautifying.
|
||||
|
||||
`examples/Hello.protel` demonstrates the usual form:
|
||||
|
||||
```protel
|
||||
#!/usr/bin/env protel-run
|
||||
% Hello World — run with: ./Hello.protel
|
||||
SECTION Hello;
|
||||
...
|
||||
```
|
||||
|
||||
**`protel-run`** is a small helper in the project root. The kernel runs `protel-run /path/to/script.protel [args]`; the helper invokes `protel script.protel --run -- [args]`.
|
||||
|
||||
### Setup
|
||||
|
||||
From the PROTEL project directory:
|
||||
|
||||
```bash
|
||||
make install
|
||||
export PATH="$PWD:$PATH"
|
||||
./examples/Hello.protel
|
||||
```
|
||||
|
||||
From any directory (replace with your checkout path):
|
||||
|
||||
```bash
|
||||
export PATH="$HOME/PROTEL:$PATH"
|
||||
~/PROTEL/examples/Hello.protel
|
||||
```
|
||||
|
||||
`make install` marks `protel`, `protel-run`, `Pb`, and `examples/Hello.protel` executable.
|
||||
|
||||
**PATH pitfall:** `export PATH="~/PROTEL:$PATH"` does **not** work — the shell does not expand `~` inside double quotes, so `env` cannot find `protel-run`. Use **`$PWD`**, **`$HOME/PROTEL`**, or an absolute path instead.
|
||||
|
||||
### ENTRY programs with arguments
|
||||
|
||||
Add the same shebang to programs such as `examples/intro_5_5_2_args_exit.protel`, then:
|
||||
|
||||
```bash
|
||||
chmod +x examples/intro_5_5_2_args_exit.protel
|
||||
./examples/intro_5_5_2_args_exit.protel one two
|
||||
```
|
||||
|
||||
The kernel invokes `protel --run -- script.protel one two`. The driver promotes the script path from after **`--`** to the input positional, then forwards **`one`** and **`two`** to the ENTRY procedure.
|
||||
|
||||
### Notes
|
||||
|
||||
| Topic | Detail |
|
||||
|-------|--------|
|
||||
| `%` comments | Cannot replace the shebang; the kernel only recognizes `#!` at the start of the file |
|
||||
| **`Pb`** | Copies line 1 unchanged when it begins with `#!` |
|
||||
| **`protel-mode`** | Displays the shebang line with `protel-comment-face` (§7.2) |
|
||||
| **Parser** | `strip_shebang()` in `src/preprocess.py` runs before keyword normalization |
|
||||
| **Output binary** | Default `a.out` in the current working directory (same as `protel … --run`) |
|
||||
| `env: protel: No such file or directory` | **`PATH` wrong** — avoid quoted `~/PROTEL`; use `export PATH="$PWD:$PATH"` from the repo or `export PATH="$HOME/PROTEL:$PATH"` |
|
||||
|
||||
---
|
||||
|
||||
## See also
|
||||
|
||||
- §11.3 — **Type errors** (`docs/reference-error-messages.md`)
|
||||
- §5.5.2 — **UNIX argc/argv and shell exit status**
|
||||
- §7.0.5 — **Runnable `.protel` files (shebang)**
|
||||
- §7.2 — **EMACS PROTEL mode** (`.emacs` setup; automatic `.protel`, `.P`, and PLS suffix detection)
|
||||
- §7.3 — Foreign language interoperability
|
||||
@@ -0,0 +1,237 @@
|
||||
# 7.2 EMACS PROTEL Mode
|
||||
|
||||
**PROTEL Introductory Manual 2026** — Vincente D'Ingianni
|
||||
|
||||
---
|
||||
|
||||
## 7.2 EMACS PROTEL Mode
|
||||
|
||||
The PROTEL 2026 distribution includes **EMACS PROTEL mode** for GNU Emacs. It provides:
|
||||
|
||||
- **Keyword highlighting** for PROTEL 2026 reserved words (UPPERCASE keywords per the Reference Manual)
|
||||
- **Comment** recognition (`%` to end of line)
|
||||
- **String** highlighting (single-quoted PROTEL strings)
|
||||
- **Pascal-style indentation** for `BLOCK` / `ENDBLOCK`, `IF` / `ENDIF`, and related structures
|
||||
|
||||
The mode file is `emacs/protel-mode.el` in the PROTEL project tree. It honors Bill Conn, original author of EMACS PROTEL mode at Nortel/BNR.
|
||||
|
||||
---
|
||||
|
||||
## 7.2.1 Enabling PROTEL mode in `.emacs`
|
||||
|
||||
The PROTEL tree includes a ready-made **`.emacs`**. You may use it in either of these ways:
|
||||
|
||||
**Option A — copy to `~/.emacs` (recommended)**
|
||||
|
||||
```bash
|
||||
cp /path/to/PROTEL/.emacs ~/.emacs
|
||||
```
|
||||
|
||||
Edit the copy once and set your checkout path (required when the file is not inside the PROTEL tree):
|
||||
|
||||
```elisp
|
||||
(setq protel-user-root "/path/to/PROTEL")
|
||||
```
|
||||
|
||||
The project `.emacs` also searches, in order: `PROTEL_ROOT` environment variable, the directory containing this init file (if it still lives inside PROTEL), the `protel` executable on your `PATH`, then `~/PROTEL`.
|
||||
|
||||
**Option B — load from the project directory**
|
||||
|
||||
```bash
|
||||
cd /path/to/PROTEL
|
||||
emacs -l .emacs
|
||||
```
|
||||
|
||||
When `.emacs` remains inside the PROTEL tree, `protel-root` is discovered automatically.
|
||||
|
||||
After Emacs loads this block, `protel-mode` is available and **file-name detection is automatic** for:
|
||||
|
||||
| Extension | Mode started |
|
||||
|-----------|--------------|
|
||||
| `.protel` | `protel-mode` |
|
||||
| `.pt` | `protel-mode` |
|
||||
| `.ptl` | `protel-mode` |
|
||||
| `.P` | `protel-mode` (legacy Nortel/BNR suffix; case-sensitive match) |
|
||||
| `.AA01` … `.ZZ99` | `protel-mode` (PLS edition suffix: two letters + two digits) |
|
||||
|
||||
The PLS pattern matches any extension of the form **`.LLNN`** where `L` is a letter (`A`–`Z` or `a`–`z`) and `N` is a decimal digit. Examples: `vdi.aa01`, `module.AB18`, `Hello.aa01`.
|
||||
|
||||
No extra `auto-mode-alist` configuration is required in `.emacs`; `protel-mode.el` registers these extensions when it is loaded.
|
||||
|
||||
### Optional: user-wide install
|
||||
|
||||
To install the mode once under your home directory instead of pointing at the project tree:
|
||||
|
||||
```bash
|
||||
mkdir -p ~/.emacs.d/lisp
|
||||
cp /path/to/PROTEL/emacs/protel-mode.el ~/.emacs.d/lisp/
|
||||
```
|
||||
|
||||
Then in `.emacs`:
|
||||
|
||||
```elisp
|
||||
(add-to-list 'load-path "~/.emacs.d/lisp")
|
||||
(require 'protel-mode)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7.2.2 Verifying automatic detection
|
||||
|
||||
1. Restart Emacs (or evaluate the `.emacs` block with `M-x eval-buffer`).
|
||||
2. Open a PROTEL source file, for example `examples/Hello.protel`, legacy `examples/Hello.P`, or PLS `examples/Hello.aa01`.
|
||||
3. Confirm the mode line shows **`PROTEL`** (not `Fundamental`).
|
||||
4. Alternatively, run `M-x describe-mode` — it should report `protel-mode`.
|
||||
|
||||
To enable the mode manually on the current buffer (for example, a buffer without a recognized extension):
|
||||
|
||||
```
|
||||
M-x protel-mode RET
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7.2.3 Syntax highlighting and faces
|
||||
|
||||
`protel-mode` uses Emacs **Font Lock** to highlight keywords, comments, and strings. The mode does **not** hard-code RGB colors in the buffer; it assigns syntax classes to named **faces**, and Emacs (or your color theme) supplies the actual display attributes.
|
||||
|
||||
| Syntax | Face | Default appearance |
|
||||
|--------|------|------------------|
|
||||
| Reserved keywords | `protel-keyword-face` | **Bold white** (via `face-remap` from `font-lock-keyword-face`) |
|
||||
| `%` comments | `protel-comment-face` | *Green italic* |
|
||||
| `#!` shebang (line 1) | `protel-comment-face` | *Green italic* (runnable scripts, §7.0.5) |
|
||||
| `'...'` strings | `protel-string-face` | Orange (inherits theme string color) |
|
||||
|
||||
Font Lock applies the standard `font-lock-*` faces; `protel-mode` **remaps** them to `protel-*` faces so highlighting works reliably and you can customize colors with `set-face-attribute` or `M-x customize-face`.
|
||||
|
||||
The project `.emacs` in the PROTEL distribution loads `protel-mode`, enables global Font Lock, and sets the `protel-*` face colors (bold keywords, green italic comments).
|
||||
|
||||
### Customizing colors in `.emacs`
|
||||
|
||||
Override any face after loading the mode:
|
||||
|
||||
```elisp
|
||||
(with-eval-after-load 'protel-mode
|
||||
;; Bold keywords (default)
|
||||
(set-face-attribute 'protel-keyword-face nil :weight 'bold)
|
||||
|
||||
;; Green italic comments (default)
|
||||
(set-face-attribute 'protel-comment-face nil
|
||||
:foreground "green"
|
||||
:slant 'italic)
|
||||
|
||||
;; Optional: softer green on dark backgrounds
|
||||
;; (set-face-attribute 'protel-comment-face nil :foreground "dark olive green"))
|
||||
```
|
||||
|
||||
Use `M-x customize-face RET protel-comment-face RET` to adjust interactively.
|
||||
|
||||
### Beautify with Pb
|
||||
|
||||
`protel-mode` pipes the buffer (or active region) through **Pb**, the PROTEL beautifier (keyword uppercasing per the Reference Manual).
|
||||
|
||||
| Key | Command | Action |
|
||||
|-----|---------|--------|
|
||||
| `C-c C-f` | `protel-beautify` | Beautify region, or whole buffer if no region |
|
||||
| `C-c C-b` | `protel-beautify` | Same (mnemonic: **b**eautify / **P**b) |
|
||||
| `TAB` | `protel-indent-line` | Indent current line (Pascal style) |
|
||||
| `C-M-\` | `indent-region` | Indent active region (built-in) |
|
||||
|
||||
**Why `C-c C-f`?** In Emacs language modes, `C-c` is the standard mode-specific prefix. **`C-c C-f`** is widely used for *format* / *beautify*:
|
||||
|
||||
| Mode | `C-c C-f` |
|
||||
|------|-----------|
|
||||
| `go-mode` | `gofmt` |
|
||||
| `rust-mode` | `rustfmt` |
|
||||
| `csharp-mode` | reformat |
|
||||
| Many `clang-format` setups | format buffer/region |
|
||||
|
||||
PROTEL follows that convention. `C-c C-b` is a secondary mnemonic binding for Pb.
|
||||
|
||||
Pb must be on your `PATH`, or set in `.emacs`:
|
||||
|
||||
```elisp
|
||||
(setq protel-root "/path/to/PROTEL")
|
||||
;; optional:
|
||||
(setq protel-beautify-command "/path/to/PROTEL/Pb")
|
||||
(setq protel-beautify-extra-args "--bold") ; ANSI bold keywords on stdout
|
||||
```
|
||||
|
||||
### Other options
|
||||
|
||||
| Variable | Default | Purpose |
|
||||
|----------|---------|---------|
|
||||
| `protel-indent-offset` | `3` | Spaces to indent nested block bodies |
|
||||
| `protel-beautify-command` | *(auto)* | Path to Pb |
|
||||
| `protel-beautify-extra-args` | `""` | Extra Pb flags (e.g. `--bold`) |
|
||||
|
||||
Example:
|
||||
|
||||
```elisp
|
||||
(with-eval-after-load 'protel-mode
|
||||
(setq protel-indent-offset 3
|
||||
protel-root "/path/to/PROTEL"))
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7.2.4 Terminal Emacs (emacs-nox / `emacs -nw`)
|
||||
|
||||
**PROTEL mode works in terminal Emacs** — no GUI or X11 required. This includes **emacs-nox** on Linux and **`emacs -nw`** in macOS Terminal or iTerm.
|
||||
|
||||
| Feature | Terminal support |
|
||||
|---------|------------------|
|
||||
| `protel-mode` auto-detection | Yes |
|
||||
| Pascal-style **indentation** (`TAB`, `protel-indent-offset`) | Yes |
|
||||
| **Bold** keywords | Yes (terminal bold attribute; macOS Terminal and iTerm support this) |
|
||||
| Comment **colors** | Yes (green foreground on tty) |
|
||||
| Comment *italic* | Optional — many terminals ignore italic; color still applies |
|
||||
| Strings / other faces | Yes (terminal color palette) |
|
||||
|
||||
From the PROTEL project directory:
|
||||
|
||||
```bash
|
||||
emacs -nw -l .emacs examples/Hello.protel
|
||||
```
|
||||
|
||||
On Linux with a separate nox build:
|
||||
|
||||
```bash
|
||||
emacs-nox -l /path/to/PROTEL/.emacs /path/to/PROTEL/examples/Hello.protel
|
||||
```
|
||||
|
||||
Ensure **Font Lock** is on (`M-x font-lock-mode` or `(global-font-lock-mode 1)` in `.emacs`, already set in the project file). If keywords are not bold, verify your terminal profile allows bold text (Terminal.app: Settings → Profiles → Text → “Use bold fonts”).
|
||||
|
||||
---
|
||||
|
||||
## 7.2.5 Requirements
|
||||
|
||||
- **GNU Emacs** 26.1 or later (lexical binding is enabled in `protel-mode.el`)
|
||||
- **`cc-mode`** — included with standard Emacs and emacs-nox packages; `protel-mode` requires it for derived-mode support
|
||||
|
||||
---
|
||||
|
||||
## 7.2.6 Related development tools
|
||||
|
||||
| Tool | Manual section | Purpose |
|
||||
|------|----------------|---------|
|
||||
| `protel` | §7.1 | Transpile and compile PROTEL sources |
|
||||
| `Pb` | §7.0 | Beautify / uppercase keywords |
|
||||
| `protel-mode` | §7.2 (this section) | Emacs editing support |
|
||||
|
||||
For foreign-language linking from PROTEL, see §7.3.
|
||||
|
||||
---
|
||||
|
||||
## 7.2.7 Troubleshooting
|
||||
|
||||
| Symptom | Likely cause | Remedy |
|
||||
|---------|--------------|--------|
|
||||
| `Cannot open load file: protel-mode` | `load-path` does not include `emacs/` | In `~/.emacs`, set `(setq protel-user-root "/path/to/PROTEL")` |
|
||||
| `PROTEL mode not loaded` warning at startup | Copied `~/.emacs` without checkout path | Set `protel-user-root` in `~/.emacs`, or add PROTEL `protel` to `PATH` |
|
||||
| `Cannot find Pb` on beautify | Pb not on PATH | Set `protel-root` or `protel-beautify-command` after mode loads |
|
||||
| Buffer stays in `Fundamental` mode | `protel-mode` not loaded, or wrong extension | Add `(require 'protel-mode)`; use `.protel`, `.P`, `.AA01`-style PLS suffix, or `M-x protel-mode` |
|
||||
| No keyword highlighting | Font-lock not enabled | `M-x font-lock-mode` (normally on by default) |
|
||||
| `Cannot open load file: cc-mode` | Incomplete Emacs install | Use a full GNU Emacs build, not a minimal editor |
|
||||
| No bold in Terminal | Terminal profile disables bold | Enable bold fonts in Terminal.app / iTerm; `protel-keyword-face` uses `:weight bold` |
|
||||
| Plain text in emacs-nox | Font Lock off or mode not loaded | `M-x global-font-lock-mode`; `(require 'protel-mode)` in `.emacs` |
|
||||
@@ -0,0 +1,349 @@
|
||||
# 7.3 Foreign Language Interoperability
|
||||
|
||||
**PROTEL Introductory Manual 2026** — Vincente D'Ingianni
|
||||
|
||||
---
|
||||
|
||||
## 7.3 Foreign Language Interoperability
|
||||
|
||||
PROTEL 2026 programs can call routines written in **C**, **C++**, **Swift**, **Rust**, **Python** (via a thin C embedding shim), **Java** (via JNI), and other languages that support the **C calling convention**. Likewise, foreign programs can call procedures you publish from PROTEL. This is the recommended approach for I/O, platform APIs, and performance-critical libraries: PROTEL does **not** use **INTRINSIC** for ordinary host I/O in the 2026 toolchain.
|
||||
|
||||
All foreign interop goes through two declarations:
|
||||
|
||||
| Direction | PROTEL keyword | Foreign side |
|
||||
|-----------|----------------|--------------|
|
||||
| Foreign → PROTEL | **EXPORT** | Import C symbol (e.g. `extern "C"`, Swift `@_silgen_name`, Rust `extern "C"`, Python `ctypes.CDLL`, Java JNI bridge) |
|
||||
| PROTEL → Foreign | **EXTERNAL** | Export C symbol (e.g. C compilation unit, `#[no_mangle]`, `@_cdecl`, Python C API shim, JNI JVM embed) |
|
||||
|
||||
Working examples live under `examples/interop/` in the PROTEL distribution.
|
||||
|
||||
---
|
||||
|
||||
## 7.3.1 Strings and the C boundary
|
||||
|
||||
PROTEL character strings are **tuples of bytes** with a compile-time length. They are **not** automatically NUL-terminated when passed to C.
|
||||
|
||||
When a C, Swift, Rust, Python (via C), or Java (via JNI/C) function expects a `char*` / C string, append an explicit **zero** byte in a char tuple:
|
||||
|
||||
```
|
||||
writeln({'Hello', 0});
|
||||
swift_greet({'PROTEL', 0}, 42);
|
||||
```
|
||||
|
||||
The compiler emits a `(const char[]){ ... }` array; the trailing `0` is your responsibility.
|
||||
|
||||
---
|
||||
|
||||
## 7.3.2 Calling foreign code from PROTEL (EXTERNAL)
|
||||
|
||||
### Step 1 — Declare the foreign routine
|
||||
|
||||
```
|
||||
DCL writeln PROC(msg PTR TO char) EXTERNAL;
|
||||
```
|
||||
|
||||
### Step 2 — Implement it in the foreign language
|
||||
|
||||
**C** (`examples/interop` pattern / `protel_io.c`):
|
||||
|
||||
```c
|
||||
void writeln(const char* msg) {
|
||||
/* write(2) based implementation */
|
||||
}
|
||||
```
|
||||
|
||||
**Rust** (`rust_greet.rs`):
|
||||
|
||||
```rust
|
||||
#[no_mangle]
|
||||
pub extern "C" fn rust_greet(name: *const i8, value: i16) { /* ... */ }
|
||||
```
|
||||
|
||||
**Swift** (`swift_greet.swift`):
|
||||
|
||||
```swift
|
||||
@_cdecl("swift_greet")
|
||||
public func swift_greet(_ name: UnsafePointer<CChar>, _ value: Int32) { /* ... */ }
|
||||
```
|
||||
|
||||
**Python** (via C embedding shim `python_greet.c`):
|
||||
|
||||
```c
|
||||
void python_greet(const char* name, int16_t value) {
|
||||
/* Py_Initialize, import greet_helper, call greet(name, value) */
|
||||
}
|
||||
```
|
||||
|
||||
PROTEL does not call the Python interpreter directly. A small **C** translation unit exposes `python_greet` with C linkage; it embeds Python and forwards to `greet_helper.py`. Link against `libpython` (Python development headers required).
|
||||
|
||||
**Java** (via JNI embedding shim `java_greet.c`):
|
||||
|
||||
```c
|
||||
void java_greet(const char* name, int16_t value) {
|
||||
/* JNI_CreateJavaVM, FindClass("GreetHelper"), CallStaticVoidMethod greet */
|
||||
}
|
||||
```
|
||||
|
||||
PROTEL does not call the JVM directly. A **C** shim exposes `java_greet` with C linkage, embeds the JVM, and forwards to `GreetHelper.java`. Link against `libjvm` (`$JAVA_HOME/include`, JDK required).
|
||||
|
||||
### Step 3 — Call from PROTEL
|
||||
|
||||
```
|
||||
DCL Start PROC() IS ENTRY;
|
||||
|
||||
DCL Start PROC() IS
|
||||
BLOCK
|
||||
rust_greet({'PROTEL', 0}, 42);
|
||||
writeln({'done', 0});
|
||||
ENDBLOCK;
|
||||
```
|
||||
|
||||
### Step 4 — Build and link
|
||||
|
||||
```bash
|
||||
protel myprog.protel --keep -o build/myprog.cpp
|
||||
clang++ -std=c++20 -c build/myprog.cpp -o build/myprog.o
|
||||
rustc --crate-type staticlib rust_greet.rs -o build/librust_greet.a
|
||||
clang++ build/myprog.o build/librust_greet.a src/runtime/protel_io.c -o myprog
|
||||
```
|
||||
|
||||
See `examples/interop/build_interop.sh`, `build_interop_rust.sh`, `build_interop_python.sh`, and `build_interop_java.sh`.
|
||||
|
||||
---
|
||||
|
||||
## 7.3.3 Publishing PROTEL routines for foreign callers (EXPORT)
|
||||
|
||||
### Step 1 — Forward EXPORT declaration
|
||||
|
||||
```
|
||||
INTERFACE protel_math;
|
||||
|
||||
TYPE integer {-32768 TO 32767};
|
||||
|
||||
DCL protel_add PROC(a integer, b integer) RETURNS integer EXPORT;
|
||||
```
|
||||
|
||||
### Step 2 — Procedure body (same section)
|
||||
|
||||
```
|
||||
DCL protel_add PROC(a integer, b integer) RETURNS integer IS
|
||||
BLOCK
|
||||
RETURN a + b;
|
||||
ENDBLOCK;
|
||||
```
|
||||
|
||||
The transpiler places the implementation in an `extern "C"` block so the symbol name is `protel_add`.
|
||||
|
||||
### Step 3 — Import from foreign code
|
||||
|
||||
**C**:
|
||||
|
||||
```c
|
||||
#include <stdint.h>
|
||||
int16_t protel_add(int16_t a, int16_t b);
|
||||
```
|
||||
|
||||
**Rust** (`rust_calls_protel.rs`):
|
||||
|
||||
```rust
|
||||
extern "C" {
|
||||
fn protel_add(a: i16, b: i16) -> i16;
|
||||
}
|
||||
```
|
||||
|
||||
**Swift** (`swift_calls_protel.swift`):
|
||||
|
||||
```swift
|
||||
@_silgen_name("protel_add")
|
||||
func protel_add(_ a: Int16, _ b: Int16) -> Int16
|
||||
```
|
||||
|
||||
**Python** (`python_calls_protel.py`):
|
||||
|
||||
```python
|
||||
import ctypes
|
||||
|
||||
lib = ctypes.CDLL("build/interop/libprotel_for_python.dylib") # .so on Linux
|
||||
lib.protel_add.argtypes = (ctypes.c_int16, ctypes.c_int16)
|
||||
lib.protel_add.restype = ctypes.c_int16
|
||||
sum = lib.protel_add(40, 2)
|
||||
```
|
||||
|
||||
Build the PROTEL EXPORT module as a **shared library** (`.dylib` / `.so`), then load it with **`ctypes.CDLL`**. No Python extension module is required on the PROTEL side.
|
||||
|
||||
**Java** (`JavaCallsProtel.java` + `java_calls_protel.c`):
|
||||
|
||||
```java
|
||||
public final class JavaCallsProtel {
|
||||
static { System.loadLibrary("javacallsprotel"); }
|
||||
private static native short protelAdd(short a, short b);
|
||||
}
|
||||
```
|
||||
|
||||
Compile the EXPORT module and a thin **JNI** bridge into `libjavacallsprotel.dylib` (`.so` on Linux). `System.loadLibrary` loads the combined library; the JNI stub forwards to the EXPORTed `protel_add` symbol.
|
||||
|
||||
### Step 4 — Link
|
||||
|
||||
```bash
|
||||
protel protel_for_rust.protel --keep -o build/protel_for_rust.cpp
|
||||
clang++ -std=c++20 -c build/protel_for_rust.cpp -o build/protel_for_rust.o
|
||||
rustc rust_calls_protel.rs build/protel_for_rust.o -o rust_calls_protel
|
||||
```
|
||||
|
||||
**Python** (shared library + ctypes):
|
||||
|
||||
```bash
|
||||
protel protel_for_python.protel --keep -o build/protel_for_python.cpp
|
||||
clang++ -std=c++20 -fPIC -c build/protel_for_python.cpp -o build/protel_for_python.o
|
||||
clang++ -shared -o build/libprotel_for_python.so build/protel_for_python.o
|
||||
python3 examples/interop/python_calls_protel.py
|
||||
```
|
||||
|
||||
**Java** (shared library + JNI):
|
||||
|
||||
```bash
|
||||
protel protel_for_java.protel --keep -o build/protel_for_java.cpp
|
||||
clang++ -std=c++20 -fPIC -c build/protel_for_java.cpp -o build/protel_for_java.o
|
||||
clang -fPIC -c java_calls_protel.c -o build/java_calls_protel.o -I$JAVA_HOME/include
|
||||
clang++ -shared -o build/libjavacallsprotel.so build/protel_for_java.o build/java_calls_protel.o
|
||||
java -Djava.library.path=build/interop -cp build/interop JavaCallsProtel
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7.3.4 Complete minimal examples
|
||||
|
||||
### PROTEL calls Rust
|
||||
|
||||
File: `examples/interop/protel_calls_rust.protel`
|
||||
|
||||
```
|
||||
SECTION protel_calls_rust;
|
||||
|
||||
TYPE char {0 TO 255};
|
||||
TYPE integer {-32768 TO 32767};
|
||||
|
||||
DCL rust_greet PROC(name PTR TO char, value integer) EXTERNAL;
|
||||
DCL writeln PROC(msg PTR TO char) EXTERNAL;
|
||||
|
||||
DCL Start PROC() IS ENTRY;
|
||||
|
||||
DCL Start PROC() IS
|
||||
BLOCK
|
||||
rust_greet({'PROTEL', 0}, 42);
|
||||
writeln({'PROTEL called Rust', 0});
|
||||
ENDBLOCK;
|
||||
```
|
||||
|
||||
### PROTEL calls Python
|
||||
|
||||
File: `examples/interop/protel_calls_python.protel`
|
||||
|
||||
```
|
||||
SECTION protel_calls_python;
|
||||
|
||||
TYPE char {0 TO 255};
|
||||
TYPE integer {-32768 TO 32767};
|
||||
|
||||
DCL python_greet PROC(name PTR TO char, value integer) EXTERNAL;
|
||||
DCL writeln PROC(msg PTR TO char) EXTERNAL;
|
||||
|
||||
DCL Start PROC() IS ENTRY;
|
||||
|
||||
DCL Start PROC() IS
|
||||
BLOCK
|
||||
python_greet({'PROTEL', 0}, 42);
|
||||
writeln({'PROTEL called Python', 0});
|
||||
ENDBLOCK;
|
||||
```
|
||||
|
||||
### Rust calls PROTEL
|
||||
|
||||
File: `examples/interop/protel_for_rust.protel` (same EXPORT library as Swift example)
|
||||
|
||||
```
|
||||
INTERFACE protel_math;
|
||||
|
||||
TYPE integer {-32768 TO 32767};
|
||||
|
||||
DCL protel_add PROC(a integer, b integer) RETURNS integer EXPORT;
|
||||
|
||||
DCL protel_add PROC(a integer, b integer) RETURNS integer IS
|
||||
BLOCK
|
||||
RETURN a + b;
|
||||
ENDBLOCK;
|
||||
```
|
||||
|
||||
### PROTEL calls Java
|
||||
|
||||
File: `examples/interop/protel_calls_java.protel`
|
||||
|
||||
```
|
||||
SECTION protel_calls_java;
|
||||
|
||||
TYPE char {0 TO 255};
|
||||
TYPE integer {-32768 TO 32767};
|
||||
|
||||
DCL java_greet PROC(name PTR TO char, value integer) EXTERNAL;
|
||||
DCL writeln PROC(msg PTR TO char) EXTERNAL;
|
||||
|
||||
DCL Start PROC() IS ENTRY;
|
||||
|
||||
DCL Start PROC() IS
|
||||
BLOCK
|
||||
java_greet({'PROTEL', 0}, 42);
|
||||
writeln({'PROTEL called Java', 0});
|
||||
ENDBLOCK;
|
||||
```
|
||||
|
||||
### Python calls PROTEL
|
||||
|
||||
File: `examples/interop/python_calls_protel.py` loads `protel_for_python.protel` as a shared library via **ctypes**.
|
||||
|
||||
### Java calls PROTEL
|
||||
|
||||
File: `examples/interop/JavaCallsProtel.java` loads `protel_for_java.protel` via **JNI** (`java_calls_protel.c` bridge).
|
||||
|
||||
---
|
||||
|
||||
## 7.3.5 ENTRY vs EXPORT
|
||||
|
||||
- **IS ENTRY** — marks the **program** entry point. The transpiler emits a host `int main()` that calls your ENTRY procedure. Required for standalone executables.
|
||||
|
||||
- **EXPORT** — marks a **library** entry point for foreign languages. Does not create `main`. Use in INTERFACE sections intended to be called from Swift, Rust, Python, Java, C, etc.
|
||||
|
||||
A single module may have **one ENTRY** and **many EXPORT** procedures.
|
||||
|
||||
---
|
||||
|
||||
## 7.3.6 Quick reference
|
||||
|
||||
| Task | PROTEL | Build driver |
|
||||
|------|--------|--------------|
|
||||
| Hello World (PROTEL only) | `protel Hello.protel --run` | `protel` + `clang++` |
|
||||
| Hello World (direct shebang) | `./examples/Hello.protel` | §7.0.5 — `export PATH="$PWD:$PATH"`, file executable |
|
||||
| PROTEL + Swift | `make interop-swift` | `examples/interop/build_interop.sh` |
|
||||
| PROTEL + Rust | `make interop-rust` | `examples/interop/build_interop_rust.sh` |
|
||||
| PROTEL + Python | `make interop-python` | `examples/interop/build_interop_python.sh` |
|
||||
| PROTEL + Java | `make interop-java` | `examples/interop/build_interop_java.sh` |
|
||||
| Edit `.protel` / `.P` / PLS `.AA01` in Emacs | Add `emacs/` to `load-path`, `(require 'protel-mode)` | See §7.2 |
|
||||
| Beautify source | `Pb file.protel` | `Pb` |
|
||||
|
||||
---
|
||||
|
||||
## 7.3.7 Troubleshooting
|
||||
|
||||
| Symptom | Likely cause |
|
||||
|---------|----------------|
|
||||
| Link error: undefined symbol | EXTERNAL name mismatch; foreign function not `extern "C"` / `#[no_mangle]` / `@_cdecl` |
|
||||
| Garbled string output | Missing `, 0` NUL suffix on char tuple |
|
||||
| Duplicate `main` at link | Swift/Rust object compiled with its own `main`; use `-parse-as-library` for helper objects |
|
||||
| `ctypes` cannot load library | Build EXPORT module as `.dylib`/`.so` first; pass full path to `CDLL` |
|
||||
| Python embed link error | Install Python dev package (`python3-dev`); use `python3-config --embed --ldflags` (or `python3-config --ldflags`) |
|
||||
| Python greet silent | Embedded `print` may need `flush=True` when stdout is not a TTY |
|
||||
| `UnsatisfiedLinkError` (Java) | Set `-Djava.library.path` to the directory containing `libjavacallsprotel`; build JNI bridge first |
|
||||
| JVM embed link error | Set `JAVA_HOME` to a JDK; link `-L$JAVA_HOME/lib/server -ljvm` with rpath |
|
||||
| No `main` at link | Program section missing **IS ENTRY** |
|
||||
| `multiple ENTRY` error | More than one `IS ENTRY` in the module |
|
||||
|
||||
For the formal semantics of **EXTERNAL** and **EXPORT**, see Reference Manual §13.0.
|
||||
@@ -0,0 +1,177 @@
|
||||
# 13.0 EXTERNAL and EXPORT Procedures
|
||||
|
||||
**PROTEL Reference Manual 2026** — Vincente D'Ingianni
|
||||
|
||||
---
|
||||
|
||||
## 13.0 EXTERNAL and EXPORT Procedures
|
||||
|
||||
PROCEDURE constants may be declared **EXTERNAL** (import a foreign routine) or **EXPORT** (publish a PROTEL routine for foreign callers). In the PROTEL 2026 transpiler, both map to the **C application binary interface (ABI)** so that code produced by GCC, Clang, Swift, Rust, Python (via **ctypes** or a C embedding shim), Java (via **JNI**), and other C-compatible toolchains can be linked with PROTEL modules.
|
||||
|
||||
Ordinary procedure constants (without EXTERNAL or EXPORT) are emitted inside C++ namespaces and are intended for use within the PROTEL module. EXTERNAL and EXPORT procedures use `extern "C"` linkage at the generated C/C++ level so that symbol names are stable and unmangled.
|
||||
|
||||
---
|
||||
|
||||
## SYNTAX
|
||||
|
||||
```
|
||||
<DCL GROUP> ::= <PROCEDURE DCL GROUP><PROCEDURE INIT>
|
||||
|
||||
<PROCEDURE INIT> ::= IS <BLOCK>
|
||||
| IS INTRINSIC
|
||||
| IS ENTRY
|
||||
| EXTERNAL
|
||||
| EXPORT
|
||||
|
||||
<PROCEDURE DCL GROUP> ::= <ID LIST><PROCEDURE ATTRIBUTES>
|
||||
```
|
||||
|
||||
**EXTERNAL** and **EXPORT** are mutually exclusive with **IS** \<block\>, **IS INTRINSIC**, and **IS ENTRY** on the same declaration.
|
||||
|
||||
An EXPORT or EXTERNAL declaration is a **forward** declaration (like **IS FORWARD**). The procedure body, if any, appears in a subsequent **DCL** statement with the same name and compatible signature in the **same section**.
|
||||
|
||||
---
|
||||
|
||||
## EXTERNAL (import)
|
||||
|
||||
### Description
|
||||
|
||||
1. **EXTERNAL** declares that the named procedure is **implemented outside** the current module (typically in C, C++, Rust, Swift, Python via a C API bridge, Java via JNI, or another language that exports C-callable symbols).
|
||||
|
||||
2. The transpiler emits an `extern "C"` **prototype** only. No code is generated for the body.
|
||||
|
||||
3. Parameter and return types are mapped to C types (for example, **integer** → `int16_t`, **PTR TO char** → `const char*` for string arguments in external calls).
|
||||
|
||||
4. **PROTEL strings are not NUL-terminated.** When passing text to an EXTERNAL procedure that expects a C string, the caller must supply an **explicit trailing `0`** byte in a char tuple (see Introductory Manual §7.3).
|
||||
|
||||
### Examples
|
||||
|
||||
```
|
||||
DCL writeln PROC(msg PTR TO char) EXTERNAL;
|
||||
|
||||
DCL rust_greet PROC(name PTR TO char, value integer) EXTERNAL;
|
||||
|
||||
DCL python_greet PROC(name PTR TO char, value integer) EXTERNAL;
|
||||
|
||||
DCL java_greet PROC(name PTR TO char, value integer) EXTERNAL;
|
||||
|
||||
DCL read_config PROC(path PTR TO char, size integer) RETURNS integer EXTERNAL;
|
||||
```
|
||||
|
||||
### Generated C++ (conceptual)
|
||||
|
||||
```cpp
|
||||
extern "C" {
|
||||
void writeln(const char* msg);
|
||||
void rust_greet(const char* name, int16_t value);
|
||||
int16_t read_config(const char* path, int16_t size);
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## EXPORT (publish)
|
||||
|
||||
### Description
|
||||
|
||||
1. **EXPORT** declares that a procedure constant is part of the **public foreign interface** of the module. The implementation is emitted with `extern "C"` linkage at global scope (outside C++ namespaces).
|
||||
|
||||
2. There is no restriction on the number of EXPORT procedures per module (unlike **ENTRY**, which is limited to one per module).
|
||||
|
||||
3. EXPORT implies a forward declaration. The body must follow in the same section with an identical signature (parameter list, **RETURNS** type, and procedure class).
|
||||
|
||||
4. EXPORT procedures are suitable for **INTERFACE** sections that act as libraries for C, Swift, Rust, Python, Java, or other hosts. They do not, by themselves, define a program entry point. Use **IS ENTRY** for executable programs.
|
||||
|
||||
5. EXPORT must not be combined with **IS ENTRY** on the same procedure name.
|
||||
|
||||
### Examples
|
||||
|
||||
```
|
||||
INTERFACE protel_math;
|
||||
|
||||
TYPE integer {-32768 TO 32767};
|
||||
|
||||
DCL protel_add PROC(a integer, b integer) RETURNS integer EXPORT;
|
||||
|
||||
DCL protel_add PROC(a integer, b integer) RETURNS integer IS
|
||||
BLOCK
|
||||
RETURN a + b;
|
||||
ENDBLOCK;
|
||||
```
|
||||
|
||||
### Generated C++ (conceptual)
|
||||
|
||||
```cpp
|
||||
namespace protel_generated {
|
||||
namespace protel_math {
|
||||
/* EXPORT protel_add (defined with C linkage below) */
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
int16_t protel_add(int16_t a, int16_t b)
|
||||
{
|
||||
return (a + b);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Comparison with other procedure initialisers
|
||||
|
||||
| Clause | Purpose | Body location | Linkage | Count per module |
|
||||
|--------|---------|---------------|---------|------------------|
|
||||
| *(none)* | Internal procedure | Same section | C++ namespace | unlimited |
|
||||
| **IS FORWARD** | Forward reference | Later in module | C++ namespace | unlimited |
|
||||
| **IS ENTRY** | Program entry | Same or other section | C++ namespace; host `main` calls ENTRY | **one** |
|
||||
| **IS INTRINSIC** | Machine primitive | Compiler provided | implementation-defined | unlimited |
|
||||
| **EXTERNAL** | Import foreign | Foreign object file | `extern "C"` import | unlimited |
|
||||
| **EXPORT** | Export to foreign | Same section | `extern "C"` export | unlimited |
|
||||
|
||||
---
|
||||
|
||||
## Type mapping for foreign calls
|
||||
|
||||
| PROTEL type | C/C++ type (typical) | Notes |
|
||||
|-------------|----------------------|-------|
|
||||
| **integer** | `int16_t` | Range {-32768 TO 32767} |
|
||||
| **char** (range) | `uint8_t` | Single byte |
|
||||
| **PTR TO char** | `const char*` | Caller supplies NUL if C string required |
|
||||
| **BOOL** | `bool` / `uint8_t` | Implementation-defined packing |
|
||||
| **PROC** (procedure value) | function pointer | Rare in EXTERNAL declarations |
|
||||
|
||||
---
|
||||
|
||||
## Linking
|
||||
|
||||
1. Transpile PROTEL to `.cpp` with the PROTEL 2026 compiler (`protel`).
|
||||
|
||||
2. Compile generated C++ with `g++` or `clang++` (`-std=c++20`).
|
||||
|
||||
3. Compile foreign sources (`.c`, `.rs`, `.swift`, `.java`, etc.) to objects or static libraries using the foreign toolchain. For **Python → PROTEL**, compile the EXPORT module as a shared library and load it with **`ctypes.CDLL`**. For **PROTEL → Python**, link a C shim that embeds the Python interpreter (`python_greet.c` in `examples/interop/`). For **Java → PROTEL**, link the EXPORT module with a JNI bridge (`java_calls_protel.c`). For **PROTEL → Java**, link a C shim that embeds the JVM (`java_greet.c`).
|
||||
|
||||
4. Link all objects with the same C ABI conventions. On macOS and Linux, `clang++` is commonly used as the final link driver. Python embedding requires linking `libpython` (see `python3-config --embed --ldflags`). Java embedding requires linking `libjvm` from `$JAVA_HOME/lib/server`.
|
||||
|
||||
5. The minimal PROTEL I/O runtime (`protel_io.c`) is linked only when EXTERNAL procedures such as `writeln` from the example runtime are referenced.
|
||||
|
||||
---
|
||||
|
||||
## Restrictions and diagnostics
|
||||
|
||||
1. EXPORT and EXTERNAL declarations and their bodies must reside in the **same section**.
|
||||
|
||||
2. Duplicate EXPORT declarations or duplicate bodies for the same EXPORT name are rejected at transpile time.
|
||||
|
||||
3. EXPORT declaration and body signatures must match exactly.
|
||||
|
||||
4. Calling an EXTERNAL routine with a PROTEL string literal that lacks an explicit `0` suffix produces a **warning** (C interop requires NUL-terminated buffers for `const char*` parameters).
|
||||
|
||||
---
|
||||
|
||||
## See also
|
||||
|
||||
- §12.0 Intrinsic Procedures (machine-level **INTRINSIC**; not used for ordinary I/O in PROTEL 2026)
|
||||
- Introductory Manual §7.2 — EMACS PROTEL mode
|
||||
- Introductory Manual §7.3 — Foreign language interoperability (how-to)
|
||||
- Introductory Manual §5.5.2 — **IS ENTRY**, UNIX `argc`/`argv`, and shell exit status (`docs/intro-5-5-2-entry-args-exit.md`)
|
||||
@@ -0,0 +1,126 @@
|
||||
# PROTEL 2026 Error Messages
|
||||
|
||||
**PROTEL Reference Manual 2026** — Vincente D'Ingianni
|
||||
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
The `protel` driver reports errors in three phases: **parse**, **type check**, and **compile**. Error examples live under `examples/errors/`.
|
||||
|
||||
```bash
|
||||
./protel examples/errors/err_assign_integer_to_char.protel
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 1. Parse errors
|
||||
|
||||
**Prefix:** `protel: parse error in '<file>':`
|
||||
|
||||
The Lark parser rejects invalid syntax before transpilation.
|
||||
|
||||
| Message pattern | Typical cause |
|
||||
|-----------------|---------------|
|
||||
| `Unexpected token` | Misspelled keyword, missing `;`, `ENDIF`, or `ENDBLOCK` |
|
||||
| `Unexpected input` | Tokens where a declaration or statement is required |
|
||||
| `No terminal matches` | Unterminated string or malformed tuple |
|
||||
|
||||
**Example**
|
||||
|
||||
```protel
|
||||
DCL demo PROC() IS
|
||||
BLOCK
|
||||
IF x > 0 THEN
|
||||
x + 1 -> x;
|
||||
% missing ENDIF
|
||||
ENDBLOCK;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 2. Type errors (strong typing)
|
||||
|
||||
**Prefix:** `protel: type error in '<file>':`
|
||||
|
||||
PROTEL 2026 enforces Reference Manual §11.3 compatibility at transpile time. Wide **integer** values cannot be stored in **char**, **PACK** byte fields, or **BOOL** without an explicit **CAST**.
|
||||
|
||||
| Message | Meaning | Example file |
|
||||
|---------|---------|--------------|
|
||||
| `cannot assign integer to char in …` | A variable or expression typed **integer** is assigned to **char {0 TO 255}** | `examples/errors/err_assign_integer_to_char.protel` |
|
||||
| `cannot assign wide integer to packed byte-sized type {…} PACK(n) in …` | **integer** assigned to a **PACK** subrange | `examples/errors/err_assign_integer_to_packed_byte.protel` |
|
||||
| `cannot assign integer to BOOL in …` | Numeric value assigned to **BOOL** | `examples/errors/err_assign_integer_to_bool.protel` |
|
||||
| `value L..H is out of range for {A TO B} in …` | Numeric literal exceeds destination subrange | `examples/errors/err_constant_out_of_range.protel` |
|
||||
| `cannot assign PTR TO T1 to PTR TO T2 in …` | Pointer pointee types differ | `examples/errors/err_assign_incompatible_pointer.protel` |
|
||||
| `RETURN type T1 is not compatible with RETURNS T2 in procedure 'P'` | **RETURN** expression does not match **RETURNS** type | `examples/errors/err_return_type_mismatch.protel` |
|
||||
| `procedure 'P' cannot RETURN a value` | **RETURN** with value in a `PROC()` without **RETURNS** | — |
|
||||
| `procedure 'P' must RETURN T` | Missing **RETURN** in a **RETURNS** procedure | — |
|
||||
| `unknown identifier 'x' in …` | Assignment to an undeclared name | — |
|
||||
|
||||
**Valid contrast:** `examples/errors/ok_assign_char_literal.protel` assigns literal `65` to **char** (within `{0 TO 255}`).
|
||||
|
||||
### Strong typing rules (summary)
|
||||
|
||||
1. **integer** does not silently narrow to **char** or **PACK** types.
|
||||
2. Numeric literals must fit the destination subrange.
|
||||
3. **BOOL** accepts only boolean expressions.
|
||||
4. **PTR TO** assignments require compatible pointee types.
|
||||
5. **RETURN** must match the procedure **RETURNS** type.
|
||||
|
||||
---
|
||||
|
||||
## 3. Transpile errors
|
||||
|
||||
**Prefix:** `protel: transpile error in '<file>':`
|
||||
|
||||
Structural or linkage problems in the transpiler (not type compatibility).
|
||||
|
||||
| Message | Cause |
|
||||
|---------|-------|
|
||||
| `multiple ENTRY procedures (…)` | More than one **IS ENTRY** per module |
|
||||
| `ENTRY procedure 'P' has no procedure body` | **IS ENTRY** forward without matching body |
|
||||
| `ENTRY procedure 'P' parameters must match …` | **IS ENTRY** declaration differs from body |
|
||||
| `EXPORT procedure 'P' has no procedure body` | **EXPORT** without implementation |
|
||||
| `duplicate EXPORT declaration for 'P'` | Two **EXPORT** declarations for one name |
|
||||
| `compile mode requires C++ output (omit --emit-c)` | `--emit-c` with `--compile` |
|
||||
|
||||
---
|
||||
|
||||
## 4. Driver and I/O errors
|
||||
|
||||
| Message | Cause |
|
||||
|---------|-------|
|
||||
| `protel: cannot read '<file>': …` | Missing or unreadable source path |
|
||||
| `protel: cannot write '<file>': …` | Output path not writable |
|
||||
| `protel: '<file>' is not a PROTEL source file` | Unrecognized extension |
|
||||
| `protel: warning: only the first source file is compiled` | Multiple inputs on command line |
|
||||
|
||||
---
|
||||
|
||||
## 5. Compile and link errors
|
||||
|
||||
**Prefix:** `protel: compile error:`
|
||||
|
||||
Emitted when GCC/clang++ rejects generated C++. Often caused by invalid generated code or missing runtime symbols.
|
||||
|
||||
| Underlying message | Typical fix |
|
||||
|--------------------|-------------|
|
||||
| `undefined reference to 'writeln'` | Link without `protel_io.c` (use default `protel` compile, not raw `g++` on `.cpp` alone) |
|
||||
| `functions that differ only in their return type` | Duplicate ENTRY/forward signatures (report as transpile bug) |
|
||||
| `no C++ compiler found` | Install `g++`, `clang++`, or `c++` on `PATH` |
|
||||
|
||||
---
|
||||
|
||||
## 6. Runtime warnings
|
||||
|
||||
| Message | Cause |
|
||||
|---------|-------|
|
||||
| `protel: warning: call to P uses a PROTEL string without an explicit NUL (0) suffix` | **EXTERNAL** call with string missing `, 0` in char tuple |
|
||||
|
||||
---
|
||||
|
||||
## See also
|
||||
|
||||
- Reference Manual §11.0–§11.3 — type compatibility
|
||||
- `examples/errors/` — intentional error and OK contrast programs
|
||||
- Introductory Manual §7.0 — `protel` driver usage
|
||||
Reference in New Issue
Block a user