Skip to content

Commit

Permalink
$exec may now provide a stdin parameter. Deprecated path.append, …
Browse files Browse the repository at this point in the history
…`path.tappend`, `getcwd`, `tgetcwd`, `path.absolute`, `ls`. Deprecated `env::get_config_dir`, replaced by `env::new_get_config_dir`. Added `path.has_extension`, `path.new_append`, `path.temp_append`, `new_cwd`, `temp_cwd`, `path.new_absolute`, `new_ls`, `temp_ls`. Added `dstring.replace`
  • Loading branch information
lerno committed Aug 14, 2024
1 parent 6bc4864 commit b60f2c6
Show file tree
Hide file tree
Showing 18 changed files with 296 additions and 141 deletions.
9 changes: 0 additions & 9 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -558,15 +558,6 @@ jobs:
cd resources/testfragments
../../build/c3c compile --reloc=none --target wasm32 -g0 --link-libc=no --no-entry -Os wasm4.c3
- name: Install QEMU and Risc-V toolchain
run: |
sudo apt-get install opensbi qemu-system-misc u-boot-qemu gcc-riscv64-unknown-elf
- name: Compile and run Baremetal Risc-V Example
run: |
cd resources/examples/embedded/riscv-qemu
make C3C_PATH=../../../../build/ run
- name: Build testproject direct linker
run: |
cd resources/testproject
Expand Down
15 changes: 3 additions & 12 deletions lib/std/core/builtin.c3
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,17 @@ import libc, std::hash, std::io, std::os::backtrace;
/**
* Use `IteratorResult` when reading the end of an iterator, or accessing a result out of bounds.
**/
fault IteratorResult
{
NO_MORE_ELEMENT
}
fault IteratorResult { NO_MORE_ELEMENT }

/**
* Use `SearchResult` when trying to return a value from some collection but the element is missing.
**/
fault SearchResult
{
MISSING
}
fault SearchResult { MISSING }

/**
* Use `CastResult` when an attempt at conversion fails.
**/
fault CastResult
{
TYPE_MISMATCH
}
fault CastResult { TYPE_MISMATCH }

