From 7333b32418c6808240904ee965b1a7657ff65759 Mon Sep 17 00:00:00 2001 From: Judah Caruso Date: Sun, 13 Jul 2025 08:56:58 -0600 Subject: [PATCH] various vm things --- vm/interp.jai | 27 ++++++++++++------------ vm/module.jai | 3 --- vm/parser.jai | 58 +++++++++++++++++++++++++++++++-------------------- 3 files changed, 49 insertions(+), 39 deletions(-) diff --git a/vm/interp.jai b/vm/interp.jai index 6c1762a..86b2270 100644 --- a/vm/interp.jai +++ b/vm/interp.jai @@ -36,19 +36,9 @@ Interp_Value :: struct { } } -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) { + try_lazy_init(i); + for i.toplevel { interp_statement(i, it, i.global); } @@ -309,7 +299,6 @@ 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; } @@ -318,3 +307,15 @@ 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); +} diff --git a/vm/module.jai b/vm/module.jai index e99e3f9..151495d 100644 --- a/vm/module.jai +++ b/vm/module.jai @@ -18,8 +18,6 @@ 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 @@ -87,7 +85,6 @@ strings :: #import "String"; // @future interp: Interp; interp.toplevel = parser.toplevel; - init(*interp, context.allocator); interp_program(*interp); } diff --git a/vm/parser.jai b/vm/parser.jai index 293b178..30ad998 100644 --- a/vm/parser.jai +++ b/vm/parser.jai @@ -208,6 +208,14 @@ 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 { @@ -225,6 +233,11 @@ 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 { @@ -254,6 +267,11 @@ 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 { @@ -261,6 +279,10 @@ Node_Block :: struct { n.kind = .block; body: [..]*Node; + + init :: (n: *Node_Block, allocator: Allocator) { + mem.lazy_set_allocator(*n.body, allocator); + } } Node_Return :: struct { @@ -268,6 +290,10 @@ Node_Return :: struct { n.kind = .return_; values: [..]*Node; + + init :: (n: *Node_Return, allocator: Allocator) { + mem.lazy_set_allocator(*n.values, allocator); + } } Parser :: struct { @@ -280,12 +306,9 @@ Parser :: struct { offset: int; } -init :: (p: *Parser, allocator: Allocator) { - p.allocator = allocator; - p.toplevel.allocator = allocator; -} - parse_string :: (p: *Parser, source: string) -> bool { + try_lazy_init(p); + p.source = source; p.offset = 0; @@ -753,17 +776,8 @@ parse_expression_base :: (p: *Parser) -> *Node { make_node :: (p: *Parser, $T: Type) -> *T { node := mem.request_memory(T,, allocator = 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); + #if #exists(T.init) { + T.init(node, p.allocator); } return node; @@ -772,13 +786,7 @@ 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; - - #if type_kind == { - case .procedure; - array.init(*type.procedure_arguments, p.allocator); - array.init(*type.procedure_returns, p.allocator); - } - + Node_Type.init(type, p.allocator); return type; } @@ -948,3 +956,7 @@ 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); +}