From 82a07029f33e7a37becf65e6b97059a0006cde09 Mon Sep 17 00:00:00 2001 From: Vic Nightfall Date: Wed, 10 Jan 2024 20:00:19 +0100 Subject: [PATCH] Make structs work in default arguments --- src/compiler.pr | 6 +++++- src/consteval.pr | 7 +++++++ src/typechecking.pr | 3 +++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/compiler.pr b/src/compiler.pr index 53550532..a8373e42 100644 --- a/src/compiler.pr +++ b/src/compiler.pr @@ -4528,7 +4528,11 @@ export def walk_expression(node: &parser::Node, state: &State) -> Value { var expr = NO_VALUE if not node { return NO_VALUE } let scpe = state.scope - state.scope = node.scope + + if node.scope { + state.scope = node.scope + } + switch node.kind !int { case parser::NodeKind::NULL: expr = walk_Null(node, state) diff --git a/src/consteval.pr b/src/consteval.pr index ccc33a13..ac673ea6 100644 --- a/src/consteval.pr +++ b/src/consteval.pr @@ -201,6 +201,11 @@ def walk_From(node: &parser::Node, state: &typechecking::State) { export def evaluate_parameter(node: &parser::Node, param: &parser::Node, tpe: &typechecking::Type, state: &typechecking::State) -> &compiler::Value { var value: &compiler::Value = null if param.value.param.value { + // Set up temporary scope + let prev_scope = compiler_state.module.scope + let temp_scope = scope::enter_scope(state.module.scope) + + compiler_state.scope = temp_scope let previous_block = compiler_state.current_block let function = set_current_function() typechecking::walk(node, param.value.param.value, state) @@ -209,6 +214,8 @@ export def evaluate_parameter(node: &parser::Node, param: &parser::Node, tpe: &t let gvalue = compiler::make_global_value(result.tpe, "default_param", null, compiler_state) let estate = eval::eval(compiler_state) result = eval::get_value(result, estate) + + compiler_state.scope = prev_scope if result.kind != compiler::ValueKind::NULL { eval::set_value(gvalue, result, estate) diff --git a/src/typechecking.pr b/src/typechecking.pr index 81fa64df..ac679d12 100644 --- a/src/typechecking.pr +++ b/src/typechecking.pr @@ -96,6 +96,9 @@ export type StructuralTypeMember = struct { return_t: &Vector(&Type) } +// TODO Really, types should not be copied. There should only be one instance for every given type. +// This would make comparing them so much easier but changing that at this point might be too much work. +// It would also make the whole TypeMeta thing useless in the process. export type Type = struct { kind: TypeKind // Source line