Hello,
There are some updates on the project: right now I slowed down with Arduino/ESP-IDF/FreeRTOS bindings and trying
to make the language more PHP7/8-like.
Compiler changes:
- functions now can have a return type (e.g. :int or :mixed), like in modern php:
function Hello(int $arg) :int {
return $arg + 1;
}
Type hint for arguments (int) is an extension to the language, which performs automatic cast, so code inside function will be guaranteed to get an int and not what was passed
function Hello(int $arg) :int {
var_dump($arg); // <--- will display "int(666)"
return $arg + 1;
}
Hello('666');
Return type casting is not done yet, but i see how it could be done. Right now the code below compiles and works by ignoring 'int' type on 'return'; This is a bad behaviour and it will be fixed
Function return value is autocasted to its function type. Code below will return int(666), not string('666')
function func() :int {
return '666';
}
-
mixed type is recognized and compiled correctly. mixed type is a valid type for enum language construct unlike classic PHP
-
enum language construct has been added:
Standard PHP supports two types of enumerations: pure enums and backed enums.
A pure enum contains only named cases, for example
enum Status {
case Draft;
case Published;
case Archived;
}
//A backed enum additionally declares its type (e.g.`int` or `string`) and a //value for each case:
enum Status: string {
case Draft = 'draft';
case Published = 'published';
case Archived = 'archived';
}
```
PH7 implements an extended enum model. Enum cases may have values of different types, for example
enum Example {
case Number = 123;
case Text = 'hello';
case Flag = true;
case F = 66.6;
}
Cases without an explicitly assigned value support automatic numbering:
enum Example {
case A;
case B;
case C = 10;
case D;
}
where A, B, and D receive the values 0, 1, and 2, respectevely.
Typed enums are also supported:
enum Example: int {
case A;
case B;
case C = '123';
}
function test(Example $arg) {
var_dump($arg);
}
test(Example::C); // Displays: "int(123)"
Also comma operator is supported for enums with explicit values:
enum Name { // No type - ":mixed" is assuned, no autocasting for enum values
case A = 0, B= 1, C = 10;
case STR = 'String';
}
Enum values (and constants as well) can have complex initializer values:
case A = 'Hello' . ' ' . rand_str(3);
PS:
digitalWrite($gpio, 1);
digitalWrite($gpio, 0);
are showing ~300KHz in a tight loop on ESP32-S3. Didn't measure it on P4, but I think it should be 2x or more.