mirror of
https://github.com/kanaka/mal.git
synced 2024-08-17 09:40:21 +03:00
- Reorder README to have implementation list after "learning tool" bullet. - This also moves tests/ and libs/ into impls. It would be preferrable to have these directories at the top level. However, this causes difficulties with the wasm implementations which need pre-open directories and have trouble with paths starting with "../../". So in lieu of that, symlink those directories to the top-level. - Move the run_argv_test.sh script into the tests directory for general hygiene.
51 lines
1.4 KiB
Zig
51 lines
1.4 KiB
Zig
const Allocator = @import("std").mem.Allocator;
|
|
const readline = @cImport(
|
|
@cInclude("readline/readline.h"));
|
|
const rl_hist = @cImport(
|
|
@cInclude("readline/history.h"));
|
|
const free = @import("std").c.free;
|
|
const addNullByte = @import("std").cstr.addNullByte;
|
|
|
|
const warn = @import("std").debug.warn;
|
|
|
|
pub fn slice_from_cstr(allocator: *Allocator, str: [*]const u8) ![]u8{
|
|
var length: usize = 0;
|
|
while(true) {
|
|
if(str[length] == 0)
|
|
break;
|
|
length += 1;
|
|
}
|
|
// TODO: check for 0-length
|
|
const slice = try allocator.alloc(u8, length);
|
|
var i: usize = 0;
|
|
while(i < length) {
|
|
slice[i] = str[i];
|
|
i += 1;
|
|
}
|
|
return slice;
|
|
}
|
|
|
|
pub fn getline(allocator: *Allocator) !?[] u8 {
|
|
var input: ?[*] u8 = readline.readline(c"user> ");
|
|
if(input) |actual| {
|
|
const aslice = try slice_from_cstr(allocator, actual);
|
|
rl_hist.add_history(actual);
|
|
free(actual);
|
|
return aslice;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
pub fn getline_prompt(allocator: *Allocator, prompt: []const u8) !?[] u8 {
|
|
const null_terminated_prompt = try addNullByte(allocator, prompt);
|
|
var input: ?[*] u8 = readline.readline(&null_terminated_prompt[0]);
|
|
allocator.free(null_terminated_prompt);
|
|
if(input) |actual| {
|
|
const aslice = try slice_from_cstr(allocator, actual);
|
|
rl_hist.add_history(actual);
|
|
free(actual);
|
|
return aslice;
|
|
}
|
|
return null;
|
|
}
|