Skip to content

Commit 6b76e96

Browse files
committed
wip
1 parent b1b6072 commit 6b76e96

16 files changed

+143
-61
lines changed

lib/map.zion

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
# Implements the default Map type in Zion
22

33
import list {List, Cons, Nil, remove_if}
4-
import hash {hash}
5-
import copy {Copy, copy}
64
export {Map, MapStorage, keys, values, from_pairs}
75

86
newtype MapKeyValueCell key value = MapKeyValueCell(Int, key, var value)
97
newtype MapStorage key value = MapStorage([List (MapKeyValueCell key value)])
108
newtype Map key value = Map(MapStorage key value, var Int)
119

12-
fn str_tuple_show(key, value) => "${repr(key)}: ${value}"
13-
fn repr_tuple_show(key, value) => "${repr(key)}: ${repr(value)}"
14-
1510
fn map_render(map, tuple_show) {
1611
let Map(MapStorage(storage), _) = map
1712
let results = []
@@ -39,6 +34,9 @@ instance Repr (Map key value) {
3934
fn repr(map) => map_render(map, repr_tuple_show)
4035
}
4136

37+
fn str_tuple_show(key, value) => "${repr(key)}: ${value}"
38+
fn repr_tuple_show(key, value) => "${repr(key)}: ${repr(value)}"
39+
4240
instance HasSetMembership (Map key value) key {
4341
fn in(key, map) Bool {
4442
let Map(map_storage, var size) = map

lib/std.zion

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import append {MutatingAppend, append}
33
import result {Result, Ok, Err}
44
import defaults {HasDefault, HasDefaultGet, new, get}
55
import maybe {Maybe, Nothing, Just}
6-
import map {Map, keys, values}
76
import math {+, -, *, /, abs, negate, Num, Bounded, from_int, identity}
7+
import map {Map, keys, values}
88
import set {Set, set}
99
import sys {open, read, write, close, readlines, stdin, stdout, stderr}
1010
import vector {Vector, flatten, reserve, vector, reset, resize}
@@ -23,7 +23,19 @@ export {String, split, chomp, strip, concat, has_substring, has_prefix, has_suff
2323
join}
2424
export {Result, Ok, Err}
2525
export {u8, i8}
26-
# export {nothing}
26+
export {Eq, ==, !=}
27+
export {Iterable, iter, nothing}
28+
export {not, True, False, Str, str, alloc}
29+
export {HasLength, len}
30+
export {Modular, %}
31+
export {Range, range, Ref}
32+
export {HasInsertableItems, insert, remove}
33+
export {compare, <, <=, >, >=, min, max}
34+
export {isspace, isdigit, isalpha, islower, isupper, tolower, toupper}
35+
export {pointer_subtraction, strlen, |}
36+
export {Either, Left, Right, serialize}
37+
export {any, all, not_in}
38+
export {print, int, float}
2739

2840
link pkg "bdw-gc"
2941

@@ -813,7 +825,7 @@ instance Serializeable Buffer {
813825
serialize = id
814826
}
815827

816-
fn __ptr_sub(p1 *Char, p2 *Char) Int {
828+
fn pointer_subtraction(p1 *Char, p2 *Char) Int {
817829
return (p1 as! Int) - (p2 as! Int)
818830
}
819831

lib/string.zion

+2-2
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,10 @@ fn split(input String, delim String) [String] {
352352
append(result, String(cur_big, big_len_left))
353353
return result
354354
} else {
355-
append(result, String(cur_big, __ptr_sub(next_little, cur_big)))
355+
append(result, String(cur_big, pointer_subtraction(next_little, cur_big)))
356356

357357
cur_big = __builtin_ptr_add(next_little, little_len)
358-
big_len_left = orig_big_len - __ptr_sub(cur_big, orig_big)
358+
big_len_left = orig_big_len - pointer_subtraction(cur_big, orig_big)
359359
if big_len_left == 0 {
360360
append(result, "")
361361
return result

lib/sys.zion

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import bufio {BufferedFileIO}
2+
23
export {
34
get_args, FileDescriptor, File, open, close, unlink, read, stdin, stdout,
45
stderr, Errno, get_errno, write, BufferedFileIO, readlines, CreateMode,

local.vimrc

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ autocmd FileType c nnoremap <F4> :wa<CR> :e %:p:s,.h$,.X123X,:s,.cpp$,.h,:s,.X12
55
autocmd FileType c inoremap <F4> <Esc> <F4>
66
autocmd FileType c vnoremap <F4> <Esc> <F4>
77
nnoremap <Leader>` :echom system("./zion-tags\ .")<CR>
8+
autocmd FileType * setlocal makeprg=make\ debug
89
let &path=substitute(
910
\ system('pkg-config bdw-gc --variable=includedir'),
1011
\ '\n', '', 'g') . ',.'

src/builtins.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "builtins.h"
22
#include "tld.h"
33

4-
std::string ARROW_TYPE_OPERATOR = "->";
5-
std::string PTR_TYPE_OPERATOR = "*";
4+
std::string ARROW_TYPE_OPERATOR = zion::tld::mktld("std", "->");
5+
std::string PTR_TYPE_OPERATOR = zion::tld::mktld("std", "*");
66
std::string REF_TYPE_OPERATOR = zion::tld::mktld("std", "Ref");
77

88
std::string CHAR_TYPE = "Char";

src/colors.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#define _bright_green(x) _COLOR(_ANSI_COLOR_BRIGHT_GREEN, x)
3939

4040
/* Semantic coloring helpers */
41-
#define C_MODULE _ANSI_COLOR_MAGENTA
41+
#define C_MODULE _ANSI_COLOR_GREEN
4242
#define C_LINE_REF _ANSI_COLOR_DIM
4343
#define C_ID _ANSI_COLOR_MAGENTA
4444
#define C_CONTROL _ANSI_COLOR_CYAN

src/compiler.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ struct GlobalParserState {
184184
modules_map_by_name[ps.module_name] = module;
185185
modules_map_by_filename[ps.filename] = module;
186186

187-
debug_above(8, log("while parsing %s got dependencies {%s}",
187+
debug_above(8, log("while parsing %s got module dependencies {%s}",
188188
ps.module_name.c_str(),
189189
join(dependencies, ", ").c_str()));
190190
for (auto dependency : dependencies) {
@@ -230,10 +230,12 @@ std::set<std::string> get_top_level_decls(
230230
top_level_decls.insert(overload_pair.first);
231231
}
232232
}
233+
/*
233234
for (auto &import : imports) {
234-
assert(tld::is_fqn(import.name, true /*default_special*/));
235+
assert(tld::is_fqn(import.name));
235236
top_level_decls.insert(import.name);
236237
}
238+
*/
237239
debug_above(8, log("tlds are %s", ::join(top_level_decls, ", ").c_str()));
238240
return top_level_decls;
239241
}
@@ -259,13 +261,14 @@ std::shared_ptr<Compilation> merge_compilation(
259261
module->imports);
260262

261263
std::set<std::string> bindings;
264+
log("bindings for %s", module->name.c_str());
262265
for (auto binding: maybe_not_tld_bindings) {
263266
bindings.insert(binding);
264267
bindings.insert(tld::tld(binding));
268+
log("bindings += %s", binding.c_str());
265269
}
266270
const Module *module_rebound = prefix(bindings, module);
267271

268-
269272
/* now all locally referring vars are fully qualified */
270273
for (const Decl *decl : module_rebound->decls) {
271274
program_decls.push_back(decl);
@@ -343,7 +346,9 @@ Compilation::ref parse_program(
343346
debug_above(11, log(log_info, "parse_module of %s succeeded",
344347
module_name.c_str(), false /*global*/));
345348

346-
/* find the import rewriting rules */
349+
/* find the import rewriting rules. this finds the final transitive
350+
* endpoint of any aliasing edges in the graph of import/exports, and
351+
* rewrites all non-leaf nodes as the leaf (endpoint) node. */
347352
RewriteImportRules rewriting_imports_rules = solve_rewriting_imports(
348353
gps.symbol_imports, gps.symbol_exports);
349354

src/import_rules.cpp

+48-7
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,62 @@ RewriteImportRules solve_rewriting_imports(
1111
const parser::SymbolExports &symbol_exports) {
1212
std::map<Identifier, Identifier> graph;
1313
std::set<std::string> legal_exports;
14+
1415
#ifdef ZION_DEBUG
1516
for (auto &module_pair : symbol_imports) {
1617
for (auto &pair_ids : module_pair.second) {
17-
debug_above(3,
18-
log("import: %s: %s -> %s", module_pair.first.c_str(),
19-
pair_ids.first.c_str(), join(pair_ids.second).c_str()));
18+
debug_above(3, log("import: " c_module("%s") " imports from " c_module(
19+
"%s") " {%s}",
20+
module_pair.first.c_str(), pair_ids.first.c_str(),
21+
join(pair_ids.second).c_str()));
2022
}
2123
}
2224
#endif
2325

26+
if (getenv("DOT_DEPS") != nullptr) {
27+
std::string filename = "zion.dot";
28+
FILE *fp = fopen(filename.c_str(), "wt");
29+
if (fp == nullptr) {
30+
throw zion::user_error(INTERNAL_LOC(),
31+
"unable to open %s for writing DOT_DEPS",
32+
filename.c_str());
33+
}
34+
fprintf(fp, "digraph G {\n");
35+
36+
std::unordered_set<std::string> deps;
37+
38+
for (auto &module_pair : symbol_imports) {
39+
for (auto &pair_ids : module_pair.second) {
40+
for (auto symbol : pair_ids.second) {
41+
42+
deps.insert(string_format("\"%s\" -> \"%s\";",
43+
tld::tld(module_pair.first).c_str(),
44+
tld::mktld(module_pair.first, symbol.name).c_str()));
45+
deps.insert(string_format("\"%s\" -> \"%s\";",
46+
tld::tld(module_pair.first).c_str(),
47+
tld::tld(pair_ids.first).c_str()));
48+
deps.insert(string_format("\"%s\" -> \"%s\";",
49+
tld::mktld(module_pair.first, symbol.name).c_str(),
50+
tld::mktld(pair_ids.first, symbol.name).c_str()));
51+
}
52+
}
53+
}
54+
55+
for (auto dep : deps) {
56+
fprintf(fp, " %s\n", dep.c_str());
57+
}
58+
fprintf(fp, "}\n");
59+
fclose(fp);
60+
}
61+
2462
for (auto &module_pair : symbol_exports) {
2563
for (auto &id_pair : module_pair.second) {
2664
debug_above(2, log("export: %s: %s -> %s", module_pair.first.c_str(),
2765
id_pair.first.str().c_str(),
2866
id_pair.second.str().c_str()));
2967
if (id_pair.second.name != id_pair.first.name) {
3068
/* this export actually leads back to something else */
31-
if (graph.count(id_pair.first) == 1) {
69+
if (graph.count(id_pair.first) != 0) {
3270
throw user_error(id_pair.first.location, "ambiguous export %s vs. %s",
3371
id_pair.first.str().c_str(),
3472
graph.at(id_pair.first).str().c_str());
@@ -53,7 +91,7 @@ RewriteImportRules solve_rewriting_imports(
5391
Identifier resolved_id = pair.second;
5492
std::set<Identifier> visited;
5593
std::list<Identifier> visited_list;
56-
while (graph.count(resolved_id) == 1) {
94+
while (graph.count(resolved_id) != 0) {
5795
visited.insert(resolved_id);
5896
visited_list.push_back(resolved_id);
5997

@@ -69,6 +107,7 @@ RewriteImportRules solve_rewriting_imports(
69107
throw error;
70108
}
71109
}
110+
72111
/* rewrite the graph as we go to avoid wasting time for future traversals */
73112
for (auto &id : visited_list) {
74113
graph[id] = resolved_id;
@@ -105,8 +144,10 @@ RewriteImportRules solve_rewriting_imports(
105144
debug_above(3, log("couldn't find %s in legal_exports. found %s",
106145
fqn.c_str(), join(legal_exports).c_str()));
107146
illegal_imports.push_back(
108-
{Identifier{tld::mktld(source_module, symbol.name), symbol.location},
109-
Identifier{tld::mktld(dest_module, symbol.name), symbol.location}});
147+
{Identifier{tld::mktld(source_module, symbol.name),
148+
symbol.location},
149+
Identifier{tld::mktld(dest_module, symbol.name),
150+
symbol.location}});
110151
}
111152
}
112153
}

src/main.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,16 @@ tarjan::Graph build_program_graph(const std::vector<const Decl *> &decls) {
351351
tarjan::Graph graph;
352352
for (auto decl : decls) {
353353
const auto &name = decl->id.name;
354-
graph.insert({name, get_free_vars(decl->value, {})});
354+
const auto free_vars = get_free_vars(decl->value, {});
355+
for (auto free_var : free_vars) {
356+
if (!zion::tld::is_fqn(free_var)) {
357+
throw user_error(
358+
decl->id.location,
359+
"found free_var \"%s\" that is not fully qualified within %s",
360+
free_var.c_str(), name.c_str());
361+
}
362+
}
363+
graph.insert({name, free_vars});
355364
}
356365
return graph;
357366
}
@@ -492,13 +501,13 @@ const Decl *find_overload_for_instance(std::string name,
492501
* impl.
493502
*/
494503
for (const Decl *decl : instance->decls) {
495-
assert(name == "*" || tld::is_fqn(name, true /*default_special*/));
504+
assert(tld::is_fqn(name));
496505
if (decl->id.name == name) {
497506
return decl;
498507
}
499508
}
500509
for (const Decl *decl : type_class->default_decls) {
501-
assert(tld::is_fqn(name, true /*default_special*/));
510+
assert(tld::is_fqn(name));
502511
if (decl->id.name == name) {
503512
return decl;
504513
}

src/parse_state.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,19 @@ Identifier ParseState::identifier_and_advance(bool map_id) {
8282
}
8383

8484
Identifier ParseState::id_mapped(Identifier id) {
85-
if (tld::is_fqn(id.name, true /*default_special*/)) {
85+
if (tld::is_fqn(id.name)) {
8686
/* this has already been mapped */
8787
return id;
8888
}
8989
auto iter = term_map.find(id.name);
9090
if (iter != term_map.end()) {
9191
return Identifier{iter->second, id.location};
9292
} else {
93-
if (id.name == "offset") {
94-
for (auto pair : term_map) {
95-
log("term_map = %s -> %s", pair.first.c_str(), pair.second.c_str());
96-
}
97-
}
93+
/* if (id.name == "offset") { */
94+
/* for (auto pair : term_map) { */
95+
/* log("term_map = %s -> %s", pair.first.c_str(), pair.second.c_str()); */
96+
/* } */
97+
/* } */
9898
return id;
9999
}
100100
}
@@ -114,7 +114,7 @@ void ParseState::add_term_map(Location location,
114114
std::string key,
115115
std::string value,
116116
bool allow_override) {
117-
// log("adding %s to term map => %s", key.c_str(), value.c_str());
117+
debug_above(3, log("adding %s to term map => %s", key.c_str(), value.c_str()));
118118
if (!allow_override && in(key, term_map)) {
119119
throw user_error(location, "symbol %s imported twice", key.c_str())
120120
.add_info(location, "%s was already mapped to %s", key.c_str(),

0 commit comments

Comments
 (0)