Update README.md and add examples/ directory

This commit is contained in:
germax26 2024-11-21 10:09:43 +11:00
parent 689034a522
commit eba473c0d9
Signed by: germax26
SSH Key Fingerprint: SHA256:N3w+8798IMWBt7SYH8G1C0iJlIa2HIIcRCXwILT5FvM
4 changed files with 74 additions and 10 deletions

View File

@ -20,6 +20,15 @@ b: int = 0;
c: str; // declared, but not yet assigned
```
## Expressions
```
a: int = 34;
b: int = 35;
c: int = 1;
d: int = a + b * c; // with correct operator precedence
```
## Structs
```
@ -29,10 +38,10 @@ struct MyStruct {
field3: SomeOtherStruct
}
my_struct: MyStruct = MyStruct{
my_struct: MyStruct = MyStruct.{
field1=1,
field2="Hello, World!",
field3=SomeOtherStruct{...} // whatever SomeOtherStruct takes
field3=SomeOtherStruct.{...} // whatever SomeOtherStruct takes
};
my_struct.field1 = 2;
@ -55,12 +64,12 @@ enum Weekday {
day: Weekday = Weekday.Tuesday;
match day in {
case Monday do { ... }
case Tuesday do { ... }
case Wednesday do { ... }
case Thursday do { ... }
case Friday do { ... }
case _ do { ... } // default
case Monday { ... } // handle Monday
case Tuesday { ... } // handle Tuesday
case Wednesday { ... } // and so on...
case Thursday { ... }
case Friday { ... }
case _ { ... } // default
}
```
or
@ -73,8 +82,8 @@ enum IntResult {
result: IntResult = IntResult.Ok(5);
match result in {
case Ok(num) do { ... } // num is defined in this scope
case Err(err) do { ... } // similarly err is defined in this scope
case Ok(num) { ... } // num is defined in this scope
case Err(err) { ... } // similarly err is defined in this scope
}
```
Currently, polymorphic structs/enums are not supported, however I plan to eventually do so.
@ -98,10 +107,28 @@ the_min: int = min(5, 10);
## Arrays
```
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)
```
## For loops
```
// prints numbers 0-9
for i in range(0, 10) {
print(int_to_str(i)+"\n");
}
```
## While loops
a: int = 0;
while a < 10 {
print(int_to_str(a)+"\n");
a = a + 1;
}
## Reading files
## Importing

5
examples/numbers.ppp Normal file
View File

@ -0,0 +1,5 @@
numbers: int[] = [:int, 0, 1, 2, 3, 4, 5];
numbers = [:int, 2*x for x in numbers];
for number in numbers print(int_to_str(number)+"\n");

11
examples/person.ppp Normal file
View File

@ -0,0 +1,11 @@
struct Person {
age: int,
name: str
}
my_person: Person = Person.{
age=50,
name="Hello, World"
};
print(int_to_str(my_person.age)+" "+my_person.name+"\n");

21
examples/weekday.ppp Normal file
View File

@ -0,0 +1,21 @@
enum Weekday {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
day: Weekday = Weekday.Monday;
match day {
case Monday print("It's Monday!\n");
case Tuesday print("It's Tuesday!\n");
case Wednesday print("It's Wednesday!\n");
case Thursday print("It's Thursday!\n");
case Friday print("It's Friday!\n");
case Saturday print("It's Saturday!\n");
case Sunday print("It's Sunday!\n");
}