Compare commits
No commits in common. "90590b964a454aaa86231e9900f32a2918065e7a" and "c143b4d7894621bfb2bfb29b04adec5f14897d55" have entirely different histories.
90590b964a
...
c143b4d789
8 changed files with 64 additions and 90 deletions
|
|
@ -1,30 +1,26 @@
|
|||
// @todo(judah): replace array_add
|
||||
|
||||
append :: inline (arr: *[..]$T, value: T) -> *T {
|
||||
mem.lazy_set_allocator(arr);
|
||||
init :: inline (arr: *[..]$T, allocator: Allocator) {
|
||||
arr.allocator = allocator;
|
||||
}
|
||||
|
||||
append :: inline (arr: *[..]$T, value: T) -> *T {
|
||||
ptr := basic.array_add(arr,, allocator = arr.allocator);
|
||||
ptr.* = value;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
append :: inline (arr: *[..]$T, values: ..T) -> *T {
|
||||
mem.lazy_set_allocator(arr);
|
||||
|
||||
count := arr.count;
|
||||
basic.array_add(arr, ..values,, allocator = arr.allocator);
|
||||
return *arr.data[count];
|
||||
}
|
||||
|
||||
append :: inline (arr: *[..]$T) -> *T {
|
||||
mem.lazy_set_allocator(arr);
|
||||
|
||||
return basic.array_add(arr,, allocator = arr.allocator);
|
||||
}
|
||||
|
||||
resize :: inline (arr: *[..]$T, new_count: int) {
|
||||
mem.lazy_set_allocator(arr);
|
||||
|
||||
if new_count <= arr.allocated return;
|
||||
basic.array_reserve(arr, new_count,, allocator = arr.allocator);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,11 @@ Stable_Array :: struct(T: Type, items_per_chunk := 32) {
|
|||
Chunk :: Static_Array(items_per_chunk, T);
|
||||
}
|
||||
|
||||
init :: (a: *Stable_Array, allocator: Allocator) {
|
||||
a.allocator = allocator;
|
||||
a.chunks.allocator = allocator;
|
||||
}
|
||||
|
||||
append :: (a: *Stable_Array) -> *a.T {
|
||||
chunk := find_or_create_chunk(a, 1);
|
||||
a.count += 1;
|
||||
|
|
@ -114,14 +119,19 @@ find_or_create_chunk :: (a: *Stable_Array, amount: int) -> *a.Chunk {
|
|||
}
|
||||
|
||||
create_chunk :: (a: *Stable_Array) -> *a.Chunk {
|
||||
mem.lazy_set_allocator(a);
|
||||
mem.lazy_set_allocator(*a.chunks);
|
||||
inline try_lazy_init(a);
|
||||
|
||||
chunk := mem.request_memory(a.Chunk,, allocator = a.allocator);
|
||||
append(*a.chunks, chunk);
|
||||
return chunk;
|
||||
}
|
||||
|
||||
try_lazy_init :: (a: *Stable_Array) {
|
||||
if a.allocator.proc == null {
|
||||
init(a, context.allocator);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// TESTS
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ Buffer :: struct {
|
|||
}
|
||||
|
||||
append :: inline (buf: *Buffer, ptr: *void, count: int) {
|
||||
inline meta.lazy_set_allocator(buf);
|
||||
|
||||
free_space := ensure_buffer_has_room(buf, count);
|
||||
memcpy(free_space, ptr, count);
|
||||
buf.count += count;
|
||||
|
|
@ -28,7 +26,6 @@ reset :: inline (buf: *Buffer) {
|
|||
#scope_file;
|
||||
|
||||
array :: #import "jc/array";
|
||||
meta :: #import "jc/meta";
|
||||
|
||||
ensure_buffer_has_room :: (buf: *Buffer, count: int) -> *u8 {
|
||||
ptr := buf.data.data + buf.count;
|
||||
|
|
|
|||
|
|
@ -18,6 +18,12 @@ Kv :: struct(Key: Type, Value: Type) {
|
|||
number_of_items_to_allocate_initially :: 16; // @note(judah): must be a power of two
|
||||
}
|
||||
|
||||
init :: (kv: *Kv, allocator: Allocator) {
|
||||
kv.allocator = allocator;
|
||||
kv.slots.allocator = allocator;
|
||||
kv.free_slots.allocator = allocator;
|
||||
}
|
||||
|
||||
get :: (kv: *Kv, key: kv.Key) -> kv.Value, bool {
|
||||
slot, ok := find_slot(kv, get_hash(kv, key));
|
||||
if !ok {
|
||||
|
|
@ -118,9 +124,9 @@ mark_slot_for_reuse :: (kv: *Kv, index: int) {
|
|||
}
|
||||
|
||||
try_lazy_init :: inline (kv: *Kv) {
|
||||
mem.lazy_set_allocator(kv);
|
||||
mem.lazy_set_allocator(*kv.slots);
|
||||
mem.lazy_set_allocator(*kv.free_slots);
|
||||
if kv.allocator.proc == null {
|
||||
init(kv, context.allocator);
|
||||
}
|
||||
}
|
||||
|
||||
mem :: #import "jc/memory";
|
||||
|
|
|
|||
|
|
@ -147,31 +147,6 @@ release_memory :: inline (str: string) {
|
|||
release_memory(str.data.(*void));
|
||||
}
|
||||
|
||||
// @todo(judah): why can't we use $T/struct{ allocator: Allocator }?
|
||||
lazy_set_allocator :: (thing: *$T, allocator := context.allocator) #modify {
|
||||
info := T.(*Type_Info_Struct);
|
||||
|
||||
ok := false;
|
||||
if info.type == .STRUCT ok = true;
|
||||
|
||||
if ok for info.members if it.name == "allocator" && it.type == Allocator.(*Type_Info) {
|
||||
ok = true;
|
||||
break;
|
||||
}
|
||||
|
||||
return ok, "can only set allocator on struct with allocator field or dynamic array";
|
||||
} #expand {
|
||||
if thing.allocator.proc == null {
|
||||
thing.allocator = allocator;
|
||||
}
|
||||
}
|
||||
|
||||
lazy_set_allocator :: (array: *[..]$T, allocator := context.allocator) #expand {
|
||||
if array.allocator.proc == null {
|
||||
array.allocator = allocator;
|
||||
}
|
||||
}
|
||||
|
||||
#load "allocators.jai";
|
||||
|
||||
#scope_file;
|
||||
|
|
|
|||
|
|
@ -36,9 +36,19 @@ Interp_Value :: struct {
|
|||
}
|
||||
}
|
||||
|
||||
interp_program :: (i: *Interp) {
|
||||
try_lazy_init(i);
|
||||
init :: (i: *Interp, allocator: Allocator) {
|
||||
value_nil = make_interp_value(i, .nil);
|
||||
|
||||
value_true = make_interp_value(i, .bool);
|
||||
value_true.b = true;
|
||||
|
||||
value_false = make_interp_value(i, .bool);
|
||||
value_false.b = false;
|
||||
|
||||
i.global = make_scope(null,, allocator = allocator);
|
||||
}
|
||||
|
||||
interp_program :: (i: *Interp) {
|
||||
for i.toplevel {
|
||||
interp_statement(i, it, i.global);
|
||||
}
|
||||
|
|
@ -299,6 +309,7 @@ find_symbol :: (scope: *Interp_Scope, symbol: string, all_the_way_up := true) ->
|
|||
make_scope :: (parent: *Interp_Scope) -> *Interp_Scope {
|
||||
scope := mem.request_memory(Interp_Scope);
|
||||
scope.parent = parent;
|
||||
kv.init(*scope.bindings, context.allocator);
|
||||
return scope;
|
||||
}
|
||||
|
||||
|
|
@ -307,15 +318,3 @@ make_interp_value :: (i: *Interp, kind: Interp_Value.Kind) -> *Interp_Value {
|
|||
value.kind = kind;
|
||||
return value;
|
||||
}
|
||||
|
||||
try_lazy_init :: (i: *Interp) {
|
||||
value_nil = make_interp_value(i, .nil);
|
||||
|
||||
value_true = make_interp_value(i, .bool);
|
||||
value_true.b = true;
|
||||
|
||||
value_false = make_interp_value(i, .bool);
|
||||
value_false.b = false;
|
||||
|
||||
i.global = make_scope(null);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ strings :: #import "String"; // @future
|
|||
|
||||
#run {
|
||||
parser: Parser;
|
||||
init(*parser, context.allocator);
|
||||
|
||||
ok := parse_string(*parser, #string END
|
||||
fn add(l, r) do return l + r end
|
||||
fn sub(l, r) do return l - r end
|
||||
|
|
@ -85,6 +87,7 @@ strings :: #import "String"; // @future
|
|||
|
||||
interp: Interp;
|
||||
interp.toplevel = parser.toplevel;
|
||||
init(*interp, context.allocator);
|
||||
|
||||
interp_program(*interp);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -208,14 +208,6 @@ Node_Type :: struct {
|
|||
array;
|
||||
procedure;
|
||||
}
|
||||
|
||||
init :: (n: *Node_Type, allocator: Allocator) {
|
||||
if n.type_kind == {
|
||||
case .procedure;
|
||||
mem.lazy_set_allocator(*n.procedure_arguments, allocator);
|
||||
mem.lazy_set_allocator(*n.procedure_returns, allocator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Node_Procedure :: struct {
|
||||
|
|
@ -233,11 +225,6 @@ Node_Procedure :: struct {
|
|||
Flag :: enum_flags {
|
||||
must_inline;
|
||||
}
|
||||
|
||||
init :: (n: *Node_Procedure, allocator: Allocator) {
|
||||
mem.lazy_set_allocator(*n.arguments, allocator);
|
||||
mem.lazy_set_allocator(*n.returns, allocator);
|
||||
}
|
||||
}
|
||||
|
||||
Parameter :: struct {
|
||||
|
|
@ -267,11 +254,6 @@ Node_Procedure_Call :: struct {
|
|||
call_expr: *Node;
|
||||
named_arguments: kv.Kv(*Node, *Node);
|
||||
all_arguments: [..]*Node;
|
||||
|
||||
init :: (n: *Node_Procedure_Call, allocator: Allocator) {
|
||||
mem.lazy_set_allocator(*n.all_arguments, allocator);
|
||||
mem.lazy_set_allocator(*n.named_arguments, allocator);
|
||||
}
|
||||
}
|
||||
|
||||
Node_Block :: struct {
|
||||
|
|
@ -279,10 +261,6 @@ Node_Block :: struct {
|
|||
n.kind = .block;
|
||||
|
||||
body: [..]*Node;
|
||||
|
||||
init :: (n: *Node_Block, allocator: Allocator) {
|
||||
mem.lazy_set_allocator(*n.body, allocator);
|
||||
}
|
||||
}
|
||||
|
||||
Node_Return :: struct {
|
||||
|
|
@ -290,10 +268,6 @@ Node_Return :: struct {
|
|||
n.kind = .return_;
|
||||
|
||||
values: [..]*Node;
|
||||
|
||||
init :: (n: *Node_Return, allocator: Allocator) {
|
||||
mem.lazy_set_allocator(*n.values, allocator);
|
||||
}
|
||||
}
|
||||
|
||||
Parser :: struct {
|
||||
|
|
@ -306,9 +280,12 @@ Parser :: struct {
|
|||
offset: int;
|
||||
}
|
||||
|
||||
parse_string :: (p: *Parser, source: string) -> bool {
|
||||
try_lazy_init(p);
|
||||
init :: (p: *Parser, allocator: Allocator) {
|
||||
p.allocator = allocator;
|
||||
p.toplevel.allocator = allocator;
|
||||
}
|
||||
|
||||
parse_string :: (p: *Parser, source: string) -> bool {
|
||||
p.source = source;
|
||||
p.offset = 0;
|
||||
|
||||
|
|
@ -776,8 +753,17 @@ parse_expression_base :: (p: *Parser) -> *Node {
|
|||
make_node :: (p: *Parser, $T: Type) -> *T {
|
||||
node := mem.request_memory(T,, allocator = p.allocator);
|
||||
|
||||
#if #exists(T.init) {
|
||||
T.init(node, p.allocator);
|
||||
#if T == { // nodes that require initialization
|
||||
case Node_Block;
|
||||
array.init(*node.body, p.allocator);
|
||||
case Node_Return;
|
||||
array.init(*node.values, p.allocator);
|
||||
case Node_Procedure_Call;
|
||||
array.init(*node.all_arguments, p.allocator);
|
||||
kv.init(*node.named_arguments, p.allocator);
|
||||
case Node_Procedure;
|
||||
array.init(*node.arguments, p.allocator);
|
||||
array.init(*node.returns, p.allocator);
|
||||
}
|
||||
|
||||
return node;
|
||||
|
|
@ -786,7 +772,13 @@ make_node :: (p: *Parser, $T: Type) -> *T {
|
|||
make_node :: (p: *Parser, $type_kind: Node_Type.Type_Kind) -> *Node_Type {
|
||||
type := mem.request_memory(Node_Type,, allocator = p.allocator);
|
||||
type.type_kind = type_kind;
|
||||
Node_Type.init(type, p.allocator);
|
||||
|
||||
#if type_kind == {
|
||||
case .procedure;
|
||||
array.init(*type.procedure_arguments, p.allocator);
|
||||
array.init(*type.procedure_returns, p.allocator);
|
||||
}
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
|
|
@ -956,7 +948,3 @@ expect_token :: (p: *Parser, kinds: ..Token.Kind) -> Token, bool {
|
|||
return t, false;
|
||||
}
|
||||
|
||||
try_lazy_init :: (p: *Parser) {
|
||||
mem.lazy_set_allocator(p);
|
||||
mem.lazy_set_allocator(*p.toplevel);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue