Minor cleanup

- Add back old byte formatting with __str__
- Remove some magic numbers in code
pull/5/head
A. R. Shajii 2021-10-18 13:36:06 -04:00
parent 70a8864e9f
commit 111080438c
4 changed files with 13 additions and 7 deletions

View File

@ -238,9 +238,10 @@ std::shared_ptr<ImportFile> getImportFile(const std::string &argv0,
}
if (!isStdLib && startswith(s, module0Root))
root = module0Root;
seqassert(startswith(s, root) && endswith(s, ".codon"),
"bad path substitution: {}, {}", s, root);
auto module = s.substr(root.size() + 1, s.size() - root.size() - 7);
const std::string ext = ".codon";
seqassert(startswith(s, root) && endswith(s, ext), "bad path substitution: {}, {}",
s, root);
auto module = s.substr(root.size() + 1, s.size() - root.size() - ext.size() - 1);
std::replace(module.begin(), module.end(), '/', '.');
return ImportFile{(!isStdLib && root == module0Root) ? ImportFile::PACKAGE
: ImportFile::STDLIB,

View File

@ -32,12 +32,13 @@ StmtPtr SimplifyVisitor::apply(
auto stdlib = std::make_shared<SimplifyContext>(STDLIB_IMPORT, cache);
auto stdlibPath =
getImportFile(cache->argv0, STDLIB_INTERNAL_MODULE, "", true, cache->module0);
if (!stdlibPath ||
stdlibPath->path.substr(stdlibPath->path.size() - 14) != "__init__.codon")
const std::string initFile = "__init__.codon";
if (!stdlibPath || !endswith(stdlibPath->path, initFile))
ast::error("cannot load standard library");
if (barebones)
stdlibPath->path =
stdlibPath->path.substr(0, stdlibPath->path.size() - 7) + "test__.codon";
stdlibPath->path.substr(0, stdlibPath->path.size() - initFile.size()) +
"__init_test__.codon";
stdlib->setFilename(stdlibPath->path);
cache->imports[STDLIB_IMPORT] = {stdlibPath->path, stdlib};

View File

@ -63,6 +63,10 @@ class byte:
%0 = icmp uge i8 %self, %other
%1 = zext i1 %0 to i8
ret i8 %1
def __str__(self):
p = Ptr[byte](1)
p[0] = self
return str(p, 1)
def __repr__(self):
return f'byte({str(__ptr__(self), 1).__repr__()})'
@pure

View File

@ -60,7 +60,7 @@ def test_conversions():
assert float(byte(42)) == 42.0
assert bool(byte(0)) == False
assert bool(byte(42)) == True
assert str(byte(42)) == "byte('*')"
assert str(byte(42)) == '*'
# intN -> int, float, bool, str | N < 64
assert int(i32(-42)) == -42