Skip to content

Commit c32a52f

Browse files
committed
Rationalised imaginary number constant suffixes, started documentation
1 parent 45327b3 commit c32a52f

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

docs/SOUL_Language.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ The primitive types currently supported are:
147147
- `float32` - 32-bit float type
148148
- `float64` - 64-bit float type
149149
- `float` - fastest floating-point type that provides at least 32-bit precision
150+
- `complex32` - 32-bit complex type
151+
- `complex64` - 64-bit complex type
152+
- `complex` - fastest complex type that provides at least 32-bit precision
150153
- `wrap<size>` - a range-constrained `int32` type. The `size` value must be a compile-time constant, and operations on this type are guaranteed to result in values where 0 <= x < size. If incrementing or decrementing it, out-of-range values will wrap around.
151154
- `clamp<size>` - similar to `wrap<size>` but out-of-range values are clamped rather than wrapped
152155
- `string` - a character literal. These can be passed around but no operations which could incur runtime allocation are possible (i.e. concatenation, etc), and slicing/indexing is not supported - the main purpose of string literals and this type is for use in debug logging functions.
@@ -167,6 +170,8 @@ SOUL largely follows familiar C/Java/Javascript conventions for numeric values:
167170
For a 32-bit float, you can use either `f` or `f32` as the suffix, e.g. `123.0f` or `123.0f32`.
168171
For 64-bit floats, either write it without a suffix, or use `f64`, `123.0` or `123.0f64`
169172
For readability, you can also add an underscore before any of these suffixes, e.g. `123.0_f64`
173+
- ##### Complex numbers
174+
Complex numbers are made from a real and imaginary component. The real component uses the same syntax as the assocated floating point formats (so `f` or `f32` to indicate a 32 bit floating point value) whilst imaginary numbers are specified using the `fi` or `f32i` suffix for 32 bit imaginary components, or `i` or `f64i` for 64 bit imaginary components. Complex constants with both real and imaginary components can be declared by adding these components together, for example `1.2f + 3.4fi`
170175
- ##### Boolean
171176
`true` and `false` are built-in keywords
172177

@@ -307,6 +312,20 @@ Dynamic slices are handled internally as a "fat pointer", i.e. a pointer + size,
307312

308313
Slices may be left uninitialised, in which case they have a size of zero and any access will return a value of 0 (in whatever element type they're using).
309314

315+
#### Complex components
316+
317+
As complex numbers are primitives, vectors and arrays of complex numbers can be constructed. The real and imaginary components of the complex number can be retrieved using the `.real` and `.imag` accessors respectively. For a complex vector, these accessors will return a vector of real or imaginary components.
318+
319+
```C++
320+
let c = 1.0f + 2.0fi; // c is a complex32 with real: 1.0f, imag: 2.0f
321+
let d = c * c; // d is a complex32 with real: -3.0f, imag: 4.0f
322+
let e = d.real; // e is a float32 with value -3.0f
323+
let f = d.imag; // f is a float32 with value 4.0f
324+
325+
complex64<4> g = 1.0; // vector of 4 complex64 with real: 1.0, imag: 0.0
326+
let h = g.real; // vector of 4 float64 all with value 1.0
327+
```
328+
310329
#### 'External' variables
311330

312331
Sometimes a processor needs to randomly-access large blocks of read-only data (e.g. for audio samples, etc), or to allow the runtime API to provide other constants which aren't part of the source code. To allow the app to get this data across to the SOUL program, the `external` qualifier can be added to a variable.

source/modules/soul_core/utilities/soul_Tokeniser.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,10 @@ struct Tokeniser
348348

349349
TokenType parseSuffixForFloatLiteral()
350350
{
351-
if (input.advanceIfStartsWith ("if", "fi")) return Token::literalImag32;
352-
if (input.advanceIfStartsWith ("i")) return Token::literalImag64;
353-
if (input.advanceIfStartsWith ("f64", "_f64")) return Token::literalFloat64;
354-
if (input.advanceIfStartsWith ("f32", "_f32", "f", "_f")) return Token::literalFloat32;
351+
if (input.advanceIfStartsWith ("f32i", "_f32i", "fi")) return Token::literalImag32;
352+
if (input.advanceIfStartsWith ("f64i", "_f64i", "i")) return Token::literalImag64;
353+
if (input.advanceIfStartsWith ("f64", "_f64")) return Token::literalFloat64;
354+
if (input.advanceIfStartsWith ("f32", "_f32", "f", "_f")) return Token::literalFloat32;
355355

356356
return Token::literalFloat64;
357357
}

0 commit comments

Comments
 (0)