3

手写编程语言-实现运算符重载

 1 year ago
source link: https://studygolang.com/articles/35873?fr=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

手写编程语言-实现运算符重载

crossoverJie · 1天之前 · 243 次点击 · 预计阅读时间 4 分钟 · 大约8小时之前 开始浏览    

e6c9d24ely1h6at95ynkaj21900u07aw.jpg

先带来日常的 GScript 更新:新增了可变参数的特性,语法如下:

int add(string s, int ...num){
    println(s);
    int sum = 0;
    for(int i=0;i<len(num);i++){
        int v = num[i];
        sum = sum+v;
    }
    return sum;
}
int x = add("abc", 1,2,3,4);
println(x);
assertEqual(x, 10);

得益于可变参数,所以新增了格式化字符串的内置函数:

//formats according to a format specifier and writes to standard output.
printf(string format, any ...a){}

//formats according to a format specifier and returns the resulting string.
string sprintf(string format, any ...a){}

下面重点看看 GScript 所支持的运算符重载是如何实现的。

运算符重载其实也是多态的一种表现形式,我们可以重写运算符的重载函数,从而改变他们的计算规则。

println(100+2*2);

以这段代码的运算符为例,输出的结果自然是:104.

但如果我们是对两个对象进行计算呢,举个例子:

class Person{
    int age;
    Person(int a){
        age = a;
    }
}
Person p1 = Person(10);
Person p2 = Person(20);
Person p3 = p1+p2;

这样的写法在 Java/Go 中都会报编译错误,这是因为他们两者都不支持运算符重载;

Python/C# 是支持的,相比之下我觉得 C# 的实现方式更符合 GScript 语法,所以参考 C# 实现了以下的语法规则。

Person operator + (Person p1, Person p2){
    Person pp = Person(p1.age+p2.age);
    return pp;
}
Person p3 = p1+p2;
println("p3.age="+p3.age);
assertEqual(p3.age, 30);

有几个硬性条件:

  • 函数名必须是 operator
  • 名称后跟上运算符即可。

目前支持的运算符有:+-*/ == != < <= > >=

以前在使用 Python 运算符重载时就有想过它是如何实现的?但没有深究,这次借着自己实现相关功能从而需要深入理解。

其中重点就为两步:

  1. 编译期间:记录所有的重载函数和运算符的关系。
  2. 运行期:根据当前的运算找到声明的函数,直接运行即可。

第一步的重点是扫描所有的重载函数,将重载函数与运算符存放起来,需要关注的是函数的返回值与运算符类型。

// OpOverload 重载符
type OpOverload struct {
    function  *Func
    tokenType int
}

// 运算符重载自定义函数
opOverloads []*symbol.OpOverload

在编译器中使用一个切片存放。

而在运行期中当两个入参类型相同时,则需要查找重载函数。 e6c9d24ely1h6b310mefaj21ky05u3zz.jpg

// GetOpFunction 获取运算符重载函数
// 通过返回值以及运算符号(+-*/) 匹配重载函数
func (a *AnnotatedTree) GetOpFunction(returnType symbol.Type, tokenType int) *symbol.Func {
    for _, overload := range a.opOverloads {
        isType := overload.GetFunc().GetReturnType().IsType(returnType)
        if isType && overload.GetTokenType() == tokenType {
            return overload.GetFunc()
        }
    }
    return nil
}

查找方式就是通过编译期存放的数据进行匹配,拿到重载函数后自动调用便实现了重载。

感兴趣的朋友可以查看相关代码:

运算符重载其实并不是一个常用的功能;因为会改变运算符的语义,比如明明是加法却在重载函数中写为减法。

这会使得代码阅读起来困难,但在某些情况下我们又非常希望语言本身能支持运算符重载。

比如在 Go 中常用的一个第三方精度库decimal.Decimal,进行运算时只能使用 d1.Add(d2) 这样的函数,当运算复杂时:

a5 = (a1.Add(a2).Add(a3)).Mul(a4);
a5 = (a1+a2+a3)*a4;

就不如下面这种直观,所以有利有弊吧,多一个选项总不是坏事。

GScript 源码: https://github.com/crossoverJie/gscript


有疑问加站长微信联系(非本文作者))

280

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK