Added using, pointers and mathematical expressions

WerWolv 2021-01-08 00:33:40 +01:00
parent 7d5b1be975
commit 307b13b38c
1 changed files with 98 additions and 1 deletions

@ -4,6 +4,15 @@ The Pattern Language is ImHex custom built programming language used to create b
This document is meant as an overview of all the features the Pattern Language has. This document is meant as an overview of all the features the Pattern Language has.
## Table of Contents ## Table of Contents
- [Built-in Types](#built-in-types)
- [Variable Placements](#variable-placements)
- [Arrays](#arrays)
- [Structs](#structs)
- [Unions](#unions)
- [Pointers](#pointers)
- [Enums](#enums)
- [Bitfields](#bitfields)
- [Mathematical Expressions](#mathematical-expressions)
## Built-in Types ## Built-in Types
@ -93,6 +102,28 @@ Color color @ 0x100;
![Union](https://puu.sh/H4LY2/0d40d2ac34.png) ![Union](https://puu.sh/H4LY2/0d40d2ac34.png)
## Pointers
A pointer is a member that points to another place in memory. It uses the value at its address as an offset from the start of the current data to find the location of the value that it points to.
To define a pointer, first the type of the value being pointed to is specified followed by a `*` star and the name of the variable. After the `:` colon, the size of the pointer is required. This needs to be an integral, built-in type which specifies what data gets interpreted as an offset.
```
<typeName> *<pointerName> : <builtinTypeName>;
//Example
struct Child {
u32 value;
};
struct Parent {
Child *child : u16;
};
Parent parent @ 0x200;
```
![Pointer](https://puu.sh/H4Nau/cc4b8bb2a3.png)
## Enums ## Enums
Enums are types whose value is restricted to a distinct number of values. When placed in memory, the Pattern Data View will show the relevant enum entry name instead of the numerical value. Enums are types whose value is restricted to a distinct number of values. When placed in memory, the Pattern Data View will show the relevant enum entry name instead of the numerical value.
@ -143,3 +174,69 @@ Permission perm @ 0x20;
``` ```
![Bitfields](https://puu.sh/H4MC9/24980dcaae.png) ![Bitfields](https://puu.sh/H4MC9/24980dcaae.png)
## Type Aliasing
To give an existing type a new name, a `using` declaration can be used. This will not replace the old name of the type with a new one, it will create a new type with a new name that is the same as the old type. Therefore both can be used afterwards.
```cpp
using <newTypeName> = <oldTypeName>;
// Example
using uint32_t = u32;
using Header = ElfHeader;
```
## Mathematical Expressions
In any place where a numeric value is required, a mathematical expression can be inserted. This can be as easy as `1 + 1` but can get much more complex as well by accessing values within structs or enum constants. These expressions work the same as in basically every other language as well with the following operators being supported:
- `a + b` : Addition
- `a - b` : Subtraction
- `a * b` : Multiplication
- `a / b` : Division
- `a >> b` : Bit shift left
- `a << b` : Bit shift right
- `a & b` : Bitwise AND
- `a | b` : Bitwise OR
- `a ^ b` : Bitwise XOR
- `a == b` : Equality comparison
- `a != b` : Inequality comparison
- `a > b` : Greater-than comparison
- `a >= b` : Greater-than-or-equals comparison
- `a < b` : Less-than comparison
- `a <= b` : Less-than-or-equals comparison
- `a && b` : Boolean AND
- `a || b` : Boolean OR
- `a ^^ b` : Boolean XOR
- `a ? b : c` : Ternary comparison
Additionally, variable names and the dot `.` operator may be used access the value of variables in these expressions.
```cpp
struct SubHeader {
u16 numEntries;
};
struct Entry {
// ...
};
struct Header {
u32 magic;
SubHeader subHeader;
Entry entries[subHeader.numEntries + 5];
};
```
To use constants in an expression, the `::` scope resolution operator can be used.
```cpp
enum Offsets {
Header = 0x00,
SectionList = 0x1000,
StringList = 0x5000
};
Section sections[10] @ Offsets::SectionList;
```
As seen above, this may be used to create arrays whose size depends on the value of other members and similar things.