Skip to content

Commit

Permalink
allow prelude to import and export modules into the global namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
wbbradley committed Aug 19, 2019
1 parent 03df15b commit 7c1c8a3
Show file tree
Hide file tree
Showing 28 changed files with 268 additions and 95 deletions.
2 changes: 1 addition & 1 deletion lib/list.zion
Expand Up @@ -58,7 +58,7 @@ instance Iterable (List a) a {
}
}

instance Show (List a) {
instance Str (List a) {
fn str(list) {
return join(", ", list)
}
Expand Down
48 changes: 45 additions & 3 deletions lib/map.zion
Expand Up @@ -2,13 +2,13 @@

import list {List, Cons, Nil, remove_if}
import hash {hash}
export {Map}
export {Map, keys, values}

newtype MapKeyValueCell key value = MapKeyValueCell(Int, key, var value)
newtype MapStorage key value = MapStorage([List (MapKeyValueCell key value)])
newtype Map key value = Map(MapStorage key value, var Int)

instance Show (Map key value) {
instance Str (Map key value) {
fn str(map) {
let Map(MapStorage(storage), _) = map
let results = []
Expand Down Expand Up @@ -42,7 +42,7 @@ instance HasSetMembership (Map key value) key {
}
}

instance Show (MapKeyValueCell key value) {
instance Str (MapKeyValueCell key value) {
fn str(key_value_cell) String {
let MapKeyValueCell(hash_value, key, var value) = key_value_cell
return "MapKeyValueCell(${hash_value}, ${key}, ${value})"
Expand Down Expand Up @@ -213,3 +213,45 @@ instance HasAssignableIndexableItems (Map key value) key value {
}
}
}

fn keys(map Map key value) [key] {
# Returns a copy of the keys in a Vector
let Map(MapStorage(storage), var size) = map
let results = []
reserve(results, size)
for match Cons(MapKeyValueCell(_, key, _), var next) in storage {
append(results, key)
var next_list = next
while match next_list {
Cons(MapKeyValueCell(_, key, _), var next_next) {
append(results, key)
next_list = next_next
}
Nil {
break
}
}
}
return results
}

fn values(map Map key value) [value] {
# Returns a copy of the values in a Vector
let Map(MapStorage(storage), var size) = map
let results = [] as [value]
reserve(results, size)
for match Cons(MapKeyValueCell(_, _, var value), var next) in storage {
append(results, value)
var next_list = next
while match next_list {
Cons(MapKeyValueCell(_, _, var value), var next_next) {
append(results, value)
next_list = next_next
}
Nil {
break
}
}
}
return results
}
24 changes: 23 additions & 1 deletion lib/math.zion
@@ -1,10 +1,32 @@
import trigonometry {PI, sin, cos, tan, acos, asin, sinh, cosh, tanh, atan,
atan2}
export {PI, even, odd, sqrt, sum, sin, cos, tan, acos, asin, sinh, cosh, tanh,
atan, atan2}
atan, atan2, Num, +, *, -, /, abs, negate, Bounded, from_int,
max_bound, min_bound}

link "m"

class Num a {
has Eq a
fn from_int(Int) a
fn +(a, a) a
fn *(a, a) a
fn -(a, a) a
fn /(a, a) a
fn abs(a) a
fn negate(a) a
}

class Bounded a {
fn min_bound() a
fn max_bound() a
}

instance Bounded Int {
fn min_bound() => __builtin_min_int
fn max_bound() => __builtin_max_int
}

fn even(x Int) Bool => x % 2 == 0
fn odd(x Int) Bool => x % 2 == 1
fn sqrt(x Float) Float => __builtin_ffi_1("sqrt", x)
Expand Down
17 changes: 15 additions & 2 deletions lib/set.zion
@@ -1,8 +1,15 @@
import map {Map}
export {Set}
export {Set, set}

newtype Set a = Set(Map a ())

fn set(xs) {
let set = Set({})
for x in xs {
insert(set, x)
}
return set
}

instance HasInsertableItems (Set a) a {
fn insert(set, value) {
let Set(map) = set
Expand Down Expand Up @@ -46,3 +53,9 @@ instance Iterable (Set a) a {
inner_map)) as fn () Maybe a
}
}

instance Str (Set a) {
fn str(xs) {
return "{${join(", ", xs)}}"
}
}
53 changes: 17 additions & 36 deletions lib/std.zion
@@ -1,7 +1,8 @@
import map {Map}
import set {Set}
export {Map, Set}

import map {Map, keys, values}
import set {Set, set}
import math {+, -, *, /, abs, negate, Num, Bounded, from_int}
export {Map, keys, values, Set, set}
export {+, -, *, /, abs, negate, Num, Bounded, from_int}

link pkg "bdw-gc"

Expand Down Expand Up @@ -33,17 +34,19 @@ class HasDefaultGet collection key value {
fn get(collection, key, value) value
}

# The default string type. Strings are stored null-terminated and the length is
# cached.
newtype String = String(*Char, Int)

class Repr a {
fn repr(a) String
}

class Show a {
class Str a {
fn str(a) String
}

instance Show () {
instance Str () {
fn str(a) => "()"
}

Expand All @@ -53,7 +56,7 @@ instance Repr () {

fn strlen(sz *Char) Int => __builtin_ffi_1("zion_strlen", sz)

instance Show Int {
instance Str Int {
fn str(x Int) {
let sz = __builtin_ffi_1("zion_itoa", x)
return String(sz, strlen(sz))
Expand All @@ -64,7 +67,7 @@ instance Repr Int {
repr = str
}

instance Show Float {
instance Str Float {
fn str(x) {
let sz = __builtin_ffi_1("zion_ftoa", x)
let length = __builtin_ffi_1("zion_strlen", sz)
Expand All @@ -76,11 +79,11 @@ instance Repr Float {
repr = str
}

instance Show (var a) {
instance Str (var a) {
fn str(var a) => str(a)
}

instance Show String {
instance Str String {
str = id
}

Expand Down Expand Up @@ -108,7 +111,7 @@ instance Repr String {
}
}

instance Show (* Char) {
instance Str (* Char) {
fn str(a) => String(a, __builtin_ffi_1("zion_strlen", a))
}

Expand Down Expand Up @@ -204,7 +207,7 @@ fn join(delim_ String, xs) String {
return String(buf, i)
}

instance Show [a] {
instance Str [a] {
fn str(xs) {
let strs = []
for x in xs {
Expand Down Expand Up @@ -346,23 +349,6 @@ instance Bitwise Int {
fn ^(a, b) => __builtin_int_bitwise_xor(a, b)
}

class Num a {
# TODO: test "has Eq" with no "a"
has Eq a
fn from_int(Int) a
fn +(a, a) a
fn *(a, a) a
fn -(a, a) a
fn /(a, a) a
fn abs(a) a
fn negate(a) a
}

class Bounded a {
fn min_bound() a
fn max_bound() a
}

data Ordering {
EQ
LT
Expand Down Expand Up @@ -470,11 +456,6 @@ instance Num Char {
fn abs(a) => __builtin_abs_char(a)
}

instance Bounded Int {
fn min_bound() => __builtin_min_int
fn max_bound() => __builtin_max_int
}

instance Num Float {
fn from_int(a) => __builtin_int_to_float(a)
fn +(a, b) => __builtin_add_float(a, b)
Expand Down Expand Up @@ -516,7 +497,7 @@ data Maybe t {
Nothing
}

instance Show (Maybe a) {
instance Str (Maybe a) {
fn str(ma) => match ma {
Just(a) => "Just(${a})"
Nothing => "Nothing"
Expand Down Expand Up @@ -563,7 +544,7 @@ fn resize(xs [a], new_len Int, default a) () {
}
}

instance Show Char {
instance Str Char {
fn str(ch) {
let ys = __builtin_calloc(1)
ys[0] = ch
Expand Down
2 changes: 1 addition & 1 deletion lib/sys.zion
Expand Up @@ -90,7 +90,7 @@ instance Repr FileDescriptor {

newtype Errno = Errno(Int)

instance Show Errno {
instance Str Errno {
fn str(errno) {
let Errno(errno) = errno
let buffer = alloc(1024) as *Char
Expand Down
2 changes: 1 addition & 1 deletion lib/time.zion
Expand Up @@ -16,7 +16,7 @@ newtype EpochMilliseconds = EpochMilliseconds(Int)

fn time() => EpochMilliseconds(__builtin_ffi_0("zion_epoch_millis"))

instance Show EpochMilliseconds {
instance Str EpochMilliseconds {
fn str(e) {
let EpochMilliseconds(t) = e
return "${t}"
Expand Down
2 changes: 1 addition & 1 deletion play/conway.zion
Expand Up @@ -92,7 +92,7 @@ fn Step(l Life) {
}

# String returns the game board as a str.
instance Show Life {
instance Str Life {
fn str(l) {
# TODO: make a better string builder type thing
var buf = ""
Expand Down
5 changes: 4 additions & 1 deletion src/ast.cpp
Expand Up @@ -419,7 +419,10 @@ const Predicate *IrrefutablePredicate::rewrite(

types::Ref TypeDecl::get_type() const {
std::vector<types::Ref> types;
assert(isupper(id.name[0]));
#ifdef ZION_DEBUG
std::vector<std::string> pieces = split(id.name, ".");
assert(isupper(pieces.back()[0]));
#endif
types.push_back(type_id(id));
for (auto param : params) {
assert(islower(param.name[0]));
Expand Down
4 changes: 3 additions & 1 deletion src/ast.h
Expand Up @@ -492,20 +492,22 @@ struct Instance {

struct Module {
Module(std::string name,
const std::vector<const Identifier> &imports,
const std::vector<const Decl *> &decls,
const std::vector<const TypeDecl *> &type_decls,
const std::vector<const TypeClass *> &type_classes,
const std::vector<const Instance *> &instances,
const ParsedCtorIdMap &ctor_id_map,
const ParsedDataCtorsMap &data_ctors_map,
const types::TypeEnv &type_env)
: name(name), decls(decls), type_decls(type_decls),
: name(name), imports(imports), decls(decls), type_decls(type_decls),
type_classes(type_classes), instances(instances),
ctor_id_map(ctor_id_map), data_ctors_map(data_ctors_map),
type_env(type_env) {
}

std::string const name;
std::vector<const Identifier> imports;
std::vector<const Decl *> decls;
std::vector<const TypeDecl *> type_decls;
std::vector<const TypeClass *> type_classes;
Expand Down
9 changes: 7 additions & 2 deletions src/compiler.cpp
Expand Up @@ -205,7 +205,8 @@ struct GlobalParserState {
std::set<std::string> get_top_level_decls(
const std::vector<const Decl *> &decls,
const std::vector<const TypeDecl *> &type_decls,
const std::vector<const TypeClass *> &type_classes) {
const std::vector<const TypeClass *> &type_classes,
const std::vector<const Identifier> &imports) {
std::map<std::string, Location> module_decls;
for (const Decl *decl : decls) {
if (module_decls.find(decl->id.name) != module_decls.end()) {
Expand All @@ -228,6 +229,9 @@ std::set<std::string> get_top_level_decls(
top_level_decls.insert(overload_pair.first);
}
}
for (auto &import : imports) {
top_level_decls.insert(import.name);
}
debug_above(8, log("tlds are %s", ::join(top_level_decls, ", ").c_str()));
return top_level_decls;
}
Expand All @@ -249,7 +253,8 @@ std::shared_ptr<Compilation> merge_compilation(
for (const Module *module : modules) {
/* get a list of all top-level decls */
std::set<std::string> bindings = get_top_level_decls(
module->decls, module->type_decls, module->type_classes);
module->decls, module->type_decls, module->type_classes,
module->imports);

const Module *module_rebound = prefix(bindings, module);

Expand Down
3 changes: 2 additions & 1 deletion src/compiler.h
Expand Up @@ -49,7 +49,8 @@ std::string resolve_module_filename(Location location,
std::set<std::string> get_top_level_decls(
const std::vector<const ast::Decl *> &decls,
const std::vector<const ast::TypeDecl *> &type_decls,
const std::vector<const ast::TypeClass *> &type_classes);
const std::vector<const ast::TypeClass *> &type_classes,
const std::vector<const Identifier> &imports);
}; // namespace compiler

std::string strip_zion_extension(std::string module_name);
Expand Down

0 comments on commit 7c1c8a3

Please sign in to comment.