Skip to content

Commit

Permalink
Docs for value ownership and references. Placeholder types for refere…
Browse files Browse the repository at this point in the history
…nces. Ref slice of array.
  • Loading branch information
fubark committed Jul 28, 2024
1 parent 884668a commit e5f458c
Show file tree
Hide file tree
Showing 17 changed files with 848 additions and 160 deletions.
387 changes: 362 additions & 25 deletions docs/docs.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/gen-docs.cy
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ hljs.registerLanguage('cy', function() {
keyword: [
'func', 'mod', 'for', 'coinit', 'coresume', 'coyield', 'use', 'await', 'context',
'return', 'if', 'else', 'as', 'while', 'var', 'let', 'dynobject', 'object', 'struct', 'cstruct', 'with', 'caught',
'break', 'continue', 'switch', 'pass', 'or', 'and', 'not', 'is', 'error', 'throws',
'break', 'continue', 'switch', 'pass', 'or', 'and', 'not', 'is', 'error', 'throws', 'move',
'true', 'false', 'none', 'throw', 'try', 'catch', 'recover', 'enum', 'type', 'case', 'trait'
],
type: [
Expand Down
5 changes: 5 additions & 0 deletions docs/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ blockquote p {
padding: 0px;
}

p {
margin-block-start: 1.3em;
margin-block-end: 1.3em;
}

blockquote em {
color: #fb6767;
}
Expand Down
45 changes: 32 additions & 13 deletions src/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ pub const NodeType = enum(u7) {
enumMember,
error_lit,
expandOpt,
expand_ptr,
exprStmt,
falseLit,
forIterStmt,
Expand Down Expand Up @@ -75,9 +74,15 @@ pub const NodeType = enum(u7) {
octLit,
opAssignStmt,
passStmt,
pointer_slice,
ptr,
ptr_slice,
range,
raw_string_lit,

// Ref type or address of operator.
ref,

ref_slice,
returnExprStmt,
returnStmt,
root,
Expand Down Expand Up @@ -114,7 +119,7 @@ pub const AttributeType = enum(u8) {
host,
};

const PointerSlice = struct {
const PtrSlice = struct {
elem: *Node align(8),
pos: u32,
};
Expand All @@ -124,7 +129,17 @@ const ExpandOpt = struct {
pos: u32,
};

const ExpandPtr = struct {
const RefSlice = struct {
elem: *Node align(8),
pos: u32,
};

const Ref = struct {
elem: *Node align(8),
pos: u32,
};

const Ptr = struct {
elem: *Node align(8),
pos: u32,
};
Expand Down Expand Up @@ -586,6 +601,8 @@ fn NodeData(comptime node_t: NodeType) type {
.await_expr => AwaitExpr,
.binExpr => BinExpr,
.binLit => Span,
.ref => Ref,
.ref_slice => RefSlice,
.breakStmt => Token,
.caseBlock => CaseBlock,
.callExpr => CallExpr,
Expand All @@ -610,7 +627,6 @@ fn NodeData(comptime node_t: NodeType) type {
.enumMember => EnumMember,
.error_lit => Span,
.expandOpt => ExpandOpt,
.expand_ptr => ExpandPtr,
.exprStmt => ExprStmt,
.falseLit => Token,
.forIterStmt => ForIterStmt,
Expand Down Expand Up @@ -641,7 +657,8 @@ fn NodeData(comptime node_t: NodeType) type {
.octLit => Span,
.opAssignStmt => OpAssignStmt,
.passStmt => Token,
.pointer_slice => PointerSlice,
.ptr => Ptr,
.ptr_slice => PtrSlice,
.range => Range,
.raw_string_lit => Span,
.returnExprStmt => ReturnExprStmt,
Expand Down Expand Up @@ -747,9 +764,7 @@ pub const Node = struct {
.enumMember => self.cast(.enumMember).name.pos(),
.error_lit => self.cast(.error_lit).pos-6,
.expandOpt => self.cast(.expandOpt).pos,
.expand_ptr => self.cast(.expand_ptr).pos,
.exprStmt => self.cast(.exprStmt).child.pos(),
.pointer_slice => self.cast(.pointer_slice).pos,
.falseLit => self.cast(.falseLit).pos,
.floatLit => self.cast(.floatLit).pos,
.forIterStmt => self.cast(.forIterStmt).pos,
Expand Down Expand Up @@ -779,8 +794,12 @@ pub const Node = struct {
.octLit => self.cast(.octLit).pos,
.opAssignStmt => self.cast(.opAssignStmt).left.pos(),
.passStmt => self.cast(.passStmt).pos,
.ptr => self.cast(.ptr).pos,
.ptr_slice => self.cast(.ptr_slice).pos,
.range => self.cast(.range).pos,
.raw_string_lit => self.cast(.raw_string_lit).pos,
.ref => self.cast(.ref).pos,
.ref_slice => self.cast(.ref_slice).pos,
.returnExprStmt => self.cast(.returnExprStmt).pos,
.returnStmt => self.cast(.returnStmt).pos,
.root => self.cast(.root).stmts[0].pos(),
Expand Down Expand Up @@ -883,7 +902,7 @@ pub const UnaryOp = enum(u8) {
};

test "ast internals." {
try t.eq(std.enums.values(NodeType).len, 98);
try t.eq(std.enums.values(NodeType).len, 101);
try t.eq(@sizeOf(NodeHeader), 1);
}

Expand Down Expand Up @@ -1472,17 +1491,17 @@ pub const Encoder = struct {
try w.writeAll("...");
try w.writeByte('}');
},
.pointer_slice => {
.ptr_slice => {
try w.writeAll("[*]");
try self.write(w, node.cast(.pointer_slice).elem);
try self.write(w, node.cast(.ptr_slice).elem);
},
.expandOpt => {
try w.writeByte('?');
try self.write(w, node.cast(.expandOpt).param);
},
.expand_ptr => {
.ptr => {
try w.writeAll("*");
try self.write(w, node.cast(.expand_ptr).elem);
try self.write(w, node.cast(.ptr).elem);
},
.comptimeExpr => {
try self.write(w, node.cast(.comptimeExpr).child);
Expand Down
27 changes: 23 additions & 4 deletions src/builtins/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,18 @@ pub fn listSetIndex(vm: *cy.VM) Value {
return Value.Void;
}

pub fn listSlice(vm: *cy.VM) anyerror!Value {
pub fn List_slice(vm: *cy.VM) anyerror!Value {
const list = vm.getValue(0).asHeapObject();
const range = vm.getValue(1).castHeapObject(*cy.heap.Range);
const inner = cy.ptrAlignCast(*cy.List(Value), &list.list.list);
if (range.start < 0) {
return vm.prepPanic("Out of bounds.");
return error.OutOfBounds;
}
if (range.end > inner.len) {
return vm.prepPanic("Out of bounds.");
return error.OutOfBounds;
}
if (range.end < range.start) {
return vm.prepPanic("Out of bounds.");
return error.OutOfBounds;
}

const elems = inner.buf[@intCast(range.start)..@intCast(range.end)];
Expand All @@ -275,6 +275,25 @@ pub fn listSlice(vm: *cy.VM) anyerror!Value {
return cy.heap.allocListDyn(vm, elems);
}

pub fn ListValue_slice(vm: *cy.VM) anyerror!Value {
const list = vm.getValue(0).asHeapObject();
const slice_t: cy.TypeId = @intCast(vm.getInt(1));
const range = vm.getValue(2).castHeapObject(*cy.heap.Range);
const inner = cy.ptrAlignCast(*cy.List(Value), &list.list.list);
if (range.start < 0) {
return error.OutOfBounds;
}
if (range.end > inner.len) {
return error.OutOfBounds;
}
if (range.end < range.start) {
return error.OutOfBounds;
}

const ptr = inner.buf.ptr + @as(usize, @intCast(range.start));
return vm.allocRefSlice(slice_t, ptr, @intCast(range.end - range.start));
}

pub fn listInsert(vm: *cy.VM) Value {
const index: i64 = @intCast(vm.getInt(1));
const value = vm.getValue(2);
Expand Down
53 changes: 41 additions & 12 deletions src/builtins/builtins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const funcs = [_]C.HostFuncEntry{
func("performGC", zErrFunc(performGC)),
func("ptrcast_", zErrFunc(ptrcast)),
func("print", print),
func("refcast", zErrFunc(refcast)),
func("queueTask", zErrFunc(queueTask)),
func("runestr", zErrFunc(runestr)),
func("sizeof_", sizeof),
Expand Down Expand Up @@ -116,7 +117,7 @@ const funcs = [_]C.HostFuncEntry{

// List
func("List.$index", bindings.listIndex),
func("List.$indexRange", zErrFunc(bindings.listSlice)),
func("List.$indexRange", zErrFunc(bindings.List_slice)),
func("List.$setIndex", bindings.listSetIndex),
func("List.append", zErrFunc(bindings.listAppend)),
func("List.appendAll", zErrFunc(bindings.listAppendAll)),
Expand All @@ -132,6 +133,20 @@ const funcs = [_]C.HostFuncEntry{
// ListIterator
func("ListIterator.next_", zErrFunc(bindings.listIteratorNext)),

// Slice
// func("Slice.endsWith", Slice_endsWith),
// func("Slice.find", Slice_find),
// func("Slice.split", zErrFunc(sliceSplit)),
// func("Slice.startsWith", Slice_startsWith),
// func("Slice.trim", sliceTrim),

// RefSlice
// func("RefSlice.endsWith", RefSlice_endsWith),
// func("RefSlice.find", RefSlice_find),
// func("RefSlice.split", zErrFunc(sliceSplit)),
// func("RefSlice.startsWith", RefSlice_startsWith),
// func("RefSlice.trim", sliceTrim),

// Tuple
func("Tuple.$index", bindings.tupleIndex),

Expand Down Expand Up @@ -231,7 +246,7 @@ const funcs = [_]C.HostFuncEntry{

// DefaultMemory
func("DefaultMemory.alloc", zErrFunc(DefaultMemory_alloc)),
func("DefaultMemory.free", zErrFunc(DefaultMemory_free)),
func("DefaultMemory.free", zErrFunc(DefaultMemory_free)),
};

const types = [_]C.HostTypeEntry{
Expand Down Expand Up @@ -293,8 +308,8 @@ pub const BuiltinsData = struct {
OptionTuple: cy.TypeId,
OptionMap: cy.TypeId,
OptionString: cy.TypeId,
PointerVoid: cy.TypeId,
SliceByte: cy.TypeId,
PtrVoid: cy.TypeId,
PtrSliceByte: cy.TypeId,
};

pub fn create(vm: *cy.VM, r_uri: []const u8) C.Module {
Expand Down Expand Up @@ -350,7 +365,7 @@ fn onLoad(vm_: ?*C.VM, mod: C.Sym) callconv(.C) void {

const void_t = C.newType(vm_, bt.Void);
defer C.release(vm_, void_t);
assert(C.expandTemplateType(pointer_tmpl, &void_t, 1, &data.PointerVoid));
assert(C.expandTemplateType(pointer_tmpl, &void_t, 1, &data.PtrVoid));

const list_tmpl = chunk_sym.getMod().getSym("List").?.toC();

Expand All @@ -362,10 +377,10 @@ fn onLoad(vm_: ?*C.VM, mod: C.Sym) callconv(.C) void {
const list_iter_tmpl = chunk_sym.getMod().getSym("ListIterator").?.toC();
assert(C.expandTemplateType(list_iter_tmpl, &dynamic_t, 1, &temp));

const slice_tmpl = chunk_sym.getMod().getSym("Slice").?.toC();
const ptr_slice_tmpl = chunk_sym.getMod().getSym("PtrSlice").?.toC();
const byte_t = C.newType(vm_, bt.Byte);
defer C.release(vm_, byte_t);
assert(C.expandTemplateType(slice_tmpl, &byte_t, 1, &data.SliceByte));
assert(C.expandTemplateType(ptr_slice_tmpl, &byte_t, 1, &data.PtrSliceByte));

// Verify all core types have been initialized.
if (cy.Trace) {
Expand Down Expand Up @@ -446,9 +461,11 @@ pub fn listFill(vm: *cy.VM) Value {
return vm.allocListFill(vm.getValue(0), @intCast(vm.getInt(1))) catch cy.fatal();
}

pub fn refcast(vm: *cy.VM) anyerror!Value {
return vm.getValue(0);
}

pub fn ptrcast(vm: *cy.VM) anyerror!Value {
const ptr_t: cy.TypeId = @intCast(vm.getInt(0));
_ = ptr_t;
return vm.getValue(1);
}

Expand Down Expand Up @@ -754,7 +771,7 @@ pub fn DefaultMemory_alloc(vm: *cy.VM) anyerror!Value {
const ptr_v = Value.initRaw(@intCast(@intFromPtr(ptr)));

const data = vm.getData(*BuiltinsData, "builtins");
return vm.allocObjectSmall(data.SliceByte, &.{ ptr_v, Value.initInt(@intCast(size)) });
return vm.allocObjectSmall(data.PtrSliceByte, &.{ ptr_v, Value.initInt(@intCast(size)) });
}

pub fn DefaultMemory_free(vm: *cy.VM) anyerror!Value {
Expand All @@ -766,6 +783,18 @@ pub fn DefaultMemory_free(vm: *cy.VM) anyerror!Value {
return Value.Void;
}

fn Slice_startsWith(vm: *cy.VM) Value {
const slice = vm.getObject(*cy.heap.Array, 0).getSlice();
const needle = vm.getArray(1);
return Value.initBool(std.mem.startsWith(u8, slice, needle));
}

fn Slice_endsWith(vm: *cy.VM) Value {
const slice = vm.getObject(*cy.heap.Array, 0).getSlice();
const needle = vm.getArray(1);
return Value.initBool(std.mem.endsWith(u8, slice, needle));
}

fn arrayConcat(vm: *cy.VM) Value {
const slice = vm.getArray(0);
const rslice = vm.getArray(1);
Expand Down Expand Up @@ -815,7 +844,7 @@ fn arrayInsert(vm: *cy.VM) anyerror!Value {
return Value.initNoCycPtr(new);
}

fn arrayFind(vm: *cy.VM) Value {
fn Slice_find(vm: *cy.VM) Value {
const obj = vm.getObject(*cy.heap.Array, 0);
const slice = obj.getSlice();
const needle = vm.getArray(1);
Expand Down Expand Up @@ -1200,7 +1229,7 @@ fn pointerGet(vm: *cy.VM) anyerror!Value {
const addr: usize = @intFromPtr(ptr) + @as(usize, @intCast(uoff));
const val = @as(*?*anyopaque, @ptrFromInt(addr)).*;
const data = vm.getData(*BuiltinsData, "builtins");
return vm.allocPointer(data.PointerVoid, val);
return vm.allocPointer(data.PtrVoid, val);
},
else => {
return error.InvalidArgument;
Expand Down
Loading

0 comments on commit e5f458c

Please sign in to comment.