🚀 JetPascal™ Architecture Deep Dive - Post v0.1.0 #6
jarroddavis68
started this conversation in
DevLog
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Compiler Architecture Built from the Ground Up
Hey JetPascal community! 👋
I'm excited to share the technical details of our redesigned compiler architecture that will be powering JetPascal v0.2.0+. This is a enhancement of the architecture in v0.1.0 and represents careful design and refinement.
🎯 The Big Picture: What Changed
Out with the Old
In with the New
🏗️ The Two-Layer Architecture
We've separated the compiler into two distinct layers, each with specific responsibilities:
Layer 1: JetPascal.Transpiler (Core Engine)
The transpiler is the heart of the compilation system. It provides:
Tokenizer Services:
Error Management:
Code Generation:
#linedirective emission for debugging (C++ errors map back to Pascal source!)Integer→ C++int, etc.)Build Settings:
Layer 2: JetPascal.Compiler (Project Manager)
The compiler sits on top of the transpiler and handles project-level concerns:
Project Management:
Build System Integration:
build.ziggeneration based on project settingsHigh-Level API:
Init()- Create new projects from templatesBuild()- Full compile-and-build pipelineClean()- Remove generated artifactsRun()- Execute compiled programsZig()- Direct Zig command passthrough💡 The SmartToken Innovation
This is where JetPascal really shines. Instead of building a traditional Abstract Syntax Tree (AST), we use SmartTokens - intelligent, self-contained compilation units.
What is a SmartToken?
Every Pascal language construct is represented by a SmartToken class that knows how to:
The SmartToken Base Class
Real-World Example: The WriteLn Token
Let's see how a
WriteLnstatement works:Pascal Source:
What TWriteLnToken Does:
Parse Phase:
RegisterHeaders Phase:
ACompiler.RegisterHeader('<print>')to ensurestd::printis availableEmitCpp Phase:
"Hello, {}!"namestd::println("Hello, {}!", name);Why SmartTokens Are Brilliant
Encapsulation: Each language feature is completely self-contained in its own unit file
JetPascal.Token.WriteStmt.pas- Write/WriteLn statementsJetPascal.Token.ForLoopStmt.pas- For loopsJetPascal.Token.VarDecl.pas- Variable declarationsJetPascal.Token.ProgramDecl.pas- Program declarationsExtensibility: Adding new language features is straightforward:
JetPascal.Token.XXX.pasunitParseandEmitCppmethodsMaintainability: When a bug is found or enhancement is needed:
🔄 The Compilation Pipeline
Here's how everything flows together:
Step 1: Registration
Step 2: Source Loading
Transpiler.SetSource(PascalCode, 'myprogram.pas');Step 3: Root Token Detection
Step 4: Parse Tree Construction
Step 5: Header Registration
RootToken.RegisterHeaders(Transpiler); // Each token in the tree registers its required C++ headersStep 6: Code Generation
Step 7: File Output
RootToken.WriteToDirectory(Transpiler, OutputDir); // Writes .cpp and .h files as appropriateStep 8: Zig Build
🎨 Design Patterns We Use
1. Factory Pattern
Keywords are mapped to token classes, allowing dynamic instantiation:
FKeywordMap.AddOrSetValue('writeln', TWriteLnToken);2. Visitor Pattern (Sort Of)
The transpiler "visits" tokens by calling their
ParseandEmitCppmethods.3. Strategy Pattern
Different token types provide different parsing and emission strategies.
4. Template Method Pattern
TCompilationUnitprovides a base template thatTProgramToken,TUnitToken, andTLibraryTokencustomize.📊 Benefits of This Architecture
For Users:
✅ Better error messages - Precise line/column information with context
✅ Faster compilation - No intermediate AST serialization
✅ Smaller binaries - Direct Pascal→C++ translation without overhead
✅ Debugging support - C++ errors map back to Pascal source via #line directives
For Contributors:
✅ Easy to understand - Each token is self-documenting
✅ Easy to extend - Add features without touching existing code
✅ Easy to test - Unit test individual token types
✅ Easy to debug - Clear separation of concerns
For the Project:
✅ No external dependencies for parsing (only relies on Delphi RTL)
✅ Complete control over compilation process
✅ Future-proof - Easy to add new features as Pascal or C++ evolves
🔮 What's Next?
With this architecture in place, we're now positioned to rapidly add features:
Coming in a future release:
Each feature will be a new SmartToken, plugged into the system without disrupting what already works.
🤔 Technical Q&A
Q: Why not use a traditional AST?
A: Traditional ASTs require building a complete tree in memory, then traversing it multiple times. Our SmartToken approach processes the source in a single pass (with recursive sub-passes for units), reducing memory overhead and simplifying the code.
Q: How does unit dependency resolution work?
A: When a
usesclause is parsed,TUsesDeclTokenlocates each unit file, pushes the current parser state, processes the unit recursively, then pops back to continue with the main file. Circular dependencies are detected and prevented.Q: What about compile times?
A: By skipping the AST construction phase, we save significant time. The bottleneck is actually the Zig C++ compilation phase, not our Pascal→C++ transpilation.
Q: How are you handling Pascal's case-insensitivity?
A: The tokenizer handles this transparently. Keywords are always matched case-insensitively, and we maintain the original casing for identifiers while doing case-insensitive lookups in symbol tables.
🙏 Acknowledgments
This architecture wouldn't exist without:
std::print📝 Closing Thoughts
We didn't just build a compiler - we built a platform for language innovation. The SmartToken architecture gives us the flexibility to experiment, iterate, and deliver features quickly while maintaining code quality.
Want to contribute? Check out our GitHub repo! Adding a new language feature is as simple as creating a new
JetPascal.Token.XXX.pasfile. The compiler handles the rest.Let's accelerate Pascal together! 🚀
Have questions about the architecture? Drop them in the comments and I'll do my deep dive explanations!
Beta Was this translation helpful? Give feedback.
All reactions