1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
//! Root-level doc comment (module doc).
//! This file exercises every syntax category for highlighting verification.
//! Open in Nova and check that all constructs colour correctly.
const std = @import("std");
// ── Keywords: pub, const, var, comptime, extern, export, inline, noinline ──────
/// Doc comment on a public constant.
pub const VERSION: []const u8 = "0.15.0";
pub var mutable_counter: u32 = 0;
// ── Enums ──────────────────────────────────────────────────────────────────────
pub const Color = enum(u8) {
red = 0,
green = 1,
blue = 2,
pub fn toHex(self: Color) u24 {
return switch (self) {
.red => 0xFF0000,
.green => 0x00FF00,
.blue => 0x0000FF,
};
}
};
// ── Structs with default field values ─────────────────────────────────────────
pub const Point = struct {
x: f64 = 0.0,
y: f64 = 0.0,
pub fn distanceTo(self: Point, other: Point) f64 {
const dx = self.x - other.x;
const dy = self.y - other.y;
return std.math.sqrt(dx * dx + dy * dy);
}
/// Generic format method — tests `anytype` keyword.
pub fn format(self: Point, comptime fmt: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
_ = fmt;
try writer.print("({d:.2}, {d:.2})", .{ self.x, self.y });
}
};
// ── Tagged union ───────────────────────────────────────────────────────────────
pub const Number = union(enum) {
integer: i64,
float: f64,
pub fn isZero(self: Number) bool {
return switch (self) {
.integer => |v| v == 0,
.float => |v| v == 0.0,
};
}
};
// ── Opaque type ───────────────────────────────────────────────────────────────
pub const Handle = opaque {};
// ── Packed struct ─────────────────────────────────────────────────────────────
pub const Flags = packed struct {
enabled: bool,
verbose: bool,
debug: bool,
_pad: u5 = 0,
};
// ── Comptime ──────────────────────────────────────────────────────────────────
pub fn typeName(comptime T: type) []const u8 {
return @typeName(T);
}
/// Comptime block with labeled break.
pub const BITS_IN_BYTE: comptime_int = blk: {
var n: u8 = 255;
var count: usize = 0;
while (n > 0) : (n >>= 1) count += 1;
break :blk count;
};
// ── Generics ──────────────────────────────────────────────────────────────────
pub fn Stack(comptime T: type) type {
return struct {
items: std.ArrayList(T) = .empty,
pub fn deinit(self: *@This(), allocator: std.mem.Allocator) void {
self.items.deinit(allocator);
}
pub fn push(self: *@This(), allocator: std.mem.Allocator, value: T) !void {
try self.items.append(allocator, value);
}
pub fn pop(self: *@This()) ?T {
return self.items.pop();
}
};
}
// ── Builtin functions ─────────────────────────────────────────────────────────
pub fn builtinDemo(value: i32) u32 {
const as_u32: u32 = @intCast(value);
const as_f32: f32 = @floatFromInt(value);
_ = as_f32;
_ = @sizeOf(u64);
_ = @alignOf(*u8);
_ = @typeInfo(Color);
return as_u32;
}
// ── String literal variants ───────────────────────────────────────────────────
pub const PLAIN: []const u8 = "hello, Nova!";
pub const ESCAPE: []const u8 = "tab:\there\nnewline\x00null";
/// Multiline string — each line starts with `\\`.
pub const MULTILINE: []const u8 =
\\first line
\\second line
\\third line
;
pub const CHAR_LITERAL: u8 = 'Z';
pub const UNICODE_ESCAPE: []const u8 = "\u{2744}"; // ❄
pub const HEX_ESCAPE: u8 = '\xff';
// ── Error sets and error union ─────────────────────────────────────────────────
pub const ParseError = error{
UnexpectedToken,
EndOfStream,
Overflow,
};
pub fn parseU8(s: []const u8) ParseError!u8 {
if (s.len == 0) return error.EndOfStream;
return std.fmt.parseInt(u8, s, 10) catch error.UnexpectedToken;
}
// ── Optional / orelse / if capture ───────────────────────────────────────────
pub fn firstByte(s: []const u8) ?u8 {
return if (s.len == 0) null else s[0];
}
pub fn firstByteOr(s: []const u8, fallback: u8) u8 {
return firstByte(s) orelse fallback;
}
// ── Switch with integer ranges ────────────────────────────────────────────────
pub fn classify(n: i32) []const u8 {
return switch (n) {
std.math.minInt(i32)...-1 => "negative",
0 => "zero",
1...100 => "small positive",
else => "large positive",
};
}
// ── For with index, multi-sequence ────────────────────────────────────────────
pub fn sumSlice(items: []const i32) i64 {
var total: i64 = 0;
for (items, 0..) |item, idx| {
_ = idx;
total += item;
}
return total;
}
// ── While with continue expression ───────────────────────────────────────────
pub fn countDown(start: u32) u32 {
var i = start;
var steps: u32 = 0;
while (i > 0) : (i -= 1) steps += 1;
return steps;
}
// ── Defer / errdefer ─────────────────────────────────────────────────────────
pub fn withDefer(allocator: std.mem.Allocator) ![]u8 {
const buf = try allocator.alloc(u8, 16);
errdefer allocator.free(buf);
@memset(buf, 0);
return buf;
}
// ── Pointer types, alignment ──────────────────────────────────────────────────
pub fn ptrDemo(p: *align(8) u64) u64 {
return p.*;
}
// ── inline / noinline ─────────────────────────────────────────────────────────
pub inline fn inlineAdd(a: u32, b: u32) u32 {
return a + b;
}
pub noinline fn noinlineAdd(a: u32, b: u32) u32 {
return a + b;
}
// ── threadlocal ───────────────────────────────────────────────────────────────
threadlocal var tl_counter: u32 = 0;
pub fn bumpCounter() void {
tl_counter += 1;
}
// ── extern ────────────────────────────────────────────────────────────────────
extern fn c_strlen(ptr: [*:0]const u8) usize;
// ── Inline assembly ───────────────────────────────────────────────────────────
pub fn nop() void {
asm volatile ("nop");
}
// ── Tests ─────────────────────────────────────────────────────────────────────
test "syntax-tour: classify" {
try std.testing.expectEqualStrings("zero", classify(0));
try std.testing.expectEqualStrings("negative", classify(-5));
try std.testing.expectEqualStrings("small positive", classify(42));
try std.testing.expectEqualStrings("large positive", classify(200));
}
test "syntax-tour: sumSlice" {
try std.testing.expectEqual(@as(i64, 15), sumSlice(&.{ 1, 2, 3, 4, 5 }));
try std.testing.expectEqual(@as(i64, 0), sumSlice(&.{}));
}
test "syntax-tour: parseU8 errors" {
try std.testing.expectError(error.EndOfStream, parseU8(""));
try std.testing.expectError(error.UnexpectedToken, parseU8("abc"));
}
test "syntax-tour: firstByte" {
try std.testing.expectEqual(@as(?u8, 'h'), firstByte("hello"));
try std.testing.expectEqual(@as(?u8, null), firstByte(""));
}
test "syntax-tour: Color.toHex" {
try std.testing.expectEqual(@as(u24, 0xFF0000), Color.red.toHex());
try std.testing.expectEqual(@as(u24, 0x0000FF), Color.blue.toHex());
}
test "syntax-tour: Number.isZero" {
try std.testing.expect((Number{ .integer = 0 }).isZero());
try std.testing.expect(!(Number{ .float = 3.14 }).isZero());
}
test "syntax-tour: Stack(i32)" {
const gpa = std.testing.allocator;
var s = Stack(i32){};
defer s.deinit(gpa);
try s.push(gpa, 10);
try s.push(gpa, 20);
try std.testing.expectEqual(@as(?i32, 20), s.pop());
try std.testing.expectEqual(@as(?i32, 10), s.pop());
try std.testing.expectEqual(@as(?i32, null), s.pop());
}
test "syntax-tour: BITS_IN_BYTE" {
try std.testing.expectEqual(@as(comptime_int, 8), BITS_IN_BYTE);
}
|