mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[code] Dynamic toolbox that displays script functions and variables.
Change-Id: Id1494580b3832540633d3aefaf04710d21e0a44c
This commit is contained in:
@@ -2,6 +2,11 @@
|
||||
#include "string.h"
|
||||
#include <stddef.h>
|
||||
|
||||
extern "C" {
|
||||
#include "py/lexer.h"
|
||||
#include "py/nlr.h"
|
||||
}
|
||||
|
||||
namespace Code {
|
||||
|
||||
constexpr char ScriptStore::k_scriptExtension[];
|
||||
@@ -104,6 +109,116 @@ bool ScriptStore::isFull() {
|
||||
return (numberOfScripts() >= k_maxNumberOfScripts || m_accordion.freeSpaceSize() < k_fullFreeSpaceSizeLimit);
|
||||
}
|
||||
|
||||
void ScriptStore::scanScriptsForFunctionsAndVariables(
|
||||
void * context,
|
||||
FunctionCallTwoIntArgs storeFunctionsCountForScriptAtIndex,
|
||||
FunctionCallOneIntOneConstCharPointerArg storeFunctionNameStartAtIndex,
|
||||
FunctionCallTwoIntArgs storeGlobalVariablesCountForScriptAtIndex,
|
||||
FunctionCallOneIntOneConstCharPointerArg storeGlobalVariableNameStartAtIndex
|
||||
) {
|
||||
int totalFunctionsCount = 0;
|
||||
int totalGlobalVariablesCount = 0;
|
||||
for (int scriptIndex = 0; scriptIndex < numberOfScripts(); scriptIndex++) {
|
||||
// Handle lexer or parser errors with nlr.
|
||||
nlr_buf_t nlr;
|
||||
if (nlr_push(&nlr) == 0) {
|
||||
const char * scriptContent = scriptAtIndex(scriptIndex).content();
|
||||
mp_lexer_t *lex = mp_lexer_new_from_str_len(0, scriptContent, strlen(scriptContent), false);
|
||||
mp_parse_tree_t parseTree = mp_parse(lex, MP_PARSE_FILE_INPUT);
|
||||
mp_parse_node_t pn = parseTree.root;
|
||||
|
||||
if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
|
||||
storeFunctionsCountForScriptAtIndex(context, scriptIndex, 0);
|
||||
storeGlobalVariablesCountForScriptAtIndex(context, scriptIndex, 0);
|
||||
mp_parse_tree_clear(&parseTree);
|
||||
nlr_pop();
|
||||
continue;
|
||||
}
|
||||
|
||||
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
|
||||
|
||||
// The script is only a single function definition.
|
||||
if (((uint)(MP_PARSE_NODE_STRUCT_KIND(pns))) == k_functionDefinitionParseNodeStructKind) {
|
||||
const char * id = structID(pns);
|
||||
if (id == nullptr) {
|
||||
continue;
|
||||
}
|
||||
storeFunctionsCountForScriptAtIndex(context, scriptIndex, 1);
|
||||
storeFunctionNameStartAtIndex(context, totalFunctionsCount, id);
|
||||
totalFunctionsCount++;
|
||||
storeGlobalVariablesCountForScriptAtIndex(context, scriptIndex, 0);
|
||||
mp_parse_tree_clear(&parseTree);
|
||||
nlr_pop();
|
||||
continue;
|
||||
}
|
||||
|
||||
// The script is only a single global variable definition.
|
||||
if (((uint)(MP_PARSE_NODE_STRUCT_KIND(pns))) == k_expressionStatementParseNodeStructKind) {
|
||||
const char * id = structID(pns);
|
||||
if (id == nullptr) {
|
||||
continue;
|
||||
}
|
||||
storeGlobalVariablesCountForScriptAtIndex(context, scriptIndex, 1);
|
||||
storeGlobalVariableNameStartAtIndex(context, totalGlobalVariablesCount, id);
|
||||
totalGlobalVariablesCount++;
|
||||
storeFunctionsCountForScriptAtIndex(context, scriptIndex, 0);
|
||||
mp_parse_tree_clear(&parseTree);
|
||||
nlr_pop();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (((uint)(MP_PARSE_NODE_STRUCT_KIND(pns))) != k_fileInput2ParseNodeStructKind) {
|
||||
// The script node is not of type "file_input_2", thus it will not have main
|
||||
// structures of the wanted type.
|
||||
storeFunctionsCountForScriptAtIndex(context, scriptIndex, 0);
|
||||
storeGlobalVariablesCountForScriptAtIndex(context, scriptIndex, 0);
|
||||
mp_parse_tree_clear(&parseTree);
|
||||
nlr_pop();
|
||||
continue;
|
||||
}
|
||||
|
||||
// Count the number of structs in child nodes.
|
||||
int numberOfFunctionsInCurrentScript = 0;
|
||||
int numberOfGlobalVariablesInCurrentScript = 0;
|
||||
|
||||
size_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
mp_parse_node_t child = pns->nodes[i];
|
||||
if (MP_PARSE_NODE_IS_STRUCT(child)) {
|
||||
mp_parse_node_struct_t *child_pns = (mp_parse_node_struct_t*)(child);
|
||||
if (((uint)(MP_PARSE_NODE_STRUCT_KIND(child_pns))) == k_functionDefinitionParseNodeStructKind) {
|
||||
const char * id = structID(child_pns);
|
||||
if (id == nullptr) {
|
||||
continue;
|
||||
}
|
||||
storeFunctionNameStartAtIndex(context, totalFunctionsCount, id);
|
||||
numberOfFunctionsInCurrentScript++;
|
||||
totalFunctionsCount++;
|
||||
} else if (((uint)(MP_PARSE_NODE_STRUCT_KIND(child_pns))) == k_expressionStatementParseNodeStructKind) {
|
||||
const char * id = structID(child_pns);
|
||||
if (id == nullptr) {
|
||||
continue;
|
||||
}
|
||||
storeGlobalVariableNameStartAtIndex(context, totalGlobalVariablesCount, id);
|
||||
numberOfGlobalVariablesInCurrentScript++;
|
||||
totalGlobalVariablesCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
storeFunctionsCountForScriptAtIndex(context, scriptIndex, numberOfFunctionsInCurrentScript);
|
||||
storeGlobalVariablesCountForScriptAtIndex(context, scriptIndex, numberOfGlobalVariablesInCurrentScript);
|
||||
|
||||
mp_parse_tree_clear(&parseTree);
|
||||
nlr_pop();
|
||||
} else {
|
||||
// The lexer or the parser failed.
|
||||
storeFunctionsCountForScriptAtIndex(context, scriptIndex, 0);
|
||||
storeGlobalVariablesCountForScriptAtIndex(context, scriptIndex, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char * ScriptStore::contentOfScript(const char * name) {
|
||||
Script script = scriptNamed(name);
|
||||
if (script.isNull()) {
|
||||
@@ -149,4 +264,20 @@ int ScriptStore::accordionIndexOfContentOfScriptAtIndex(int index) const {
|
||||
return index * Script::NumberOfStringsPerScript + 2;
|
||||
}
|
||||
|
||||
const char * ScriptStore::structID(mp_parse_node_struct_t *structNode) {
|
||||
// Find the id child node, which stores the struct's name
|
||||
size_t childNodesCount = MP_PARSE_NODE_STRUCT_NUM_NODES(structNode);
|
||||
if (childNodesCount < 1) {
|
||||
return nullptr;
|
||||
}
|
||||
mp_parse_node_t child = structNode->nodes[0];
|
||||
if (MP_PARSE_NODE_IS_LEAF(child)
|
||||
&& MP_PARSE_NODE_LEAF_KIND(child) == MP_PARSE_NODE_ID)
|
||||
{
|
||||
uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(child);
|
||||
return qstr_str(arg);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user