Skip to content

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

@dadou997

Description

@dadou997

环境: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;
    }

    ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions