← Dolaima← Docs indexPascal原文中文

Pascal

Clearly structured; well suited to writing game state machines.

How to run it on your phone

vml run examples/pascal/test/sysinfo.pas

Compiling takes a while (one to two minutes for C, a few seconds for scripting languages). Once the program is running, the gamepad is at the bottom of the screen - the D-pad and the four action buttons are all there. Tap the back arrow in the top-right corner to return to the Shell.

What to know before you write

Examples

FileWhat it shows
test/catch.pasCatch the blocks (global arrays plus parameterless procedures; structurally the closest counterpart to the C version)
test/sysinfo.pasDevice information

Also in this folder: CREDITS.md, LICENSES.txt, bgi/demo_bgi.pas, demo/demo_std.pas, swag_graphics_0038.pas, test/libs.pas, tty/demo_tty.pas, ui/demo_ui.pas

Pitfalls we hit on real devices


The rest of this page is taken straight from the VML source (third_party/vml/VMLPrepares/PascalCompiler/): the README covers what this front end supports and how to compile it, and the language reference covers the syntax itself. When upstream changes, regenerating this page brings it up to date.

Language reference

Pascal language compiler specification

Version: v1.0 | Date: 2026-07-06 | Revised by: Shenzhen Tanso Intelligent Technology Co., Ltd.

Specification standard

FieldValue
Target standardTurbo Pascal 7.0 (default) + 5 dialects
CLI option--pascaltype turbo|delphi|freepascal|iso|ucsd|oberon
Release year1992 (Turbo Pascal)
Completeness~92%
MCU completeness~90%
Tests0 (the test directory is yet to be created)
Updates2026-05-18: resolved the contradictions in the break/continue/pointer status; updated the test count

Keywords

program unit uses var const type begin end procedure function if then else while do for to downto repeat until case of break continue exit goto label array record set file text string integer real char boolean true false nil and or not xor div mod in shl shr with asm absolute external forward inline interrupt

Overview

This compiler compiles the Pascal language (compatible with a Turbo Pascal 7.0 subset) into VML (Virtual Machine Language) assembly. It supports Pascal's basic syntax, control flow, data types, procedures and functions, and the Crt console unit.

Supported language features

1. Data types
Literals
Basic data types
Enumeration types
type
  TDir = (Up, Down, Left, Right);
  TColor = (Red, Green, Blue);

Enumeration values compile to integer constants (increasing from 0).

Derived types
2. Program structure
Program declaration
program HelloWorld;
uses Crt;  // use the Crt unit (parsed but ignored)
var
    x: integer;
begin
    x := 10;
    writeln('Hello, World!');
end.
Variable declarations
var
    counter: integer;
    pi: real;
    ch: char;
    flag: boolean;
    arr: array[1..10] of integer;
    r: record
        x, y: integer;
    end;
Constant declarations
const
    MAX_SIZE = 100;
    PI = 3.14159;
    MESSAGE = 'Hello';
Type definitions
type
    TColor = (Red, Green, Blue);
    TRange = 1..100;
    TPoint = record
        x, y: integer;
    end;
    TMatrix = array[1..10] of array[1..10] of integer;
3. Control structures
Conditionals
if x > 0 then
    writeln('Positive')
else if x = 0 then
    writeln('Zero')
else
    writeln('Negative');
Loop statements
// while loop
while i <= 10 do
begin
    writeln(i);
    i := i + 1;
end;

// for loop
for i := 1 to 10 do
    writeln(i);

for i := 10 downto 1 do
    writeln(i);

// repeat loop
repeat
    writeln(i);
    i := i - 1;
until i = 0;
case statement
case grade of
    'A': writeln('Excellent');
    'B', 'C': writeln('Pass');
    'D': writeln('Poor');
    'F': writeln('Fail');
otherwise
    writeln('Invalid');
end;
4. Procedures and functions
Procedure declaration
procedure PrintMessage(msg: string);
begin
    writeln(msg);
end;
Function declaration
function Add(a, b: integer): integer;
begin
    Add := a + b;  // the result is returned by assigning to the function name
end;
Parameter passing
forward declarations
procedure Foo(x: integer); forward;

procedure Bar;
begin
    Foo(10);
end;

procedure Foo(x: integer);
begin
    writeln(x);
end;
5. Operators
Arithmetic operators
Relational operators
Logical operators
Set operators
6. Input and output
Standard output
write('Hello');         // print without a newline
writeln('World');       // print and then a newline
writeln('Value: ', x);  // print several arguments
writeln(x:5);           // formatted output (width 5)
Standard input
readln(x);              // read an integer
readln(ch);             // read a character
readln(str);            // read a string
File operations
var
    f: text;
