Skip to content

Commit

Permalink
More stuff in internals documentation.
Browse files Browse the repository at this point in the history
- Type "Function" renamed to "func", because until the future new
  version of Primi, this type did not even in fact exist, so whatever.
- The same with type "Module" now being named "module.
  • Loading branch information
smuuf committed Mar 27, 2022
1 parent 5f01401 commit e7e017a
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 30 deletions.
38 changes: 28 additions & 10 deletions docs/internals.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
# Primi internals and PHP API

## Terminology
## Basic types
_Basic types_ in Primi are those types that have objects _(instances)_ implemented directly via their own PHP class (e.g. `StringValue` PHP class).

### List of basic types
- `null` represented by `\Smuuf\Primi\Values\NullValue`.
- `bool` represented by `\Smuuf\Primi\Values\BoolValue`.
- `number` represented by `\Smuuf\Primi\Values\NumberValue`.
- `string` represented by `\Smuuf\Primi\Values\StringValue`.
- `list` represented by `\Smuuf\Primi\Values\ListValue`.
- `dict` represented by `\Smuuf\Primi\Values\DictValue`.
- `tuple` represented by `\Smuuf\Primi\Values\TupleValue`.
- `type` represented by `\Smuuf\Primi\Values\TypeValue`.
- `regex` represented by `\Smuuf\Primi\Values\RegexValue`.
- `module` represented by `\Smuuf\Primi\Values\ModuleValue`.
- `func` represented by `\Smuuf\Primi\Values\FuncValue`.
- `iteratorfactory` represented by `\Smuuf\Primi\Values\IteratorFactoryValue`.

## Other terminology
- `Couple`: A tuple with two items _(also `2-tuple`)_. Inside Primi internals this usually describes a PHP array with two items, without explicitly specified indices.
- For example this is a `couple`:
```php
["value A", "value B"]
```
- `Pair`: A mapping key-value pair. Inside Primi internals this usually describes what ``
- For example this code yields a key-value pair:
- `Pair`: A mapping/key-value pair.
- For example a generator would yield a key-value pair:
```php
yield $key => $value;
```
or
```php
[
$keyA => $valueA, // This is pair.
$keyB => $valueB, // This is another pair.
];
```
- Or some PHP array can represent key-value pairs:
```php
[
$keyA => $valueA, // This is pair.
$keyB => $valueB, // This is another pair.
...
];
```
14 changes: 7 additions & 7 deletions example/functions.primi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function add(x, y) {
assert(c == 100, 'Declaring variable inside function block overwrites the variable from outer scope')
return x + y
}
assert(type(add) == types.Function, 'Declaring ordinary function')
assert(type(add) == types.func, 'Declaring ordinary function')

