add check for symbol definition

This commit is contained in:
ValKmjolnir 2023-08-13 22:12:07 +08:00
parent 7d81246695
commit fc2be49621
5 changed files with 177 additions and 48 deletions

View File

@ -75,11 +75,14 @@ void codegen::regist_str(const std::string& str) {
void codegen::find_symbol(code_block* node) { void codegen::find_symbol(code_block* node) {
auto finder = std::unique_ptr<symbol_finder>(new symbol_finder); auto finder = std::unique_ptr<symbol_finder>(new symbol_finder);
for(const auto& i : finder->do_find(node)) { for(const auto& i : finder->do_find(node)) {
if (!experimental_namespace.count(i.file)) { if (native_function_mapper.count(i.name)) {
experimental_namespace[i.file] = {}; die("definition conflicts with native function", i.location);
} }
if (local.empty() && !experimental_namespace.at(i.file).count(i.name)) { if (!experimental_namespace.count(i.location.file)) {
experimental_namespace.at(i.file).insert(i.name); 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); add_symbol(i.name);
} }

View File

@ -107,9 +107,19 @@ void nas_ghost::set(
} }
void nas_ghost::clear() { void nas_ghost::clear() {
if (!ptr || !dtor_ptr) { // do nothing if pointer is null
if (!ptr) {
return; return;
} }
// do clear pointer if destructor function pointer is null
if (!dtor_ptr) {
type_name = "";
ptr = nullptr;
return;
}
// do destruction
dtor_ptr(ptr); dtor_ptr(ptr);
type_name = ""; type_name = "";
ptr = nullptr; ptr = nullptr;

View File

@ -4,13 +4,13 @@ bool symbol_finder::visit_definition_expr(definition_expr* node) {
if (node->get_variable_name()) { if (node->get_variable_name()) {
symbols.push_back({ symbols.push_back({
node->get_variable_name()->get_name(), node->get_variable_name()->get_name(),
node->get_variable_name()->get_location().file node->get_variable_name()->get_location()
}); });
} else { } else {
for(auto i : node->get_variables()->get_variables()) { for(auto i : node->get_variables()->get_variables()) {
symbols.push_back({ symbols.push_back({
i->get_name(), 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()) { if (node->get_name()) {
symbols.push_back({ symbols.push_back({
node->get_name()->get_name(), node->get_name()->get_name(),
node->get_name()->get_location().file}); node->get_name()->get_location()
});
} }
return true; return true;
} }

View File

@ -11,7 +11,7 @@ class symbol_finder:public ast_visitor {
public: public:
struct symbol_info { struct symbol_info {
std::string name; std::string name;
std::string file; span location;
}; };
private: private:

View File

@ -10,42 +10,152 @@
# available in C++; just use node.getNode(path).whatever() instead. # available in C++; just use node.getNode(path).whatever() instead.
# #
# unfinished var _new = func {
var getprop = func {} return {
var fgcommand = func {} type: "",
var _globals = func {} name: "",
var _createCondition = func {} value: nil,
var _new = func {} parent_list:[],
var string = {}; child_list:[]
var _getNode = func {}; };
var _getParent = func {}; }
var _getChildren = func {};
var _setChildren = func {}; var _fg_props_globals = _new();
var _alias = func {}; var _globals = func {
var _equals = func {}; return _fg_props_globals;
var _unalias = func {}; }
var _adjustValue = func {};
var _setDoubleValue = func {}; var getprop = func(arg...) {
var _setIntValue = func {}; die("unimplemented");
var _setBoolValue = func {}; }
var _toggleBoolValue = func {};
var _setValue = func {}; var addcommand = func(name, code) {
var _setValues = func {}; die("unimplemented");
var _getAttribute = func {}; }
var _setAttribute = func {};
var _getValue = func {}; var fgcommand = func(name, args = nil) {
var _getType = func {}; die("unimplemented");
var _isNumeric = func {}; }
var _isInt = func {};
var _getIndex = func {}; var _createCondition = func {
var _getName = func {}; die("unimplemented");
var _getAliasTarget = func {}; }
var _getChild = func {};
var _addChild = func {}; var _getNode = func {
var _addChildren = func {}; die("unimplemented");
var _removeChild = func {}; };
var _removeChildren = func {};
var _removeAllChildren = func {}; 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 = { var Node = {
@ -190,8 +300,9 @@ var Node = {
# #
Node.new = func(values = nil) { Node.new = func(values = nil) {
var result = wrapNode(_new()); var result = wrapNode(_new());
if(ishash(values)) if(ishash(values)) {
result.setValues(values); result.setValues(values);
}
return result; return result;
} }
@ -331,14 +442,18 @@ var wrap = func(node) {
# Node object and its _g (ghost) field set to the specified object. # Node object and its _g (ghost) field set to the specified object.
# Nasal's literal syntax can be pleasingly terse. I like that. :) # 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? # Global property tree. Set once at initialization. Is that OK?
# Does anything ever call globals.set_props() from C++? May need to # Does anything ever call globals.set_props() from C++? May need to
# turn this into a function if so. # turn this into a function if so.
# #
var globals = wrapNode(_globals()); var globals = func {
return wrapNode(_globals());
}();
## ##
# Shortcut for props.globals.getNode(). # Shortcut for props.globals.getNode().