/**
* Stores a variable on the stack, then restores it at the end of the
Expand Down
50 changes: 50 additions & 0 deletions lib/std/core/dstring.c3
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,56 @@ fn DString new(String c = "", Allocator allocator = allocator::heap())

fn DString temp_new(String s = "") => new(s, allocator::temp()) @inline;


fn void DString.replace_char(self, char ch, char replacement)
{
StringData* data = self.data();
foreach (&c : data.chars[:data.len])
{
if (*c == ch) *c = replacement;
}
}

fn void DString.replace(&self, String needle, String replacement)
{
StringData* data = self.data();
usz needle_len = needle.len;
if (!data || data.len < needle_len) return;
usz replace_len = replacement.len;
if (needle_len == 1 && replace_len == 1)
{
self.replace_char(needle[0], replacement[0]);
return;
}
@pool(data.allocator) {
String str = self.tcopy_str();
self.clear();
usz len = str.len;
usz match = 0;
foreach (i, c : str)
{
if (c == needle[match])
{
match++;
if (match == needle_len)
{
self.append_chars(replacement);
match = 0;
continue;
}
continue;
}
if (match > 0)
{
self.append_chars(str[i - match:match]);
match = 0;
}
self.append_char(c);
}
if (match > 0) self.append_chars(str[^match:match]);
};
}

fn DString DString.new_concat(self, DString b, Allocator allocator = allocator::heap())
{
DString string;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/io/os/rmtree.c3
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn void! native_rmtree(Path dir)
{
String name = ((ZString)&entry.name).str_view();
if (!name || name == "." || name == "..") continue;
Path new_path = dir.tappend(name)!;
Path new_path = dir.temp_append(name)!;
if (entry.d_type == posix::DT_DIR)
{
native_rmtree(new_path)!;
Expand Down
71 changes: 60 additions & 11 deletions lib/std/io/path.c3
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ fault PathResult
NO_PARENT,
}

struct Path (Printable)
def Path = PathImp;

struct PathImp (Printable)
{
String path_string;
PathEnv env;
Expand All @@ -27,7 +29,15 @@ enum PathEnv
POSIX
}

fn Path! getcwd(Allocator allocator = allocator::heap())
fn Path! new_cwd(Allocator allocator = allocator::heap())
{
@pool(allocator)
{
return new(os::getcwd(allocator::temp()), allocator);
};
}

fn Path! getcwd(Allocator allocator = allocator::heap()) @deprecated("Use new_cwd()")
{
@pool(allocator)
{
Expand All @@ -39,7 +49,8 @@ fn bool is_dir(Path path) => os::native_is_dir(path.str_view());
fn bool is_file(Path path) => os::native_is_file(path.str_view());
fn usz! file_size(Path path) => os::native_file_size(path.str_view());
fn bool exists(Path path) => os::native_file_or_dir_exists(path.str_view());
fn Path! tgetcwd() => getcwd(allocator::temp()) @inline;
fn Path! temp_cwd() => new_cwd(allocator::temp()) @inline;
fn Path! tgetcwd() @deprecated("Use temp_cwd()") => new_cwd(allocator::temp()) @inline;
fn void! chdir(Path path) => os::native_chdir(path) @inline;
fn Path! temp_directory(Allocator allocator = allocator::heap()) => os::native_temp_directory(allocator);
fn void! delete(Path path) => os::native_remove(path.str_view()) @inline;
Expand All @@ -59,7 +70,17 @@ macro bool is_win32_separator(char c)
return c == '/' || c == '\\';
}

fn PathList! ls(Path dir, bool no_dirs = false, bool no_symlinks = false, String mask = "", Allocator allocator = allocator::heap())
fn PathList! ls(Path dir, bool no_dirs = false, bool no_symlinks = false, String mask = "", Allocator allocator = allocator::heap()) @deprecated("use new_ls")
{
return new_ls(dir, no_dirs, no_symlinks, mask, allocator);

}
fn PathList! temp_ls(Path dir, bool no_dirs = false, bool no_symlinks = false, String mask = "")
{
return new_ls(dir, no_dirs, no_symlinks, mask, allocator::temp()) @inline;
}

fn PathList! new_ls(Path dir, bool no_dirs = false, bool no_symlinks = false, String mask = "", Allocator allocator = allocator::heap())
{
$if $defined(os::native_ls):
return os::native_ls(dir, no_dirs, no_symlinks, mask, allocator);
Expand Down Expand Up @@ -139,12 +160,17 @@ fn bool Path.equals(self, Path p2)
return self.env == p2.env && self.path_string == p2.path_string;
}

fn Path! Path.append(self, String filename, Allocator allocator = allocator::heap()) @deprecated("Use path.new_append(...)")
{
return self.new_append(filename, allocator) @inline;
}

/**
* Append the string to the current path.
*
* @param [in] filename
**/
fn Path! Path.append(self, String filename, Allocator allocator = allocator::heap())
fn Path! Path.new_append(self, String filename, Allocator allocator = allocator::heap())
{
if (!self.path_string.len) return new(filename, allocator, self.env)!;
assert(!is_separator(self.path_string[^1], self.env));
Expand All @@ -159,7 +185,9 @@ fn Path! Path.append(self, String filename, Allocator allocator = allocator::hea
};
}

fn Path! Path.tappend(self, String filename) => self.append(filename, allocator::temp());
fn Path! Path.temp_append(self, String filename) => self.new_append(filename, allocator::temp());

fn Path! Path.tappend(self, String filename) @deprecated("Use path.temp_append(...)") => self.new_append(filename, allocator::temp());

fn usz Path.start_of_base_name(self) @local
{
Expand Down Expand Up @@ -193,10 +221,15 @@ fn bool! Path.is_absolute(self)
return path_start < path_str.len && is_separator(path_str[path_start], self.env);
}

fn Path! Path.absolute(self, Allocator allocator = allocator::heap()) @deprecated("Use path.new_absolute()")
{
return self.new_absolute(allocator) @inline;
}

/**
* @require self.env == DEFAULT_PATH_ENV : "This method is only available on native paths"
**/
fn Path! Path.absolute(self, Allocator allocator = allocator::heap())
fn Path! Path.new_absolute(self, Allocator allocator = allocator::heap())
{
String path_str = self.str_view();
if (!path_str.len) return PathResult.INVALID_PATH?;
Expand All @@ -220,7 +253,7 @@ fn Path! Path.absolute(self, Allocator allocator = allocator::heap())
};
$else
String cwd = os::getcwd(allocator::temp())!;
return Path { cwd, self.env }.append(path_str, allocator)!;
return Path { cwd, self.env }.new_append(path_str, allocator)!;
$endif
}

Expand Down Expand Up @@ -250,6 +283,22 @@ fn String Path.dirname(self)
return path_str[:basename_start - 1];
}

/**
* Test if the path has the given extension, so given the path /foo/bar.c3
* this would be true matching the extension "c3"
*
* @param [in] extension `The extension name (not including the leading '.')`
* @require extension.len > 0 : `The extension cannot be empty`
* @return `true if the extension matches`
**/
fn bool Path.has_extension(self, String extension)
{
String basename = self.basename();
if (basename.len <= extension.len) return false;
if (basename[^extension.len + 1] != '.') return false;
return basename[^extension.len..] == extension;
}

