cpyte 1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cpyte-1.0/PKG-INFO +1598 -0
- cpyte-1.0/pyproject.toml +24 -0
- cpyte-1.0/setup.cfg +4 -0
- cpyte-1.0/source/cpy_language_documentation.md +1588 -0
- cpyte-1.0/source/cpyte.egg-info/PKG-INFO +1598 -0
- cpyte-1.0/source/cpyte.egg-info/SOURCES.txt +8 -0
- cpyte-1.0/source/cpyte.egg-info/dependency_links.txt +1 -0
- cpyte-1.0/source/cpyte.egg-info/entry_points.txt +2 -0
- cpyte-1.0/source/cpyte.egg-info/requires.txt +1 -0
- cpyte-1.0/source/cpyte.egg-info/top_level.txt +1 -0
cpyte-1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,1598 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cpyte
|
|
3
|
+
Version: 1.0
|
|
4
|
+
Summary: The Cpyte programming language compiler
|
|
5
|
+
Author: Hoang Duy Tung
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
Requires-Dist: llvmlite>=0.45
|
|
10
|
+
|
|
11
|
+
# Cpy Language Reference: A Comprehensive Guide to Syntax and Semantics
|
|
12
|
+
|
|
13
|
+
## Status Notice
|
|
14
|
+
|
|
15
|
+
**This documentation describes both currently implemented features and planned future enhancements.**
|
|
16
|
+
|
|
17
|
+
Features marked as **"Planned"** or **"In Development"** are not yet available in the current implementation but are part of the language design roadmap.
|
|
18
|
+
|
|
19
|
+
## Executive Summary
|
|
20
|
+
|
|
21
|
+
Cpy is a sophisticated compiled programming language that combines Python-like syntax with systems-level capabilities. It features static typing, manual memory management, direct C interoperability, and compilation to native machine code via LLVM. The language is designed for developers who require both productivity and low-level control.
|
|
22
|
+
|
|
23
|
+
## Table of Contents
|
|
24
|
+
|
|
25
|
+
1. [Language Overview](#language-overview)
|
|
26
|
+
2. [Lexical Structure](#lexical-structure)
|
|
27
|
+
3. [Type System](#type-system)
|
|
28
|
+
4. [64-bit Support](#64-bit-support)
|
|
29
|
+
5. [Expressions and Operators](#expressions-and-operators)
|
|
30
|
+
6. [Statements and Control Flow](#statements-and-control-flow)
|
|
31
|
+
7. [Functions](#functions)
|
|
32
|
+
8. [Structures and User-Defined Types](#structures-and-user-defined-types)
|
|
33
|
+
9. [Memory Management](#memory-management)
|
|
34
|
+
10. [C Interoperability](#c-interoperability)
|
|
35
|
+
11. [Compilation Model](#compilation-model)
|
|
36
|
+
12. [Standard Library](#standard-library)
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Language Overview
|
|
41
|
+
|
|
42
|
+
Cpy represents a unique fusion of high-level language ergonomics with low-level system programming capabilities. The language targets developers who need:
|
|
43
|
+
|
|
44
|
+
- **Performance**: Native compilation via LLVM with optimization levels
|
|
45
|
+
- **Control**: Manual memory management with pointer operations
|
|
46
|
+
- **Interoperability**: Seamless C library integration
|
|
47
|
+
- **Safety**: Static type checking with semantic analysis
|
|
48
|
+
- **Productivity**: Python-inspired syntax for rapid development
|
|
49
|
+
|
|
50
|
+
### Design Philosophy
|
|
51
|
+
|
|
52
|
+
The language follows these core principles:
|
|
53
|
+
|
|
54
|
+
1. **Explicit is better than implicit**: Memory operations and type conversions are explicit
|
|
55
|
+
2. **Systems programming first**: Designed for scenarios where C would traditionally be used
|
|
56
|
+
3. **Modern syntax**: Leverages familiar Python-like patterns while maintaining systems semantics
|
|
57
|
+
4. **Zero-cost abstractions**: High-level features compile to efficient machine code
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## Lexical Structure
|
|
62
|
+
|
|
63
|
+
### Source Code Representation
|
|
64
|
+
|
|
65
|
+
Cpy source files use the `.cpy` extension and are represented as UTF-8 encoded text files. The language uses significant whitespace (indentation) for block structure, similar to Python.
|
|
66
|
+
|
|
67
|
+
### Comments
|
|
68
|
+
|
|
69
|
+
```cpy
|
|
70
|
+
# Single-line comments extend to the end of the line
|
|
71
|
+
# There are no multi-line comment delimiters
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Identifiers
|
|
75
|
+
|
|
76
|
+
Identifiers follow these rules:
|
|
77
|
+
- Must start with a letter (a-z, A-Z) or underscore (_)
|
|
78
|
+
- Subsequent characters can be letters, digits, or underscores
|
|
79
|
+
- Case-sensitive
|
|
80
|
+
- No length limit
|
|
81
|
+
|
|
82
|
+
```cpy
|
|
83
|
+
variable_name = 42
|
|
84
|
+
_private_var = "internal"
|
|
85
|
+
CONSTANT_VALUE = 3.14
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Keywords
|
|
89
|
+
|
|
90
|
+
The following reserved words cannot be used as identifiers:
|
|
91
|
+
|
|
92
|
+
**Control Flow:**
|
|
93
|
+
- `if`, `else`, `elif`, `while`, `for`, `in`, `break`, `continue`
|
|
94
|
+
- `switch`, `case`, `default`
|
|
95
|
+
|
|
96
|
+
**Declarations:**
|
|
97
|
+
- `def`, `class`, `return`, `struct`
|
|
98
|
+
|
|
99
|
+
**Access Modifiers:**
|
|
100
|
+
- `public`, `private`, `static`, `virtual`, `override`
|
|
101
|
+
|
|
102
|
+
**Memory and Types:**
|
|
103
|
+
- `new`, `sizeof`, `ref`, `int64`, `uint64`
|
|
104
|
+
|
|
105
|
+
**Literals and Constants:**
|
|
106
|
+
- `true`, `false`, `null`
|
|
107
|
+
|
|
108
|
+
**Operators:**
|
|
109
|
+
- `and`, `or`, `not`
|
|
110
|
+
|
|
111
|
+
**I/O:**
|
|
112
|
+
- `print`, `input`
|
|
113
|
+
|
|
114
|
+
**Modules:**
|
|
115
|
+
- `import`
|
|
116
|
+
|
|
117
|
+
### Literals
|
|
118
|
+
|
|
119
|
+
#### Numeric Literals
|
|
120
|
+
|
|
121
|
+
```cpy
|
|
122
|
+
# Integer literals
|
|
123
|
+
42
|
|
124
|
+
-17
|
|
125
|
+
0
|
|
126
|
+
|
|
127
|
+
# 64-bit integer literals
|
|
128
|
+
9223372036854775807
|
|
129
|
+
-9223372036854775808
|
|
130
|
+
18446744073709551615
|
|
131
|
+
|
|
132
|
+
# Hexadecimal literals (useful for 64-bit values)
|
|
133
|
+
0x7FFFFFFFFFFFFFFF # Maximum int64
|
|
134
|
+
0xFFFFFFFFFFFFFFFF # Maximum uint64
|
|
135
|
+
|
|
136
|
+
# Floating-point literals
|
|
137
|
+
3.14
|
|
138
|
+
-0.001
|
|
139
|
+
2.0e10 # Scientific notation
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
#### String Literals
|
|
143
|
+
|
|
144
|
+
```cpy
|
|
145
|
+
"Hello, World!"
|
|
146
|
+
'Single quotes also work'
|
|
147
|
+
"Escaped characters: \n\t\r\\\""
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
#### Boolean and Null Literals
|
|
151
|
+
|
|
152
|
+
```cpy
|
|
153
|
+
is_valid = true
|
|
154
|
+
is_error = false
|
|
155
|
+
empty_value = null
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Operators and Punctuation
|
|
159
|
+
|
|
160
|
+
#### Arithmetic Operators
|
|
161
|
+
- `+` (addition), `-` (subtraction)
|
|
162
|
+
- `*` (multiplication), `/` (division)
|
|
163
|
+
- `//` (integer division), `%` (modulo)
|
|
164
|
+
- `**` (exponentiation)
|
|
165
|
+
|
|
166
|
+
#### Comparison Operators
|
|
167
|
+
- `==` (equality), `!=` (inequality)
|
|
168
|
+
- `<` (less than), `>` (greater than)
|
|
169
|
+
- `<=` (less than or equal), `>=` (greater than or equal)
|
|
170
|
+
|
|
171
|
+
#### Logical Operators
|
|
172
|
+
- `and` (logical AND), `or` (logical OR), `not` (logical NOT)
|
|
173
|
+
|
|
174
|
+
#### Bitwise Operators
|
|
175
|
+
- `&` (bitwise AND), `|` (bitwise OR), `^` (bitwise XOR)
|
|
176
|
+
- `~` (bitwise NOT)
|
|
177
|
+
- `<<` (left shift), `>>` (right shift)
|
|
178
|
+
|
|
179
|
+
#### Assignment Operators
|
|
180
|
+
- `=` (simple assignment)
|
|
181
|
+
- `+=`, `-=`, `*=`, `/=`, `//=`, `:=` (compound assignment)
|
|
182
|
+
|
|
183
|
+
#### Memory Operators
|
|
184
|
+
- `*` (dereference), `&` (address-of)
|
|
185
|
+
- `--` (decrement)
|
|
186
|
+
|
|
187
|
+
#### Other Operators
|
|
188
|
+
- `->` (return type annotation)
|
|
189
|
+
- `.` (member access), `[]` (array indexing)
|
|
190
|
+
- `()` (function call), `,` (comma separator)
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## Type System
|
|
195
|
+
|
|
196
|
+
Cpy employs a static type system with type inference and explicit type annotations. The type system is designed to catch errors at compile time while maintaining flexibility.
|
|
197
|
+
|
|
198
|
+
### Primitive Types
|
|
199
|
+
|
|
200
|
+
#### Integer Types
|
|
201
|
+
|
|
202
|
+
Cpy supports multiple integer types with different bit widths and signedness:
|
|
203
|
+
|
|
204
|
+
**32-bit Integers:**
|
|
205
|
+
```cpy
|
|
206
|
+
int x = 42
|
|
207
|
+
int negative = -17
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
**64-bit Integers:**
|
|
211
|
+
```cpy
|
|
212
|
+
int64 large_number = 9223372036854775807
|
|
213
|
+
int64 negative_large = -9223372036854775808
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
**Unsigned 64-bit Integers:**
|
|
217
|
+
```cpy
|
|
218
|
+
uint64 unsigned_large = 18446744073709551615
|
|
219
|
+
uint64 hex_value = 0xFFFFFFFFFFFFFFFF
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
**Integer Type Summary:**
|
|
223
|
+
- `int`: 32-bit signed integer (range: -2,147,483,648 to 2,147,483,647)
|
|
224
|
+
- `int64`: 64-bit signed integer (range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
|
|
225
|
+
- `uint64`: 64-bit unsigned integer (range: 0 to 18,446,744,073,709,551,615)
|
|
226
|
+
|
|
227
|
+
#### Floating-Point Type
|
|
228
|
+
```cpy
|
|
229
|
+
float pi = 3.14159
|
|
230
|
+
float precision = 0.0001
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
#### String Type
|
|
234
|
+
```cpy
|
|
235
|
+
str name = "Cpy Programming"
|
|
236
|
+
str empty = ""
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
#### Boolean Type
|
|
240
|
+
```cpy
|
|
241
|
+
bool is_valid = true
|
|
242
|
+
bool is_error = false
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Pointer Types
|
|
246
|
+
|
|
247
|
+
Cpy supports pointers for manual memory management:
|
|
248
|
+
|
|
249
|
+
```cpy
|
|
250
|
+
int* ptr # Pointer to integer
|
|
251
|
+
int** ptr_to_ptr # Pointer to pointer
|
|
252
|
+
void* generic_ptr # Generic pointer
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
**64-bit Address Space:**
|
|
256
|
+
- All pointers are 64-bit on 64-bit platforms
|
|
257
|
+
- Supports addressing up to 2^64 bytes of memory
|
|
258
|
+
- Compatible with modern 64-bit operating systems and hardware
|
|
259
|
+
|
|
260
|
+
**Additional Pointer Types:**
|
|
261
|
+
```cpy
|
|
262
|
+
int64* large_ptr # Pointer to 64-bit integer
|
|
263
|
+
uint64* ulong_ptr # Pointer to unsigned 64-bit integer
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### Array Types
|
|
267
|
+
|
|
268
|
+
Arrays are dynamically allocated with explicit size:
|
|
269
|
+
|
|
270
|
+
```cpy
|
|
271
|
+
int[] numbers # Array of integers
|
|
272
|
+
str[] strings # Array of strings
|
|
273
|
+
Point[] points # Array of Point structures
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### Type Annotations
|
|
277
|
+
|
|
278
|
+
Type annotations use the `->` syntax for functions and `:` syntax for variables:
|
|
279
|
+
|
|
280
|
+
```cpy
|
|
281
|
+
public function_name(param1: int, param2: str) -> int:
|
|
282
|
+
# Function body
|
|
283
|
+
return 0
|
|
284
|
+
|
|
285
|
+
int x: int = 42
|
|
286
|
+
str name: str = "example"
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### Type Inference
|
|
290
|
+
|
|
291
|
+
The compiler can infer types in many contexts:
|
|
292
|
+
|
|
293
|
+
```cpy
|
|
294
|
+
# Type inferred from literal
|
|
295
|
+
x = 42 # Inferred as int
|
|
296
|
+
y = 3.14 # Inferred as float
|
|
297
|
+
|
|
298
|
+
# Large literals automatically inferred as 64-bit
|
|
299
|
+
large = 9223372036854775807 # Inferred as int64
|
|
300
|
+
hex_val = 0xFFFFFFFFFFFFFFFF # Inferred as uint64
|
|
301
|
+
|
|
302
|
+
# Type inferred from expression
|
|
303
|
+
result = x + y # Type inferred based on operation
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
---
|
|
307
|
+
|
|
308
|
+
## 64-bit Support
|
|
309
|
+
|
|
310
|
+
Cpy provides comprehensive 64-bit support for modern computing platforms, enabling developers to work with large datasets, high-precision calculations, and memory-intensive applications.
|
|
311
|
+
|
|
312
|
+
### Platform Architecture
|
|
313
|
+
|
|
314
|
+
Cpy supports 64-bit computing environments with the following capabilities:
|
|
315
|
+
|
|
316
|
+
- **64-bit address space**: Support for up to 2^64 bytes of addressable memory
|
|
317
|
+
- **64-bit integer arithmetic**: Native support for 64-bit signed and unsigned integers
|
|
318
|
+
- **64-bit pointers**: All pointer types are 64-bit on supported platforms
|
|
319
|
+
- **LLVM backend**: Leverages LLVM's 64-bit optimization capabilities
|
|
320
|
+
|
|
321
|
+
### 64-bit Integer Types
|
|
322
|
+
|
|
323
|
+
#### Signed 64-bit Integers (int64)
|
|
324
|
+
|
|
325
|
+
The `int64` type provides a 64-bit signed integer with the following characteristics:
|
|
326
|
+
|
|
327
|
+
- **Range**: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
|
|
328
|
+
- **Size**: 8 bytes (64 bits)
|
|
329
|
+
- **Use cases**: Large counters, timestamps, file sizes, database keys
|
|
330
|
+
|
|
331
|
+
```cpy
|
|
332
|
+
# 64-bit signed integer usage
|
|
333
|
+
int64 file_size = 9223372036854775807
|
|
334
|
+
int64 timestamp = 1699999999999
|
|
335
|
+
int64 database_id = 1234567890123456789
|
|
336
|
+
|
|
337
|
+
# Arithmetic operations
|
|
338
|
+
int64 result = file_size + 1024
|
|
339
|
+
int64 multiplied = timestamp * 2
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
#### Unsigned 64-bit Integers (uint64)
|
|
343
|
+
|
|
344
|
+
The `uint64` type provides a 64-bit unsigned integer with the following characteristics:
|
|
345
|
+
|
|
346
|
+
- **Range**: 0 to 18,446,744,073,709,551,615
|
|
347
|
+
- **Size**: 8 bytes (64 bits)
|
|
348
|
+
- **Use cases**: Memory addresses, bit masks, unsigned counters, hashes
|
|
349
|
+
|
|
350
|
+
```cpy
|
|
351
|
+
# 64-bit unsigned integer usage
|
|
352
|
+
uint64 memory_address = 0xFFFFFFFFFFFFFFFF
|
|
353
|
+
uint64 bit_mask = 0x123456789ABCDEF0
|
|
354
|
+
uint64 hash_value = 18446744073709551615
|
|
355
|
+
|
|
356
|
+
# Bitwise operations
|
|
357
|
+
uint64 masked = memory_address & 0x00000000FFFFFFFF
|
|
358
|
+
uint64 shifted = hash_value >> 16
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
### Type Conversions
|
|
362
|
+
|
|
363
|
+
#### Safe Conversions
|
|
364
|
+
|
|
365
|
+
```cpy
|
|
366
|
+
# 32-bit to 64-bit conversion (always safe)
|
|
367
|
+
int x = 42
|
|
368
|
+
int64 large_x = x # Automatic promotion
|
|
369
|
+
|
|
370
|
+
# 64-bit to 32-bit conversion (potential overflow)
|
|
371
|
+
int64 large_value = 9223372036854775807
|
|
372
|
+
int reduced = (int)large_value # Explicit cast required
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
#### Implicit Type Promotion
|
|
376
|
+
|
|
377
|
+
The compiler automatically promotes types in mixed expressions:
|
|
378
|
+
|
|
379
|
+
```cpy
|
|
380
|
+
# int to int64 promotion
|
|
381
|
+
int small = 42
|
|
382
|
+
int64 large = 1000000000000
|
|
383
|
+
int64 result = small + large # small is automatically promoted to int64
|
|
384
|
+
|
|
385
|
+
# int64 to uint64 conversion (allowed)
|
|
386
|
+
int64 signed_val = -100
|
|
387
|
+
uint64 unsigned_val = signed_val # Implicit conversion allowed
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
#### Overflow Handling
|
|
391
|
+
|
|
392
|
+
```cpy
|
|
393
|
+
# 64-bit arithmetic overflow
|
|
394
|
+
int64 max_int64 = 9223372036854775807
|
|
395
|
+
int64 overflow_result = max_int64 + 1 # Implementation-defined behavior
|
|
396
|
+
|
|
397
|
+
# Unsigned overflow (wraps around)
|
|
398
|
+
uint64 max_uint64 = 18446744073709551615
|
|
399
|
+
uint64 wrapped = max_uint64 + 1 # Wraps to 0
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
### Memory Operations with 64-bit Types
|
|
403
|
+
|
|
404
|
+
#### 64-bit Pointers
|
|
405
|
+
|
|
406
|
+
```cpy
|
|
407
|
+
# 64-bit pointer operations
|
|
408
|
+
int64* ptr_to_large = new int64
|
|
409
|
+
*ptr_to_large = 9223372036854775807
|
|
410
|
+
|
|
411
|
+
uint64* ptr_to_unsigned = new uint64
|
|
412
|
+
*ptr_to_unsigned = 0xFFFFFFFFFFFFFFFF
|
|
413
|
+
|
|
414
|
+
# Pointer arithmetic on 64-bit types
|
|
415
|
+
int64* array_start = new int64[1000]
|
|
416
|
+
int64* fifth_element = array_start + 4
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
#### 64-bit Array Allocation
|
|
420
|
+
|
|
421
|
+
```cpy
|
|
422
|
+
# Large array allocation with 64-bit indexing
|
|
423
|
+
int64[] large_array = new int64[1000000]
|
|
424
|
+
uint64[] huge_array = new uint64[10000000]
|
|
425
|
+
|
|
426
|
+
# 64-bit array indexing
|
|
427
|
+
int64 index = 500000000
|
|
428
|
+
int64 value = large_array[index]
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
### Platform-Specific Considerations
|
|
432
|
+
|
|
433
|
+
#### Data Model
|
|
434
|
+
|
|
435
|
+
Cpy follows the LP64 data model on 64-bit platforms:
|
|
436
|
+
|
|
437
|
+
- `int`: 32 bits
|
|
438
|
+
- `long`: 64 bits (when available)
|
|
439
|
+
- `pointer`: 64 bits
|
|
440
|
+
- `int64`: 64 bits
|
|
441
|
+
- `uint64`: 64 bits
|
|
442
|
+
|
|
443
|
+
#### Endianness
|
|
444
|
+
|
|
445
|
+
Cpy supports both little-endian and big-endian architectures, with automatic handling based on the target platform:
|
|
446
|
+
|
|
447
|
+
```cpy
|
|
448
|
+
# Platform-independent byte order handling
|
|
449
|
+
uint64 value = 0x123456789ABCDEF0
|
|
450
|
+
# Compiler handles endianness conversion automatically
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
### Performance Considerations
|
|
454
|
+
|
|
455
|
+
#### Native 64-bit Operations
|
|
456
|
+
|
|
457
|
+
Modern 64-bit processors provide native support for 64-bit operations:
|
|
458
|
+
|
|
459
|
+
```cpy
|
|
460
|
+
# 64-bit operations are typically single CPU instructions
|
|
461
|
+
int64 a = 9223372036854775807
|
|
462
|
+
int64 b = 1000000000
|
|
463
|
+
int64 result = a + b # Single instruction on 64-bit CPUs
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
#### Memory Bandwidth
|
|
467
|
+
|
|
468
|
+
64-bit types can improve memory bandwidth utilization:
|
|
469
|
+
|
|
470
|
+
```cpy
|
|
471
|
+
# Processing 64-bit values can be more efficient
|
|
472
|
+
int64[] data = new int64[1000]
|
|
473
|
+
# Each memory operation transfers 8 bytes
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
### Use Cases for 64-bit Types
|
|
477
|
+
|
|
478
|
+
#### File System Operations
|
|
479
|
+
|
|
480
|
+
```cpy
|
|
481
|
+
# Large file handling
|
|
482
|
+
int64 file_size = get_file_size("large_file.dat")
|
|
483
|
+
int64 position = seek_file(file_handle, 9223372036854775807)
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
#### Database Operations
|
|
487
|
+
|
|
488
|
+
```cpy
|
|
489
|
+
# 64-bit primary keys
|
|
490
|
+
int64 user_id = 1234567890123456789
|
|
491
|
+
int64 transaction_id = generate_unique_id()
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
#### Cryptographic Operations
|
|
495
|
+
|
|
496
|
+
```cpy
|
|
497
|
+
# 64-bit blocks for cryptographic operations
|
|
498
|
+
uint64 block = 0x123456789ABCDEF0
|
|
499
|
+
uint64 encrypted = encrypt_block(block)
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
#### High-Precision Timestamps
|
|
503
|
+
|
|
504
|
+
```cpy
|
|
505
|
+
# Nanosecond precision timestamps
|
|
506
|
+
int64 timestamp_ns = get_current_time_ns()
|
|
507
|
+
int64 elapsed = timestamp_ns - start_time
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
#### Memory Addressing
|
|
511
|
+
|
|
512
|
+
```cpy
|
|
513
|
+
# Direct memory addressing
|
|
514
|
+
uint64 address = 0x123456789ABC
|
|
515
|
+
uint64* memory_ptr = (uint64*)address
|
|
516
|
+
uint64 value = *memory_ptr
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
### Compatibility and Portability
|
|
520
|
+
|
|
521
|
+
#### Cross-Platform 64-bit Support
|
|
522
|
+
|
|
523
|
+
Cpy's 64-bit support is consistent across platforms:
|
|
524
|
+
|
|
525
|
+
- **Linux x86_64**: Full 64-bit support
|
|
526
|
+
- **macOS ARM64/x86_64**: Full 64-bit support
|
|
527
|
+
- **Windows x64**: Full 64-bit support
|
|
528
|
+
|
|
529
|
+
#### ABI Compatibility
|
|
530
|
+
|
|
531
|
+
Cpy maintains ABI compatibility with C 64-bit types:
|
|
532
|
+
|
|
533
|
+
```cpy
|
|
534
|
+
# Compatible with C long long and unsigned long long
|
|
535
|
+
import "c_library.c"
|
|
536
|
+
int64 result = c_function_64bit(9223372036854775807)
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
### Best Practices for 64-bit Programming
|
|
540
|
+
|
|
541
|
+
#### Type Selection Guidelines
|
|
542
|
+
|
|
543
|
+
1. **Use `int` for general-purpose arithmetic** when values fit in 32-bit range
|
|
544
|
+
2. **Use `int64` for large values** that may exceed 32-bit limits
|
|
545
|
+
3. **Use `uint64` for unsigned operations** and bit manipulation
|
|
546
|
+
4. **Avoid mixing signed/unsigned** without explicit conversions
|
|
547
|
+
|
|
548
|
+
#### Overflow Prevention
|
|
549
|
+
|
|
550
|
+
```cpy
|
|
551
|
+
# Check for potential overflow before operations
|
|
552
|
+
public safe_add_64(a: int64, b: int64) -> int64:
|
|
553
|
+
if a > 0 and b > (9223372036854775807 - a):
|
|
554
|
+
print("Overflow detected")
|
|
555
|
+
return 0
|
|
556
|
+
return a + b
|
|
557
|
+
```
|
|
558
|
+
|
|
559
|
+
#### Memory Alignment
|
|
560
|
+
|
|
561
|
+
```cpy
|
|
562
|
+
# 64-bit types should be properly aligned
|
|
563
|
+
struct AlignedData:
|
|
564
|
+
int32 small_value
|
|
565
|
+
int32 padding # Ensure 64-bit alignment
|
|
566
|
+
int64 large_value
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
### 64-bit Literal Syntax
|
|
570
|
+
|
|
571
|
+
#### Decimal Literals
|
|
572
|
+
|
|
573
|
+
```cpy
|
|
574
|
+
int64 large_decimal = 9223372036854775807
|
|
575
|
+
uint64 unsigned_decimal = 18446744073709551615
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
#### Hexadecimal Literals
|
|
579
|
+
|
|
580
|
+
```cpy
|
|
581
|
+
int64 hex_max = 0x7FFFFFFFFFFFFFFF
|
|
582
|
+
uint64 hex_unsigned_max = 0xFFFFFFFFFFFFFFFF
|
|
583
|
+
int64 hex_value = 0x123456789ABCDEF0
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
#### Binary Literals (if supported)
|
|
587
|
+
|
|
588
|
+
```cpy
|
|
589
|
+
int64 binary_max = 0b0111111111111111111111111111111111111111111111111111111111111111
|
|
590
|
+
```
|
|
591
|
+
|
|
592
|
+
### 64-bit in Standard Library
|
|
593
|
+
|
|
594
|
+
#### 64-bit I/O Operations
|
|
595
|
+
|
|
596
|
+
```cpy
|
|
597
|
+
# Print 64-bit values
|
|
598
|
+
print(9223372036854775807) # Handles 64-bit integers automatically
|
|
599
|
+
print(0xFFFFFFFFFFFFFFFF) # Handles 64-bit hexadecimal
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
#### 64-bit Math Functions
|
|
603
|
+
|
|
604
|
+
```cpy
|
|
605
|
+
# 64-bit mathematical operations
|
|
606
|
+
int64 absolute = abs_64(-9223372036854775807)
|
|
607
|
+
int64 minimum = min_64(a, b)
|
|
608
|
+
int64 maximum = max_64(a, b)
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
### Debugging 64-bit Code
|
|
612
|
+
|
|
613
|
+
#### Common Issues
|
|
614
|
+
|
|
615
|
+
1. **Silent overflow**: 64-bit overflow may not be immediately apparent
|
|
616
|
+
2. **Sign extension**: Accidental sign extension in mixed-type operations
|
|
617
|
+
3. **Endianness issues**: When working with binary data
|
|
618
|
+
4. **Alignment problems**: Misaligned 64-bit accesses on some platforms
|
|
619
|
+
|
|
620
|
+
#### Debugging Techniques
|
|
621
|
+
|
|
622
|
+
```cpy
|
|
623
|
+
# Use printf-style debugging for 64-bit values
|
|
624
|
+
int64 value = 9223372036854775807
|
|
625
|
+
print("Value: %lld", value) # Platform-specific format specifier
|
|
626
|
+
|
|
627
|
+
# Boundary testing
|
|
628
|
+
int64 test_max = 9223372036854775807
|
|
629
|
+
int64 test_min = -9223372036854775808
|
|
630
|
+
print("Max int64: ", test_max)
|
|
631
|
+
print("Min int64: ", test_min)
|
|
632
|
+
```
|
|
633
|
+
|
|
634
|
+
---
|
|
635
|
+
|
|
636
|
+
## Expressions and Operators
|
|
637
|
+
|
|
638
|
+
### Operator Precedence
|
|
639
|
+
|
|
640
|
+
Operators are evaluated in the following order (highest to lowest):
|
|
641
|
+
|
|
642
|
+
1. **Primary expressions**: literals, identifiers, parenthesized expressions
|
|
643
|
+
2. **Postfix operators**: `()`, `[]`, `.`, `->`
|
|
644
|
+
3. **Unary operators**: `*`, `&`, `+`, `-`, `~`, `not`
|
|
645
|
+
4. **Exponentiation**: `**`
|
|
646
|
+
5. **Multiplicative**: `*`, `/`, `//`, `%`
|
|
647
|
+
6. **Additive**: `+`, `-`
|
|
648
|
+
7. **Shift**: `<<`, `>>`
|
|
649
|
+
8. **Relational**: `<`, `>`, `<=`, `>=`
|
|
650
|
+
9. **Equality**: `==`, `!=`
|
|
651
|
+
10. **Bitwise AND**: `&`
|
|
652
|
+
11. **Bitwise XOR**: `^`
|
|
653
|
+
12. **Bitwise OR**: `|`
|
|
654
|
+
13. **Logical AND**: `and`
|
|
655
|
+
14. **Logical OR**: `or`
|
|
656
|
+
|
|
657
|
+
### Arithmetic Expressions
|
|
658
|
+
|
|
659
|
+
```cpy
|
|
660
|
+
# Basic arithmetic
|
|
661
|
+
result = 10 + 5 * 2 # 20 (multiplication has higher precedence)
|
|
662
|
+
division = 20 / 4 # 5.0 (float division)
|
|
663
|
+
int_div = 20 // 4 # 5 (integer division)
|
|
664
|
+
modulo = 17 % 5 # 2
|
|
665
|
+
power = 2 ** 10 # 1024
|
|
666
|
+
|
|
667
|
+
# Compound assignment
|
|
668
|
+
x += 5 # x = x + 5
|
|
669
|
+
x -= 3 # x = x - 3
|
|
670
|
+
x *= 2 # x = x * 2
|
|
671
|
+
x /= 4 # x = x / 4
|
|
672
|
+
```
|
|
673
|
+
|
|
674
|
+
### Comparison Expressions
|
|
675
|
+
|
|
676
|
+
```cpy
|
|
677
|
+
# Equality and inequality
|
|
678
|
+
if x == y:
|
|
679
|
+
print("equal")
|
|
680
|
+
if x != y:
|
|
681
|
+
print("not equal")
|
|
682
|
+
|
|
683
|
+
# Relational comparisons
|
|
684
|
+
if x < y:
|
|
685
|
+
print("less than")
|
|
686
|
+
if x <= y:
|
|
687
|
+
print("less than or equal")
|
|
688
|
+
if x > y:
|
|
689
|
+
print("greater than")
|
|
690
|
+
if x >= y:
|
|
691
|
+
print("greater than or equal")
|
|
692
|
+
```
|
|
693
|
+
|
|
694
|
+
### Logical Expressions
|
|
695
|
+
|
|
696
|
+
```cpy
|
|
697
|
+
# Logical operators
|
|
698
|
+
if condition1 and condition2:
|
|
699
|
+
print("both true")
|
|
700
|
+
|
|
701
|
+
if condition1 or condition2:
|
|
702
|
+
print("at least one true")
|
|
703
|
+
|
|
704
|
+
if not condition:
|
|
705
|
+
print("condition is false")
|
|
706
|
+
```
|
|
707
|
+
|
|
708
|
+
### Bitwise Expressions
|
|
709
|
+
|
|
710
|
+
```cpy
|
|
711
|
+
# Bitwise operations
|
|
712
|
+
a = 0b1010 # Binary literal
|
|
713
|
+
b = 0b1100
|
|
714
|
+
|
|
715
|
+
result = a & b # Bitwise AND: 0b1000
|
|
716
|
+
result = a | b # Bitwise OR: 0b1110
|
|
717
|
+
result = a ^ b # Bitwise XOR: 0b0110
|
|
718
|
+
result = ~a # Bitwise NOT: 0b0101
|
|
719
|
+
result = a << 2 # Left shift: 0b101000
|
|
720
|
+
result = a >> 1 # Right shift: 0b101
|
|
721
|
+
```
|
|
722
|
+
|
|
723
|
+
### Memory Operations
|
|
724
|
+
|
|
725
|
+
```cpy
|
|
726
|
+
# Address-of operator
|
|
727
|
+
int x = 42
|
|
728
|
+
int* ptr = &x # ptr now holds the address of x
|
|
729
|
+
|
|
730
|
+
# Dereference operator
|
|
731
|
+
print(*ptr) # Prints 42 (value at address stored in ptr)
|
|
732
|
+
*ptr = 99 # Sets x to 99 through pointer
|
|
733
|
+
|
|
734
|
+
# Pointer-to-pointer
|
|
735
|
+
int** pptr = &ptr
|
|
736
|
+
print(**pptr) # Prints 99
|
|
737
|
+
```
|
|
738
|
+
|
|
739
|
+
### String Operations
|
|
740
|
+
|
|
741
|
+
```cpy
|
|
742
|
+
# String concatenation
|
|
743
|
+
str greeting = "Hello"
|
|
744
|
+
str target = "World"
|
|
745
|
+
str message = greeting + ", " + target # "Hello, World"
|
|
746
|
+
|
|
747
|
+
# String indexing (returns char)
|
|
748
|
+
first_char = message[0] # 'H'
|
|
749
|
+
```
|
|
750
|
+
|
|
751
|
+
### Sizeof Operator
|
|
752
|
+
|
|
753
|
+
```cpy
|
|
754
|
+
# Get size of types
|
|
755
|
+
int_size = sizeof(int) # Size of integer type
|
|
756
|
+
struct_size = sizeof(Point) # Size of custom structure
|
|
757
|
+
```
|
|
758
|
+
|
|
759
|
+
### New Expression (Memory Allocation)
|
|
760
|
+
|
|
761
|
+
```cpy
|
|
762
|
+
# Allocate single object
|
|
763
|
+
int* ptr = new int # Allocate memory for one integer
|
|
764
|
+
Point* p = new Point # Allocate memory for Point structure
|
|
765
|
+
|
|
766
|
+
# Allocate array
|
|
767
|
+
int[] arr = new int[10] # Allocate array of 10 integers
|
|
768
|
+
Point[] points = new Point[5] # Allocate array of 5 Points
|
|
769
|
+
```
|
|
770
|
+
|
|
771
|
+
---
|
|
772
|
+
|
|
773
|
+
## Statements and Control Flow
|
|
774
|
+
|
|
775
|
+
### Expression Statements
|
|
776
|
+
|
|
777
|
+
Any expression can be a statement:
|
|
778
|
+
|
|
779
|
+
```cpy
|
|
780
|
+
x = 42
|
|
781
|
+
print(x)
|
|
782
|
+
function_call(arg1, arg2)
|
|
783
|
+
```
|
|
784
|
+
|
|
785
|
+
### Variable Declarations
|
|
786
|
+
|
|
787
|
+
```cpy
|
|
788
|
+
# Simple declaration
|
|
789
|
+
int x
|
|
790
|
+
str name
|
|
791
|
+
|
|
792
|
+
# Declaration with initialization
|
|
793
|
+
int age = 25
|
|
794
|
+
str city = "New York"
|
|
795
|
+
|
|
796
|
+
# Multiple declarations
|
|
797
|
+
int a, b, c
|
|
798
|
+
```
|
|
799
|
+
|
|
800
|
+
### Assignment Statements
|
|
801
|
+
|
|
802
|
+
```cpy
|
|
803
|
+
# Simple assignment
|
|
804
|
+
x = 10
|
|
805
|
+
|
|
806
|
+
# Assignment with compound operators
|
|
807
|
+
x += 5
|
|
808
|
+
x *= 2
|
|
809
|
+
|
|
810
|
+
# Member assignment
|
|
811
|
+
point.x = 15
|
|
812
|
+
point.y = 20
|
|
813
|
+
|
|
814
|
+
# Array element assignment
|
|
815
|
+
arr[0] = 42
|
|
816
|
+
arr[5] = 100
|
|
817
|
+
```
|
|
818
|
+
|
|
819
|
+
### Conditional Statements
|
|
820
|
+
|
|
821
|
+
#### If-Else Statement
|
|
822
|
+
|
|
823
|
+
```cpy
|
|
824
|
+
if condition:
|
|
825
|
+
print("condition is true")
|
|
826
|
+
elif other_condition:
|
|
827
|
+
print("other condition is true")
|
|
828
|
+
else:
|
|
829
|
+
print("no condition matched")
|
|
830
|
+
```
|
|
831
|
+
|
|
832
|
+
#### Switch Statement
|
|
833
|
+
|
|
834
|
+
```cpy
|
|
835
|
+
switch value:
|
|
836
|
+
case 1:
|
|
837
|
+
print("one")
|
|
838
|
+
case 2:
|
|
839
|
+
print("two")
|
|
840
|
+
default:
|
|
841
|
+
print("other")
|
|
842
|
+
```
|
|
843
|
+
|
|
844
|
+
### Loop Statements
|
|
845
|
+
|
|
846
|
+
#### While Loop
|
|
847
|
+
|
|
848
|
+
```cpy
|
|
849
|
+
while condition:
|
|
850
|
+
print("loop body")
|
|
851
|
+
# Update condition to avoid infinite loop
|
|
852
|
+
```
|
|
853
|
+
|
|
854
|
+
#### For Loop
|
|
855
|
+
|
|
856
|
+
```cpy
|
|
857
|
+
for item in collection:
|
|
858
|
+
print(item)
|
|
859
|
+
```
|
|
860
|
+
|
|
861
|
+
### Jump Statements
|
|
862
|
+
|
|
863
|
+
#### Break Statement
|
|
864
|
+
|
|
865
|
+
```cpy
|
|
866
|
+
while true:
|
|
867
|
+
if condition:
|
|
868
|
+
break # Exit the loop
|
|
869
|
+
```
|
|
870
|
+
|
|
871
|
+
#### Continue Statement
|
|
872
|
+
|
|
873
|
+
```cpy
|
|
874
|
+
for i in range(10):
|
|
875
|
+
if i % 2 == 0:
|
|
876
|
+
continue # Skip even numbers
|
|
877
|
+
print(i)
|
|
878
|
+
```
|
|
879
|
+
|
|
880
|
+
#### Return Statement
|
|
881
|
+
|
|
882
|
+
```cpy
|
|
883
|
+
public calculate(x: int, y: int) -> int:
|
|
884
|
+
return x + y
|
|
885
|
+
```
|
|
886
|
+
|
|
887
|
+
### Block Statements
|
|
888
|
+
|
|
889
|
+
Blocks are defined by indentation:
|
|
890
|
+
|
|
891
|
+
```cpy
|
|
892
|
+
public example():
|
|
893
|
+
int x = 10
|
|
894
|
+
# This is a block
|
|
895
|
+
if x > 5:
|
|
896
|
+
int y = 20
|
|
897
|
+
# Nested block
|
|
898
|
+
print(x + y)
|
|
899
|
+
```
|
|
900
|
+
|
|
901
|
+
---
|
|
902
|
+
|
|
903
|
+
## Functions
|
|
904
|
+
|
|
905
|
+
### Function Declaration
|
|
906
|
+
|
|
907
|
+
Functions are declared using the `def` keyword with optional access modifiers:
|
|
908
|
+
|
|
909
|
+
```cpy
|
|
910
|
+
public function_name(param1: int, param2: str) -> int:
|
|
911
|
+
# Function body
|
|
912
|
+
return 0
|
|
913
|
+
```
|
|
914
|
+
|
|
915
|
+
### Access Modifiers
|
|
916
|
+
|
|
917
|
+
```cpy
|
|
918
|
+
# Public function (can be called from outside)
|
|
919
|
+
public public_function() -> int:
|
|
920
|
+
return 42
|
|
921
|
+
|
|
922
|
+
# Private function (internal use only)
|
|
923
|
+
private helper_function() -> int:
|
|
924
|
+
return 0
|
|
925
|
+
|
|
926
|
+
# Static function (class-level)
|
|
927
|
+
static static_function() -> int:
|
|
928
|
+
return 1
|
|
929
|
+
```
|
|
930
|
+
|
|
931
|
+
### Function Parameters
|
|
932
|
+
|
|
933
|
+
```cpy
|
|
934
|
+
# Parameters with type annotations
|
|
935
|
+
public process_numbers(a: int, b: int, c: float) -> float:
|
|
936
|
+
return a + b + c
|
|
937
|
+
|
|
938
|
+
# Default parameters (if supported)
|
|
939
|
+
public greet(name: str = "World") -> str:
|
|
940
|
+
return "Hello, " + name
|
|
941
|
+
```
|
|
942
|
+
|
|
943
|
+
### Return Types
|
|
944
|
+
|
|
945
|
+
```cpy
|
|
946
|
+
# Explicit return type
|
|
947
|
+
public add(a: int, b: int) -> int:
|
|
948
|
+
return a + b
|
|
949
|
+
|
|
950
|
+
# Void return (if supported)
|
|
951
|
+
public print_message(msg: str):
|
|
952
|
+
print(msg)
|
|
953
|
+
```
|
|
954
|
+
|
|
955
|
+
### Function Calls
|
|
956
|
+
|
|
957
|
+
```cpy
|
|
958
|
+
# Basic function call
|
|
959
|
+
result = add(10, 20)
|
|
960
|
+
|
|
961
|
+
# Call with named parameters (if supported)
|
|
962
|
+
result = process_numbers(a=5, b=10, c=2.5)
|
|
963
|
+
|
|
964
|
+
# Method call on object
|
|
965
|
+
object.method(arg1, arg2)
|
|
966
|
+
```
|
|
967
|
+
|
|
968
|
+
### Recursive Functions
|
|
969
|
+
|
|
970
|
+
```cpy
|
|
971
|
+
public factorial(n: int) -> int:
|
|
972
|
+
if n <= 1:
|
|
973
|
+
return 1
|
|
974
|
+
return n * factorial(n - 1)
|
|
975
|
+
```
|
|
976
|
+
|
|
977
|
+
---
|
|
978
|
+
|
|
979
|
+
## Structures and User-Defined Types
|
|
980
|
+
|
|
981
|
+
### Structure Definition
|
|
982
|
+
|
|
983
|
+
Structures are defined using the `struct` keyword:
|
|
984
|
+
|
|
985
|
+
```cpy
|
|
986
|
+
struct Point:
|
|
987
|
+
int x
|
|
988
|
+
int y
|
|
989
|
+
```
|
|
990
|
+
|
|
991
|
+
### Structure Instantiation
|
|
992
|
+
|
|
993
|
+
```cpy
|
|
994
|
+
Point p
|
|
995
|
+
p.x = 10
|
|
996
|
+
p.y = 20
|
|
997
|
+
```
|
|
998
|
+
|
|
999
|
+
### Nested Structures
|
|
1000
|
+
|
|
1001
|
+
```cpy
|
|
1002
|
+
struct Line:
|
|
1003
|
+
Point start
|
|
1004
|
+
Point end
|
|
1005
|
+
|
|
1006
|
+
Line line
|
|
1007
|
+
line.start.x = 0
|
|
1008
|
+
line.start.y = 0
|
|
1009
|
+
line.end.x = 100
|
|
1010
|
+
line.end.y = 100
|
|
1011
|
+
```
|
|
1012
|
+
|
|
1013
|
+
### Generic Structures
|
|
1014
|
+
|
|
1015
|
+
Cpy supports generic programming with parameterized types:
|
|
1016
|
+
|
|
1017
|
+
```cpy
|
|
1018
|
+
struct Pair<T, U>:
|
|
1019
|
+
T first
|
|
1020
|
+
U second
|
|
1021
|
+
|
|
1022
|
+
Pair<int, str> pair
|
|
1023
|
+
pair.first = 42
|
|
1024
|
+
pair.second = "answer"
|
|
1025
|
+
```
|
|
1026
|
+
|
|
1027
|
+
### Structure Methods
|
|
1028
|
+
|
|
1029
|
+
```cpy
|
|
1030
|
+
struct Point:
|
|
1031
|
+
int x
|
|
1032
|
+
int y
|
|
1033
|
+
|
|
1034
|
+
public add(other: Point) -> Point:
|
|
1035
|
+
Point result
|
|
1036
|
+
result.x = self.x + other.x
|
|
1037
|
+
result.y = self.y + other.y
|
|
1038
|
+
return result
|
|
1039
|
+
```
|
|
1040
|
+
|
|
1041
|
+
### Self-Referential Structures
|
|
1042
|
+
|
|
1043
|
+
```cpy
|
|
1044
|
+
struct ListNode:
|
|
1045
|
+
int value
|
|
1046
|
+
ListNode* next
|
|
1047
|
+
```
|
|
1048
|
+
|
|
1049
|
+
---
|
|
1050
|
+
|
|
1051
|
+
## Memory Management
|
|
1052
|
+
|
|
1053
|
+
### Stack Allocation
|
|
1054
|
+
|
|
1055
|
+
Regular variables are allocated on the stack:
|
|
1056
|
+
|
|
1057
|
+
```cpy
|
|
1058
|
+
int x = 42 # Stack allocation
|
|
1059
|
+
Point p # Stack allocation
|
|
1060
|
+
p.x = 10
|
|
1061
|
+
p.y = 20
|
|
1062
|
+
```
|
|
1063
|
+
|
|
1064
|
+
### Heap Allocation
|
|
1065
|
+
|
|
1066
|
+
Use the `new` operator for heap allocation:
|
|
1067
|
+
|
|
1068
|
+
```cpy
|
|
1069
|
+
# Single object allocation
|
|
1070
|
+
int* ptr = new int
|
|
1071
|
+
*ptr = 42
|
|
1072
|
+
|
|
1073
|
+
# Array allocation
|
|
1074
|
+
int[] arr = new int[10]
|
|
1075
|
+
arr[0] = 1
|
|
1076
|
+
arr[9] = 10
|
|
1077
|
+
```
|
|
1078
|
+
|
|
1079
|
+
### Memory Operations
|
|
1080
|
+
|
|
1081
|
+
```cpy
|
|
1082
|
+
# Address-of operator
|
|
1083
|
+
int x = 42
|
|
1084
|
+
int* ptr = &x
|
|
1085
|
+
|
|
1086
|
+
# Dereference operator
|
|
1087
|
+
print(*ptr) # 42
|
|
1088
|
+
*ptr = 99 # Modify value through pointer
|
|
1089
|
+
|
|
1090
|
+
# Pointer arithmetic (if supported)
|
|
1091
|
+
int* arr = new int[10]
|
|
1092
|
+
int* second = arr + 1 # Point to second element
|
|
1093
|
+
```
|
|
1094
|
+
|
|
1095
|
+
### Memory Safety Considerations
|
|
1096
|
+
|
|
1097
|
+
Cpy provides manual memory management, which requires careful attention to:
|
|
1098
|
+
|
|
1099
|
+
- **Memory leaks**: Always free allocated memory when no longer needed
|
|
1100
|
+
- **Dangling pointers**: Avoid using pointers to freed memory
|
|
1101
|
+
- **Buffer overflows**: Ensure array accesses are within bounds
|
|
1102
|
+
- **Null pointer dereferences**: Check pointers before dereferencing
|
|
1103
|
+
|
|
1104
|
+
---
|
|
1105
|
+
|
|
1106
|
+
## C Interoperability
|
|
1107
|
+
|
|
1108
|
+
### Importing Cpy Module Files
|
|
1109
|
+
|
|
1110
|
+
Cpy files can import other `.cpy` files, making all `public` functions and struct definitions available to the importer:
|
|
1111
|
+
|
|
1112
|
+
```cpy
|
|
1113
|
+
import "math_utils.cpy"
|
|
1114
|
+
import "data_structs.cpy"
|
|
1115
|
+
```
|
|
1116
|
+
|
|
1117
|
+
Only functions marked with the `public` keyword are exported. Imported functions and structs are inlined into the same LLVM module, ensuring cross-module optimization is preserved.
|
|
1118
|
+
|
|
1119
|
+
```cpy
|
|
1120
|
+
# math_utils.cpy
|
|
1121
|
+
public def add(a int, b int) -> int:
|
|
1122
|
+
return a + b
|
|
1123
|
+
|
|
1124
|
+
private def helper():
|
|
1125
|
+
pass
|
|
1126
|
+
```
|
|
1127
|
+
|
|
1128
|
+
```cpy
|
|
1129
|
+
# main.cpy
|
|
1130
|
+
import "math_utils.cpy"
|
|
1131
|
+
|
|
1132
|
+
def main():
|
|
1133
|
+
int result = add(3, 4) # calls public function from math_utils.cpy
|
|
1134
|
+
print(result)
|
|
1135
|
+
```
|
|
1136
|
+
|
|
1137
|
+
### Importing C Libraries
|
|
1138
|
+
|
|
1139
|
+
Cpy can import C libraries directly:
|
|
1140
|
+
|
|
1141
|
+
```cpy
|
|
1142
|
+
import "math_library.c"
|
|
1143
|
+
import "string_operations.c"
|
|
1144
|
+
```
|
|
1145
|
+
|
|
1146
|
+
### Calling C Functions
|
|
1147
|
+
|
|
1148
|
+
```cpy
|
|
1149
|
+
# After importing a C library
|
|
1150
|
+
result = c_function(arg1, arg2)
|
|
1151
|
+
```
|
|
1152
|
+
|
|
1153
|
+
### C Type Mapping
|
|
1154
|
+
|
|
1155
|
+
Cpy types map to C types as follows:
|
|
1156
|
+
|
|
1157
|
+
- `int` → `int` or `long` (platform-dependent)
|
|
1158
|
+
- `int64` (Planned) → `long long` or `int64_t`
|
|
1159
|
+
- `uint64` (Planned) → `unsigned long long` or `uint64_t`
|
|
1160
|
+
- `float` → `double`
|
|
1161
|
+
- `str` → `char*`
|
|
1162
|
+
- `int*` → `int*`
|
|
1163
|
+
- `int64*` (Planned) → `long long*` or `int64_t*`
|
|
1164
|
+
- `uint64*` (Planned) → `unsigned long long*` or `uint64_t*`
|
|
1165
|
+
- `void*` → `void*`
|
|
1166
|
+
|
|
1167
|
+
### Example: C Library Integration
|
|
1168
|
+
|
|
1169
|
+
```cpy
|
|
1170
|
+
import "test_import.c"
|
|
1171
|
+
|
|
1172
|
+
def main():
|
|
1173
|
+
int x
|
|
1174
|
+
x = my_add(3, 4) # Call C function
|
|
1175
|
+
print(x)
|
|
1176
|
+
x = my_double(10) # Call another C function
|
|
1177
|
+
print(x)
|
|
1178
|
+
```
|
|
1179
|
+
|
|
1180
|
+
---
|
|
1181
|
+
|
|
1182
|
+
## Compilation Model
|
|
1183
|
+
|
|
1184
|
+
### Compilation Pipeline
|
|
1185
|
+
|
|
1186
|
+
Cpy source code goes through several compilation stages:
|
|
1187
|
+
|
|
1188
|
+
1. **Lexical Analysis**: Source code is tokenized
|
|
1189
|
+
2. **Syntax Analysis**: Tokens are parsed into an Abstract Syntax Tree (AST)
|
|
1190
|
+
3. **Semantic Analysis**: Type checking and scope validation, including 64-bit type safety
|
|
1191
|
+
4. **LLVM IR Generation**: AST is converted to LLVM Intermediate Representation with 64-bit support
|
|
1192
|
+
5. **Optimization**: LLVM optimization passes improve performance, including 64-bit specific optimizations
|
|
1193
|
+
6. **Code Generation**: Native machine code is generated for 64-bit architectures
|
|
1194
|
+
|
|
1195
|
+
### Compiler Invocation
|
|
1196
|
+
|
|
1197
|
+
The Cpy compiler can be invoked with different optimization levels:
|
|
1198
|
+
|
|
1199
|
+
```bash
|
|
1200
|
+
# Compile with default optimization
|
|
1201
|
+
./program source.cpy
|
|
1202
|
+
|
|
1203
|
+
# Compile with specific optimization level
|
|
1204
|
+
./program source.cpy -O2
|
|
1205
|
+
```
|
|
1206
|
+
|
|
1207
|
+
### JIT Compilation
|
|
1208
|
+
|
|
1209
|
+
Cpy supports Just-In-Time compilation for rapid development:
|
|
1210
|
+
|
|
1211
|
+
```python
|
|
1212
|
+
# Python example of JIT execution
|
|
1213
|
+
from source.compiling import run_jit
|
|
1214
|
+
|
|
1215
|
+
run_jit(module, opt_level=3)
|
|
1216
|
+
```
|
|
1217
|
+
|
|
1218
|
+
### AOT Compilation
|
|
1219
|
+
|
|
1220
|
+
Ahead-Of-Time compilation produces optimized native binaries:
|
|
1221
|
+
|
|
1222
|
+
```python
|
|
1223
|
+
# Python example of AOT compilation
|
|
1224
|
+
from source.compiling import run_aot
|
|
1225
|
+
|
|
1226
|
+
run_aot(module, output="program.o", opt_level=3)
|
|
1227
|
+
```
|
|
1228
|
+
|
|
1229
|
+
### Optimization Levels
|
|
1230
|
+
|
|
1231
|
+
- `-O0`: No optimization (fast compilation, slow execution)
|
|
1232
|
+
- `-O1`: Basic optimization
|
|
1233
|
+
- `-O2`: Standard optimization (recommended)
|
|
1234
|
+
- `-O3`: Aggressive optimization (slowest compilation, fastest execution)
|
|
1235
|
+
|
|
1236
|
+
---
|
|
1237
|
+
|
|
1238
|
+
## Standard Library
|
|
1239
|
+
|
|
1240
|
+
### I/O Operations
|
|
1241
|
+
|
|
1242
|
+
#### Print Function
|
|
1243
|
+
|
|
1244
|
+
```cpy
|
|
1245
|
+
print(42) # Print integer
|
|
1246
|
+
print(3.14) # Print float
|
|
1247
|
+
print("Hello") # Print string
|
|
1248
|
+
print(x + y) # Print expression result
|
|
1249
|
+
```
|
|
1250
|
+
|
|
1251
|
+
#### Input Function
|
|
1252
|
+
|
|
1253
|
+
```cpy
|
|
1254
|
+
int value = input() # Read integer from standard input
|
|
1255
|
+
```
|
|
1256
|
+
|
|
1257
|
+
### Mathematical Operations
|
|
1258
|
+
|
|
1259
|
+
The language supports basic mathematical operations through operators and potentially standard library functions.
|
|
1260
|
+
|
|
1261
|
+
### String Operations
|
|
1262
|
+
|
|
1263
|
+
```cpy
|
|
1264
|
+
# String concatenation
|
|
1265
|
+
str result = "Hello" + " " + "World"
|
|
1266
|
+
|
|
1267
|
+
# String length (through C library)
|
|
1268
|
+
int len = strlen(string_ptr)
|
|
1269
|
+
```
|
|
1270
|
+
|
|
1271
|
+
---
|
|
1272
|
+
|
|
1273
|
+
## Advanced Features
|
|
1274
|
+
|
|
1275
|
+
### Access Control
|
|
1276
|
+
|
|
1277
|
+
```cpy
|
|
1278
|
+
# Public members accessible from anywhere
|
|
1279
|
+
public public_var = 42
|
|
1280
|
+
|
|
1281
|
+
# Private members accessible only within the same scope
|
|
1282
|
+
private private_var = 10
|
|
1283
|
+
```
|
|
1284
|
+
|
|
1285
|
+
### Static Members
|
|
1286
|
+
|
|
1287
|
+
```cpy
|
|
1288
|
+
# Static members belong to the type rather than instances
|
|
1289
|
+
static class_variable = 100
|
|
1290
|
+
```
|
|
1291
|
+
|
|
1292
|
+
### Virtual Functions
|
|
1293
|
+
|
|
1294
|
+
```cpy
|
|
1295
|
+
# Virtual functions enable runtime polymorphism
|
|
1296
|
+
virtual override function_name() -> int:
|
|
1297
|
+
return 0
|
|
1298
|
+
```
|
|
1299
|
+
|
|
1300
|
+
### References
|
|
1301
|
+
|
|
1302
|
+
The `ref` keyword may be used for reference semantics (implementation-specific):
|
|
1303
|
+
|
|
1304
|
+
```cpy
|
|
1305
|
+
def process(ref value: int):
|
|
1306
|
+
value = 42 # Modifies original
|
|
1307
|
+
```
|
|
1308
|
+
|
|
1309
|
+
---
|
|
1310
|
+
|
|
1311
|
+
## Best Practices
|
|
1312
|
+
|
|
1313
|
+
### Memory Management
|
|
1314
|
+
|
|
1315
|
+
1. **Always initialize pointers**: Uninitialized pointers can cause undefined behavior
|
|
1316
|
+
2. **Free allocated memory**: Prevent memory leaks by releasing heap allocations
|
|
1317
|
+
3. **Check for null**: Validate pointers before dereferencing
|
|
1318
|
+
4. **Use stack allocation when possible**: Stack allocation is faster and automatic
|
|
1319
|
+
|
|
1320
|
+
### Type Safety
|
|
1321
|
+
|
|
1322
|
+
1. **Use type annotations**: Explicit types improve code clarity and safety
|
|
1323
|
+
2. **Enable strict type checking**: Catch type errors at compile time
|
|
1324
|
+
3. **Avoid implicit conversions**: Use explicit type conversions when needed
|
|
1325
|
+
|
|
1326
|
+
### Performance
|
|
1327
|
+
|
|
1328
|
+
1. **Prefer stack allocation**: Stack allocation is faster than heap allocation
|
|
1329
|
+
2. **Use appropriate optimization levels**: Balance compilation time and runtime performance
|
|
1330
|
+
3. **Minimize pointer indirection**: Direct access is faster than pointer dereferencing
|
|
1331
|
+
4. **Leverage 64-bit operations**: Use 64-bit types when appropriate for better memory bandwidth utilization
|
|
1332
|
+
5. **Consider alignment**: Ensure 64-bit types are properly aligned for optimal performance
|
|
1333
|
+
|
|
1334
|
+
### Code Organization
|
|
1335
|
+
|
|
1336
|
+
1. **Use meaningful names**: Improve code readability with descriptive identifiers
|
|
1337
|
+
2. **Keep functions small**: Single responsibility functions are easier to maintain
|
|
1338
|
+
3. **Organize structures logically**: Group related data in structures
|
|
1339
|
+
4. **Document complex logic**: Use comments to explain non-obvious code
|
|
1340
|
+
|
|
1341
|
+
---
|
|
1342
|
+
|
|
1343
|
+
## Example Programs
|
|
1344
|
+
|
|
1345
|
+
### Hello World
|
|
1346
|
+
|
|
1347
|
+
```cpy
|
|
1348
|
+
public main() -> int:
|
|
1349
|
+
print("Hello, World!")
|
|
1350
|
+
return 0
|
|
1351
|
+
```
|
|
1352
|
+
|
|
1353
|
+
### Factorial Calculation
|
|
1354
|
+
|
|
1355
|
+
```cpy
|
|
1356
|
+
public factorial(n: int) -> int:
|
|
1357
|
+
if n <= 1:
|
|
1358
|
+
return 1
|
|
1359
|
+
return n * factorial(n - 1)
|
|
1360
|
+
|
|
1361
|
+
public main() -> int:
|
|
1362
|
+
int result = factorial(5)
|
|
1363
|
+
print(result) # 120
|
|
1364
|
+
return 0
|
|
1365
|
+
```
|
|
1366
|
+
|
|
1367
|
+
### Linked List Implementation
|
|
1368
|
+
|
|
1369
|
+
```cpy
|
|
1370
|
+
struct ListNode:
|
|
1371
|
+
int value
|
|
1372
|
+
ListNode* next
|
|
1373
|
+
|
|
1374
|
+
public create_list(n: int) -> ListNode*:
|
|
1375
|
+
ListNode* head = 0
|
|
1376
|
+
ListNode* current = 0
|
|
1377
|
+
int i = 0
|
|
1378
|
+
|
|
1379
|
+
while i < n:
|
|
1380
|
+
ListNode* node = new ListNode
|
|
1381
|
+
node.value = i * 10
|
|
1382
|
+
node.next = 0
|
|
1383
|
+
|
|
1384
|
+
if head == 0:
|
|
1385
|
+
head = node
|
|
1386
|
+
else:
|
|
1387
|
+
current.next = node
|
|
1388
|
+
|
|
1389
|
+
current = node
|
|
1390
|
+
i = i + 1
|
|
1391
|
+
|
|
1392
|
+
return head
|
|
1393
|
+
|
|
1394
|
+
public sum_list(head: ListNode*) -> int:
|
|
1395
|
+
int total = 0
|
|
1396
|
+
ListNode* current = head
|
|
1397
|
+
|
|
1398
|
+
while current != 0:
|
|
1399
|
+
total = total + current.value
|
|
1400
|
+
current = current.next
|
|
1401
|
+
|
|
1402
|
+
return total
|
|
1403
|
+
|
|
1404
|
+
public main() -> int:
|
|
1405
|
+
ListNode* list = create_list(5)
|
|
1406
|
+
int total = sum_list(list)
|
|
1407
|
+
print(total) # 100 (0 + 10 + 20 + 30 + 40)
|
|
1408
|
+
return 0
|
|
1409
|
+
```
|
|
1410
|
+
|
|
1411
|
+
### Matrix Operations
|
|
1412
|
+
|
|
1413
|
+
```cpy
|
|
1414
|
+
struct Matrix:
|
|
1415
|
+
int[][] data
|
|
1416
|
+
int rows
|
|
1417
|
+
int cols
|
|
1418
|
+
|
|
1419
|
+
public create_matrix(rows: int, cols: int) -> Matrix:
|
|
1420
|
+
Matrix m
|
|
1421
|
+
m.rows = rows
|
|
1422
|
+
m.cols = cols
|
|
1423
|
+
m.data = new int[rows][cols]
|
|
1424
|
+
return m
|
|
1425
|
+
|
|
1426
|
+
public matrix_multiply(a: Matrix, b: Matrix) -> Matrix:
|
|
1427
|
+
if a.cols != b.rows:
|
|
1428
|
+
return 0 # Error: incompatible dimensions
|
|
1429
|
+
|
|
1430
|
+
Matrix result = create_matrix(a.rows, b.cols)
|
|
1431
|
+
|
|
1432
|
+
int i = 0
|
|
1433
|
+
while i < a.rows:
|
|
1434
|
+
int j = 0
|
|
1435
|
+
while j < b.cols:
|
|
1436
|
+
int sum = 0
|
|
1437
|
+
int k = 0
|
|
1438
|
+
while k < a.cols:
|
|
1439
|
+
sum = sum + a.data[i][k] * b.data[k][j]
|
|
1440
|
+
k = k + 1
|
|
1441
|
+
result.data[i][j] = sum
|
|
1442
|
+
j = j + 1
|
|
1443
|
+
i = i + 1
|
|
1444
|
+
|
|
1445
|
+
return result
|
|
1446
|
+
```
|
|
1447
|
+
|
|
1448
|
+
### 64-bit File Processing (Planned)
|
|
1449
|
+
|
|
1450
|
+
```cpy
|
|
1451
|
+
public process_large_file(filename: str) -> int64:
|
|
1452
|
+
int64 total_bytes = 0
|
|
1453
|
+
int64 buffer_size = 65536 # 64KB buffer
|
|
1454
|
+
int64[] buffer = new int64[buffer_size / 8]
|
|
1455
|
+
|
|
1456
|
+
# Simulate file processing
|
|
1457
|
+
int64 chunk = 0
|
|
1458
|
+
while chunk < 1000000: # Simulate 1GB file
|
|
1459
|
+
total_bytes = total_bytes + buffer_size
|
|
1460
|
+
chunk = chunk + 1
|
|
1461
|
+
|
|
1462
|
+
return total_bytes
|
|
1463
|
+
|
|
1464
|
+
public main() -> int:
|
|
1465
|
+
int64 file_size = process_large_file("large_file.dat")
|
|
1466
|
+
print("Total bytes processed: ")
|
|
1467
|
+
print(file_size)
|
|
1468
|
+
return 0
|
|
1469
|
+
```
|
|
1470
|
+
|
|
1471
|
+
### 64-bit Cryptographic Hash Example (Planned)
|
|
1472
|
+
|
|
1473
|
+
```cpy
|
|
1474
|
+
public simple_hash_64(data: str) -> uint64:
|
|
1475
|
+
uint64 hash = 0xFFFFFFFFFFFFFFFF
|
|
1476
|
+
int64 length = strlen(data)
|
|
1477
|
+
int64 i = 0
|
|
1478
|
+
|
|
1479
|
+
while i < length:
|
|
1480
|
+
uint64 byte = (uint64)data[i]
|
|
1481
|
+
hash = hash ^ byte
|
|
1482
|
+
hash = hash * 0x100000001B3
|
|
1483
|
+
i = i + 1
|
|
1484
|
+
|
|
1485
|
+
return hash
|
|
1486
|
+
|
|
1487
|
+
public main() -> int:
|
|
1488
|
+
str message = "Hello, World!"
|
|
1489
|
+
uint64 hash_value = simple_hash_64(message)
|
|
1490
|
+
print("Hash value: ")
|
|
1491
|
+
print(hash_value)
|
|
1492
|
+
return 0
|
|
1493
|
+
```
|
|
1494
|
+
|
|
1495
|
+
### 64-bit Timestamp Processing (Planned)
|
|
1496
|
+
|
|
1497
|
+
```cpy
|
|
1498
|
+
public timestamp_to_days(timestamp_ns: int64) -> int64:
|
|
1499
|
+
int64 ns_per_day = 86400000000000 # 24 * 60 * 60 * 1e9
|
|
1500
|
+
return timestamp_ns / ns_per_day
|
|
1501
|
+
|
|
1502
|
+
public main() -> int:
|
|
1503
|
+
int64 current_time = 1699999999999999999 # Example timestamp
|
|
1504
|
+
int64 days = timestamp_to_days(current_time)
|
|
1505
|
+
print("Days since epoch: ")
|
|
1506
|
+
print(days)
|
|
1507
|
+
return 0
|
|
1508
|
+
```
|
|
1509
|
+
|
|
1510
|
+
---
|
|
1511
|
+
|
|
1512
|
+
## Language Limitations and Future Directions
|
|
1513
|
+
|
|
1514
|
+
### Current Limitations
|
|
1515
|
+
|
|
1516
|
+
1. **Limited standard library**: Basic I/O and mathematical operations
|
|
1517
|
+
2. **Manual memory management**: No garbage collection
|
|
1518
|
+
3. **No exception handling**: Error handling through return codes
|
|
1519
|
+
4. **Limited generics**: Basic generic type support
|
|
1520
|
+
5. **No modules system**: All code in single file or C imports
|
|
1521
|
+
6. **64-bit support**: 64-bit integer types (`int64`, `uint64`) are planned but not yet implemented
|
|
1522
|
+
|
|
1523
|
+
### Potential Future Enhancements
|
|
1524
|
+
|
|
1525
|
+
1. **Enhanced standard library**: More comprehensive built-in functions
|
|
1526
|
+
2. **Optional garbage collection**: Hybrid memory management
|
|
1527
|
+
3. **Exception handling**: Structured error handling mechanism
|
|
1528
|
+
4. **Advanced generics**: More sophisticated generic programming
|
|
1529
|
+
5. **Module system**: Better code organization and reuse
|
|
1530
|
+
6. **Concurrency primitives**: Threads, async/await, etc.
|
|
1531
|
+
7. **Standard library containers**: Lists, dictionaries, sets, etc.
|
|
1532
|
+
8. **64-bit integer support**: Implementation of `int64` and `uint64` types
|
|
1533
|
+
|
|
1534
|
+
---
|
|
1535
|
+
|
|
1536
|
+
## Conclusion
|
|
1537
|
+
|
|
1538
|
+
Cpy represents a thoughtful balance between high-level language ergonomics and low-level system control. Its Python-inspired syntax makes it accessible to developers familiar with modern scripting languages, while its C-like capabilities provide the performance and control needed for systems programming.
|
|
1539
|
+
|
|
1540
|
+
The language is particularly well-suited for:
|
|
1541
|
+
|
|
1542
|
+
- **Systems programming**: Operating systems, drivers, embedded systems
|
|
1543
|
+
- **Performance-critical applications**: Game engines, scientific computing
|
|
1544
|
+
- **C library integration**: Wrapping existing C libraries with modern syntax
|
|
1545
|
+
- **Education**: Teaching systems programming concepts with approachable syntax
|
|
1546
|
+
|
|
1547
|
+
As the language continues to evolve, it has the potential to become a compelling alternative to traditional systems programming languages while maintaining the productivity that modern developers expect.
|
|
1548
|
+
|
|
1549
|
+
---
|
|
1550
|
+
|
|
1551
|
+
## Appendix: Complete Grammar Reference
|
|
1552
|
+
|
|
1553
|
+
### Formal Grammar (Simplified)
|
|
1554
|
+
|
|
1555
|
+
```
|
|
1556
|
+
program ::= {declaration | function | struct_def}
|
|
1557
|
+
declaration ::= type IDENTIFIER ["=" expression]
|
|
1558
|
+
function ::= ACCESS_MODIFIER? "def" IDENTIFIER "(" parameters ")" ["->" type] block
|
|
1559
|
+
struct_def ::= "struct" IDENTIFIER [generic_params] ":" {member_decl}
|
|
1560
|
+
block ::= NEWLINE INDENT statement DEDENT
|
|
1561
|
+
statement ::= expression_stmt | if_stmt | while_stmt | for_stmt | return_stmt
|
|
1562
|
+
expression_stmt ::= expression
|
|
1563
|
+
if_stmt ::= "if" expression block {"elif" expression block} ["else" block]
|
|
1564
|
+
while_stmt ::= "while" expression block
|
|
1565
|
+
for_stmt ::= "for" IDENTIFIER "in" expression block
|
|
1566
|
+
return_stmt ::= "return" [expression]
|
|
1567
|
+
expression ::= assignment
|
|
1568
|
+
assignment ::= logical_or (ASSIGN_OP assignment)?
|
|
1569
|
+
logical_or ::= logical_and {"or" logical_and}
|
|
1570
|
+
logical_and ::= equality {"and" equality}
|
|
1571
|
+
equality ::= relational {("==" | "!=") relational}
|
|
1572
|
+
relational ::= shift {("<" | ">" | "<=" | ">=") shift}
|
|
1573
|
+
shift ::= additive {("<<" | ">>") additive}
|
|
1574
|
+
additive ::= multiplicative {("+" | "-") multiplicative}
|
|
1575
|
+
multiplicative ::= power {("*" | "/" | "//" | "%") power}
|
|
1576
|
+
power ::= unary ["**" power]
|
|
1577
|
+
unary ::= ("+" | "-" | "not" | "~" | "*" | "&") unary | postfix
|
|
1578
|
+
postfix ::= primary {("()" | "[]" | "." IDENTIFIER)}
|
|
1579
|
+
primary ::= NUMBER | STRING | IDENTIFIER | "(" expression ")" | "new" type | "sizeof" "(" type ")"
|
|
1580
|
+
type ::= "int" | "float" | "str" | "bool" | type "*" | type "[]"
|
|
1581
|
+
```
|
|
1582
|
+
|
|
1583
|
+
**Note:** The grammar shown above represents the currently implemented language. 64-bit types (`int64`, `uint64`) and hexadecimal literals are planned additions.
|
|
1584
|
+
|
|
1585
|
+
### Token Specifications
|
|
1586
|
+
|
|
1587
|
+
```
|
|
1588
|
+
NUMBER ::= [0-9]+ ("." [0-9]+)? ([eE][+-]?[0-9]+)?
|
|
1589
|
+
STRING ::= '"' ([^"\\] | "\\" .)* '"'
|
|
1590
|
+
IDENTIFIER ::= [a-zA-Z_] [a-zA-Z0-9_]*
|
|
1591
|
+
```
|
|
1592
|
+
|
|
1593
|
+
**Planned additions:**
|
|
1594
|
+
- `HEX_NUMBER ::= "0x" [0-9A-Fa-f]+` (for 64-bit hexadecimal literals)
|
|
1595
|
+
|
|
1596
|
+
---
|
|
1597
|
+
|
|
1598
|
+
*This documentation covers the Cpy language as implemented in the current codebase. Features may vary based on the specific version and implementation details.*
|