xdpw

2025-12-10 0 332

XD Pascal for Windows

Dedicated to my father Mikhail Tereshkov, who instilled in me a taste for engineering

Summary

XD Pascal is a small embeddable self-hosting compiler for a Pascal language dialect. Any comments, suggestions, or bug reports are appreciated. Feel free to contact the author on GitHub or by e-mail VTereshkov@mail.ru. Enjoy.

Features

  • Go-style methods and interfaces
  • Native x86 code generation (32 bit Windows executables)
  • Support for both console and GUI applications
  • No external assembler or linker needed
  • Floating-point arithmetic using the x87 FPU
  • Integration with the Raylib game development library
  • Integration with Geany IDE
  • Compiler source for Delphi 6/7, Free Pascal and XD Pascal itself (Delphi 2009+ migration is straightforward)

Detailed description

Usage

Type in the command prompt:

xdpw <file.pas>

The source file should be specified with its extension (.pas).

Language

XD Pascal is similar to Delphi 6/7 and Free Pascal with the following changes:

Enhancements

  • The compiler is self-hosting
  • The compiler is extremely compact (~10000 lines) and can be easily embedded into larger systems
  • Go-style methods and interfaces are supported

Differences

  • Strings are null-terminated arrays of characters (C style), but indexed from 1 for Pascal compatibility
  • The Text type is equivalent to file. It can be used for both text and untyped files
  • Method calls and procedural variable calls require parentheses even for empty parameter lists

Limitations

  • No classical (C++/Delphi style) object-oriented programming
  • No visual components
  • Units cannot be compiled separately
  • Only peephole optimizations
  • Extended is equivalent to Double
  • No High and Low functions for open arrays. Open array length should be explicitly passed to a subroutine
  • Statement labels cannot be numerical

Formal grammar

