{"id":18879308,"url":"https://github.com/vtereshkov/xdpw","last_synced_at":"2025-04-13T10:58:13.225Z","repository":{"id":43308840,"uuid":"201050372","full_name":"vtereshkov/xdpw","owner":"vtereshkov","description":"XD Pascal: A small embeddable self-hosting Pascal compiler for Windows. Supports Go-style methods and interfaces","archived":false,"fork":false,"pushed_at":"2020-03-31T12:22:41.000Z","size":5705,"stargazers_count":285,"open_issues_count":10,"forks_count":33,"subscribers_count":17,"default_branch":"master","last_synced_at":"2025-03-27T02:11:44.751Z","etag":null,"topics":["compiler","compiler-construction","compiler-design","delphi","free-pascal","game-development","go","golang","parser","pascal-compiler","pascal-language","programming-language","raylib","self-hosting","win32","winapi","windows","x86","x86-32","xd-pascal"],"latest_commit_sha":null,"homepage":"","language":"Pascal","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vtereshkov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-07T12:54:50.000Z","updated_at":"2025-02-22T02:50:34.000Z","dependencies_parsed_at":"2022-09-02T22:10:59.042Z","dependency_job_id":null,"html_url":"https://github.com/vtereshkov/xdpw","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vtereshkov%2Fxdpw","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vtereshkov%2Fxdpw/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vtereshkov%2Fxdpw/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vtereshkov%2Fxdpw/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vtereshkov","download_url":"https://codeload.github.com/vtereshkov/xdpw/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248703196,"owners_count":21148117,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compiler","compiler-construction","compiler-design","delphi","free-pascal","game-development","go","golang","parser","pascal-compiler","pascal-language","programming-language","raylib","self-hosting","win32","winapi","windows","x86","x86-32","xd-pascal"],"created_at":"2024-11-08T06:35:11.479Z","updated_at":"2025-04-13T10:58:13.207Z","avatar_url":"https://github.com/vtereshkov.png","language":"Pascal","readme":"\u003cimg src=\"logo.png\"\u003e\n\n# XD Pascal for Windows\n\n_Dedicated to my father Mikhail Tereshkov, who instilled in me a taste for engineering_ \n\n## Summary\nXD 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.\n\n### Features\n* [Go-style methods and interfaces](https://medium.com/@vtereshkov/how-i-implemented-go-style-interfaces-in-my-own-pascal-compiler-a0f8d37cd297?source=friends_link\u0026sk=72a20752cb866c716daac13abc1fab22)\n* Native x86 code generation (32 bit Windows executables)\n* Support for both console and GUI applications\n* No external assembler or linker needed\n* Floating-point arithmetic using the x87 FPU\n* [Integration](https://github.com/vtereshkov/raylib-xdpw) with the [Raylib](https://www.raylib.com) game development library\n* Integration with Geany IDE\n* Compiler source for Delphi 6/7, Free Pascal and XD Pascal itself (Delphi 2009+ migration is [straightforward](https://github.com/vtereshkov/xdpw/issues/2#issuecomment-573929657))\n\n![](maze.png)\n\n## Detailed description\n\n### Usage\nType in the command prompt:\n```\nxdpw \u003cfile.pas\u003e\n```\nThe source file should be specified with its extension (.pas).\n \n### Language\n\nXD Pascal is similar to Delphi 6/7 and Free Pascal with the following changes:\n\n#### Enhancements\n* The compiler is self-hosting\n* The compiler is extremely compact (~10000 lines) and can be easily embedded into larger systems\n* Go-style methods and interfaces are supported\n\n#### Differences\n* Strings are null-terminated arrays of characters (C style), but indexed from 1 for Pascal compatibility\n* The `Text` type is equivalent to `file`. It can be used for both text and untyped files\n* Method calls and procedural variable calls require parentheses even for empty parameter lists\n\n#### Limitations\n* No classical (C++/Delphi style) object-oriented programming\n* No visual components\n* Units cannot be compiled separately\n* Only peephole optimizations\n* `Extended` is equivalent to `Double`\n* No `High` and `Low` functions for open arrays. Open array length should be explicitly passed to a subroutine \n* Statement labels cannot be numerical \n\n#### Formal grammar\n```\nProgramOrUnit = [(\"program\" | \"unit\") Ident \";\"] \n                [\"interface\"] [UsesClause] Block \".\" .\n                \nUsesClause = \"uses\" Ident {\",\" Ident} \";\" .                \n\nBlock = { Declarations } (CompoundStatement | \"end\") .\n\nDeclarations = DeclarationSection [\"implementation\" DeclarationSection] .\n\nDeclarationSection = LabelDeclarations |\n                     ConstDeclarations | \n                     TypeDeclarations |\n                     VarDeclarations |\n                     ProcFuncDeclarations .\n                     \nInitializer = ConstExpression |\n              StringLiteral |\n              \"(\" Initializer {\",\" Initializer} \")\" |\n              \"(\" Ident \":\" Initializer {\";\" Ident \":\" Initializer} \")\" |\n              SetConstructor .                     \n               \nLabelDeclarations = \"label\" Ident {\",\" Ident} \";\"               \n             \nConstDeclarations = (UntypedConstDeclaration | TypedConstDeclaration)\n               {\";\" (UntypedConstDeclaration | TypedConstDeclaration)} .\n\nUntypedConstDeclaration = \"const\" Ident \"=\" ConstExpression .\n                                 \nTypedConstDeclaration = \"const\" Ident \":\" Type \"=\" Initializer .\n\nTypeDeclarations = \"type\" Ident \"=\" Type \";\" {Ident \"=\" Type \";\"} .\n\nVarDeclarations = \"var\" IdentList \":\" Type [\"=\" Initializer] \";\" \n                       {IdentList \":\" Type [\"=\" Initializer] \";\"} .\n\nProcFuncDeclarations = (\"procedure\" | \"function\") Ident \n                       [Receiver] [FormalParams] [\":\" TypeIdent] \n                       [CallModifier] \";\" [(Directive | Block) \";\"] .\n\nReceiver = \"for\" Ident \":\" TypeIdent .\n\nCallModifier = \"stdcall\" | \"cdecl\" .\n\nDirective = \"forward\" | \"external\" ConstExpression .         \n\nActualParams = \"(\" [ (Expression | Designator) |\n                {\",\" (Expression | Designator)} ] \")\" .\n\nFormalParams = \"(\" FormalParamList {\";\" FormalParamList} \")\" .\n              \nFormalParamList = [\"const\" | \"var\"] IdentList [\":\" [\"array\" \"of\"] TypeIdent] \n                                              [\"=\" ConstExpression] .             \n\nIdentList = Ident {\",\" Ident} .\n\nType = \"(\" Ident {\",\" Ident} \")\" |\n       \"^\" TypeIdent |\n       [\"packed\"] \"array\" \"[\" Type {\",\" Type} \"]\" \"of\" Type |\n       [\"packed\"] \"record\" Fields \"end\" |\n       [\"packed\"] \"interface\" FixedFields \"end\" |\n       [\"packed\"] \"set\" \"of\" Type |\n       [\"packed\"] \"string\" [ \"[\" ConstExpression \"]\" ] |\n       [\"packed\"] \"file\" [\"of\" Type] |\n       ConstExpression \"..\" ConstExpression |\n       (\"procedure\" | \"function\") [FormalParams] [\":\" TypeIdent] [CallModifier] |\n       Ident .\n       \nFields = FixedFields \n           [\"case\" [Ident \":\"] Type \"of\" \n               ConstExpression {\",\" ConstExpression} \":\" \"(\" Fields \")\"\n          {\";\" ConstExpression {\",\" ConstExpression} \":\" \"(\" Fields \")\"}] [\";\"] .       \n       \nFixedFields = IdentList \":\" Type {\";\" IdentList \":\" Type} .       \n       \nTypeIdent = \"string\" | \"file\" | Ident .       \n\nDesignator = BasicDesignator {Selector} .\n\nBasicDesignator = Ident |\n                  Ident [ActualParams] |\n                  Ident \"(\" Expression \")\" .\n\nSelector = \"^\" | \n           \"[\" Expression {\",\" Expression} \"]\" | \n           \".\" Ident | \n           \"(\" ActualParams \")\".\n\nStatement = [Label \":\"] [ (Designator | Ident) \":=\" Expression | \n                          (Designator | Ident) [ActualParams] {Selector} |\n                          CompoundStatement |\n                          IfStatement |\n                          CaseStatement |\n                          WhileStatement |\n                          RepeatStatement | \n                          ForStatement |\n                          GotoStatement |\n                          WithStatement ] .\n                          \nLabel = Ident .                          \n\nStatementList = Statement {\";\" Statement} .\n\nCompoundStatement = \"begin\" StatementList \"end\" .\n\nIfStatement = \"if\" Expression \"then\" Statement [\"else\" Statement] .\n\nCaseStatement = \"case\" Expression \"of\" CaseElement {\";\" CaseElement} \n                    [\";\"] [\"else\" StatementList] [\";\"] \"end\" .\n                    \nWhileStatement = \"while\" Expression \"do\" Statement .\n\nRepeatStatement = \"repeat\" StatementList \"until\" Expression .\n\nForStatement = \"for\" Ident \":=\" Expression (\"to\" | \"downto\") Expression \"do\" Statement.\n\nGotoStatement = \"goto\" Label .\n\nWithStatement = \"with\" Designator {\",\" Designator} \"do\" Statement .                    \n \nCaseElement = CaseLabel {\",\" CaseLabel} \":\" Statement .\n\nCaseLabel = ConstExpression [\"..\" ConstExpression] .\n\nConstExpression = Expression .\n\nExpression = SimpleExpression [(\"=\"|\"\u003c\u003e\"|\"\u003c\"|\"\u003c=\"|\"\u003e\"|\"\u003e=\"|\"in\") SimpleExpression] .\n\nSimpleExpression = [\"+\"|\"-\"] Term {(\"+\"|\"-\"|\"or\"|\"xor\") Term}.\n\nTerm = Factor {(\"*\"|\"/\"|\"div\"|\"mod\"|\"shl\"|\"shr\"|\"and\") Factor}.\n\nFactor = (Designator | Ident) [ActualParams] {Selector} |\n         Designator |\n         \"@\" Designator | \n         Number | \n         CharLiteral |\n         StringLiteral |  \n         \"(\" Expression \")\" | \n         \"not\" Factor |\n         SetConstructor |\n         \"nil\" |\n         Ident \"(\" Expression \")\" {Selector} .\n         \nSetConstructor = \"[\" [Expression [\"..\" Expression] \n                     {\",\" Expression [\"..\" Expression]}] \"]\" .         \n\nIdent = (Letter | \"_\") {Letter | \"_\" | Digit}.\n\nNumber = \"$\" HexDigit {HexDigit} | \n         Digit {Digit} [\".\" {Digit}] [\"e\" [\"+\" | \"-\"] Digit {Digit}] .\n\nCharLiteral = \"'\" (Character | \"'\" \"'\") \"'\" | \n              \"#\" Number .\n\nStringLiteral = \"'\" {Character | \"'\" \"'\"} \"'\".\n```\n\n### Compiler \nThe compiler is based on a recursive descent parser. It directly builds a Windows PE executable without using any external assembler or linker.\n\n#### Directives\n* `$APPTYPE` - Set application type. Examples: `{$APPTYPE GUI}`, `{$APPTYPE CONSOLE}`\n* `$UNITPATH` - Set additional unit search path. Example: `{$UNITPATH ..\\units\\}`\n\n#### Optimizations\nSome simple peephole optimizations are performed:\n* Push/pop elimination\n* FPU push/pop elimination\n* Local variable loading optimizations\n* Array element access optimizations\n* Record field access optimizations\n* Assignment optimizations\n* Comparison optimizations\n* Condition testing optimizations\n\n#### Inlined procedures and functions\nThe following identifiers are implemented as part of the compiler. Their names are not reserved words and can be locally redefined by the user.\n```pascal\nprocedure Inc(var x: Integer);\nprocedure Dec(var x: Integer);\nprocedure Read([var F: file;] var x1 {; var xi});\nprocedure Write([var F: file;] x1[:w[:d]] {; xi[:w[:d]]});\nprocedure ReadLn([var F: file;] var x1 {; var xi});\nprocedure WriteLn([var F: file;] x1[:w[:d]] {; xi[:w[:d]]});\nprocedure New(var P: Pointer);\nprocedure Dispose(var P: Pointer);\nprocedure Break;\nprocedure Continue;\nprocedure Exit;\nprocedure Halt[(const error: Integer)];\nfunction SizeOf(var x | T): Integer;\nfunction Ord(x: T): Integer;\nfunction Chr(x: Integer): Char;\nfunction Low(var x: T | T): T;\nfunction High(var x: T | T): T;\nfunction Pred(x: T): T;\nfunction Succ(x: T): T;\nfunction Round(x: Real): Integer;\nfunction Abs(x: T): T;\nfunction Sqr(x: T): T;\nfunction Sin(x: Real): Real;  \nfunction Cos(x: Real): Real;  \nfunction Arctan(x: Real): Real;  \nfunction Exp(x: Real): Real;\nfunction Ln(x: Real): Real;\nfunction SqRt(x: Real): Real;\n```\n\n### Standard library\n\n#### System unit\n```pascal\nfunction Timer: Integer;\nprocedure GetMem(var P: Pointer; Size: Integer);\nprocedure FreeMem(var P: Pointer);\nprocedure Randomize;\nfunction Random: Real;\nprocedure Assign(var F: file; const Name: string);\nprocedure Rewrite(var F: file[; BlockSize: Integer]);\nprocedure Reset(var F: file[; BlockSize: Integer]);\nprocedure Close(var F: file);\nprocedure BlockRead(var F: file; var Buf; Len: Integer; var LenRead: Integer);\nprocedure BlockWrite(var F: file; var Buf; Len: Integer);\nprocedure Seek(var F: file; Pos: Integer);\nfunction FileSize(var F: file): Integer;\nfunction FilePos(var F: file): Integer;\nfunction EOF(var F: file): Boolean;\nfunction IOResult: Integer;\nfunction Length(const s: string): Integer;\nprocedure Move(var Source; var Dest; Count: Integer);\nfunction Copy(const S: string; Index, Count: Integer): string;\nprocedure FillChar(var Data; Count: Integer; Value: Char);\nfunction ParamCount: Integer;\nfunction ParamStr(Index: Integer): string;\nprocedure Val(const s: string; var Number: Real; var Code: Integer);\nprocedure Str(Number: Real; var s: string[; DecPlaces: Integer]);\nprocedure IVal(const s: string; var Number: Integer; var Code: Integer);\nprocedure IStr(Number: Integer; var s: string);\nfunction UpCase(ch: Char): Char;\n```\n\n#### SysUtils unit\n```pascal\nfunction IntToStr(n: Integer): string;\nfunction StrToInt(const s: string): Integer;\nfunction FloatToStr(x: Real): string;\nfunction FloatToStrF(x: Real; Format: TFloatFormat; Precision, Digits: Integer): string;\nfunction StrToFloat(const s: string): Real;\nfunction StrToPChar(const s: string): PChar;\nfunction PCharToStr(p: PChar): string;\nfunction StrToPWideChar(const s: string): PWideChar;\nfunction PWideCharToStr(p: PWideChar): string;\n```\n\n### Samples\n* `factor.pas`   - Integer factorization demo\n* `lineq.pas`    - Linear equation solver. Uses `gauss.pas` unit. Requires `eq.txt`, `eqerr.txt`, or similar data file\n* `life.pas`     - The Game of Life\n* `sort.pas`     - Array sorting demo\n* `fft.pas`      - Fast Fourier Transform demo\n* `inserr.pas`   - Inertial navigation system error estimation demo. Uses `kalman.pas` unit\n* `list.pas`     - Linked list operations demo\n* `map.pas`      - Heterogenous list operations and Map function demo. Demonstrates XD Pascal methods and interfaces\n* `gui.pas`      - GUI application demo. Uses `windows.pas` unit\n* `raytracer.pas`- Raytracer demo. Demonstrates XD Pascal methods and interfaces. Equivalent to `raytracer.go`\n\n\u003cimg src=\"scene.png\"\u003e\n\n### Known issues\n\nWindows Defender antivirus is known to give false positive results on some programs compiled with XD Pascal.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvtereshkov%2Fxdpw","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvtereshkov%2Fxdpw","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvtereshkov%2Fxdpw/lists"}