From 5b5a3038aa08c430f60533153b0823267b3dbdff Mon Sep 17 00:00:00 2001 From: chloe caruso Date: Sun, 21 Jul 2024 15:39:07 -0700 Subject: [PATCH] remove $ as a valid identifier character The casing conventions have not been set, but there is no universe where we allow $ as a recommended identifier character. we had this initially because JavaScript allows it, but in practice people just use it in frameworks to mean "magic". (see jQuery, Svelte, Qwik, etc) --- CHANGELOG.md | 5 +++-- src/core/Report.zig | 7 +------ src/core/Tokenizer.zig | 6 +++--- src/core/Transform.zig | 20 ++++++++------------ 4 files changed, 15 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 155e6db9170c549615b492f72d313e96f5247013..078597fe175ba66eb41bb8f117258af5117ee87b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,10 @@ ## 0.1.4 -- feat: implement string identifiers -- feat: implement basic struct types +- feat: implement basic struct types (#42) +- feat: implement string identifiers (#54) - feat: include a `README.html` alongside compiler with documentation and code samples. - feat(transform): include a warning for leading zero in numbers +- feat(tokenizer): `$` is no longer valid in identifiers - fix(sema): miscompilation using left side of all binary expression for both sides - fix(sema): handle recursive function calls - fix(sema): quit block analysis when hitting `noreturn` diff --git a/src/core/Report.zig b/src/core/Report.zig index 49bb6ebd1250af3acd43aa534357a5d3cd4d3197..317ee696d37a439c83265b4c73c23757d2fa3c71 100644 --- a/src/core/Report.zig +++ b/src/core/Report.zig @@ -1130,12 +1130,7 @@ fn formatIdImpl( var i: usize = 0; while (i < inner.len) { switch (inner[i]) { - 'a'...'z', - 'A'...'Z', - '0'...'9', - '_', - '$', - => { + 'a'...'z', 'A'...'Z', '0'...'9', '_' => { i += 1; continue; }, diff --git a/src/core/Tokenizer.zig b/src/core/Tokenizer.zig index d8583b1b8e274716d41c8ab53ddaf14e725e2d29..910ecb2ae6bdb8eab88975580815f139bdf6e79e 100644 --- a/src/core/Tokenizer.zig +++ b/src/core/Tokenizer.zig @@ -322,7 +322,7 @@ pub fn next(t: *Tokenizer) !?Token { switch (t.byte()) { ' ', '\t', '\n', '\r' => unreachable, // call skipWhitespace - 'a'...'z', 'A'...'Z', '_', '$' => { + 'a'...'z', 'A'...'Z', '_' => { const start = t.pos; t.pos += 1; return try t.tokenizeIdentifier(start); @@ -563,7 +563,7 @@ fn codePoint(t: Tokenizer) !struct { u3, u21 } { fn isAtIdentifierStart(t: *Tokenizer) !bool { switch (t.byte()) { - 'a'...'z', 'A'...'Z', '_', '$' => return true, + 'a'...'z', 'A'...'Z', '_' => return true, else => return false, 0x80...0xFF => { _, const code_point = try t.codePoint(); @@ -578,7 +578,7 @@ fn tokenizeIdentifier(t: *Tokenizer, start_byte: u32) !Token { while (true) { switch (t.byte()) { - 'a'...'z', 'A'...'Z', '0'...'9', '_', '$' => { + 'a'...'z', 'A'...'Z', '0'...'9', '_' => { t.pos += 1; }, 0x80...0xFF => { diff --git a/src/core/Transform.zig b/src/core/Transform.zig index e0013ca2c468039ed62ee36581986f8920dc52cb..2cf3d708e317aba9ee69c2d1b3811421b3e0cb00 100644 --- a/src/core/Transform.zig +++ b/src/core/Transform.zig @@ -6,8 +6,8 @@ //! - Annotating expression statements with `.discard` and `.ensure_void` //! - Flattening nested expressions into instruction sequences. //! -//! Some trivial diagnostics such as unused variables, unknown result locations, and -//! invalid assignment targets are also reported here. +//! Some trivial diagnostics such as unused variables, unknown result types, +//! invalid assignment targets, and other style issues are also reported here. gpa: Allocator, ast: Ast, insts: Inst.List = .{}, @@ -22,9 +22,9 @@ scopes: std.ArrayListUnmanaged(Scope) = .{}, report: Report.Builder, pub const Error = error{ - OutOfMemory, + /// A `Report` will contain at least one error. TransformFail, -}; +} || Allocator.Error; pub fn transform( ast: Ast, @@ -61,7 +61,7 @@ pub fn transform( try t.insts.setCapacity(t.gpa, t.insts.len); - return Teg{ + return .{ .insts = t.insts.toOwnedSlice(), .string_bytes = try t.string_bytes.toOwnedSlice(t.gpa), .extra = try t.extra.toOwnedStore(t.gpa), @@ -142,18 +142,14 @@ const Scope = struct { } }; -const ScratchType = enum { - ref, - index, -}; - const BlockContext = struct { node_base: i32, return_target: ?*?Inst.Index = null, break_target: ?*BreakTarget = null, - /// This struct is always stored on the stack; the idea is to pass - /// mutable pointers to it. When `break :brk` is encountered + /// This struct is always stored on the stack; the idea is to pass mutable + /// pointers to it. When `break :brk` is encountered, it will generate the + /// block instruction. This way, redundant blocks are never created. const BreakTarget = struct { label: ?[]const u8, /// This allows `continue` as well as implicit break targeting. -- 2.54.0