Initial PROTEL 2026 toolchain import

This commit is contained in:
2026-06-08 18:04:03 -05:00
commit 98a0bb329d
125 changed files with 12223 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
#!/usr/bin/env protel-run
% Hello World - PROTEL 2026 (Introductory Manual 5.5.2 / 7.0)
% Run from repo root: export PATH="$PWD:$PATH" && ./examples/Hello.protel
% EXTERNAL C I/O via minimal runtime; no INTRINSIC, no iostream.
% PROTEL strings are char tuples; append 0 explicitly for C calls.
SECTION Hello;
TYPE char {0 TO 255};
DCL writeln PROC(msg PTR TO char) EXTERNAL;
DCL Start PROC() IS ENTRY;
DCL Start PROC() IS
BLOCK
writeln({'Hello, World!', 0});
ENDBLOCK;
+16
View File
@@ -0,0 +1,16 @@
% Hello World - legacy .P extension (Introductory Manual 5.5.2 / 7.0 / 7.2)
% Same program as Hello.protel; .P is the historical Nortel/BNR suffix.
% EXTERNAL C I/O via minimal runtime; no INTRINSIC, no iostream.
SECTION Hello;
TYPE char {0 TO 255};
DCL writeln PROC(msg PTR TO char) EXTERNAL;
DCL Start PROC() IS ENTRY;
DCL Start PROC() IS
BLOCK
writeln({'Hello, World!', 0});
ENDBLOCK;
+16
View File
@@ -0,0 +1,16 @@
% Hello World - PLS edition suffix .aa01 (Introductory Manual 7.0 / 7.2)
% Legacy PLS naming: two letters + two digits (.AA01 through .ZZ99).
% Same program as Hello.protel; edition AA01 is illustrative only.
SECTION Hello;
TYPE char {0 TO 255};
DCL writeln PROC(msg PTR TO char) EXTERNAL;
DCL Start PROC() IS ENTRY;
DCL Start PROC() IS
BLOCK
writeln({'Hello, World!', 0});
ENDBLOCK;
+18
View File
@@ -0,0 +1,18 @@
#!/usr/bin/env protel-run
% Hello World - PROTEL 2026 (Introductory Manual 5.5.2 / 7.0)
% Run from repo root: export PATH="$PWD:$PATH" && ./examples/Hello.protel
% EXTERNAL C I/O via minimal runtime; no INTRINSIC, no iostream.
% PROTEL strings are char tuples; append 0 explicitly for C calls.
SECTION Hello;
TYPE char {0 TO 255};
DCL writeln PROC(msg PTR TO char) EXTERNAL;
DCL Start PROC() IS ENTRY;
DCL Start PROC() IS
BLOCK
writeln({'Hello, World!', 0});
ENDBLOCK;
@@ -0,0 +1,16 @@
% ERROR EXAMPLE — incompatible pointer assignment (§11.3 rule 5)
% Expected: protel: type error: cannot assign PTR TO ...
SECTION err_ptr_mismatch;
TYPE integer {-32768 TO 32767};
TYPE char {0 TO 255};
DCL demo PROC() IS
BLOCK
DCL i integer;
DCL ch char;
DCL ip PTR TO integer INIT NIL;
DCL cp PTR TO char INIT NIL;
cp -> ip;
ENDBLOCK;
@@ -0,0 +1,13 @@
% ERROR EXAMPLE — integer assigned to BOOL (§11.3)
% Expected: protel: type error: cannot assign ... to BOOL
SECTION err_int_to_bool;
TYPE integer {-32768 TO 32767};
DCL demo PROC() IS
BLOCK
DCL flag BOOL;
DCL n integer INIT 1;
n -> flag;
ENDBLOCK;
@@ -0,0 +1,14 @@
% ERROR EXAMPLE — integer assigned to char (Reference Manual §11.3)
% Expected: protel: type error: cannot assign integer to char
SECTION err_int_to_char;
TYPE char {0 TO 255};
TYPE integer {-32768 TO 32767};
DCL demo PROC() IS
BLOCK
DCL ch char;
DCL n integer INIT 65;
n -> ch;
ENDBLOCK;
@@ -0,0 +1,14 @@
% ERROR EXAMPLE — integer assigned to PACK byte (§7.2.3 / §11.3)
% Expected: protel: type error: cannot assign wide integer to packed byte-sized type
SECTION err_int_to_packed;
TYPE integer {-32768 TO 32767};
TYPE nibble PACK (4) {0 TO 15};
DCL demo PROC() IS
BLOCK
DCL slot nibble;
DCL value integer INIT 9;
value -> slot;
ENDBLOCK;
@@ -0,0 +1,12 @@
% ERROR EXAMPLE — numeric literal out of subrange (§11.3)
% Expected: protel: type error: value ... out of range for {0 TO 255}
SECTION err_out_of_range;
TYPE char {0 TO 255};
DCL demo PROC() IS
BLOCK
DCL ch char;
300 -> ch;
ENDBLOCK;
@@ -0,0 +1,12 @@
% ERROR EXAMPLE — RETURN type incompatible with RETURNS clause (§9.2 / §11.3)
% Expected: protel: type error: RETURN type ... is not compatible with RETURNS BOOL
SECTION err_return_mismatch;
TYPE integer {-32768 TO 32767};
DCL demo PROC() RETURNS BOOL IS
BLOCK
DCL n integer INIT 0;
RETURN n;
ENDBLOCK;
@@ -0,0 +1,11 @@
% OK EXAMPLE — char literal within range (contrast with err_* examples)
SECTION ok_char_literal;
TYPE char {0 TO 255};
DCL demo PROC() IS
BLOCK
DCL ch char;
65 -> ch;
ENDBLOCK;
+10
View File
@@ -0,0 +1,10 @@
/** Java helper invoked from PROTEL via the java_greet JNI embedding shim. */
public final class GreetHelper {
private GreetHelper() {
}
public static void greet(String name, short value) {
System.out.println("Java: java_greet(name=" + name + ", value=" + value + ")");
System.out.flush();
}
}
+13
View File
@@ -0,0 +1,13 @@
/** Java program calling an EXPORTed PROTEL procedure via JNI (C ABI bridge). */
public final class JavaCallsProtel {
static {
System.loadLibrary("javacallsprotel");
}
private static native short protelAdd(short a, short b);
public static void main(String[] args) {
short sum = protelAdd((short) 40, (short) 2);
System.out.println("Java: protel_add(40, 2) = " + sum);
}
}
+37
View File
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
# Build Swift <-> PROTEL interop demos (macOS / Linux with Swift installed).
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
BUILD="$ROOT/build/interop"
PROTEL="$ROOT/protel"
RUNTIME="$ROOT/src/runtime/protel_io.c"
mkdir -p "$BUILD"
if ! command -v swiftc >/dev/null 2>&1; then
echo "interop: swiftc not found; install Swift toolchain to build runnable demos" >&2
exit 1
fi
CXX="$(command -v clang++ || command -v g++)"
echo "=== PROTEL calls Swift ==="
"$PROTEL" "$ROOT/examples/interop/protel_calls_swift.protel" \
--keep -o "$BUILD/protel_calls_swift.cpp"
"$CXX" -std=c++20 -Wall -I"$ROOT/src/runtime" -c "$BUILD/protel_calls_swift.cpp" \
-o "$BUILD/protel_calls_swift.o"
swiftc -parse-as-library -c "$ROOT/examples/interop/swift_greet.swift" \
-o "$BUILD/swift_greet.o"
"$CXX" "$BUILD/protel_calls_swift.o" "$BUILD/swift_greet.o" "$RUNTIME" \
-o "$BUILD/protel_calls_swift"
"$BUILD/protel_calls_swift"
echo "=== Swift calls PROTEL ==="
"$PROTEL" "$ROOT/examples/interop/protel_for_swift.protel" \
--keep -o "$BUILD/protel_for_swift.cpp"
"$CXX" -std=c++20 -Wall -I"$ROOT/src/runtime" -c "$BUILD/protel_for_swift.cpp" \
-o "$BUILD/protel_for_swift.o"
swiftc "$ROOT/examples/interop/swift_calls_protel.swift" "$BUILD/protel_for_swift.o" \
-o "$BUILD/swift_calls_protel"
"$BUILD/swift_calls_protel"
+81
View File
@@ -0,0 +1,81 @@
#!/usr/bin/env bash
# Build Java <-> PROTEL interop demos (macOS / Linux with JDK headers).
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
BUILD="$ROOT/build/interop"
PROTEL="$ROOT/protel"
RUNTIME="$ROOT/src/runtime/protel_io.c"
INTEROP="$ROOT/examples/interop"
mkdir -p "$BUILD"
if ! command -v javac >/dev/null 2>&1 || ! command -v java >/dev/null 2>&1; then
echo "interop: javac/java not found; install a JDK" >&2
exit 1
fi
if ! java -version >/dev/null 2>&1; then
echo "interop: java runtime not available; set JAVA_HOME to a JDK" >&2
exit 1
fi
if [ -z "${JAVA_HOME:-}" ]; then
if [ "$(uname -s)" = "Darwin" ] && command -v /usr/libexec/java_home >/dev/null 2>&1; then
JAVA_HOME="$(/usr/libexec/java_home 2>/dev/null || true)"
fi
fi
if [ -z "${JAVA_HOME:-}" ] || [ ! -d "$JAVA_HOME/include" ]; then
echo "interop: JAVA_HOME must point to a JDK with JNI headers" >&2
exit 1
fi
JNI_OS="linux"
if [ "$(uname -s)" = "Darwin" ]; then
JNI_OS="darwin"
fi
JNI_CFLAGS="-I$JAVA_HOME/include -I$JAVA_HOME/include/$JNI_OS"
if [ -d "$JAVA_HOME/lib/server" ]; then
JVM_LIBDIR="$JAVA_HOME/lib/server"
else
JVM_LIBDIR="$JAVA_HOME/jre/lib/server"
fi
JNI_LDFLAGS="-L$JVM_LIBDIR -ljvm -Wl,-rpath,$JVM_LIBDIR"
CXX="$(command -v clang++ || command -v g++)"
CC="$(command -v clang || command -v gcc)"
if [ "$(uname -s)" = "Darwin" ]; then
SHLIB_EXT="dylib"
SHLIB_FLAGS="-dynamiclib"
else
SHLIB_EXT="so"
SHLIB_FLAGS="-shared -fPIC"
fi
javac -d "$BUILD" "$INTEROP/GreetHelper.java" "$INTEROP/JavaCallsProtel.java"
echo "=== PROTEL calls Java ==="
"$PROTEL" "$INTEROP/protel_calls_java.protel" \
--keep -o "$BUILD/protel_calls_java.cpp"
"$CXX" -std=c++20 -Wall -I"$ROOT/src/runtime" -c "$BUILD/protel_calls_java.cpp" \
-o "$BUILD/protel_calls_java.o"
# shellcheck disable=SC2086
"$CC" -Wall -c "$INTEROP/java_greet.c" -o "$BUILD/java_greet.o" $JNI_CFLAGS
# shellcheck disable=SC2086
"$CXX" "$BUILD/protel_calls_java.o" "$BUILD/java_greet.o" "$RUNTIME" \
$JNI_LDFLAGS -o "$BUILD/protel_calls_java"
PROTEL_ROOT="$ROOT" "$BUILD/protel_calls_java"
echo "=== Java calls PROTEL ==="
"$PROTEL" "$INTEROP/protel_for_java.protel" \
--keep -o "$BUILD/protel_for_java.cpp"
"$CXX" -std=c++20 -Wall -fPIC -I"$ROOT/src/runtime" -c "$BUILD/protel_for_java.cpp" \
-o "$BUILD/protel_for_java.o"
# shellcheck disable=SC2086
"$CC" -Wall -fPIC -c "$INTEROP/java_calls_protel.c" -o "$BUILD/java_calls_protel.o" $JNI_CFLAGS
"$CXX" $SHLIB_FLAGS -std=c++20 "$BUILD/protel_for_java.o" "$BUILD/java_calls_protel.o" \
-o "$BUILD/libjavacallsprotel.$SHLIB_EXT"
java -Djava.library.path="$BUILD" -cp "$BUILD" JavaCallsProtel
+73
View File
@@ -0,0 +1,73 @@
#!/usr/bin/env bash
# Build Python <-> PROTEL interop demos (macOS / Linux with Python 3 dev headers).
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
BUILD="$ROOT/build/interop"
PROTEL="$ROOT/protel"
RUNTIME="$ROOT/src/runtime/protel_io.c"
INTEROP="$ROOT/examples/interop"
mkdir -p "$BUILD"
if ! command -v python3 >/dev/null 2>&1; then
echo "interop: python3 not found" >&2
exit 1
fi
PYTHON="${PYTHON:-python3}"
if ! "$PYTHON" -c "import sysconfig; sysconfig.get_config_var('LIBDIR')" >/dev/null 2>&1; then
echo "interop: Python development headers not available (install python3-dev)" >&2
exit 1
fi
PY_CONFIG=""
if command -v python3-config >/dev/null 2>&1; then
PY_CONFIG="python3-config"
elif command -v "$PYTHON-config" >/dev/null 2>&1; then
PY_CONFIG="$PYTHON-config"
fi
if [ -n "$PY_CONFIG" ]; then
PY_CFLAGS="$("$PY_CONFIG" --includes)"
if "$PY_CONFIG" --embed --ldflags >/dev/null 2>&1; then
PY_LDFLAGS="$("$PY_CONFIG" --embed --ldflags)"
else
PY_LDFLAGS="$("$PY_CONFIG" --ldflags)"
fi
else
PY_CFLAGS=$("$PYTHON" -c "import sysconfig; print('-I' + sysconfig.get_config_var('INCLUDEPY'))")
PY_LDFLAGS=$("$PYTHON" -c "import sysconfig; print('-L' + sysconfig.get_config_var('LIBDIR') + ' -lpython' + sysconfig.get_config_var('VERSION') + ' ' + (sysconfig.get_config_var('LIBS') or ''))")
fi
CXX="$(command -v clang++ || command -v g++)"
CC="$(command -v clang || command -v gcc)"
if [ "$(uname -s)" = "Darwin" ]; then
SHLIB_EXT="dylib"
SHLIB_FLAGS="-dynamiclib -Wl,-undefined,dynamic_lookup"
else
SHLIB_EXT="so"
SHLIB_FLAGS="-shared -fPIC"
fi
echo "=== PROTEL calls Python ==="
"$PROTEL" "$INTEROP/protel_calls_python.protel" \
--keep -o "$BUILD/protel_calls_python.cpp"
"$CXX" -std=c++20 -Wall -I"$ROOT/src/runtime" -c "$BUILD/protel_calls_python.cpp" \
-o "$BUILD/protel_calls_python.o"
# shellcheck disable=SC2086
"$CC" -Wall -c "$INTEROP/python_greet.c" -o "$BUILD/python_greet.o" $PY_CFLAGS
# shellcheck disable=SC2086
"$CXX" "$BUILD/protel_calls_python.o" "$BUILD/python_greet.o" "$RUNTIME" \
$PY_LDFLAGS -o "$BUILD/protel_calls_python"
(cd "$ROOT" && "$BUILD/protel_calls_python")
echo "=== Python calls PROTEL ==="
"$PROTEL" "$INTEROP/protel_for_python.protel" \
--keep -o "$BUILD/protel_for_python.cpp"
"$CXX" -std=c++20 -Wall -fPIC -I"$ROOT/src/runtime" -c "$BUILD/protel_for_python.cpp" \
-o "$BUILD/protel_for_python.o"
"$CXX" $SHLIB_FLAGS -std=c++20 "$BUILD/protel_for_python.o" \
-o "$BUILD/libprotel_for_python.$SHLIB_EXT"
"$PYTHON" "$INTEROP/python_calls_protel.py"
+37
View File
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
# Build Rust <-> PROTEL interop demos (macOS / Linux with Rust installed).
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
BUILD="$ROOT/build/interop"
PROTEL="$ROOT/protel"
RUNTIME="$ROOT/src/runtime/protel_io.c"
mkdir -p "$BUILD"
if ! command -v rustc >/dev/null 2>&1; then
echo "interop: rustc not found; install Rust (rustup) to build runnable demos" >&2
exit 1
fi
CXX="$(command -v clang++ || command -v g++)"
echo "=== PROTEL calls Rust ==="
"$PROTEL" "$ROOT/examples/interop/protel_calls_rust.protel" \
--keep -o "$BUILD/protel_calls_rust.cpp"
"$CXX" -std=c++20 -Wall -I"$ROOT/src/runtime" -c "$BUILD/protel_calls_rust.cpp" \
-o "$BUILD/protel_calls_rust.o"
rustc --crate-type staticlib "$ROOT/examples/interop/rust_greet.rs" \
-o "$BUILD/librust_greet.a"
"$CXX" "$BUILD/protel_calls_rust.o" "$BUILD/librust_greet.a" "$RUNTIME" \
-o "$BUILD/protel_calls_rust"
"$BUILD/protel_calls_rust"
echo "=== Rust calls PROTEL ==="
"$PROTEL" "$ROOT/examples/interop/protel_for_rust.protel" \
--keep -o "$BUILD/protel_for_rust.cpp"
"$CXX" -std=c++20 -Wall -I"$ROOT/src/runtime" -c "$BUILD/protel_for_rust.cpp" \
-o "$BUILD/protel_for_rust.o"
rustc "$ROOT/examples/interop/rust_calls_protel.rs" "$BUILD/protel_for_rust.o" \
-o "$BUILD/rust_calls_protel"
"$BUILD/rust_calls_protel"
+11
View File
@@ -0,0 +1,11 @@
/* C function called from PROTEL via EXTERNAL (examples/interop reference). */
#include <stdint.h>
#include <stdio.h>
void c_greet(const char* name, int16_t value)
{
if (name == NULL) {
name = "";
}
printf("C: c_greet(name=%s, value=%d)\n", name, (int)value);
}
+5
View File
@@ -0,0 +1,5 @@
"""Python helper invoked from PROTEL via the python_greet C embedding shim."""
def greet(name: str, value: int) -> None:
print(f"Python: python_greet(name={name}, value={value})", flush=True)
+13
View File
@@ -0,0 +1,13 @@
/* JNI bridge: JavaCallsProtel.protelAdd -> EXPORTed protel_add (C ABI). */
#include <jni.h>
#include <stdint.h>
extern int16_t protel_add(int16_t a, int16_t b);
JNIEXPORT jshort JNICALL
Java_JavaCallsProtel_protelAdd(JNIEnv *env, jclass clazz, jshort a, jshort b)
{
(void)env;
(void)clazz;
return (jshort)protel_add(a, b);
}
+88
View File
@@ -0,0 +1,88 @@
/* C ABI entry point: embeds the JVM and calls GreetHelper.greet (PROTEL EXTERNAL). */
#include <jni.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static JavaVM *jvm;
static int jvm_ready;
static JNIEnv *attach_env(void)
{
JNIEnv *env = NULL;
if (jvm == NULL) {
return NULL;
}
if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_10) == JNI_EDETACHED) {
(*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
}
return env;
}
static void ensure_jvm(void)
{
JavaVMInitArgs vm_args;
JavaVMOption options[1];
char classpath[1024];
const char *root;
JNIEnv *env;
if (jvm_ready) {
return;
}
root = getenv("PROTEL_ROOT");
if (root == NULL || root[0] == '\0') {
root = ".";
}
snprintf(classpath, sizeof(classpath), "-Djava.class.path=%s/examples/interop", root);
options[0].optionString = classpath;
vm_args.version = JNI_VERSION_10;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
if (JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args) != 0) {
fprintf(stderr, "java_greet: JNI_CreateJavaVM failed\n");
return;
}
jvm_ready = 1;
}
void java_greet(const char *name, int16_t value)
{
JNIEnv *env;
jclass cls;
jmethodID mid;
jstring jname;
ensure_jvm();
env = attach_env();
if (env == NULL) {
return;
}
cls = (*env)->FindClass(env, "GreetHelper");
if (cls == NULL) {
(*env)->ExceptionDescribe(env);
return;
}
mid = (*env)->GetStaticMethodID(env, cls, "greet", "(Ljava/lang/String;S)V");
if (mid == NULL) {
(*env)->ExceptionDescribe(env);
(*env)->DeleteLocalRef(env, cls);
return;
}
jname = (*env)->NewStringUTF(env, name != NULL ? name : "");
(*env)->CallStaticVoidMethod(env, cls, mid, jname, (jshort)value);
if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionDescribe(env);
}
(*env)->DeleteLocalRef(env, jname);
(*env)->DeleteLocalRef(env, cls);
}
+18
View File
@@ -0,0 +1,18 @@
% PROTEL calling Java (Introductory Manual 7.3)
% C shim java_greet embeds the JVM and calls GreetHelper.greet.
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;
@@ -0,0 +1,18 @@
% PROTEL calling Python (Introductory Manual 7.3)
% C shim python_greet embeds Python and calls greet_helper.greet.
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;
+18
View File
@@ -0,0 +1,18 @@
% PROTEL calling Rust (Introductory Manual 7.3)
% Rust exports rust_greet with #[no_mangle] extern "C"; PROTEL imports EXTERNAL.
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;
@@ -0,0 +1,18 @@
% PROTEL calling Swift (Introductory Manual 7.1 / C interop)
% Swift exports swift_greet with @_cdecl; PROTEL imports it as EXTERNAL.
SECTION protel_calls_swift;
TYPE char {0 TO 255};
TYPE integer {-32768 TO 32767};
DCL swift_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
swift_greet({'PROTEL', 0}, 42);
writeln({'PROTEL finished calling Swift', 0});
ENDBLOCK;
+13
View File
@@ -0,0 +1,13 @@
% PROTEL library exported for Java (EXPORT = extern "C" linkage)
% Java loads the shared object via JNI (java_calls_protel.c bridge).
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;
+13
View File
@@ -0,0 +1,13 @@
% PROTEL library exported for Python (EXPORT = extern "C" linkage)
% Python loads the shared object with ctypes.CDLL and binds protel_add.
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;
+13
View File
@@ -0,0 +1,13 @@
% PROTEL library exported for Rust (EXPORT = extern "C" linkage)
% Rust imports protel_add with extern "C" { fn protel_add ... }.
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;
+13
View File
@@ -0,0 +1,13 @@
% PROTEL library exported for Swift (EXPORT = extern "C" linkage)
% Swift imports protel_add with @_silgen_name.
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;
+35
View File
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
"""Python program calling an EXPORTed PROTEL procedure via ctypes (C ABI)."""
from __future__ import annotations
import ctypes
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
BUILD = ROOT / "build" / "interop"
if sys.platform == "darwin":
LIB_NAME = "libprotel_for_python.dylib"
elif sys.platform == "win32":
LIB_NAME = "protel_for_python.dll"
else:
LIB_NAME = "libprotel_for_python.so"
LIB_PATH = BUILD / LIB_NAME
def main() -> None:
if not LIB_PATH.is_file():
raise SystemExit(f"missing shared library: {LIB_PATH} (run build_interop_python.sh)")
lib = ctypes.CDLL(str(LIB_PATH))
lib.protel_add.argtypes = (ctypes.c_int16, ctypes.c_int16)
lib.protel_add.restype = ctypes.c_int16
total = lib.protel_add(40, 2)
print(f"Python: protel_add(40, 2) = {total}")
if __name__ == "__main__":
main()
+50
View File
@@ -0,0 +1,50 @@
/* C ABI entry point: embeds Python and calls greet_helper.greet (PROTEL EXTERNAL). */
#include <Python.h>
#include <stdint.h>
static int py_ready;
static void ensure_python(void)
{
if (py_ready) {
return;
}
Py_Initialize();
if (PyRun_SimpleString("import sys\n"
"sys.path.insert(0, 'examples/interop')\n") != 0) {
PyErr_Print();
}
py_ready = 1;
}
void python_greet(const char* name, int16_t value)
{
PyObject *mod, *func, *args, *result;
ensure_python();
mod = PyImport_ImportModule("greet_helper");
if (mod == NULL) {
PyErr_Print();
return;
}
func = PyObject_GetAttrString(mod, "greet");
if (func == NULL || !PyCallable_Check(func)) {
PyErr_Print();
Py_DECREF(mod);
return;
}
args = Py_BuildValue("(si)", name != NULL ? name : "", (int)value);
result = PyObject_CallObject(func, args);
if (result == NULL) {
PyErr_Print();
} else {
Py_DECREF(result);
}
Py_DECREF(args);
Py_DECREF(func);
Py_DECREF(mod);
}
+9
View File
@@ -0,0 +1,9 @@
// Rust program calling an EXPORTed PROTEL procedure (C linkage).
extern "C" {
fn protel_add(a: i16, b: i16) -> i16;
}
fn main() {
let sum = unsafe { protel_add(40, 2) };
println!("Rust: protel_add(40, 2) = {sum}");
}
+15
View File
@@ -0,0 +1,15 @@
// Rust function called from PROTEL via EXTERNAL / #[no_mangle] extern "C".
use std::ffi::CStr;
use std::os::raw::c_char;
#[no_mangle]
pub extern "C" fn rust_greet(name: *const c_char, value: i16) {
let label = unsafe {
if name.is_null() {
"<null>"
} else {
CStr::from_ptr(name).to_str().unwrap_or("<invalid utf-8>")
}
};
println!("Rust: rust_greet(name={label}, value={value})");
}
@@ -0,0 +1,8 @@
// Swift program calling an EXPORTed PROTEL procedure (C linkage).
import Foundation
@_silgen_name("protel_add")
func protel_add(_ a: Int16, _ b: Int16) -> Int16
let sum = protel_add(40, 2)
print("Swift: protel_add(40, 2) = \(sum)")
+8
View File
@@ -0,0 +1,8 @@
// Swift function called from PROTEL via EXTERNAL / @_cdecl("swift_greet").
import Foundation
@_cdecl("swift_greet")
public func swift_greet(_ name: UnsafePointer<CChar>, _ value: Int32) {
let label = String(cString: name)
print("Swift: swift_greet(name=\(label), value=\(value))")
}
+17
View File
@@ -0,0 +1,17 @@
% Introductory Manual 2026 - Figure 1 (Section 1.3)
% Calculates the factorial of a number n.
SECTION factorial_demo;
TYPE integer {-32768 TO 32767};
DCL factorial PROC(n integer) RETURNS integer IS
BLOCK
DCL result, i integer;
n -> i -> result;
WHILE (i - 1 -> i) > 1
DO
i * result -> result;
ENDDO;
RETURN result;
ENDBLOCK;
+28
View File
@@ -0,0 +1,28 @@
% Introductory Manual §1.3 + §5.5.2 + §7.3
% Command-line factorial via USES intro_1_3_factorial.protel (section factorial_demo).
% Demonstrates C interop: printf() and atoi() are EXTERNAL; no INTRINSIC I/O.
SECTION factorial_cli USES factorial_demo;
TYPE integer {-32768 TO 32767};
TYPE char {0 TO 255};
DCL printf PROC(fmt PTR TO char, n integer, fact integer) EXTERNAL;
DCL atoi PROC(str PTR TO char) RETURNS integer EXTERNAL;
DCL Start PROC(argc integer, argv PTR TO PTR TO char) IS ENTRY;
DCL Start PROC(argc integer, argv PTR TO PTR TO char) RETURNS integer IS
BLOCK
DCL n integer INIT 0;
DCL result integer INIT 0;
IF argc < 2
THEN
printf({'Usage: factorial N', 0}, 0, 0);
RETURN 1;
ENDIF;
atoi(argv[1]) -> n;
factorial(n) -> result;
printf({'%d! = %d', 0}, n, result);
RETURN 0;
ENDBLOCK;
+12
View File
@@ -0,0 +1,12 @@
% Introductory Manual 2026 - Section 2.1 (selected type declarations)
SECTION types_demo;
TYPE integer {-32768 TO 32767};
TYPE digit_register TABLE[0 TO 15] OF integer;
TYPE features_table DESC OF integer;
TYPE availability BOOL;
TYPE trunk_status {in_service, out_of_service};
DCL primes TABLE[1 TO 6] OF integer IS {2, 3, 5, 7, 11, 13};
DCL seconds_per_hour integer IS 60 * 60;
@@ -0,0 +1,12 @@
% Introductory Manual 2026 - Figure 4 (constant procedure declaration)
SECTION calculate_average_demo;
TYPE integer {-32768 TO 32767};
DCL calculate_average PROC(number_1, number_2 integer) RETURNS integer IS
BLOCK
DCL average integer;
(number_1 + number_2) / 2 -> average;
RETURN average;
ENDBLOCK;
+16
View File
@@ -0,0 +1,16 @@
% Introductory Manual 2026 - Section 3.2.4 (CASE statement)
SECTION case_demo;
TYPE digit {0 TO 9};
TYPE integer {-32768 TO 32767};
DCL route_digit PROC(n digit) IS
BLOCK
CASE n IN
{0}: TRUE -> operator;
{1}: TRUE -> ddd_call;
{4}: TRUE -> assist;
OUT
ENDCASE;
ENDBLOCK;
+17
View File
@@ -0,0 +1,17 @@
% Introductory Manual 2026 - Figure 13 (FOR iterative statement)
SECTION sum_table_demo;
TYPE integer {-32768 TO 32767};
DCL t TABLE[0 TO 9] OF integer;
DCL sum_t PROC() RETURNS integer IS
BLOCK
DCL sum integer INIT 0;
FOR i FROM 0 UP TO UPB t
DO
t[i] + sum -> sum;
ENDDO;
RETURN sum;
ENDBLOCK;
+21
View File
@@ -0,0 +1,21 @@
% Introductory Manual §5.5.2 — UNIX argc/argv and shell exit status
% The ENTRY procedure receives the host command line; RETURN becomes exit status.
SECTION ArgsExit;
TYPE char {0 TO 255};
TYPE integer {-32768 TO 32767};
DCL writeln PROC(msg PTR TO char) EXTERNAL;
DCL Start PROC(argc integer, argv PTR TO PTR TO char) IS ENTRY;
DCL Start PROC(argc integer, argv PTR TO PTR TO char) RETURNS integer IS
BLOCK
DCL i integer INIT 0;
FOR i FROM 0 UP TO argc - 1
DO
writeln(argv[i]);
ENDDO;
RETURN argc;
ENDBLOCK;
+18
View File
@@ -0,0 +1,18 @@
% Reference Manual §10.0 — BIND and WITH declarations
SECTION ref_bind_with;
TYPE integer {-32768 TO 32767};
TYPE point STRUCT
x integer,
y integer
ENDSTRUCT;
DCL demo PROC() IS
BLOCK
DCL p point;
BIND px TO p AS point;
WITH p;
1 -> px.x;
ENDBLOCK;
+11
View File
@@ -0,0 +1,11 @@
% Reference Manual §11.0–§11.3 — Type compatibility and operators
SECTION ref_type_compat;
TYPE integer {-32768 TO 32767};
TYPE small {0 TO 7};
DCL combine PROC(a integer, b small) RETURNS integer IS
BLOCK
RETURN a + b;
ENDBLOCK;
+11
View File
@@ -0,0 +1,11 @@
% Reference Manual §12.0 — Intrinsic procedures
SECTION ref_intrinsic;
TYPE integer {-32768 TO 32767};
DCL writeln PROC(msg PTR TO char) IS INTRINSIC;
DCL demo PROC() IS
BLOCK
ENDBLOCK;
+22
View File
@@ -0,0 +1,22 @@
% Reference Manual §13.0 — TYPEDESC, VARDESC, REFDESC; EXTERNAL/EXPORT
SECTION ref_descriptors;
TYPE integer {-32768 TO 32767};
TYPE char {0 TO 255};
DCL writeln PROC(msg PTR TO char) EXTERNAL;
DCL exported_add PROC(a integer, b integer) RETURNS integer EXPORT;
DCL exported_add PROC(a integer, b integer) RETURNS integer IS
BLOCK
RETURN a + b;
ENDBLOCK;
DCL Start PROC() IS ENTRY;
DCL Start PROC() IS
BLOCK
DCL td integer INIT 0;
TYPEDESC integer -> td;
ENDBLOCK;
@@ -0,0 +1,19 @@
% Reference Manual §14.0 — Appendix A keyword exercise (remaining reserved words)
SECTION ref_keywords;
TYPE integer {-32768 TO 32767};
TYPE char {0 TO 255};
TYPE hue SET integer;
DCL show PROC() RETURNS integer IS
BLOCK
DCL mask hue IS {1, 2};
DCL subset hue IS {2};
DCL merged hue;
IF subset NOTINCL mask THEN
RETURN 0;
ENDIF;
merged + (subset INCL {4}) -> merged;
RETURN TDSIZE mask;
ENDBLOCK;
+9
View File
@@ -0,0 +1,9 @@
% Reference Manual §1.0 — Introduction (minimal compilation unit)
SECTION ref_intro;
TYPE integer {-32768 TO 32767};
DCL noop PROC() IS
BLOCK
ENDBLOCK;
+11
View File
@@ -0,0 +1,11 @@
% Reference Manual §3.0–§3.4 — Language format, keywords, comments, symbols
SECTION ref_lexical;
TYPE integer {-32768 TO 32767};
DCL demo PROC() IS
BLOCK
DCL total integer INIT 0;
1 + 2 * 3 -> total;
ENDBLOCK;
+11
View File
@@ -0,0 +1,11 @@
% Reference Manual §4.0–§4.5 — Identifiers, numbers, constants, tuples, strings
SECTION ref_data;
TYPE integer {-32768 TO 32767};
TYPE char {0 TO 255};
DCL nums TABLE[1 TO 3] OF integer IS {1, 2, 3};
DCL label TABLE[1 TO 5] OF char IS {'h', 'e', 'l', 'l', 'o'};
DCL flag BOOL IS TRUE;
DCL empty_tuple TABLE[1 TO 0] OF integer IS {};
@@ -0,0 +1,15 @@
% Reference Manual §5.0 — Unit of compilation (USES / OF)
SECTION ref_provider;
TYPE integer {-32768 TO 32767};
DCL provider_value integer IS 42;
SECTION ref_consumer OF ref_provider USES ref_provider;
DCL consumer PROC() IS
BLOCK
DCL local integer INIT 0;
provider_value -> local;
ENDBLOCK;
+5
View File
@@ -0,0 +1,5 @@
$LI modentry restart
$EJECT
SECTION ref_directives;
TYPE integer {-32768 TO 32767};
DCL noop PROC() IS BLOCK ENDBLOCK;
+19
View File
@@ -0,0 +1,19 @@
% Reference Manual §6.0 — Module types (FAST, PERPROCESS, DEFINITIONS, INTERFACE)
FAST SECTION ref_fast;
TYPE fast_integer {-32768 TO 32767};
PERPROCESS SECTION ref_perprocess;
TYPE pp_integer {-32768 TO 32767};
DEFINITIONS SECTION ref_definitions;
TYPE def_integer {-32768 TO 32767};
INTERFACE ref_interface;
TYPE iface_integer {-1 TO 1};
DCL iface_proc PROC() IS BLOCK ENDBLOCK;
+8
View File
@@ -0,0 +1,8 @@
% Reference Manual §7.0 — Declarations overview
SECTION ref_declarations;
TYPE integer {-32768 TO 32767};
DCL counter integer INIT 0;
DCL increment PROC() IS BLOCK counter + 1 -> counter; ENDBLOCK;
@@ -0,0 +1,14 @@
% Reference Manual §7.1.1 — Simple types (range, symbolic, BOOL, PTR, defined)
SECTION ref_simple_types;
TYPE integer {-32768 TO 32767};
TYPE digit {0 TO 9};
TYPE status {idle, busy, error};
TYPE flag BOOL;
TYPE node STRUCT
value integer,
next PTR TO node
ENDSTRUCT;
DCL root PTR TO node;
+23
View File
@@ -0,0 +1,23 @@
% Reference Manual §7.1.2 — Aggregate types (SET, STRUCT, TABLE, DESC, AREA)
% Area options include UNRESTRICTED and NONTRANSPARENT.
SECTION ref_aggregates;
TYPE integer {-32768 TO 32767};
TYPE hue SET integer;
TYPE pair STRUCT
left integer,
right integer
ENDSTRUCT;
TYPE buffer TABLE[0 TO 7] OF integer;
TYPE view DESC OF integer;
TYPE father UNRESTRICTED AREA (48)
a integer,
b integer
ENDAREA;
DCL colors hue;
DCL buf buffer;
DCL hues hue IS {1, 2, 4};
DCL merged hue IS hues INCL {8};
+17
View File
@@ -0,0 +1,17 @@
% Reference Manual §7.1.3 — Procedure types (QUICK, OPERAND, FORWARD, UPDATES, REF)
SECTION ref_procedures;
TYPE integer {-32768 TO 32767};
DCL add_pair QUICK PROC(a integer, b integer) RETURNS integer IS FORWARD;
DCL add_pair QUICK PROC(a integer, b integer) RETURNS integer IS
BLOCK
RETURN a + b;
ENDBLOCK;
DCL scale OPERAND PROC(REF factor integer, UPDATES buf TABLE[0 TO 3] OF integer) IS
BLOCK
factor * buf[0] -> buf[0];
ENDBLOCK;
@@ -0,0 +1,11 @@
% Reference Manual §7.2 — Data declarations (INIT, PACK, VAL, LITERAL, VARIABLE)
SECTION ref_data_decl;
TYPE integer {-32768 TO 32767};
TYPE packed_digit PACK (4) {0 TO 15};
TYPE val_char VAL {0 TO 255};
DCL literal_max LITERAL integer INIT 100;
DCL counter VARIABLE integer INIT 0;
DCL packed_val packed_digit IS 7;
+15
View File
@@ -0,0 +1,15 @@
% Reference Manual §7.3 — Structure overlays (OVLY)
SECTION ref_overlays;
TYPE integer {-32768 TO 32767};
TYPE word_view STRUCT
low integer,
high integer
ENDSTRUCT;
DCL OVLY word_view
{0}: low integer,
{1}: high integer
ENDOVLY;
+14
View File
@@ -0,0 +1,14 @@
% Reference Manual §8.0–§8.2 — Scope rules and local blocks
SECTION ref_scope;
TYPE integer {-32768 TO 32767};
DCL outer PROC() IS
BLOCK
DCL outer_var integer INIT 1;
BLOCK
DCL inner_var integer INIT 2;
inner_var + outer_var -> inner_var;
ENDBLOCK
ENDBLOCK;
+11
View File
@@ -0,0 +1,11 @@
% Reference Manual §9.0 — Executable statements overview
SECTION ref_statements;
TYPE integer {-32768 TO 32767};
DCL run PROC() IS
BLOCK
DCL x integer INIT 0;
x + 1 -> x;
ENDBLOCK;
+22
View File
@@ -0,0 +1,22 @@
% Reference Manual §9.1 — Expressions (operators, NIL, FALSE, MOD, CAST, descriptors)
% Runtime descriptor types: $TYPEDESC $VARDESC $REFDESC $UNIVERSAL_PTR
SECTION ref_expressions;
TYPE integer {-32768 TO 32767};
TYPE universal PTR TO integer;
DCL demo PROC() RETURNS integer IS
BLOCK
DCL a integer INIT 10;
DCL b integer INIT 3;
DCL flag BOOL INIT FALSE;
DCL cell_ptr PTR TO integer INIT NIL;
DCL slot universal INIT NIL;
DCL td integer INIT 0;
TYPEDESC integer -> td;
VARDESC a -> td;
REFDESC cell_ptr -> td;
CAST a AS integer -> a;
RETURN a MOD b;
ENDBLOCK;
+10
View File
@@ -0,0 +1,10 @@
% Reference Manual §9.2 — RETURN statement
SECTION ref_return;
TYPE integer {-32768 TO 32767};
DCL status PROC(code integer) RETURNS integer IS
BLOCK
RETURN code;
ENDBLOCK;
+14
View File
@@ -0,0 +1,14 @@
% Reference Manual §9.3 — IF statement
SECTION ref_if;
TYPE integer {-32768 TO 32767};
DCL classify PROC(n integer) RETURNS integer IS
BLOCK
IF n > 0 THEN
RETURN 1;
ELSE
RETURN 0;
ENDIF;
ENDBLOCK;
+15
View File
@@ -0,0 +1,15 @@
% Reference Manual §9.4 — CASE statement
SECTION ref_case;
TYPE digit {0 TO 9};
TYPE integer {-32768 TO 32767};
DCL route PROC(n digit) IS
BLOCK
CASE n IN
{0}: TRUE -> operator;
{1}: TRUE -> local;
OUT
ENDCASE;
ENDBLOCK;
+14
View File
@@ -0,0 +1,14 @@
% Reference Manual §9.5 — SELECT statement
SECTION ref_select;
TYPE integer {-32768 TO 32767};
DCL pick PROC(n integer) RETURNS integer IS
BLOCK
SELECT n IN
{1}: RETURN 10;
{2}: RETURN 20;
OUT RETURN 0;
ENDSELECT;
ENDBLOCK;
+17
View File
@@ -0,0 +1,17 @@
% Reference Manual §9.6 — EXIT statement
SECTION ref_exit;
TYPE integer {-32768 TO 32767};
DCL search PROC(limit integer) RETURNS integer IS
BLOCK
DCL i integer INIT 0;
FOR i FROM 0 UP TO limit
DO
IF i = 3 THEN
EXIT;
ENDIF;
ENDDO;
RETURN i;
ENDBLOCK;
+26
View File
@@ -0,0 +1,26 @@
% Reference Manual §9.7 — Iterative statements (WHILE, FOR, OVER, DOWN, BY)
SECTION ref_iterative;
TYPE integer {-32768 TO 32767};
TYPE item TABLE[0 TO 3] OF integer;
DCL iterate PROC() RETURNS integer IS
BLOCK
DCL sum integer INIT 0;
DCL i integer INIT 0;
DCL j integer INIT 3;
WHILE i < 4
DO
i + 1 -> i;
ENDDO;
FOR j FROM 3 DOWN TO 0 BY 1
DO
sum + j -> sum;
ENDDO;
OVER item
DO
sum + 1 -> sum;
ENDDO;
RETURN sum;
ENDBLOCK;
+43
View File
@@ -0,0 +1,43 @@
% Reference Manual OO extension — CLASS, METHOD, CREATE, OVERRIDING, SUPER
INTERFACE ref_oo;
TYPE integer {-32768 TO 32767};
TYPE base_node
CLASS REFINES $OBJECT
{PROTECTED, SHARED}:
depth integer;
{PRIVATE, EXCLUSIVE}:
tag integer
OPERATIONS
{ABSTRACT}:
describe()
{CREATE, OVERRIDING, HIDDEN, FIXED}:
depth() RETURNS integer
{CLASS}:
factory() RETURNS PTR TO base_node
{}:
bump()
ENDCLASS base_node;
TYPE leaf_node
CLASS REFINES base_node
{PROTECTED}:
value integer
OPERATIONS
{OVERRIDING}:
describe(),
bump()
ENDCLASS leaf_node;
IN leaf_node
DCL describe METHOD IS
BLOCK
SUPER.bump();
ENDBLOCK;
DCL bump METHOD IS
BLOCK
1 + SELF.value -> SELF.value;
ENDBLOCK;
+14
View File
@@ -0,0 +1,14 @@
% Reference Manual 2026 - STRUCT type example (task_block)
SECTION struct_demo;
TYPE integer {-32768 TO 32767};
TYPE code_block integer;
TYPE task_block STRUCT
priority {0 TO 5},
status {ready, blocked, executing},
next_task PTR TO task_block,
completed BOOL,
code code_block
ENDSTRUCT;
@@ -0,0 +1,22 @@
% Class Methods and Variables (1994) - Example 4 (invocation)
SECTION oo_invoke_demo;
TYPE integer {-32768 TO 32767};
TYPE class_with_class_method
CLASS REFINES $OBJECT
{}:
instance_var integer
OPERATIONS
{CLASS}:
class_method(parameter integer)
ENDCLASS class_with_class_method;
DCL instance class_with_class_method;
DCL demo PROC() IS
BLOCK
class_with_class_method.class_method(5);
instance.class_method(6);
ENDBLOCK;
+41
View File
@@ -0,0 +1,41 @@
/* Generated by PROTEL 2026 C++ transpiler */
/* Source: /Users/vincente/PROTEL/examples/ref_oo_class_with_var.protel */
#include <cstdint>
#include <iostream>
#include "protel_io.hpp"
namespace protel_generated {
namespace oo_class_demo {
/* FAST INTERFACE */
/* TYPE integer */
using integer = int16_t;
/* TYPE class_with_var */
class class_with_var {
public:
static int16_t class_var;
int16_t instance_var;
static void class_method(int16_t parameter);
void instance_method(int16_t value);
};
/* IN class_with_var */
int16_t class_with_var::class_var = 0;
void class_with_var::class_method(int16_t parameter)
{
class_with_var::class_var = 5;
}
void class_with_var::instance_method(int16_t value)
{
this->instance_var = value;
}
} // namespace oo_class_demo
} // namespace protel_generated
+29
View File
@@ -0,0 +1,29 @@
% Class Methods and Variables (1994) - Examples 1-3 adapted for PROTEL 2026
INTERFACE oo_class_demo;
TYPE integer {-32768 TO 32767};
TYPE class_with_var
CLASS REFINES $OBJECT
{PROTECTED}:
class_var integer;
{}:
instance_var integer
OPERATIONS
{CLASS}:
class_method(parameter integer)
{}:
instance_method(value integer)
ENDCLASS class_with_var;
IN class_with_var
DCL class_method METHOD IS
BLOCK
5 -> SELF.class_var;
ENDBLOCK;
DCL instance_method METHOD IS
BLOCK
value -> SELF.instance_var;
ENDBLOCK;