begin
    assign(f, 'data.txt');
    reset(f);           // open the file for reading
    rewrite(f);         // open the file for writing
    append(f);          // append to the file
    readln(f, x);       // read from the file
    writeln(f, x);      // write to the file
    close(f);           // close the file
end;
7. Standard library units
Crt unit
uses Crt;

begin
    ClrScr;             // clear the screen
    GotoXY(10, 5);      // move the cursor
    WhereX; WhereY;     // read the cursor position
    TextColor(Red);     // set the text colour
    TextBackground(Blue); // set the background colour
    Delay(1000);        // wait 1 second
    Sound(440);         // start a tone
    NoSound;            // stop the tone
    if KeyPressed then  // check for a keypress
        ch := ReadKey;  // read a key
    NormVideo;          // restore the default video attributes
    HighVideo; LowVideo; // high/low intensity
    InsLine; DelLine;   // insert/delete a line
    Window(x1, y1, x2, y2); // define a window
end;
System unit (built in)
8. Special syntax features
Compound statements
begin
    statement1;
    statement2;
end;
Comments
// single-line comment
{ multi-line comment }
(* another kind of multi-line comment *)
Labels and goto
label 100;
begin
    if x < 0 then goto 100;
    writeln('Positive');
    100: writeln('Done');
end;
9. VML code-generation conventions
Stack frame layout
caller: PUSH the arguments (left to right)
        CALL the function name

callee prologue:
    ENTER local_size     ; save R14, R12 -= local_size

stack layout (R14 = frame pointer):
    argument 1        <- R14+8
    argument 2        <- R14+12
    return address    <- R14+4  (implicit)
    saved R14         <- R14+0  (saved by the ENTER instruction)
    local variable 1  <- R14-4
    local variable 2  <- R14-8
    ...

callee epilogue:
    LEAVE               ; R12 = R14, POP R14
    RET                 ; return
