Pragma is a small language for numerical/HPC code that compiles to OpenMP C.
"C without pointers, with automatic parallelism": you write readable numerical
code, and pragc handles memory allocation and emits the OpenMP pragmas,
including reduction clauses. It also rejects parallel loops it can't prove
race-free.
Source files use the .pragma extension.
source.pragma → Lexer → Parser → TypeChecker → CodeGenerator → output.c
Requirements: CMake 3.20+, gcc with OpenMP, Catch2 v3 (system-wide; v2 won't work).
cmake -B build
cmake --build build
./build/bin/pragc examples/numerics.pragma --runTo run it as plain pragc from anywhere, install it onto your PATH:
cmake --install build --prefix ~/.local # just you: ~/.local/bin/pragc
sudo cmake --install build # everyone: /usr/local/bin/pragc~/.local/bin is on PATH by default on most Linux distros. If pragc isn't
found, add export PATH="$HOME/.local/bin:$PATH" to your ~/.bashrc.
Re-run the install after rebuilding to pick up changes. pragc writes
output.c (and output_bin with --run) into the directory you run it from.
fn square:int (x:int){
return x*x
}
fn sum_squares:int (n:int){
s = 0
parallel i in 0..n {
s += square(i)
}
return s
}
n = 10
out sum_squares(n)
This prints 285. The parallel loop becomes
#pragma omp parallel for reduction(+:s), and the last two lines, being outside
any function, become main.
- Functions:
fn name:return_type (param:type, ...){ ... return expr }. Several per file, callable in any order. - Variables: no declarations; the type comes from the first assignment (
s = 0makessanint). - Types:
int,float,double. Literals:1isint,1.5isfloat. - Arrays:
a = alloc(float, n), indexed witha[i]. Usea[i*n+j]for 2D. - Loops:
for i in 0..n { }runs sequentially;parallel i in 0..n { }runs in parallel with OpenMP. Ranges exclude the end value. - Assignment:
=,+=,-=,*=,/=. - Output:
out <expr>prints anintorfloat/double.
A parallel loop is accepted only when it's provably safe:
- A variable that already exists outside the loop may only be updated with
+=or*=; these become OpenMP reductions. - Array writes must be indexed by exactly the loop variable, e.g.
a[i] = .... - Arrays that are only read may use any index.
pragc <input.pragma> [--compile] [--run]- (no flags): writes
output.c --compile: also compiles it tooutput.o--run: buildsoutput_binand runs it. Needs code outside any function, which becomesmain.
A file with only functions has no main. It can still be compiled with
--compile and linked from C, as tests/runtime/ does.
examples/numerics.pragma: a full program. Midpoint-rule integration, compound growth (*=reduction), Fibonacci, 1D heat diffusion over time, and matrix multiply + trace. Output is identical with 1 and 8 threads.examples/kernels.pragma: eight small kernels (dot product, saxpy, stencil, prefix sum, ...), printed frommain.
ctest --test-dir build --output-on-failure| ctest name | What it covers |
|---|---|
Lexer |
tokenising source |
Parser |
building the syntax tree |
Typechecker |
type errors and parallel-safety rules |
Generator |
emitted C code |
Pragc |
the pragc command end to end, plus a timing benchmark |
Runtime |
generated code gives the right answer at 1 and 8 threads |
Run a single test with ctest --test-dir build -R Lexer, or a single Catch2
case with ./build/bin/TestParser "<test name>". Run test binaries from the repo
root, because they load input files from tests/lex_files/.
Known gaps, roughly in order of how often you'll hit them:
- No
if/elseyet. The keywords are reserved, but the parser doesn't handle them. - No comments:
#or//in a source file is a syntax error. - No parentheses in expressions: write
r*a - r*b, notr*(a - b). - No conversion between
intandfloat, e.g.i*hwithi:int,h:floatis a type error. - Functions can't take arrays as parameters.
- Identifiers can't contain digits (
x2lexes asxthen2). - A function with no
returncrashes the compiler. - Parse errors are printed but don't stop compilation.
- Arrays are never freed in the generated C.
MIT, see LICENSE.
