CX compiler + VM (All Platforms)
-
kinglestat
- Enthusiast

- Posts: 753
- Joined: Fri Jul 14, 2006 8:53 pm
- Location: Malta
- Contact:
CX compiler + VM (All Platforms)
Well I always wanted to build my own compiler as Purebasic - it's develops and even some of the expert coders on the forums inspired me.
CX (C eXtended) is made with Purebasic 6 tested through 6.30
This is the repository for the project with relevant docs
https://github.com/KingWolf71/CX
It closely follows C and can understand C code for the most part (to make it easy to port C code to it)
In C2-inc you can switch build types; GUI is for testing while #BUILD_COMPILER is the proper build and can build both as a console and windows compiler.
#BUILD_TYPE = #BUILD_GUI
I used Claude AI for much of the donkey work but the base is the same code I had pasted some years back
I've tested it as much as I could but of course feedback is welcome (of any type)
cheers!
UPDATE:
Coding with AI for those asking is easy and complex. I used Visual studio code. Started with Github copilot and released that most of the "correct" code was being done by Claude; so I switched to Claude exclusively. But one can use any AI. The trick is using rules (each AI has a file you use shape your results) and only making small changes whenever possible as it doesn't always follow the rules. The reason is the "creativity" allowance I believe.
It was pointed to me that D already exists so upon reflection I dubbed it CX
UPDATE 21/Jan/2026
default compiler options
https://gyazo.com/8e128b936d324cd085f0b6be478132e3
2 ways how to use/compile it
1. TEST MODE (GUI/Editor)
set #BUILD_TYPE = #BUILD_GUI in C2-IN-Vxx.pbi
compile as Windows/linux executable format
2. Compiler mode
set #BUILD_TYPE = #BUILD_COMPILER in C2-IN-Vxx.pbi
compile as Windows/linux executable format or console (console preferred)
if you use windows you'll need to build splash for long compiles
CX (C eXtended) is made with Purebasic 6 tested through 6.30
This is the repository for the project with relevant docs
https://github.com/KingWolf71/CX
It closely follows C and can understand C code for the most part (to make it easy to port C code to it)
In C2-inc you can switch build types; GUI is for testing while #BUILD_COMPILER is the proper build and can build both as a console and windows compiler.
#BUILD_TYPE = #BUILD_GUI
I used Claude AI for much of the donkey work but the base is the same code I had pasted some years back
I've tested it as much as I could but of course feedback is welcome (of any type)
cheers!
UPDATE:
Coding with AI for those asking is easy and complex. I used Visual studio code. Started with Github copilot and released that most of the "correct" code was being done by Claude; so I switched to Claude exclusively. But one can use any AI. The trick is using rules (each AI has a file you use shape your results) and only making small changes whenever possible as it doesn't always follow the rules. The reason is the "creativity" allowance I believe.
It was pointed to me that D already exists so upon reflection I dubbed it CX
UPDATE 21/Jan/2026
default compiler options
https://gyazo.com/8e128b936d324cd085f0b6be478132e3
2 ways how to use/compile it
1. TEST MODE (GUI/Editor)
set #BUILD_TYPE = #BUILD_GUI in C2-IN-Vxx.pbi
compile as Windows/linux executable format
2. Compiler mode
set #BUILD_TYPE = #BUILD_COMPILER in C2-IN-Vxx.pbi
compile as Windows/linux executable format or console (console preferred)
if you use windows you'll need to build splash for long compiles
Last edited by kinglestat on Wed Jan 21, 2026 10:44 am, edited 5 times in total.
I may not help with your coding
Just ask about mental issues!
http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
Just ask about mental issues!
http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
-
threedslider
- Enthusiast

