Skip to content

Commit b20328f

Browse files
authored
Merge pull request #21 from izaak/literal-suffix-followups
Adopt numeric literal suffixes in docs, tests and f64 helpers
2 parents dc9b0e6 + 29b1ccd commit b20328f

11 files changed

Lines changed: 91 additions & 72 deletions

File tree

benchmark/biquad_f64.lyte

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Biquad filter DSP benchmark
22
// Process 10 million samples through a lowpass filter
3-
// Double-precision version using f32 literals cast to f64.
3+
// Double-precision version
44

55
struct Biquad {
66
b0: f64, b1: f64, b2: f64,
@@ -11,28 +11,28 @@ struct Biquad {
1111

1212
// Design a 2nd-order lowpass (RBJ cookbook)
1313
lpf(fc: f64, fs: f64, q: f64) -> Biquad {
14-
var w0 = (2.0 as f64) * (3.14159265 as f64) * fc / fs
15-
var alpha = sin(w0) / ((2.0 as f64) * q)
14+
var w0 = 2.0f64 * 3.141592653589793f64 * fc / fs
15+
var alpha = sin(w0) / (2.0f64 * q)
1616
var cs = cos(w0)
1717

18-
var a0 = (1.0 as f64) + alpha
19-
var inv = (1.0 as f64) / a0
18+
var a0 = 1.0f64 + alpha
19+
var inv = 1.0f64 / a0
2020

2121
var bq: Biquad
22-
bq.b1 = ((1.0 as f64) - cs) * inv
23-
bq.b0 = bq.b1 / (2.0 as f64)
22+
bq.b1 = (1.0f64 - cs) * inv
23+
bq.b0 = bq.b1 / 2.0f64
2424
bq.b2 = bq.b0
25-
bq.a1 = ((0.0 as f64) - (2.0 as f64) * cs) * inv
26-
bq.a2 = ((1.0 as f64) - alpha) * inv
27-
bq.x1 = 0.0 as f64
28-
bq.x2 = 0.0 as f64
29-
bq.y1 = 0.0 as f64
30-
bq.y2 = 0.0 as f64
25+
bq.a1 = (0.0f64 - 2.0f64 * cs) * inv
26+
bq.a2 = (1.0f64 - alpha) * inv
27+
bq.x1 = 0.0f64
28+
bq.x2 = 0.0f64
29+
bq.y1 = 0.0f64
30+
bq.y2 = 0.0f64
3131
return bq
3232
}
3333

3434
main {
35-
var bq = lpf(1000.0 as f64, 44100.0 as f64, 0.707 as f64)
35+
var bq = lpf(1000.0f64, 44100.0f64, 0.707f64)
3636
var b0 = bq.b0
3737
var b1 = bq.b1
3838
var b2 = bq.b2
@@ -44,16 +44,16 @@ main {
4444
var y2 = bq.y2
4545

4646
var n = 10000000
47-
var phase = 0.0 as f64
48-
var freq = (440.0 as f64) / (44100.0 as f64)
49-
var last_y = 0.0 as f64
50-
var two_pi = (2.0 as f64) * (3.14159265 as f64)
47+
var phase = 0.0f64
48+
var freq = 440.0f64 / 44100.0f64
49+
var last_y = 0.0f64
50+
var two_pi = 2.0f64 * 3.141592653589793f64
5151

5252
// Process a 440 Hz sine wave through the filter
5353
for i in 0 .. n {
5454
var x = sin(phase * two_pi)
5555
phase = phase + freq
56-
if phase > (1.0 as f64) { phase = phase - (1.0 as f64) }
56+
if phase > 1.0f64 { phase = phase - 1.0f64 }
5757

5858
var y = b0 * x + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2
5959
x2 = x1

docs/GRAMMAR.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ digit = "0".."9" ;
1616
id = ( alpha ) { alpha | digit } ;
1717
1818
integer = digit { digit } ;
19-
uinteger = integer "u" ;
19+
int_lit = integer [ "i32" | "u32" | "u" ] ;
2020
real = digit { digit } "." digit { digit } ;
21+
real_lit = real [ "f32" | "f64" ] ;
2122
char_lit = "'" ( char | "\\" ( "\\" | "n" ) ) "'" ;
2223
string_lit = '"' { any - '"' } '"' ;
2324
@@ -90,7 +91,7 @@ postfix = atom { "(" [ exprlist ] ")" (* function call *)
9091
} ;
9192
9293
atom = id
93-
| integer | uinteger | real
94+
| int_lit | real_lit
9495
| char_lit | string_lit
9596
| "true" | "false"
9697
| "." id (* enum variant *)

docs/tutorial.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ Note for Lua users: Lua lets one variable hold different kinds of values over ti
7272

7373
| Type | What it is | Example values |
7474
|------|------------|----------------|
75-
| `i32` | A whole number (integer) | `42`, `-7`, `0` |
76-
| `f32` | A decimal number (float) | `3.14`, `-0.5`, `1.0` |
75+
| `i32` | A whole number (integer) | `42`, `-7`, `0`, `42i32` |
76+
| `f32` | A decimal number (float) | `3.14`, `-0.5`, `1.0`, `1.0f32` |
7777
| `bool` | True or false | `true`, `false` |
7878
| `str` | Text (a string in quotes) | `"hello"` |
7979
| `i8` | A very small whole number, used for individual characters | `65` (the letter `A`) |
80-
| `u32` | A whole number that cannot be negative (unsigned) | `42`, `0` |
80+
| `u32` | A whole number that cannot be negative (unsigned) | `42u32`, `42u`, `0u32` |
8181

8282
Single characters can also be written with single quotes: `'a'`, `'Z'`, `'\n'` (newline). This is called a character literal.
8383

@@ -103,6 +103,24 @@ In audio and DSP work, you will use `f32` constantly. Audio signals, frequencies
103103

104104
**A note on `f64`:** The language grammar also defines an `f64` type, which is a higher-precision decimal number. In Audulus DSP work, `f32` is the type you'll usually use.
105105

106+
By default, whole-number literals are `i32`, and decimal literals are `f32`.
107+
108+
### Numeric literal suffixes
109+
110+
Numeric literals can include a type suffix when you need the literal itself to have a specific type:
111+
112+
```lyte
113+
main {
114+
var count = 1i32
115+
var mask = 1u32
116+
var also_unsigned = 1u
117+
var gain = 1.0f32
118+
var precise = 1.0f64
119+
}
120+
```
121+
122+
Use `as` when converting an existing value or expression from one type to another, such as `x as i32` or `(len - 2) as f32`.
123+
106124
### Why does static typing matter?
107125

108126
In audio DSP, you are doing a lot of math very quickly. If the language has to keep figuring out what kind of value something is while it runs, that adds overhead. By knowing the types up front, Lyte can build faster code. Here, **compile** just means “turn your code into something the computer can run.”

src/compiler.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,15 +2159,15 @@ mod tests {
21592159
// f32 if-expression in void context.
21602160
("f32 if in void ctx", r#"
21612161
main {
2162-
var x = 0.0 as f32
2162+
var x = 0.0f32
21632163
if true { x = 1.0 } else { x = 2.0 }
21642164
}
21652165
"#),
21662166
// f32 assignment RHS.
21672167
("f32 assign chain", r#"
21682168
main {
2169-
var x = 0.0 as f32
2170-
var y = 0.0 as f32
2169+
var x = 0.0f32
2170+
var y = 0.0f32
21712171
x = 1.5
21722172
y = x + x
21732173
}

src/stack_optimize.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -727,9 +727,9 @@ fn fuse(func: &mut StackFunction) {
727727
// before the 3-op one so the longest match wins at each position.
728728

729729
// Constant-fold fw.f32.const v + dw.convert.f32_to_f64 → dw.f64.const.
730-
// Widening f32→f64 is exact, so this is always valid; it both folds
731-
// the many `<lit> as f64` conversions and exposes the constant to
732-
// the d-window compare-jump fusion below.
730+
// Widening f32→f64 is exact, so this is always valid; it folds explicit
731+
// f32-to-f64 conversions and exposes the constant to the d-window
732+
// compare-jump fusion below.
733733
if i + 1 < len && !spans_target(i, 2) {
734734
if let (StackOp::F32ConstF(v), StackOp::F32ToF64D) = (&ops[i], &ops[i + 1]) {
735735
let v = *v as f64;

stdlib.lyte

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ step(edge: f32, x: f32) -> f32 {
1717
}
1818

1919
step(edge: f64, x: f64) -> f64 {
20-
if x < edge { return 0.0 as f64 }
21-
return 1.0 as f64
20+
if x < edge { return 0.0f64 }
21+
return 1.0f64
2222
}
2323

2424
smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
@@ -27,8 +27,8 @@ smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
2727
}
2828

2929
smoothstep(edge0: f64, edge1: f64, x: f64) -> f64 {
30-
var t = clamp((x - edge0) / (edge1 - edge0), 0.0 as f64, 1.0 as f64)
31-
t * t * ((3.0 as f64) - (2.0 as f64) * t)
30+
var t = clamp((x - edge0) / (edge1 - edge0), 0.0f64, 1.0f64)
31+
t * t * (3.0f64 - 2.0f64 * t)
3232
}
3333

3434
mix(a: f32, b: f32, t: f32) -> f32 { a + t * (b - a) }
@@ -242,4 +242,4 @@ strcat(dst: [i8], src: [i8]) {
242242
}
243243
}
244244
}
245-
}
245+
}

tests/cases/f64_window.lyte

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ struct Acc { a: f64, b: f64, c: f64 }
2626

2727
approx(x: f64, y: f64) -> bool {
2828
var d = x - y
29-
if d < 0.0 as f64 { d = 0.0 as f64 - d }
30-
return d < 0.0001 as f64
29+
if d < 0.0f64 { d = 0.0f64 - d }
30+
return d < 0.0001f64
3131
}
3232

3333
// f64 argument and return value bridging across a call.
@@ -36,44 +36,44 @@ fma(a: f64, b: f64, c: f64) -> f64 {
3636
}
3737

3838
main {
39-
var x = 3.0 as f64
40-
var y = 2.0 as f64
39+
var x = 3.0f64
40+
var y = 2.0f64
4141

4242
// Arithmetic.
43-
assert(approx(x + y, 5.0 as f64))
44-
assert(approx(x - y, 1.0 as f64))
45-
assert(approx(x * y, 6.0 as f64))
46-
assert(approx(x / y, 1.5 as f64))
47-
assert(approx(0.0 as f64 - x, -3.0 as f64))
43+
assert(approx(x + y, 5.0f64))
44+
assert(approx(x - y, 1.0f64))
45+
assert(approx(x * y, 6.0f64))
46+
assert(approx(x / y, 1.5f64))
47+
assert(approx(0.0f64 - x, -3.0f64))
4848

4949
// All six comparisons.
5050
assert(x > y)
5151
assert(y < x)
52-
assert(x >= 3.0 as f64)
53-
assert(y <= 2.0 as f64)
54-
assert(x == 3.0 as f64)
52+
assert(x >= 3.0f64)
53+
assert(y <= 2.0f64)
54+
assert(x == 3.0f64)
5555
assert(x != y)
5656

5757
// Fused multiply-add sum chain: b0*x + b1*x1 - b2*x2.
58-
var b0 = 1.0 as f64
59-
var b1 = 2.0 as f64
60-
var b2 = 0.5 as f64
61-
var x1 = 4.0 as f64
62-
var x2 = 6.0 as f64
58+
var b0 = 1.0f64
59+
var b1 = 2.0f64
60+
var b2 = 0.5f64
61+
var x1 = 4.0f64
62+
var x2 = 6.0f64
6363
var sum = b0 * x + b1 * x1 - b2 * x2
6464
// 1*3 + 2*4 - 0.5*6 = 3 + 8 - 3 = 8
65-
assert(approx(sum, 8.0 as f64))
65+
assert(approx(sum, 8.0f64))
6666

6767
// f64 across a function call (argument + return bridging).
68-
assert(approx(fma(x, y, 1.0 as f64), 7.0 as f64))
68+
assert(approx(fma(x, y, 1.0f64), 7.0f64))
6969

7070
// Math intrinsic + struct store/load through the d-window.
7171
var acc: Acc
72-
acc.a = sqrt(16.0 as f64)
73-
acc.b = acc.a * 2.0 as f64
72+
acc.a = sqrt(16.0f64)
73+
acc.b = acc.a * 2.0f64
7474
acc.c = acc.a + acc.b
75-
assert(approx(acc.c, 12.0 as f64))
75+
assert(approx(acc.c, 12.0f64))
7676

7777
// Conversion round trip f64 -> i32.
78-
print((x * y * 7.0 as f64) as i32)
78+
print((x * y * 7.0f64) as i32)
7979
}

tests/cases/int_types.lyte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ main {
1515

1616
// u32 arithmetic
1717
var x: u32
18-
x = 100 as u32
18+
x = 100u32
1919
var y: u32
20-
y = 50 as u32
20+
y = 50u32
2121
assert(x as i32 - y as i32 == 50)
2222

2323
// u32 comparison

tests/cases/int_types_f64.lyte

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88

99
approx(a: f64, b: f64) -> bool {
1010
var d = a - b
11-
if d < 0.0 as f64 { d = 0.0 as f64 - d }
12-
return d < 0.0001 as f64
11+
if d < 0.0f64 { d = 0.0f64 - d }
12+
return d < 0.0001f64
1313
}
1414

1515
main {
1616
var x: f64
17-
x = 3.0 as f64
17+
x = 3.0f64
1818
var y: f64
19-
y = 2.0 as f64
19+
y = 2.0f64
2020

2121
// f64 arithmetic with helper function
22-
assert(approx(x + y, 5.0 as f64))
23-
assert(approx(x - y, 1.0 as f64))
24-
assert(approx(x * y, 6.0 as f64))
22+
assert(approx(x + y, 5.0f64))
23+
assert(approx(x - y, 1.0f64))
24+
assert(approx(x * y, 6.0f64))
2525

2626
// f64 comparisons
2727
assert(x > y)

tests/cases/int_types_u32.lyte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
main {
1010
// u32 arithmetic
1111
var a: u32
12-
a = 100 as u32
12+
a = 100u32
1313
var b: u32
14-
b = 40 as u32
14+
b = 40u32
1515
assert((a + b) as i32 == 140)
1616
assert((a - b) as i32 == 60)
1717
assert((a * b) as i32 == 4000)

0 commit comments

Comments
 (0)