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) {
auto finder = std::unique_ptr<symbol_finder>(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);
}

View File

@ -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;

View File

@ -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;
}

View File

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

View File

@ -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().