Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于数组的处理 #13

Open
NyakoApocalipse opened this issue Apr 24, 2024 · 1 comment
Open

关于数组的处理 #13

NyakoApocalipse opened this issue Apr 24, 2024 · 1 comment

Comments

@NyakoApocalipse
Copy link

image
我留意到文件中有说明将数组和函数进行合并处理,请问具体是如何将fortran数组译为c++对应的存储结构呢?我在自己测试的代码中发现fortran数组在c++中翻译为了一种struct,我想知道为什么不直接翻译为c++数组。

另外,我还对数组的存取方式感到疑惑,例如:

struct{
  long double a1;
  long double a2;
}test_struct;
void tet_function(){
  long double & a = test_struct.a1;
  long double & b = test_struct.a2;
  a(INOUT(1)) = b(INOUT(1));
  ...
}

其中,a和b在声明时均是long double引用,而使用时却类似于数组名,INOUT(i)函数代表下标为i,我想知道这是如何作为数组访问的。

@YHN-ice
Copy link
Owner

YHN-ice commented May 9, 2024

farray是原作者实现的,可以参考

~farray() {
if (!is_view) {
delete[] parr;
parr = nullptr;
}
delete[] lb; delete[] sz; delete[] delta;
}
T * parr = nullptr; // support inplace operation of each element
protected:
size_type * lb = nullptr, * sz = nullptr, * delta = nullptr;
};

里面用裸指针和手工内存管理实现了fortran的数组。

关于为什么这样设计,参考原作者的文档: https://github.com/YHN-ice/CFortranTranslator/blob/299110ef6d0898790572f620a9edf3bcf5925f38/docs/brief/array.md

这里给出一些具体实现转换的代码位置,或许可以作为阅读源码的起点:

语法解析

function_array_body : variable '(' paramtable ')'
{
// function call OR array index
// NOTE that array index can be A(1:2, 3:4)
ARG_OUT callable_head = YY2ARG($1);
ARG_OUT argtable = YY2ARG($3);
ParseNode newnode = gen_token(Term{TokenMeta::NT_FUCNTIONARRAY, WHEN_DEBUG_OR_EMPTY("FUNCTIONARRAY GENERATED IN REGEN_SUITE") }, callable_head, argtable);
$$ = RETURN_NT(newnode);
update_pos(YY2ARG($$), YY2ARG($1), YY2ARG($4));
CLEAN_DELETE($1, $2, $3, $4);
}

代码生成
void regen_function_array(FunctionInfo * finfo, ParseNode & callable) {

另外,提issue时如果附上代码,最好translator的input和output一起给出,可参考其他issue。截图最好注明来源。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants