2024-08-11 23:30:40 +10:00
|
|
|
# Python++
|
|
|
|
|
|
|
|
I created this language as a result of my frustrations with Python.
|
|
|
|
|
|
|
|
Currently my language is significantly worse than Python, and needs a lot of work before it can be considered functional.
|
|
|
|
|
|
|
|
## Hello World
|
|
|
|
|
|
|
|
```
|
|
|
|
print("Hello, World!\n");
|
|
|
|
```
|
|
|
|
|
|
|
|
The following is a non-comprehensive list of stuff that currently works.
|
|
|
|
|
|
|
|
## Assignment & Declaration
|
|
|
|
|
|
|
|
```
|
2024-08-11 23:33:56 +10:00
|
|
|
a: str = "Hello, World!\n";
|
2024-08-11 23:30:40 +10:00
|
|
|
b: int = 0;
|
2024-08-11 23:33:56 +10:00
|
|
|
c: str; // declared, but not yet assigned
|
2024-08-11 23:30:40 +10:00
|
|
|
```
|
|
|
|
|
2024-11-21 10:10:36 +11:00
|
|
|
## Expressions
|
|
|
|
|
|
|
|
```
|
|
|
|
a: int = 34;
|
|
|
|
b: int = 35;
|
|
|
|
c: int = 1;
|
|
|
|
d: int = a + b * c; // with correct operator precedence
|
|
|
|
```
|
|
|
|
|
2024-08-11 23:30:40 +10:00
|
|
|
## Structs
|
|
|
|
|
|
|
|
```
|
|
|
|
struct MyStruct {
|
|
|
|
field1: int,
|
|
|
|
field2: str,
|
|
|
|
field3: SomeOtherStruct
|
|
|
|
}
|
|
|
|
|
2024-11-21 10:10:36 +11:00
|
|
|
my_struct: MyStruct = MyStruct.{
|
2024-08-11 23:30:40 +10:00
|
|
|
field1=1,
|
|
|
|
field2="Hello, World!",
|
2024-11-21 10:10:36 +11:00
|
|
|
field3=SomeOtherStruct.{...} // whatever SomeOtherStruct takes
|
2024-08-11 23:30:40 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
my_struct.field1 = 2;
|
|
|
|
plus5: int = my_struct.field1 + 5;
|
|
|
|
```
|
|
|
|
|
|
|
|
## Enums
|
|
|
|
|
|
|
|
```
|
|
|
|
enum Weekday {
|
|
|
|
Monday,
|
|
|
|
Tuesday,
|
|
|
|
Wednesday,
|
|
|
|
Thursday,
|
|
|
|
Friday,
|
|
|
|
Saturday,
|
|
|
|
Sunday
|
|
|
|
}
|
|
|
|
|
|
|
|
day: Weekday = Weekday.Tuesday;
|
|
|
|
|
|
|
|
match day in {
|
2024-11-21 10:10:36 +11:00
|
|
|
case Monday { ... } // handle Monday
|
|
|
|
case Tuesday { ... } // handle Tuesday
|
|
|
|
case Wednesday { ... } // and so on...
|
|
|
|
case Thursday { ... }
|
|
|
|
case Friday { ... }
|
|
|
|
case _ { ... } // default
|
2024-08-11 23:30:40 +10:00
|
|
|
}
|
|
|
|
```
|
|
|
|
or
|
|
|
|
```
|
|
|
|
enum IntResult {
|
|
|
|
Ok(int),
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2024-08-11 23:33:56 +10:00
|
|
|
result: IntResult = IntResult.Ok(5);
|
2024-08-11 23:30:40 +10:00
|
|
|
|
|
|
|
match result in {
|
2024-11-21 10:10:36 +11:00
|
|
|
case Ok(num) { ... } // num is defined in this scope
|
|
|
|
case Err(err) { ... } // similarly err is defined in this scope
|
2024-08-11 23:30:40 +10:00
|
|
|
}
|
|
|
|
```
|
|
|
|
Currently, polymorphic structs/enums are not supported, however I plan to eventually do so.
|
|
|
|
|
|
|
|
## Functions
|
|
|
|
|
|
|
|
```
|
|
|
|
func random_number(seed: int) -> int {
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
|
2024-08-11 23:33:56 +10:00
|
|
|
my_random_number: int = random_number(69);
|
2024-08-11 23:30:40 +10:00
|
|
|
|
|
|
|
func min(a: int, b: int) -> int {
|
|
|
|
if a < b return a;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2024-08-11 23:33:56 +10:00
|
|
|
the_min: int = min(5, 10);
|
2024-08-11 23:30:40 +10:00
|
|
|
```
|
|
|
|
|
|
|
|
## Arrays
|
|
|
|
|
2024-11-21 10:10:36 +11:00
|
|
|
```
|
|
|
|
numbers: int[] = [:int, 0, 1, 2, 3, 4]; // array literal must state element type (for now)
|
|
|
|
numbers = [:int, 2*x for x in numbers]; // list comprehension must also (for now)
|
|
|
|
```
|
|
|
|
|
2024-08-11 23:30:40 +10:00
|
|
|
## For loops
|
|
|
|
|
2024-11-21 10:10:36 +11:00
|
|
|
```
|
|
|
|
// prints numbers 0-9
|
|
|
|
for i in range(0, 10) {
|
|
|
|
print(int_to_str(i)+"\n");
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-08-11 23:30:40 +10:00
|
|
|
## While loops
|
|
|
|
|
2024-11-21 10:10:36 +11:00
|
|
|
a: int = 0;
|
|
|
|
while a < 10 {
|
|
|
|
print(int_to_str(a)+"\n");
|
|
|
|
a = a + 1;
|
|
|
|
}
|
|
|
|
|
2024-08-11 23:30:40 +10:00
|
|
|
## Reading files
|
|
|
|
|
|
|
|
## Importing
|
|
|
|
|
|
|
|
## Assertions
|
|
|
|
|
|
|
|
## Tuples
|