Using CPP and HEADER Keywords for direct C++ Integration #7
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.
-
🚀 Direct C++ Integration - New
cppandheaderKeywordsVersion: JetPascal 0.2.0+
📢 Overview
We're excited to announce two powerful new language features that give you direct control over C++ code generation in JetPascal:
cpp...endblocks - Emit raw C++ code exactly as writtenheaderdeclarations - Control which C++ headers get includedThese features bridge the gap between high-level Pascal syntax and low-level C++ control, giving you the best of both worlds.
🎯 Purpose & Design Goals
Why Direct C++ Emission?
JetPascal transpiles Pascal to C++, but sometimes you need to:
std::print,std::format, etc.Instead of forcing you to edit generated C++ files (which get overwritten on recompile), JetPascal now lets you embed C++ directly in your Pascal source.
Why Manual Header Control?
The compiler automatically includes common headers, but you might need:
<raylib.h>,<SDL2/SDL.h>)<iostream>,<vector>)<windows.h>,<unistd.h>)🔧 The
cpp...endBlockSyntax
Key Features
✅ Exact Emission - Whitespace, indentation, everything preserved
✅ No Processing - JetPascal doesn't parse or validate the C++ code
✅ Line Directives - Automatic
#linedirectives for debugging✅ Access to Context - Can reference Pascal variables in scope
Where You Can Use It
cppblocks work anywhere statements are allowed:✅ Inside Program Main Block
✅ Inside Functions/Procedures
✅ Inside Any Begin..End Block
Important Rules
endkeyword - The block terminates at the firstendkeywordheaderdirective to include what you needExamples
Example 1: Using STL Containers
Example 2: Performance-Critical Code
Example 3: Platform-Specific Code
📦 The
headerDirectiveSyntax
Key Features
✅ Automatic Deduplication - Same header listed multiple times? No problem
✅ Sorted Output - Headers emitted in consistent order
✅ Top of File - Always placed at the beginning of generated C++
Where You Can Use It
headerdirectives work in two places:✅ At Compilation Unit Level
✅ Inside Begin..End Blocks
Format Validation
The
headerdirective is strict about format:✅ Valid:
❌ Invalid:
Examples
Example 1: Multiple Headers
Example 2: Third-Party Libraries
Example 3: Conditional Headers
🔄 How They Work Together
Generated C++ Output
Pascal Source:
Generated C++ (simplified):
The Pipeline
headerandcpptokens💡 Best Practices
DO:
✅ Use
headerto declare all C++ dependencies✅ Keep
cppblocks small and focused✅ Add comments explaining what the C++ code does
✅ Test C++ code separately if it's complex
✅ Use
cppblocks for prototyping new featuresDON'T:
❌ Put
endkeyword inside C++ code (terminates the block)❌ Forget to include required headers
❌ Write entire programs in
cppblocks (defeats the purpose!)❌ Assume Pascal variables have specific C++ names (check generated code)
❌ Rely on implementation details of the transpiler
🎓 Use Cases
1. Library Integration
Quickly wrap C/C++ libraries before full Pascal bindings exist.
2. Performance Tuning
Hand-optimize critical inner loops while keeping the rest in Pascal.
3. Feature Prototyping
Try out C++23 features before adding them to the Pascal language.
4. Platform-Specific Code
Embed platform-specific APIs without complex conditional compilation.
5. Debugging
Insert debug prints or breakpoints in generated C++ code.
6. Interfacing with Existing Codebases
Call into existing C++ code without writing full binding layers.
📝 Technical Notes
Semicolon Handling
headerdirective consumes its own semicolonend(treated as statement)Line Directives
cppblocks automatically emit#linedirectives:This ensures:
Header Deduplication
Headers are automatically deduplicated and sorted:
Generates:
🚦 Status & Compatibility
Status: ✅ Fully Implemented
Since: JetPascal 0.2.0
Tested: Programs, Units, Libraries
Platform: All supported targets (native, cross-compile)
🤝 Feedback Welcome!
These features are brand new! We'd love to hear:
cppandheader?Drop your thoughts in the comments below! 👇
Happy Coding! 🚀
JetPascal™ - Accelerate Your Code!
Beta Was this translation helpful? Give feedback.
All reactions