- Posts: 555
- Joined: Sat Feb 12, 2022 7:15 pm
Re: D-Plus compiler + VM (All Platforms)
Hello !
Thanks for sharing !
I try it on what i have followed in your github but when i click the first sample 001 simple while.d (GUI mode) and then the output doesn't work for me
it says :
Am i missing something ?
Thanks for sharing !
I try it on what i have followed in your github but when i click the first sample 001 simple while.d (GUI mode) and then the output doesn't work for me
it says :
Code: Select all
Loading: .\Examples\001 Simple while.d
Compile OK - Running...
Runtime: 0.00 seconds.
D+AI Version: 1.039.26
Module: .\Examples\001 Simple while.d
====[ASM Listing]=====
1: HALT
======================
Auto-close in 44...
Re: D-Plus compiler + VM (All Platforms)
That looks really interesting and a lot of work.
-
kinglestat
- Enthusiast

- Posts: 753
- Joined: Fri Jul 14, 2006 8:53 pm
- Location: Malta
- Contact:
Re: D-Plus compiler + VM (All Platforms)
@threesilder
I need more context than that; but turn on show debugger as that looks like a compilation error
In the meantime I did find a couple of more bugs and added an optimization
I need more context than that; but turn on show debugger as that looks like a compilation error
In the meantime I did find a couple of more bugs and added an optimization
I may not help with your coding
Just ask about mental issues!
http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
Just ask about mental issues!
http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
Re: CX compiler + VM (All Platforms)
Tested some examples and function print doesn't print anything
Compiled c2-modules-V25.pb in cx.exe (Win10x64 PB630)
and runned cx.exe -a "examples\002 if else.cx"
Content of 002 if else.asm
; CX Assembly Listing
; Generated by CX Compiler
.data
; slot name type
0 ?discard? int
.text
0000: HALT
0001: EOF
; End of listing
Compiled c2-modules-V25.pb in cx.exe (Win10x64 PB630)
and runned cx.exe -a "examples\002 if else.cx"
Content of 002 if else.asm
; CX Assembly Listing
; Generated by CX Compiler
.data
; slot name type
0 ?discard? int
.text
0000: HALT
0001: EOF
; End of listing
Re: CX compiler + VM (All Platforms)
Looks good, congrats !
-
kinglestat
- Enthusiast

- Posts: 753
- Joined: Fri Jul 14, 2006 8:53 pm
- Location: Malta
- Contact:
Re: CX compiler + VM (All Platforms)
that was indeed a bug and should be now fixed
Sergey wrote: Wed Jan 21, 2026 10:17 am Tested some examples and function print doesn't print anything
Compiled c2-modules-V25.pb in cx.exe (Win10x64 PB630)
and runned cx.exe -a "examples\002 if else.cx"
Content of 002 if else.asm
; CX Assembly Listing
; Generated by CX Compiler
.data
; slot name type
0 ?discard? int
.text
0000: HALT
0001: EOF
; End of listing
I may not help with your coding
Just ask about mental issues!
http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
Just ask about mental issues!
http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
- skinkairewalker
- Addict

