From fc2be49621ad1a24ace58ee39b696ad235e32065 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Sun, 13 Aug 2023 22:12:07 +0800 Subject: [PATCH] :sparkles: add check for symbol definition --- src/nasal_codegen.cpp | 11 ++- src/nasal_gc.cpp | 12 ++- src/symbol_finder.cpp | 7 +- src/symbol_finder.h | 2 +- std/props.nas | 193 +++++++++++++++++++++++++++++++++--------- 5 files changed, 177 insertions(+), 48 deletions(-) diff --git a/src/nasal_codegen.cpp b/src/nasal_codegen.cpp index 8e41af7..28477c8 100644 --- a/src/nasal_codegen.cpp +++ b/src/nasal_codegen.cpp @@ -75,11 +75,14 @@ void codegen::regist_str(const std::string& str) { void codegen::find_symbol(code_block* node) { auto finder = std::unique_ptr(new symbol_finder); for(const auto& i : finder->do_find(node)) { - if (!experimental_namespace.count(i.file)) { - experimental_namespace[i.file] = {}; + if (native_function_mapper.count(i.name)) { + die("definition conflicts with native function", i.location); } - if (local.empty() && !experimental_namespace.at(i.file).count(i.name)) { - experimental_namespace.at(i.file).insert(i.name); + if (!experimental_namespace.count(i.location.file)) { + experimental_namespace[i.location.file] = {}; + } + if (local.empty() && !experimental_namespace.at(i.location.file).count(i.name)) { + experimental_namespace.at(i.location.file).insert(i.name); } add_symbol(i.name); } diff --git a/src/nasal_gc.cpp b/src/nasal_gc.cpp index 4523adf..ee6b1e2 100644 --- a/src/nasal_gc.cpp +++ b/src/nasal_gc.cpp @@ -107,9 +107,19 @@ void nas_ghost::set( } void nas_ghost::clear() { - if (!ptr || !dtor_ptr) { + // do nothing if pointer is null + if (!ptr) { return; } + + // do clear pointer if destructor function pointer is null + if (!dtor_ptr) { + type_name = ""; + ptr = nullptr; + return; + } + + // do destruction dtor_ptr(ptr); type_name = ""; ptr = nullptr; diff --git a/src/symbol_finder.cpp b/src/symbol_finder.cpp index 58a84fe..7f34a55 100644 --- a/src/symbol_finder.cpp +++ b/src/symbol_finder.cpp @@ -4,13 +4,13 @@ bool symbol_finder::visit_definition_expr(definition_expr* node) { if (node->get_variable_name()) { symbols.push_back({ node->get_variable_name()->get_name(), - node->get_variable_name()->get_location().file + node->get_variable_name()->get_location() }); } else { for(auto i : node->get_variables()->get_variables()) { symbols.push_back({ i->get_name(), - i->get_location().file + i->get_location() }); } } @@ -30,7 +30,8 @@ bool symbol_finder::visit_iter_expr(iter_expr* node) { if (node->get_name()) { symbols.push_back({ node->get_name()->get_name(), - node->get_name()->get_location().file}); + node->get_name()->get_location() + }); } return true; } diff --git a/src/symbol_finder.h b/src/symbol_finder.h index a0eb852..545e94e 100644 --- a/src/symbol_finder.h +++ b/src/symbol_finder.h @@ -11,7 +11,7 @@ class symbol_finder:public ast_visitor { public: struct symbol_info { std::string name; - std::string file; + span location; }; private: diff --git a/std/props.nas b/std/props.nas index c10c646..c9ba748 100644 --- a/std/props.nas +++ b/std/props.nas @@ -10,42 +10,152 @@ # available in C++; just use node.getNode(path).whatever() instead. # -# unfinished -var getprop = func {} -var fgcommand = func {} -var _globals = func {} -var _createCondition = func {} -var _new = func {} -var string = {}; -var _getNode = func {}; -var _getParent = func {}; -var _getChildren = func {}; -var _setChildren = func {}; -var _alias = func {}; -var _equals = func {}; -var _unalias = func {}; -var _adjustValue = func {}; -var _setDoubleValue = func {}; -var _setIntValue = func {}; -var _setBoolValue = func {}; -var _toggleBoolValue = func {}; -var _setValue = func {}; -var _setValues = func {}; -var _getAttribute = func {}; -var _setAttribute = func {}; -var _getValue = func {}; -var _getType = func {}; -var _isNumeric = func {}; -var _isInt = func {}; -var _getIndex = func {}; -var _getName = func {}; -var _getAliasTarget = func {}; -var _getChild = func {}; -var _addChild = func {}; -var _addChildren = func {}; -var _removeChild = func {}; -var _removeChildren = func {}; -var _removeAllChildren = func {}; +var _new = func { + return { + type: "", + name: "", + value: nil, + parent_list:[], + child_list:[] + }; +} + +var _fg_props_globals = _new(); +var _globals = func { + return _fg_props_globals; +} + +var getprop = func(arg...) { + die("unimplemented"); +} + +var addcommand = func(name, code) { + die("unimplemented"); +} + +var fgcommand = func(name, args = nil) { + die("unimplemented"); +} + +var _createCondition = func { + die("unimplemented"); +} + +var _getNode = func { + die("unimplemented"); +}; + +var _getParent = func { + die("unimplemented"); +}; + +var _getChildren = func { + die("unimplemented"); +}; + +var _setChildren = func { + die("unimplemented"); +}; + +var _alias = func { + die("unimplemented"); +}; + +var _equals = func { + die("unimplemented"); +}; + +var _unalias = func { + die("unimplemented"); +}; + +var _adjustValue = func { + die("unimplemented"); +}; + +var _setDoubleValue = func { + die("unimplemented"); +}; + +var _setIntValue = func { + die("unimplemented"); +}; + +var _setBoolValue = func { + die("unimplemented"); +}; + +var _toggleBoolValue = func { + die("unimplemented"); +}; + +var _setValue = func { + die("unimplemented"); +}; + +var _setValues = func { + die("unimplemented"); +}; + +var _getAttribute = func { + die("unimplemented"); +}; + +var _setAttribute = func { + die("unimplemented"); +}; + +var _getValue = func { + die("unimplemented"); +}; + +var _getType = func { + die("unimplemented"); +}; + +var _isNumeric = func { + die("unimplemented"); +}; + +var _isInt = func { + die("unimplemented"); +}; + +var _getIndex = func { + die("unimplemented"); +}; + +var _getName = func { + die("unimplemented"); +}; + +var _getAliasTarget = func { + die("unimplemented"); +}; + +var _getChild = func { + die("unimplemented"); +}; + +var _addChild = func { + die("unimplemented"); +}; + +var _addChildren = func { + die("unimplemented"); +}; + +var _removeChild = func { + die("unimplemented"); +}; + +var _removeChildren = func { + die("unimplemented"); +}; + +var _removeAllChildren = func { + die("unimplemented"); +}; var Node = { @@ -190,8 +300,9 @@ var Node = { # Node.new = func(values = nil) { var result = wrapNode(_new()); - if(ishash(values)) + if(ishash(values)) { result.setValues(values); + } return result; } @@ -331,14 +442,18 @@ var wrap = func(node) { # Node object and its _g (ghost) field set to the specified object. # Nasal's literal syntax can be pleasingly terse. I like that. :) # -var wrapNode = func(node) { { parents : [Node], _g : node } } +var wrapNode = func(node) { + return { parents : [Node], _g : node }; +} ## # Global property tree. Set once at initialization. Is that OK? # Does anything ever call globals.set_props() from C++? May need to # turn this into a function if so. # -var globals = wrapNode(_globals()); +var globals = func { + return wrapNode(_globals()); +}(); ## # Shortcut for props.globals.getNode().