_ = "Function with parameters cannot be called without parameters"
assert_error(() => {
Expand All @@ -21,14 +21,14 @@ assert_error(() => {
function sub(x, y) {
return x - y
}
assert(type(sub) == types.Function, 'Declaring ordinary function')
assert(type(sub) == types.func, 'Declaring ordinary function')

a = 1 + add(sub(4, 3), 5)
assert(a == 7, 'Passing result of function as argument into another function yields correct result')

function nested(a) {
assert(type(add) == types.Function, 'Function block inherits functions from outer scope')
assert(type(sub) == types.Function, 'Function block inherits functions from outer scope')
assert(type(add) == types.func, 'Function block inherits functions from outer scope')
assert(type(sub) == types.func, 'Function block inherits functions from outer scope')
return add(a, sub(5, 2))
}

Expand All @@ -48,7 +48,7 @@ double_nested = (input, fn) => {
return [a, counter]

}
assert(type(double_nested) == types.Function)
assert(type(double_nested) == types.func)

result = double_nested(4, nested)
assert(result[0] == -5, "'a' variable")
Expand All @@ -60,7 +60,7 @@ decorator = (fn) => {
return "<prefix>" + fn(arg) + "<suffix>"
}
}
assert(type(decorator) == types.Function)
assert(type(decorator) == types.func)

function reverser(text) {
result = ""
Expand All @@ -69,7 +69,7 @@ function reverser(text) {
}
return result
}
assert(type(reverser) == types.Function)
assert(type(reverser) == types.func)

reversed = reverser('JELEN')
assert(reversed == 'NELEJ')
Expand Down
2 changes: 1 addition & 1 deletion example/type.list.primi
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ for (item in shuffled) {
//

assert([-1, 0, 2].map(bool) == [true, false, true])
assert([len, type, 1, true].map(type) == [types.Function, type, number, bool])
assert([len, type, 1, true].map(type) == [types.func, type, number, bool])

//
// list.contains()
Expand Down
2 changes: 1 addition & 1 deletion example/type.module.primi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import std.math
import std.types
import ._helpers: assert_not

assert(type(types) == types.Module)
assert(type(types) == types.module)

_ = "Getting length of module (number of variables in its global scope) doesn't throw error"
len(types, _)
Expand Down
2 changes: 1 addition & 1 deletion src/Stdlib/Modules/std/types.primi.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function execute(Context $ctx): array {
TupleValue::TYPE => StaticTypes::getTupleType(),

// Other basic types (basic == they're implemented in PHP).
FuncValue::TYPE => StaticTypes::getFunctionType(),
FuncValue::TYPE => StaticTypes::getFuncType(),
IteratorFactoryValue::TYPE => StaticTypes::getIteratorFactoryType(),
ModuleValue::TYPE => StaticTypes::getModuleType(),

Expand Down
6 changes: 3 additions & 3 deletions src/Stdlib/StaticTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ abstract class StaticTypes {
private static TypeValue $listType;
private static TypeValue $dictType;
private static TypeValue $tupleType;
private static TypeValue $functionType;
private static TypeValue $funcType;
private static TypeValue $iteratorFactoryType;
private static TypeValue $moduleType;

Expand Down Expand Up @@ -99,8 +99,8 @@ public static function getTupleType(): TypeValue {
??= new TypeValue(TupleValue::TYPE, self::getObjectType(), TupleTypeExtension::execute());
}

public static function getFunctionType(): TypeValue {
return self::$functionType
public static function getFuncType(): TypeValue {
return self::$funcType
??= new TypeValue(FuncValue::TYPE, self::getObjectType(), ForbiddenTypeExtension::execute());
}

Expand Down
4 changes: 2 additions & 2 deletions src/Values/FuncValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
class FuncValue extends AbstractNativeValue {

public const TYPE = "Function";
public const TYPE = "func";
private ?CallArgs $partialArgs = \null;

public function __construct(
Expand All @@ -27,7 +27,7 @@ public function __construct(
}

public function getType(): TypeValue {
return StaticTypes::getFunctionType();
return StaticTypes::getFuncType();
}

public function isTruthy(): bool {
Expand Down
2 changes: 1 addition & 1 deletion src/Values/ModuleValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
class ModuleValue extends AbstractNativeValue {

public const TYPE = "Module";
public const TYPE = "module";

/** Name of the module */
protected string $name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ assert(random_number_result == 4)
import std.math
import std.types

assert(type(math.sin) == types.Function)
assert(type(math.sin) == types.func)
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ function random_number() {
import std.types
import std.math

assert(type(math.sin) == types.Function)
assert(type(math.sin) == types.func)
2 changes: 1 addition & 1 deletion tests/integration/importing/project_1/main.primi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import std.types
// Absolute import - top-level package cannot have any other.
import module_a

assert(type(module_a) == types.Module)
assert(type(module_a) == types.module)
assert(module_a.var == 'module_a')

// Absolute import - top-level package cannot have any other.
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/importing/project_2/entrypoint.primi
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ try {
assert(success, 'Relative import beyond top-level package results in error')

import std.math
assert(type(math.sin) == types.Function)
assert(type(math.sin) == types.func)

import external_lib
assert(external_lib.var == 'hello to external_lib library')

0 comments on commit e7e017a

Please sign in to comment.