2019-05-10 11:34:20 +03:00
|
|
|
class Mal.Main : GLib.Object {
|
|
|
|
static bool eof;
|
|
|
|
|
|
|
|
static construct {
|
|
|
|
eof = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Mal.Val? READ() {
|
|
|
|
string? line = Readline.readline("user> ");
|
|
|
|
if (line != null) {
|
|
|
|
if (line.length > 0)
|
|
|
|
Readline.History.add(line);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return Reader.read_str(line);
|
|
|
|
} catch (Mal.Error err) {
|
|
|
|
GLib.stderr.printf("%s\n", err.message);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stdout.printf("\n");
|
|
|
|
eof = true;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Mal.Val EVAL(Mal.Val expr) {
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void PRINT(Mal.Val value) {
|
|
|
|
stdout.printf("%s\n", pr_str(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void rep() {
|
|
|
|
Mal.Val? val = READ();
|
|
|
|
if (val != null) {
|
|
|
|
val = EVAL(val);
|
|
|
|
PRINT(val);
|
vala: implement a garbage collector.
This fixes a lot of memory leakage due to me having relied on the Vala
reference-counting system to do my memory management.
The most obvious bad result of that design decision was the memory
leak from Mal.Nil pointing to itself as metadata. But fixing that
didn't solve the whole problem, because other kinds of reference cycle
are common.
In particular, the idiom `(def! FUNC (fn* (ARGS) BODY))`, for defining
a function in the most obvious way, would create a cycle of two
objects: from the outer environment in which FUNC is defined, to the
function object for FUNC itself, back to that same environment because
it was captured by FUNC.
And _either_ of those objects could end up being the only element of
the cycle referred to from the rest of the system: it could be the
environment, if nothing ever uses that function definition, or it
could be the function, if that function object is looked up and
returned from an outer function that was the last user of the
environment. So you can't break the cycle in the way that reference
counting systems would like you to, by making a well-chosen one of the
links weak: there's no universally right choice for which one it needs
to be.
So I've fixed it properly by writing a simple garbage collector. In
Vala's ref-counted environment, that works by being the only thing
allowed to hold an _owning_ reference to any derivative of GC.Object.
All the normal kinds of link between objects are now weak references;
each object provides a gc_traverse method which lists all the things
it links to; and when the garbage collector runs, it unlinks any
unwanted objects from its big linked list of all of them, causing the
one owned reference to each one to disappear.
Now the perf3 test can run without its memory usage gradually
increasing.
2019-05-12 12:05:34 +03:00
|
|
|
GC.Core.maybe_collect();
|
2019-05-10 11:34:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int main(string[] args) {
|
|
|
|
while (!eof)
|
|
|
|
rep();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|