Skip to content

Commit

Permalink
added allocLocalOrReuse to the IR builder
Browse files Browse the repository at this point in the history
  • Loading branch information
katsaii committed Jul 16, 2024
1 parent e9455d4 commit 1973d13
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src-lts/scripts/scr_catspeak_ir/scr_catspeak_ir.gml
Original file line number Diff line number Diff line change
Expand Up @@ -700,9 +700,39 @@ function CatspeakIRBuilder() constructor {
});
};

/// Reuses a local variable with the supplied name, if one exists in the
/// current scope. Otherwise, behaves identically to `allocLocal`.
///
/// @param {String} name
/// The name of the local variable to allocate.
///
/// @param {Real} [location]
/// The source location of this local variable.
///
/// @return {Struct}
static allocLocalOrReuse = function (name, location=undefined) {
if (CATSPEAK_DEBUG_MODE) {
__catspeak_check_arg("name", name, is_string);
}
var block = currFunctionScope.blocks[| currFunctionScope.blocksTop];
var scope = block.locals;
if (ds_map_exists(scope, name)) {
// __createTerm() will do argument validation
return __createTerm(CatspeakTerm.LOCAL, location, {
idx : scope[? name],
});
} else {
return allocLocal(name, location);
}
};

/// Allocates a new named function argument with the supplied name.
/// Returns a term to get or set the value of this argument.
///
/// @warning
/// All parameter names must be allocated before allocating any
/// local variables.
///
/// @param {String} name
/// The name of the local variable to allocate.
///
Expand Down

0 comments on commit 1973d13

Please sign in to comment.