Added conditionals, padding and comments

WerWolv 2021-01-08 00:53:02 +01:00
parent 307b13b38c
commit b787d265fc
1 changed files with 64 additions and 1 deletions

@ -4,16 +4,35 @@ 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.
## Table of Contents
- [Comments](#comments)
- [Built-in Types](#built-in-types)
- [Variable Placements](#variable-placements)
- [Arrays](#arrays)
- [Structs](#structs)
- [Padding](#padding)
- [Unions](#unions)
- [Pointers](#pointers)
- [Enums](#enums)
- [Bitfields](#bitfields)
- [Mathematical Expressions](#mathematical-expressions)
## Comments
Comments are a simple way to add documentation or instructions for other developers to your code or to remove parts of it temporarily.
There are two styles of comments available: Single line comments and multi line comments.
Single line comments are started with `//` two forward slashes and will include everything after them until the next new line.
Multi line comments are started with `/*` a forward slash followed by a star and will include everything after them until a `*/` star followed by a forward slash are found. Multiple multiline comments cannot be nested.
```cpp
/* This is a
multi line
comment
*/
// This is a single line comment
```
## Built-in Types
Built-in types are the fundamental types used in the language. Supported are various unsigned types, signed types, floating point types as well as a few special types.
@ -81,6 +100,21 @@ This code will create a new type named `Header` which again may be placed at any
![Struct](https://puu.sh/H4LSb/071a6aacf4.png)
### Padding
If padding between members is needed, it may be manually inserted using the `padding` keyword.
```cpp
struct PaddedData {
u8 index;
padding[7];
u64 height;
u32 checksum;
};
```
This will create a 7 byte gap between the `index` and `height` member which will not be displayed in the Pattern Data View
## Unions
Syntactically, unions look and work exactly the same as structs. The difference however is that all members are placed at the same address on top of each other in contrast to the struct where all members are placed after each other (The same as in C/C++). Therefore the size of the union will be the size of the biggest member within in union.
@ -240,3 +274,32 @@ 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.
## Conditionals
Sometimes structs may have different members depending on some condition. This is where `if`, `else` and `else if` statements come into play.
Inside structs and unions, these may be used to only evaluate certain members if some condition is met.
```cpp
enum Type : u8 {
Height,
PValue,
IValue,
DValue
};
struct Message {
Type type;
if (type == Type::Height) {
u8 index;
u32 height;
}
else if (type == PValue || type == IValue)
float value;
else if (type == DValue)
double value;
};
Message messages[10] @ 0x100;
```