Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Commit

Permalink
Display scope in repl
Browse files Browse the repository at this point in the history
  • Loading branch information
jamii committed Sep 2, 2021
1 parent 798215d commit a8348cd
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion bin/run.zig
Expand Up @@ -35,7 +35,7 @@ pub fn main() anyerror!void {
.desired_id = &desired_id,
};
var error_info: ?imp.lang.InterpretErrorInfo = null;
const result = imp.lang.interpret(&arena, source.items, 0, interrupter, &error_info);
const result = imp.lang.interpret(&arena, source.items, .{ .Point = 0 }, interrupter, &error_info);
const writer = std.io.getStdOut().writer();
if (result) |type_and_set| {
try type_and_set.dumpInto(allocator, writer);
Expand Down
22 changes: 17 additions & 5 deletions lib/imp/lang.zig
Expand Up @@ -64,7 +64,7 @@ pub const InterpretError = pass.parse.Error ||
pub const InterpretResult = struct {
set_type: repr.type_.SetType,
set: repr.value.Set,
watch_results: []const repr.value.Set,
watch_results: []const pass.interpret.ScopeAndSet,
watch_range: ?[2]usize,

pub fn dumpInto(self: InterpretResult, allocator: *Allocator, out_stream: anytype) anyerror!void {
Expand All @@ -73,9 +73,21 @@ pub const InterpretResult = struct {
try out_stream.writeAll("\nvalue:\n");
try self.set.dumpInto(allocator, out_stream);
if (self.watch_range) |_| {
try out_stream.writeAll("\nwatch values:\n");
for (self.watch_results) |set| {
try set.dumpInto(allocator, out_stream);
try out_stream.writeAll("\nwatch:\n\n");
for (self.watch_results) |scope_and_set| {
for (scope_and_set.scope) |arg_and_scalar| {
// TODO might want to print boxes when we have good scope detection and better box printing
if (arg_and_scalar.scalar != .Box) {
const maybe_box: []const u8 = if (arg_and_scalar.arg.unbox) "@" else "";
try std.fmt.format(out_stream, "{s}: {}{s}; ", .{
arg_and_scalar.arg.name,
arg_and_scalar.scalar,
maybe_box,
});
}
}
try out_stream.writeAll("\n");
try scope_and_set.set.dumpInto(allocator, out_stream);
try out_stream.writeAll("\n");
}
}
Expand Down Expand Up @@ -121,7 +133,7 @@ pub fn interpret(
const watch_meta = Store.getSyntaxMeta(Store.getCoreMeta(watch_expr).from);
watch_range = .{ watch_meta.start, watch_meta.end };
}
var watch_results = ArrayList(repr.value.Set).init(&arena.allocator);
var watch_results = ArrayList(pass.interpret.ScopeAndSet).init(&arena.allocator);
var interpret_error_info: ?pass.interpret.ErrorInfo = null;
const set = pass.interpret.interpret(&store, arena, core_expr, watch_expr_o, &watch_results, interrupter, &interpret_error_info) catch |err| {
if (err == error.InterpretError or err == error.NativeError) {
Expand Down
2 changes: 1 addition & 1 deletion lib/imp/lang/pass/desugar.zig
Expand Up @@ -46,7 +46,7 @@ const Desugarer = struct {
}

fn putCore(self: *Desugarer, core_expr: core.Expr) Error!*const core.Expr {
return self.store.putCore(core_expr, self.current_expr.?);
return self.store.putCore(core_expr, self.current_expr.?, try std.mem.dupe(&self.store.arena.allocator, ?syntax.Arg, self.scope.items));
}

fn desugar(self: *Desugarer, syntax_expr: *const syntax.Expr) Error!*const core.Expr {
Expand Down
29 changes: 24 additions & 5 deletions lib/imp/lang/pass/interpret.zig
Expand Up @@ -3,6 +3,7 @@ usingnamespace imp.common;
const meta = imp.meta;
const Store = imp.lang.Store;
const core = imp.lang.repr.core;
const syntax = imp.lang.repr.syntax;
const value = imp.lang.repr.value;

/// Guarantees:
Expand All @@ -13,7 +14,7 @@ pub fn interpret(
arena: *ArenaAllocator,
expr: *const core.Expr,
watch_expr_o: ?*const core.Expr,
watch_results: *ArrayList(value.Set),
watch_results: *ArrayList(ScopeAndSet),
interrupter: imp.lang.Interrupter,
error_info: *?ErrorInfo,
) Error!value.Set {
Expand Down Expand Up @@ -46,13 +47,23 @@ pub const ErrorInfo = struct {
message: []const u8,
};

pub const ScopeAndSet = struct {
scope: []const ArgAndScalar,
set: value.Set,
};

pub const ArgAndScalar = struct {
arg: syntax.Arg,
scalar: value.Scalar,
};

// --------------------------------------------------------------------------------

const Interpreter = struct {
store: *const Store,
arena: *ArenaAllocator,
watch_expr_o: ?*const core.Expr,
watch_results: *ArrayList(value.Set),
watch_results: *ArrayList(ScopeAndSet),
scope: ArrayList(value.Scalar),
time: ArrayList(value.Time),
boxes: DeepHashMap(value.LazySet, value.Set),
Expand Down Expand Up @@ -81,9 +92,17 @@ const Interpreter = struct {
const result = self.interpretInner(expr, hint);
if (self.watch_expr_o) |watch_expr|
if (expr == watch_expr)
if (result) |set|
try self.watch_results.append(set)
else |_| {};
if (result) |set| {
var scope = ArrayList(ArgAndScalar).init(&self.arena.allocator);
const watch_meta = Store.getCoreMeta(expr);
for (watch_meta.scope) |arg_o, i|
if (arg_o) |arg|
try scope.append(.{ .arg = arg, .scalar = self.scope.items[i] });
try self.watch_results.append(.{
.scope = scope.toOwnedSlice(),
.set = set,
});
} else |_| {};
return result;
}

Expand Down
4 changes: 3 additions & 1 deletion lib/imp/lang/store.zig
Expand Up @@ -12,6 +12,7 @@ pub const SyntaxMeta = struct {

pub const CoreMeta = struct {
from: *const syntax.Expr,
scope: []const ?syntax.Arg,
// this is just useful as a stable id for tests
id: usize,
};
Expand Down Expand Up @@ -43,14 +44,15 @@ pub const Store = struct {
return &expr_and_meta.expr;
}

pub fn putCore(self: *Store, expr: core.Expr, from: *const syntax.Expr) !*const core.Expr {
pub fn putCore(self: *Store, expr: core.Expr, from: *const syntax.Expr, scope: []const ?syntax.Arg) !*const core.Expr {
var expr_and_meta = try self.arena.allocator.create(ExprAndMeta(core.Expr, CoreMeta));
const id = self.next_expr_id;
self.next_expr_id += 1;
expr_and_meta.* = .{
.expr = expr,
.meta = CoreMeta{
.from = from,
.scope = scope,
.id = id,
},
};
Expand Down

0 comments on commit a8348cd

Please sign in to comment.