Debian Codegraph @

src/transition/script.h

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3
4 #pragma once
5 #include <assert.h>
6 #include <stdalign.h>
7 #include <stdbool.h>
8 #include <stddef.h>
9
10 #include <uthash.h>
11
12 struct script_context_info {
13 const char *name;
14 ptrdiff_t offset;
15 };
16
17 struct script_specialization_context {
18 ptrdiff_t offset;
19 double value;
20 };
21
22 struct script_output_info {
23 const char *name;
24 /// Slot for this variable, -1 if this variable doesn't exist.
25 int slot;
26 };
27
28 struct script_parse_config {
29 const struct script_context_info *context_info;
30 /// Set the output variables of this script, also used to receive the slot number
31 /// for those variables.
32 struct script_output_info *output_info;
33 };
34 struct script;
35 struct script_instance {
36 const struct script *script;
37 double memory[];
38 };
39 enum script_evaluation_result {
40 /// +/-inf in results
41 SCRIPT_EVAL_ERROR_INF,
42 /// NaN in results
43 SCRIPT_EVAL_ERROR_NAN,
44 /// OK
45 SCRIPT_EVAL_OK,
46 };
47 typedef struct config_setting_t config_setting_t;
48
49 #define SCRIPT_CTX_PLACEHOLDER_BASE (0x40000000)
50
51 struct script *
52 script_compile(config_setting_t *setting, struct script_parse_config cfg, char **out_err);
53 void script_free(struct script *script);
54 /// Evaluate a script instance with context. `do_branch_once` indicates whether
55 /// `BRANCH_ONCE` instructions should branch.
56 enum script_evaluation_result script_instance_evaluate(struct script_instance *instance,
57 void *context, bool do_branch_once);
58 /// Resume the script instance from another script instance that's currently running.
59 /// The script doesn't have to be the same. For resumable (explained later) transitions,
60 /// if matching variables exist in the `old` script, their starting point will be
61 /// overridden with the current value of matching variables from `old`. A resumable
62 /// transition is a transition that will "resume" from wherever its current value is.
63 /// Imagine a window flying off the screen, for some reason you decided to bring it back
64 /// when it's just halfway cross. It would be jarring if the window jumps, so you would
65 /// want it to fly back in from where it currently is, instead from out of the screen.
66 /// This resuming behavior can be turned off by setting `reset = true;` in the transition
67 /// configuration, in which case the user defined `start` value will always be used.
68 void script_instance_resume_from(struct script_instance *old, struct script_instance *new_);
69 struct script_instance *script_instance_new(const struct script *script);
70 /// Get the total duration slot of a script.
71 unsigned script_total_duration_slot(const struct script *script);
72 unsigned script_elapsed_slot(const struct script *script);
73
74 /// Specialize a script instance with a context. During evaluation of the resulting
75 /// script, what would have been read from the context will be replaced with the hardcoded
76 /// value in the specialization context.
77 void script_specialize(struct script *script,
78 const struct script_specialization_context *context,
79 unsigned n_context);
80
81 /// Check if a script instance has finished. The script instance must have been evaluated
82 /// at least once.
83 static inline bool script_instance_is_finished(const struct script_instance *instance) {
84 return instance->memory[script_elapsed_slot(instance->script)] >=
85 instance->memory[script_total_duration_slot(instance->script)];
86 }
87
88 /// Generate code for a C function that will return a script identical to `script` when
89 /// called. The generated function will take a `int *output_slots` parameter, which it
90 /// will fill in, based on `outputs` passed to this function. Specifically, the generated
91 /// function will fill in `output_slots[i]` with the slot number of the output variable
92 /// named `outputs[i].name`. The generated function will return a pointer to the script.
93 /// This function only generates the function body, you need to provide the function
94 /// signature and the function name yourself.
95 char *script_to_c(const struct script *script, const struct script_output_info *outputs);