- Posts: 825
- Joined: Fri Dec 04, 2015 9:26 pm
Re: CX compiler + VM (All Platforms)
awesome
thanks for sharing
thanks for sharing
Re: CX compiler + VM (All Platforms)
I tried to learn a bit from it! Wow! At the moment it's to hard for me! But for sure, if I have more time I will study it in detail - I guess I will learn a lot from that code!
Many tanks for sharing!!!
First I analysed the scanner: I'm wondering you use String operations for all. I'm very sure that it is possible to speed up the scanner by using Character operations instead of the 1Char String operations.
Here is a simple code of what I mean!
More Speed optimations:
Prototyping of often used String Functions like
ParseFunction( line.s, row.i )
Purebasic always creates a copy of the String when calling the functions. To prevent PB from creating a copy we can use the
Prototype command. Then PB directly use the Stringpointer and do not create a copy of the String.
This optimation brings performance. But you'll need deeper changes: You have to change all the functions to operate with String/Character-Pointers
Many tanks for sharing!!!
First I analysed the scanner: I'm wondering you use String operations for all. I'm very sure that it is possible to speed up the scanner by using Character operations instead of the 1Char String operations.
Here is a simple code of what I mean!
Code: Select all
; Any or Universal Pointer (see PurePasic IDE Common.pb Structrue PTR)
Structure pAny ; ATTENTION! Only use as Pointer Strukture! Do not define as a normal Var!
StructureUnion
; multiple and single access like an Array
a.a[0] ; ASCII : 1 Byte unsigned [0..255]
b.b[0] ; BYTE : 1 Byte signed [-128..127]
c.c[0] ; CHAR : 1 Byte for Ascii Chars 2 Bytes for unicode
w.w[0] ; WORD : 2 Byte signed [-32768..32767]
u.u[0] ; UNICODE : 2 Byte unsigned [0..65535]
l.l[0] ; LONG : 4 Byte signed [-2147483648..2147483647]
f.f[0] ; FLOAT : 4 Byte
q.q[0] ; QUAD : 8 Byte signed [-9223372036854775808..9223372036854775807]
d.d[0] ; DOUBLE : 8 Byte float
i.i[0] ; INTEGER : 4 or 8 Byte INT, depending on System
*p.pAny[0]; Pointer to Any (for C-like **, PointerPointer use) !VERY VERY DANGEROUS!
EndStructureUnion
EndStructure
; An adapted version of pAny especally for character use
Structure pChar ; ATTENTION! Only use as Pointer Structure! Do not define as a normal Var!
StructureUnion
a.a[0] ; ASCII : 1 Byte unsigned [0..255]
c.c[0] ; CHAR : 1 Byte for Ascii Chars 2 Bytes for unicode
u.u[0] ; UNICODE : 2 Byte unsigned [0..65535]
EndStructureUnion
EndStructure
Global gNextChar.c
Global *pC.pChar = @gszFileText
Macro par_NextCharacter()
; gNextChar = Mid( gszFileText, gPos, 1 )
gNextChar = *pC\c[gPos]
gPos + 1
If gNextChar = #LF$
gLineNumber + 1
gCol = 1
Else
gCol + 1
EndIf
EndMacro
; Change the Strings in the Scanner Procedrue to chars
Select gNextChar
Case gszEOF
par_AddTokenSimple( #ljEOF )
Break
Case ' ', #CR, #LF, #TAB
Continue
Case '{'
braces + 1
par_AddTokenSimple( #ljLeftBrace )
Case '}'
braces - 1
par_AddTokenSimple( #ljRightBrace )
If braces = 0 : gCurrFunction = 1 : EndIf
Case '('
par_AddTokenSimple( #ljLeftParent )
Case ')'
par_AddTokenSimple( #ljRightParent )
Case '['
par_AddTokenSimple( #ljLeftBracket )
Case ']'
par_AddTokenSimple( #ljRightBracket )
Case '+'
More Speed optimations:
Prototyping of often used String Functions like
ParseFunction( line.s, row.i )
Purebasic always creates a copy of the String when calling the functions. To prevent PB from creating a copy we can use the
Prototype command. Then PB directly use the Stringpointer and do not create a copy of the String.
Code: Select all
Prototype ParseFunctions(line.s, row.i )
Global ParseFunctions.ParseFunctions
Procedure _ParseFunctions(*line, row.i )
EndProcedure
ParseFunctions = @_ParseFunctions()
-
kinglestat
- Enthusiast

- Posts: 753
- Joined: Fri Jul 14, 2006 8:53 pm
- Location: Malta
- Contact:
Re: CX compiler + VM (All Platforms)
Thank you all for the plaudits
@Smaag
Speed is relative. For compilation readability is more important than compilation speed; I could have used a memory file (which I did in my first incarnation)
Second is stability in the VM while trying to keep speed and compatibility with Spiderbasic such that CX code can run on as many platforms as possible.
I tried to weight the pros and cons; and I'm pleased with the results. But the repo is public so you are all free to tune/upgrade
@Smaag
Speed is relative. For compilation readability is more important than compilation speed; I could have used a memory file (which I did in my first incarnation)
Second is stability in the VM while trying to keep speed and compatibility with Spiderbasic such that CX code can run on as many platforms as possible.
I tried to weight the pros and cons; and I'm pleased with the results. But the repo is public so you are all free to tune/upgrade
I may not help with your coding
Just ask about mental issues!
http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
Just ask about mental issues!
http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
-
threedslider
- Enthusiast

- Posts: 555
- Joined: Sat Feb 12, 2022 7:15 pm
Re: D-Plus compiler + VM (All Platforms)
In debug version it shows that when i have selected this first sample :kinglestat wrote: Mon Jan 19, 2026 10:01 pm @threesilder
I need more context than that; but turn on show debugger as that looks like a compilation error
In the meantime I did find a couple of more bugs and added an optimization
Code: Select all
Running version [0]
-- Preprocessing source...
-- Scanner pass: Lexical analysis and tokenization...
ExtractConstants: Scanning 0 tokens...
-- Auto-declaring struct variables...
-- AST pass: Building abstract syntax tree...
VERIFY: gnLastVariable=1
V1.030.47: STRUCT PARAM DUMP AT CODEGEN START:
V1.030.47: END STRUCT PARAM DUMP
VERIFY: Fixed 0 variables
-- MarkImplicitReturns: Marking function-end NOOPIFs...
-- InitJumpTracker: Calculating initial jump offsets...
-- TypeInference: Unified type resolution...
-- Postprocessor: Correctness passes...
-- Optimizer: Peephole and fusion optimizations...
-- BuildVariableTemplates: Creating gGlobalTemplate and gFuncTemplates...
-- FixJMP: Applying adjusted offsets and patching functions...
-- CalculateStackSizes: Auto-sizing VM arrays...
=== ARCODE: Checking STRUCT_*_LOCAL opcodes ===
=== END ARCODE CHECK ===
D+AI Version: 1.039.26
Re: CX compiler + VM (All Platforms)
Just to understand it right what this software is able to do!
Please correct me when I'm wrong!
It is a runtime compiler, what compiles a very simple C-Like Syntax in an intermediate Byte-Code.
This Bytecode is interpreted by the Virtual Machine.
- Can we say it's a simple Script language?
- We can use it to integrate CX Script language into our PB and Spider Basic programms.
- If we chage the Syntax from CX to Purebasic Syntax we get a PureBasic Script runtime Compiler/Interpreter?
Please correct me when I'm wrong!
It is a runtime compiler, what compiles a very simple C-Like Syntax in an intermediate Byte-Code.
This Bytecode is interpreted by the Virtual Machine.
- Can we say it's a simple Script language?
- We can use it to integrate CX Script language into our PB and Spider Basic programms.
- If we chage the Syntax from CX to Purebasic Syntax we get a PureBasic Script runtime Compiler/Interpreter?
Re: CX compiler + VM (All Platforms)
This is really interesting and seems like an amazing job.
It's an unfamiliar field for me, maybe it can help me learn a bit more,
if I can understand its goal, starting with SMaag's questions above and know what you have planned next, debugger,...
I have 3 cx examples with an out-of-bounds array index in c2-vm-V19.pb (line: 1493):
test nested structure.cx, test struct pointer expresions.cx, full test suite.cx
It's an unfamiliar field for me, maybe it can help me learn a bit more,
if I can understand its goal, starting with SMaag's questions above and know what you have planned next, debugger,...
I have 3 cx examples with an out-of-bounds array index in c2-vm-V19.pb (line: 1493):
test nested structure.cx, test struct pointer expresions.cx, full test suite.cx
-
kinglestat
- Enthusiast

- Posts: 753
- Joined: Fri Jul 14, 2006 8:53 pm
- Location: Malta
- Contact:
Re: CX compiler + VM (All Platforms)
@chrisR
it was a display bug which I fixed
it was a display bug which I fixed
I may not help with your coding
Just ask about mental issues!
http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
Just ask about mental issues!
http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net