Variable access
Function return values
10. Current implementation status
FeatureStatus
Program structure (program ...; ... end.)✅ implemented
Comments ({ }, (* *), //)✅ implemented
uses clause✅ implemented (parsed and skipped)
Data types
integer (32-bit signed)✅ implemented
real (floating point)✅ implemented
boolean✅ implemented
char✅ implemented
string✅ implemented
Enumeration types (A, B, C)✅ implemented
Arrays array[low..high] of type✅ implemented
Records record ... end✅ implemented
Sets set of type✅ implemented
Files file of type / text✅ implemented
Control flow
if/then/else✅ implemented
while/do✅ implemented
for to/downto do✅ implemented
repeat/until✅ implemented
case/of/otherwise/end✅ implemented
break / continue✅ implementedloop control, nested loops supported
Procedures and functions
procedure (value parameters)✅ implemented
function (return value)✅ implemented
var parameters (pass by reference)✅ implemented
forward declarations✅ implemented
Recursion✅ implemented
Operators
Arithmetic + - * / div mod✅ implemented
Relational = <> < <= > >=✅ implemented
Logical and or not✅ implemented
Set in + * -✅ implemented
Input and output
write, writeln✅ implemented
readln, read✅ implemented
Formatted output x:5✅ implemented
File I/O✅ implemented
Built-in functions
abs, sqr, chr, ord✅ implemented
pred, succ, odd✅ implemented
round, trunc✅ implemented
random, randomize✅ implemented
sqrt, sin, cos, exp, ln✅ implemented
length, concat, copy, pos✅ implemented
Crt unit
ClrScr, GotoXY, WhereX/Y✅ implemented
TextColor, TextBackground✅ implemented
Delay, Sound, NoSound✅ implemented
KeyPressed, ReadKey✅ implemented
Window, NormVideo, HighVideo, LowVideo✅ implemented
InsLine, DelLine, CursorOn, CursorOff✅ implemented
with statement⚠️ parsed onlyshorthand access to record fields
Multidimensional arrays✅ implementedmatrix[i][j] fully supported
Pointers✅ implemented^type declarations, new/dispose, p^ dereference
11. Example programs
Snake
program SnakeGame;
uses Crt;
const
  WIDTH = 40; HEIGHT = 20; MAX_SNAKE = 400;
type
  TDir = (Up, Down, Left, Right);
  TPoint = record x, y: integer; end;
var
  snake: array[1..MAX_SNAKE] of TPoint;
  len: integer; dir: TDir;
  food: TPoint; score: integer;
  gameover: boolean;
begin
  ClrScr;
  // ... the game loop
end.
A simple example
program Simple;
var
    i, sum: integer;
begin
    sum := 0;
    for i := 1 to 10 do
        sum := sum + i;
    writeln('Sum: ', sum);
end.
Array example
program ArrayTest;
var
    arr: array[1..5] of integer;
    i: integer;
begin
    for i := 1 to 5 do
        arr[i] := i * 10;
    for i := 1 to 5 do
        writeln('arr[', i, '] = ', arr[i]);
end.
Function example
program FunctionTest;
function Factorial(n: integer): integer;
begin
    if n <= 1 then Factorial := 1
    else Factorial := n * Factorial(n - 1);
end;
begin
    writeln('5! = ', Factorial(5));
end.
Floating point and 64-bit compile modes

The VML toolchain controls its strategy for floating point and 64-bit integers through three compile options:

OptionAllowed valuesDefaultDescription
--float32hard / soft / nonehardhow 32-bit floating point (real) is handled
--float64hard / soft / nonesofthow 64-bit floating point (double) is handled
--int64hard / soft / nonesofthow 64-bit integers (Int64) are handled
32-bit floating point (float32)

The real type in this language (32-bit single precision) compiles in the following modes:

64-bit floating point (double)

The double type in this language (64-bit double precision) compiles in the following modes:

64-bit integers (int64)

The Int64 / LongInt types in this language compile in the following modes:

Software emulation libraries

All of the software emulation libraries above live in Lib/shared/, are written in C and are compiled to VML by the C compiler, so every language shares them:

Library filePurposeCore functions
softfloat.cQ15.16 fixed-point emulation of 32-bit floating point__vml_float_add/sub/mul/div/neg/abs/cmp, __vml_int2float/float2int
softdouble.cIEEE 754 software emulation of 64-bit double precision__vml_double_add/sub/mul/div/neg/abs/cmp, __vml_int2double/double2int, __vml_float2double/double2float
softint64.cdouble-register emulation of 64-bit integers__vml_i64_add/sub/neg/and/or/xor/not/shl/shr
12. Compilation limits
  1. Multidimensional arrays: the array[1..3, 1..3] syntax is not supported; use array[1..3] of array[1..3] instead
  2. with statements: parsed only, no code is generated
  3. VML integers: all integers are 32-bit signed
  4. Array initialization: compile-time initialisation of array constants is not supported
  5. Strings: dynamic string operations are not supported (there is no heap allocation)
  6. Generics: features Turbo Pascal does not have are not supported
13. Integration with the VML runtime

Pascal programs interact with the VML runtime through the following mechanisms:

The standard library (stdlib.vml) provides the VML assembly implementations of the Crt unit, the maths functions, string operations and file I/O.


🆕 String encoding (v1.65.19)

This language compiler uses the VML string system indirectly through the shared libraries (Lib/shared/).

DirectiveWidthEncodingC type
.string8-bitUTF-8char*
.wstring16-bitUTF-16LEwchar_t*
.ustring32-bitUTF-32LEchar32_t*

MCU mode (default): strings are emitted as UTF-8 (.string) OS mode: the encoding can be tested with the VML_WSTRING macro

The shared libraries already provide wide-string conversion functions (wchar.h/uchar.h), which each language compiler can use as needed.

Compiler README

Pascal compiler

Path: VMLPrepares/PascalCompiler/ | Version: v1.66.33 Completeness: ~92% (Turbo Pascal) | 🟢 production ready | 6 dialects + OOP basics Standard library: Lib/pascal/ | Tests: 25

Multiple dialect support

The --pascaltype option selects one of 6 Pascal dialects:

DialectCLI valueOOPExamples
Turbo Pascal (default)turbo❌2 programs
Delphidelphi✅2 programs
Free Pascalfreepascal / fpc✅1 program
ISO Pascaliso❌1 program
UCSD Pascalucsd❌—
Oberonoberon❌—
vmltool -x pascal --pascaltype delphi test.pas -o test.vml

OOP features (Delphi/Free Pascal)

FeatureStatus
class/object✅ fields supported
constructor/destructor⚠️ token + parser
property⚠️ token only
virtual/override⚠️ AST ready
try/except/finally⚠️ token only

Features

Compile modes

MCU mode (default, --mode mcu)

MCU mode is tuned for microcontroller and bare-metal environments (Arduino/STM32/8051 and the like) and automatically skips features that need an operating system.

Skipped (no code is generated when these appear):

Kept (the low-level parts are provided by the BIOS):

OS mode (--mode os, reserved)

OS mode targets environments that have an operating system (embedded Linux, RTOS and so on). It will then support the full set of language features (file system, threads, async, exceptions, reflection and so on).

RAM level
MCU safe-coding tips

Usage

dotnet run --project VMLTool -- input.pas -o output.vml

Tests

Test/Pa/ — 0 test files (the test directory is yet to be created)