Skip to content

Commit

Permalink
using errdefer and defer for freeing stuff at the end; removed kfreeb…
Browse files Browse the repository at this point in the history
…sd from os stdlib
  • Loading branch information
bauripalash committed Jul 29, 2024
1 parent bb9355f commit f540d4b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
3 changes: 3 additions & 0 deletions a.pank
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ show(a,"\n")
show(b, "\n")
show(a[2], "\n")
show(b[3], "\n")
show("\x61","\n")
show("\xe0\xa6\xaa\xe0\xa6\xb2\xe0\xa6\xbe\xe0\xa6\xb6", "\n")
show("\U000009aa\U000009b2\U000009be\U000009b6", "\n")
50 changes: 34 additions & 16 deletions src/compiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,16 @@ pub const Compiler = struct {
return Allocator.Error.OutOfMemory;
};

errdefer {
_string.clearAndFree(al);
_string.deinit(al);
}

// errdefer {
// _string.clearAndFree(al);
// _string.deinit(al);
// }

const view = std.unicode.Utf8View.initUnchecked(lexeme);
var ite = view.iterator();

Expand Down Expand Up @@ -808,12 +818,34 @@ pub const Compiler = struct {
'r' => raw = &[_]u8{'\x0D'}, // Carriage Return
't' => raw = &[_]u8{'\t'}, // Horizontal Tab
'v' => raw = &[_]u8{'\x0B'}, // Vertical Tab
'x' => {
const uLen = 2;
const hexChars = ite.peek(uLen);

if (hexChars.len < uLen) {
self.parser.err("hex escape requires 2 chars");
return Allocator.Error.OutOfMemory;
}

var hex = [4]u8{ 0, 0, 0, 0 };
if (self.readUnicodeCodePoint(hexChars, &hex)) |hlen| {
raw = hex[0..hlen];

for (0..uLen) |_| {
_ = ite.nextCodepoint() orelse {
self.parser.err("Invalid string hex escape codepoint");
return Allocator.Error.OutOfMemory;
};
}
} else {
self.parser.err("Invalid string hex escape codepoint");
return Allocator.Error.OutOfMemory;
}
},
'u' => {
const uLen = 4;
const hexChars = ite.peek(uLen);
if (hexChars.len < uLen) {
_string.clearAndFree(al);
_string.deinit(al);
self.parser.err(compErrors.STRING_U4_NOT_4);
return Allocator.Error.OutOfMemory;
}
Expand All @@ -823,15 +855,11 @@ pub const Compiler = struct {
raw = hex[0..hlen];
for (0..uLen) |_| {
_ = ite.nextCodepoint() orelse {
_string.clearAndFree(al);
_string.deinit(al);
self.parser.err(compErrors.STRING_INVALID_CP);
return Allocator.Error.OutOfMemory;
};
}
} else {
_string.clearAndFree(al);
_string.deinit(al);
self.parser.err(compErrors.STRING_INVALID_CP);
return Allocator.Error.OutOfMemory;
}
Expand All @@ -840,8 +868,6 @@ pub const Compiler = struct {
const uLen = 8;
const hexChars = ite.peek(uLen);
if (hexChars.len < uLen) {
_string.clearAndFree(al);
_string.deinit(al);
self.parser.err(compErrors.STRING_U4_NOT_4);
return Allocator.Error.OutOfMemory;
}
Expand All @@ -851,22 +877,16 @@ pub const Compiler = struct {

for (0..uLen) |_| {
_ = ite.nextCodepoint() orelse {
_string.clearAndFree(al);
_string.deinit(al);
self.parser.err(compErrors.STRING_INVALID_CP);
return Allocator.Error.OutOfMemory;
};
}
} else {
_string.clearAndFree(al);
_string.deinit(al);
self.parser.err(compErrors.STRING_INVALID_CP);
return Allocator.Error.OutOfMemory;
}
},
else => {
_string.clearAndFree(al);
_string.deinit(al);
self.parser.err("Invalid escape characters in string");
return Allocator.Error.OutOfMemory;
},
Expand All @@ -875,8 +895,6 @@ pub const Compiler = struct {

//std.debug.print("\nraw->'{s}'<-\n", .{raw});
_string.appendSlice(al, raw) catch {
_string.clearAndFree(al);
_string.deinit(al);
self.parser.err(compErrors.STRING_TEMP_APPEND);
return Allocator.Error.OutOfMemory;
//
Expand Down
2 changes: 1 addition & 1 deletion src/stdlib/os.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn os_Name(vm: *Vm, argc: u8, values: []PValue) PValue {
.linux => "লিনাক্স", //should be unix detection instead of linux
.ios => "আইওএস",
.macos => "ম্যাকওএস",
.kfreebsd, .freebsd, .openbsd, .netbsd, .dragonfly => "বিএসডি",
.freebsd, .openbsd, .netbsd, .dragonfly => "বিএসডি",
.plan9 => "প্ল্যান9",
else => if (builtin.target.abi == .android)
"আন্ড্রয়েড"
Expand Down

0 comments on commit f540d4b

Please sign in to comment.