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

FirstClassFunction.play中bar函数在java版本解释器中无法正确调用 #35

Open
dadou997 opened this issue May 23, 2022 · 0 comments

Comments

@dadou997
Copy link

环境:playscript-java与FirstClassFunction.play

问题描述:当解释执行FirstClassFunction.play文件时,该文件中的bar(foo);函数无法正确执行。

产生原因:解释执行bar(foo);时,解释器无法找到bar函数。其原因是进行闭包foo匹配时,由于foo调用多次从而导致返回参数类型个数不停的增加,从而导致函数匹配失败。

public class Function extends Scope implements FunctionType {

    ...

    @Override
    public List<Type> getParamTypes() {
        if (paramTypes == null) {
            paramTypes = new LinkedList<Type>();
        }
        //当某个函数调用多次时,paramTypes的数量会不停的增加
        for (Variable param : parameters) {
            paramTypes.add(param.type);
        }

        return paramTypes;
    }

    ...

解决方案1:进行去重操作,判断param.type是否是同一对象,如果不是,才添加到paramTypes集合中。

public class Function extends Scope implements FunctionType {

    ...

    @Override
    public List<Type> getParamTypes() {
        if (paramTypes == null) {
            paramTypes = new LinkedList<>();
        }
        for (Variable param : parameters) {
            boolean isExist = false;
            //由于函数参数注定不会很多,所以认为这里使用for循环的时间复杂度可接受
            for (Type type : paramTypes) {
                if (type == param.type) {
                    isExist = true;
                    break;
                }
            }
            if (!isExist) {
                paramTypes.add(param.type);
            }
        }
        return paramTypes;
    }

    ...
}
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

1 participant