ProgramOrUnit = [(\"program\" | \"unit\") Ident \";\"] 
                [\"interface\"] [UsesClause] Block \".\" .
                
UsesClause = \"uses\" Ident {\",\" Ident} \";\" .                

Block = { Declarations } (CompoundStatement | \"end\") .

Declarations = DeclarationSection [\"implementation\" DeclarationSection] .

DeclarationSection = LabelDeclarations |
                     ConstDeclarations | 
                     TypeDeclarations |
                     VarDeclarations |
                     ProcFuncDeclarations .
                     
Initializer = ConstExpression |
              StringLiteral |
              \"(\" Initializer {\",\" Initializer} \")\" |
              \"(\" Ident \":\" Initializer {\";\" Ident \":\" Initializer} \")\" |
              SetConstructor .                     
               
LabelDeclarations = \"label\" Ident {\",\" Ident} \";\"               
             
ConstDeclarations = (UntypedConstDeclaration | TypedConstDeclaration)
               {\";\" (UntypedConstDeclaration | TypedConstDeclaration)} .

UntypedConstDeclaration = \"const\" Ident \"=\" ConstExpression .
                                 
TypedConstDeclaration = \"const\" Ident \":\" Type \"=\" Initializer .

TypeDeclarations = \"type\" Ident \"=\" Type \";\" {Ident \"=\" Type \";\"} .

VarDeclarations = \"var\" IdentList \":\" Type [\"=\" Initializer] \";\" 
                       {IdentList \":\" Type [\"=\" Initializer] \";\"} .

ProcFuncDeclarations = (\"procedure\" | \"function\") Ident 
                       [Receiver] [FormalParams] [\":\" TypeIdent] 
                       [CallModifier] \";\" [(Directive | Block) \";\"] .

Receiver = \"for\" Ident \":\" TypeIdent .

CallModifier = \"stdcall\" | \"cdecl\" .

Directive = \"forward\" | \"external\" ConstExpression .         

ActualParams = \"(\" [ (Expression | Designator) |
                {\",\" (Expression | Designator)} ] \")\" .

FormalParams = \"(\" FormalParamList {\";\" FormalParamList} \")\" .
              
FormalParamList = [\"const\" | \"var\"] IdentList [\":\" [\"array\" \"of\"] TypeIdent] 
                                              [\"=\" ConstExpression] .             

IdentList = Ident {\",\" Ident} .

Type = \"(\" Ident {\",\" Ident} \")\" |
       \"^\" TypeIdent |
       [\"packed\"] \"array\" \"[\" Type {\",\" Type} \"]\" \"of\" Type |
       [\"packed\"] \"record\" Fields \"end\" |
       [\"packed\"] \"interface\" FixedFields \"end\" |
       [\"packed\"] \"set\" \"of\" Type |
       [\"packed\"] \"string\" [ \"[\" ConstExpression \"]\" ] |
       [\"packed\"] \"file\" [\"of\" Type] |
       ConstExpression \"..\" ConstExpression |
       (\"procedure\" | \"function\") [FormalParams] [\":\" TypeIdent] [CallModifier] |
       Ident .
       
Fields = FixedFields 
           [\"case\" [Ident \":\"] Type \"of\" 
               ConstExpression {\",\" ConstExpression} \":\" \"(\" Fields \")\"
          {\";\" ConstExpression {\",\" ConstExpression} \":\" \"(\" Fields \")\"}] [\";\"] .       
       
FixedFields = IdentList \":\" Type {\";\" IdentList \":\" Type} .       
       
TypeIdent = \"string\" | \"file\" | Ident .       

Designator = BasicDesignator {Selector} .

BasicDesignator = Ident |
                  Ident [ActualParams] |
                  Ident \"(\" Expression \")\" .

Selector = \"^\" | 
           \"[\" Expression {\",\" Expression} \"]\" | 
           \".\" Ident | 
           \"(\" ActualParams \")\".

Statement = [Label \":\"] [ (Designator | Ident) \":=\" Expression | 
                          (Designator | Ident) [ActualParams] {Selector} |
                          CompoundStatement |
                          IfStatement |
                          CaseStatement |
                          WhileStatement |
                          RepeatStatement | 
                          ForStatement |
                          GotoStatement |
                          WithStatement ] .
                          
Label = Ident .                          

StatementList = Statement {\";\" Statement} .

CompoundStatement = \"begin\" StatementList \"end\" .

IfStatement = \"if\" Expression \"then\" Statement [\"else\" Statement] .

CaseStatement = \"case\" Expression \"of\" CaseElement {\";\" CaseElement} 
                    [\";\"] [\"else\" StatementList] [\";\"] \"end\" .
                    
WhileStatement = \"while\" Expression \"do\" Statement .

RepeatStatement = \"repeat\" StatementList \"until\" Expression .

ForStatement = \"for\" Ident \":=\" Expression (\"to\" | \"downto\") Expression \"do\" Statement.

GotoStatement = \"goto\" Label .

WithStatement = \"with\" Designator {\",\" Designator} \"do\" Statement .                    
 
CaseElement = CaseLabel {\",\" CaseLabel} \":\" Statement .

CaseLabel = ConstExpression [\"..\" ConstExpression] .

ConstExpression = Expression .

Expression = SimpleExpression [(\"=\"|\"<>\"|\"<\"|\"<=\"|\">\"|\">=\"|\"in\") SimpleExpression] .

SimpleExpression = [\"+\"|\"-\"] Term {(\"+\"|\"-\"|\"or\"|\"xor\") Term}.

Term = Factor {(\"*\"|\"/\"|\"div\"|\"mod\"|\"shl\"|\"shr\"|\"and\") Factor}.

Factor = (Designator | Ident) [ActualParams] {Selector} |
         Designator |
         \"@\" Designator | 
         Number | 
         CharLiteral |
         StringLiteral |  
         \"(\" Expression \")\" | 
         \"not\" Factor |
         SetConstructor |
         \"nil\" |
         Ident \"(\" Expression \")\" {Selector} .
         
SetConstructor = \"[\" [Expression [\"..\" Expression] 
                     {\",\" Expression [\"..\" Expression]}] \"]\" .         

Ident = (Letter | \"_\") {Letter | \"_\" | Digit}.

Number = \"$\" HexDigit {HexDigit} | 
         Digit {Digit} [\".\" {Digit}] [\"e\" [\"+\" | \"-\"] Digit {Digit}] .

CharLiteral = \"\'\" (Character | \"\'\" \"\'\") \"\'\" | 
              \"#\" Number .

StringLiteral = \"\'\" {Character | \"\'\" \"\'\"} \"\'\".

Compiler

The compiler is based on a recursive descent parser. It directly builds a Windows PE executable without using any external assembler or linker.

Directives

  • $APPTYPE – Set application type. Examples: {$APPTYPE GUI}, {$APPTYPE CONSOLE}
  • $UNITPATH – Set additional unit search path. Example: {$UNITPATH ..\\units\\}

Optimizations

Some simple peephole optimizations are performed:

  • Push/pop elimination
  • FPU push/pop elimination
  • Local variable loading optimizations
  • Array element access optimizations
  • Record field access optimizations
  • Assignment optimizations
  • Comparison optimizations
  • Condition testing optimizations

Inlined procedures and functions

The following identifiers are implemented as part of the compiler. Their names are not reserved words and can be locally redefined by the user.

procedure Inc(var x: Integer);
procedure Dec(var x: Integer);
procedure Read([var F: file;] var x1 {; var xi});
procedure Write([var F: file;] x1[:w[:d]] {; xi[:w[:d]]});
procedure ReadLn([var F: file;] var x1 {; var xi});
procedure WriteLn([var F: file;] x1[:w[:d]] {; xi[:w[:d]]});
procedure New(var P: Pointer);
procedure Dispose(var P: Pointer);
procedure Break;
procedure Continue;
procedure Exit;
procedure Halt[(const error: Integer)];
function SizeOf(var x | T): Integer;
function Ord(x: T): Integer;
function Chr(x: Integer): Char;
function Low(var x: T | T): T;
function High(var x: T | T): T;
function Pred(x: T): T;
function Succ(x: T): T;
function Round(x: Real): Integer;
function Abs(x: T): T;
function Sqr(x: T): T;
function Sin(x: Real): Real;  
function Cos(x: Real): Real;  
function Arctan(x: Real): Real;  
function Exp(x: Real): Real;
function Ln(x: Real): Real;
function SqRt(x: Real): Real;

Standard library

System unit

function Timer: Integer;
procedure GetMem(var P: Pointer; Size: Integer);
procedure FreeMem(var P: Pointer);
procedure Randomize;
function Random: Real;
procedure Assign(var F: file; const Name: string);
procedure Rewrite(var F: file[; BlockSize: Integer]);
procedure Reset(var F: file[; BlockSize: Integer]);
procedure Close(var F: file);
procedure BlockRead(var F: file; var Buf; Len: Integer; var LenRead: Integer);
procedure BlockWrite(var F: file; var Buf; Len: Integer);
procedure Seek(var F: file; Pos: Integer);
function FileSize(var F: file): Integer;
function FilePos(var F: file): Integer;
function EOF(var F: file): Boolean;
function IOResult: Integer;
function Length(const s: string): Integer;
procedure Move(var Source; var Dest; Count: Integer);
function Copy(const S: string; Index, Count: Integer): string;
procedure FillChar(var Data; Count: Integer; Value: Char);
function ParamCount: Integer;
function ParamStr(Index: Integer): string;
procedure Val(const s: string; var Number: Real; var Code: Integer);
procedure Str(Number: Real; var s: string[; DecPlaces: Integer]);
procedure IVal(const s: string; var Number: Integer; var Code: Integer);
procedure IStr(Number: Integer; var s: string);
function UpCase(ch: Char): Char;

SysUtils unit

function IntToStr(n: Integer): string;
function StrToInt(const s: string): Integer;
function FloatToStr(x: Real): string;
function FloatToStrF(x: Real; Format: TFloatFormat; Precision, Digits: Integer): string;
function StrToFloat(const s: string): Real;
function StrToPChar(const s: string): PChar;
function PCharToStr(p: PChar): string;
function StrToPWideChar(const s: string): PWideChar;
function PWideCharToStr(p: PWideChar): string;

Samples

  • factor.pas – Integer factorization demo
  • lineq.pas – Linear equation solver. Uses gauss.pas unit. Requires eq.txt, eqerr.txt, or similar data file
  • life.pas – The Game of Life
  • sort.pas – Array sorting demo
  • fft.pas – Fast Fourier Transform demo
  • inserr.pas – Inertial navigation system error estimation demo. Uses kalman.pas unit
  • list.pas – Linked list operations demo
  • map.pas – Heterogenous list operations and Map function demo. Demonstrates XD Pascal methods and interfaces
  • gui.pas – GUI application demo. Uses windows.pas unit
  • raytracer.pas– Raytracer demo. Demonstrates XD Pascal methods and interfaces. Equivalent to raytracer.go

Known issues

Windows Defender antivirus is known to give false positive results on some programs compiled with XD Pascal.

下载源码

通过命令行克隆项目:

git clone https://github.com/vtereshkov/xdpw.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 编程相关 xdpw https://www.zuozi.net/33648.html

mirai
上一篇: mirai
quarkus
下一篇: quarkus
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务