Skip to content

statpascal/spc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StatPascal

StatPascal is a Pascal based language (similar to pre-object Turbo Pascal) with vector extensions commonly used in statistical computing. It is designed as an embeddable system providing fast Pascal type scripting to host applications by generating X64 and ARM64 assembly code.

StatPascal is written in C++ and uses the Linux calling conventions on the above architectures so the generated code does not work under Windows oder MacOS.

As a toy project, a code generator for the TI99/4A (see next section) is in development.

A typical Statpascal implementation of the QuickSort algorithm looks like:

program qsortvec;

type 
    element = int32;
    vec = vector of element;

function qsort (a: vec): vec;
    begin
        if size (a) <= 1 then
            qsort := a
        else
           qsort := combine (
               qsort (a [a < a [1]]), 
               a [a = a [1]], 
               qsort (a [a > a [1]]))
    end;

const
    n = 20;
var 
    a: vec;

begin
    a := randomperm (n);
    writeln ('Unsorted: ', a);
    writeln ('Sorted:   ', qsort (a))
end.

A statistical example is the Hill estimator for the shape parameter of a heavy-tailed distribution:

program hillvec;

const 
    alpha = 2.0;
    nmax = 10000;

function hill (x: realvector): realvector;
    var 
        n: integer;
    begin
        x := log (rev (sort (x [x > 0])));
        n := size (x);
        hill := 1.0 / (cumsum (x) [intvec (1, n - 1)] / intvec (1, n-1)  - x [intvec (2, n)])
    end;

var 
    x, y: realvector;
begin
    x := power (randomdata (nmax), -1.0 / alpha);
    y := hill (x);
    writeln (y)
end.

The system is still in an early alpha state and there is not much documentation besides the examples in the test directories. The makefile produces the binary obj/sp which can load and execute programs by giving them as command line argument, e.g.

obj/sp tests/statpascal/qsortvec.sp

A collection of regression tests can be executed with

make tests

For standard Pascal programs the tests compare the output with code generated by Free Pascal while for vector extensions the expected output is provided as a text file. The test big.sp uses about 5 GB of RAM so it is likely to fail on smaller machines.

A larger test program (compatible with Free Pascal) is emul99 (an emulator of the TI99/4A home computer): https://github.com/statpascal/emul99

The next steps will be:

  • refactoring/unifying the code generators
  • samples/docs for data exchange/callbacks with host applications

Code generation for the TI99/4A

This is work in progress.

The compiler can produce an (optionally bank switched) ROM cartridge or an EA5 image for the TI99/4A. The upper memory (24 KB) is used for global variables and stack frames (and also code starting at A000 for EA5), the lower memory (8 KB) as heap for dynamic allocations.

Using the bank switching features of the FinalGROM 99, code sizes of up to 1 MB are possible.

There is no floating point yet and the runtime library is rather limited. Dynamic memory management currently uses a mark/release style. Input is limited to a single variable in a readln call.

To enable the TI99 mode, execute the makefile with the option ti99=1:

make clean
make ti99=1

This will set the default unit search path of the compiler to the ti99units directory. The system.pas in this directory (which is included by default) contains the runtime library. The tests/ti99 directory shows what is already working.

Compiling a program in this mode produces the assembler source out.a99, which can be assembled with xas99, e.g.

user@host:~/src/statpascal> obj/sp tests/ti99/sieve.pas 
user@host:~/src/statpascal> ~/ti99/xdt99/xas99.py -R -b -q out.a99 -o cart.bin
user@host:~/src/statpascal> cat cart_b*.bin >cart.bin

For small programs, the bank switching overhead can be avoided by either producing a single bank cartridge (option --cart) or an EA5 image (option --ea5); yielding a performance increase of about 15% for typical programs.

The scripts runcart.sh, runbank.sh and runea5.sh in the scripts directory show the invocation of the compiler, xas99 assembler and start the emul99 emulator.

The following steps are planned:

  • a standard runtime library
  • floating point operations (probably IEEE-754 binary32)
  • internal assembler

License

StatPascal is distributed under the MIT license.

Copyright (C) 2025 Michael Thomas

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

StatPascal Compiler

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published