fn String! Path.extension(self)
{
String basename = self.basename();
Expand Down Expand Up @@ -470,12 +519,12 @@ fn bool! Path.walk(self, PathWalker w, void* data)
const PATH_MAX = 512;
@stack_mem(PATH_MAX; Allocator allocator)
{
Path abs = self.absolute(allocator)!;
PathList files = ls(abs, .allocator = allocator)!;
Path abs = self.new_absolute(allocator)!;
PathList files = new_ls(abs, .allocator = allocator)!;
foreach (f : files)
{
if (f.str_view() == "." || f.str_view() == "..") continue;
f = abs.append(f.str_view(), allocator)!;
f = abs.new_append(f.str_view(), allocator)!;
bool is_directory = is_dir(f);
if (w(f, is_directory, data)!) return true;
if (is_directory && f.walk(w, data)!) return true;
Expand Down
18 changes: 14 additions & 4 deletions lib/std/os/env.c3
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,15 @@ fn String! get_home_dir(Allocator using = allocator::heap())
return get_var(home, using);
}

fn Path! get_config_dir(Allocator allocator = allocator::heap()) @deprecated("use new_get_config_dir()")
{
return new_get_config_dir(allocator) @inline;
}

/**
* Returns the current user's config directory.
**/
fn Path! get_config_dir(Allocator allocator = allocator::heap())
fn Path! new_get_config_dir(Allocator allocator = allocator::heap())
{
@pool(allocator)
{
Expand All @@ -100,7 +105,7 @@ fn Path! get_config_dir(Allocator allocator = allocator::heap())
String s = get_var_temp("XDG_CONFIG_HOME") ?? get_var_temp("HOME")!;
const DIR = ".config";
$endif
return path::temp_new(s).append(DIR, .allocator = allocator);
return path::temp_new(s).new_append(DIR, .allocator = allocator);
$endif
};
}
Expand All @@ -126,11 +131,16 @@ fn bool clear_var(String name)
};
}

fn String! executable_path(Allocator allocator = allocator::heap())
fn String! executable_path(Allocator allocator = allocator::heap()) @deprecated("use new_executable_path()")
{
return new_executable_path(allocator) @inline;
}

fn String! new_executable_path(Allocator allocator = allocator::heap())
{
$if env::DARWIN:
return darwin::executable_path(allocator);
$else
return "<Unsupported>";
return SearchResult.MISSING?;
$endif
}
2 changes: 1 addition & 1 deletion lib/std/os/macos/darwin.c3
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn uptr! load_address() @local
{
Darwin_segment_command_64* cmd = darwin::getsegbyname("__TEXT");
if (!cmd) return BacktraceFault.SEGMENT_NOT_FOUND?;
String path = env::executable_path(allocator::temp()) ?? BacktraceFault.EXECUTABLE_PATH_NOT_FOUND?!;
String path = env::new_executable_path(allocator::temp()) ?? BacktraceFault.EXECUTABLE_PATH_NOT_FOUND?!;
uint dyld_count = darwin::_dyld_image_count();
for (uint i = 0; i < dyld_count; i++)
{
Expand Down
5 changes: 5 additions & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- Pointers are rendered with "0x" prefix when passed to '%s'.
- Add temp allocator scribble.
- Use PIC by default on Linux.
- `$exec` may now provide a stdin parameter.

### Fixes

Expand Down Expand Up @@ -74,6 +75,10 @@

- `send` and `recv` added to `libc` for Posix / Win32.
- Add support to destroy temp allocators.
- Deprecated `path.append`, `path.tappend`, `getcwd`, `tgetcwd`, `path.absolute`, `ls`.
- Deprecated `env::get_config_dir`, replaced by `env::new_get_config_dir`.
- Added `path.has_extension`, `path.new_append`, `path.temp_append`, `new_cwd`, `temp_cwd`, `path.new_absolute`, `new_ls`, `temp_ls`.
- Added `dstring.replace`

## 0.6.1 Change list

Expand Down
4 changes: 2 additions & 2 deletions src/build/libraries.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,12 @@ void resolve_libraries(BuildTarget *build_target)
FOREACH(const char *, exec, library->execs)
{
printf("] Execute '%s' for library '%s':", exec, library->provides);
puts(execute_cmd(exec, false));
puts(execute_cmd(exec, false, NULL));
}
FOREACH(const char *, exec, target->execs)
{
printf("] Execute '%s' for library '%s':", exec, library->provides);
puts(execute_cmd(exec, false));
puts(execute_cmd(exec, false, NULL));
}
}
}
Loading

0 comments on commit b60f2c6

Please sign in to comment.