Skip to content

Commit

Permalink
💡 Documenting source code.
Browse files Browse the repository at this point in the history
  • Loading branch information
crossoverJie committed Sep 8, 2022
1 parent 8002cd5 commit ae729ce
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ varInner=22, varExternal=14
- `+-*/`
- `== != < <= > >=`

> 重载函数名称必须是 `operator`,名称后跟上运算符即可重载。
> 重载函数名称必须是 **operator**,名称后跟上运算符即可重载。
```c#
```js
class Person{
int age;
Person(int a){
Expand Down
95 changes: 95 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,101 @@ varInner=22, varExternal=14

```

## Operator overloading

Operator that `Gscript` support:

- `+-*/`
- `== != < <= > >=`

> Overloading function must be **operator**, and append operator.
```js
class Person{
int age;
Person(int a){
age = a;
}
}
Person operator + (Person p1, Person p2){
Person pp = Person(p1.age+p2.age);
return pp;
}
Person operator - (Person p1, Person p2){
Person pp = Person(p1.age-p2.age);
return pp;
}
Person operator * (Person p1, Person p2){
Person pp = Person(p1.age * p2.age);
return pp;
}
Person operator / (Person p1, Person p2){
Person pp = Person(p1.age / p2.age);
return pp;
}
bool operator == (Person p1, Person p2){
return p1.age==p2.age;
}
bool operator != (Person p1, Person p2){
return p1.age!=p2.age;
}
bool operator > (Person p1, Person p2){
return p1.age>p2.age;
}
bool operator >= (Person p1, Person p2){
return p1.age>=p2.age;
}
bool operator < (Person p1, Person p2){
return p1.age<p2.age;
}
bool operator <= (Person p1, Person p2){
return p1.age<=p2.age;
}
Person p1 = Person(10);
Person p2 = Person(20);
//Person p3 = operator(p1,p2);
Person p3 = p1+p2;
println("p3.age="+p3.age);
assertEqual(p3.age, 30);

Person p4 = p1-p2;
println("p4.age="+p4.age);
println(100-10);

Person p5 = p1*p2;
println("p5.age="+p5.age);
assertEqual(p5.age, 200);

Person p6 = p2/p1;
println("p6.age="+p6.age);
assertEqual(p6.age, 2);

bool b1 = p1 == p2;
println("b1=="+b1);
assertEqual(b1,false);

bool b2 = p1 != p2;
println("b2=="+b2);
assertEqual(b2,true);

bool b3 = p1 > p2;
println("b3=="+b3);
assertEqual(b3,false);

bool b4 = p1 >= p2;
println("b4=="+b4);
assertEqual(b4,false);

bool b5 = p1 < p2;
println("b5=="+b5);
assertEqual(b5,true);

bool b6 = p1 <= p2;
println("b6=="+b6);
assertEqual(b6,true);
```


More examples: [https://github.com/crossoverJie/gscript/tree/main/example](https://github.com/crossoverJie/gscript/tree/main/example)

# Standard library
Expand Down
24 changes: 14 additions & 10 deletions resolver/type_scope_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,30 @@ func (t *TypeScopeResolver) ExitClassDeclaration(ctx *parser.ClassDeclarationCon
// EnterOperatorOverloading is called when production operatorOverloading is entered.
func (t *TypeScopeResolver) EnterOperatorOverloading(ctx *parser.OperatorOverloadingContext) {
var opOverload *symbol.OpOverload
function, ok := t.currentScope().(*symbol.Func)
if !ok || function.GetName() != symbol.OperName {
return
}
if ctx.PLUS() != nil {
opOverload = symbol.NewOpOverload(t.currentScope().(*symbol.Func), ctx.PLUS().GetSymbol().GetTokenType())
opOverload = symbol.NewOpOverload(function, ctx.PLUS().GetSymbol().GetTokenType())
} else if ctx.SUB() != nil {
opOverload = symbol.NewOpOverload(t.currentScope().(*symbol.Func), ctx.SUB().GetSymbol().GetTokenType())
opOverload = symbol.NewOpOverload(function, ctx.SUB().GetSymbol().GetTokenType())
} else if ctx.MULT() != nil {
opOverload = symbol.NewOpOverload(t.currentScope().(*symbol.Func), ctx.MULT().GetSymbol().GetTokenType())
opOverload = symbol.NewOpOverload(function, ctx.MULT().GetSymbol().GetTokenType())
} else if ctx.DIV() != nil {
opOverload = symbol.NewOpOverload(t.currentScope().(*symbol.Func), ctx.DIV().GetSymbol().GetTokenType())
opOverload = symbol.NewOpOverload(function, ctx.DIV().GetSymbol().GetTokenType())
} else if ctx.EQUAL() != nil {
opOverload = symbol.NewOpOverload(t.currentScope().(*symbol.Func), ctx.EQUAL().GetSymbol().GetTokenType())
opOverload = symbol.NewOpOverload(function, ctx.EQUAL().GetSymbol().GetTokenType())
} else if ctx.NOTEQUAL() != nil {
opOverload = symbol.NewOpOverload(t.currentScope().(*symbol.Func), ctx.NOTEQUAL().GetSymbol().GetTokenType())
opOverload = symbol.NewOpOverload(function, ctx.NOTEQUAL().GetSymbol().GetTokenType())
} else if ctx.GT() != nil {
opOverload = symbol.NewOpOverload(t.currentScope().(*symbol.Func), ctx.GT().GetSymbol().GetTokenType())
opOverload = symbol.NewOpOverload(function, ctx.GT().GetSymbol().GetTokenType())
} else if ctx.LT() != nil {
opOverload = symbol.NewOpOverload(t.currentScope().(*symbol.Func), ctx.LT().GetSymbol().GetTokenType())
opOverload = symbol.NewOpOverload(function, ctx.LT().GetSymbol().GetTokenType())
} else if ctx.GE() != nil {
opOverload = symbol.NewOpOverload(t.currentScope().(*symbol.Func), ctx.GE().GetSymbol().GetTokenType())
opOverload = symbol.NewOpOverload(function, ctx.GE().GetSymbol().GetTokenType())
} else if ctx.LE() != nil {
opOverload = symbol.NewOpOverload(t.currentScope().(*symbol.Func), ctx.LE().GetSymbol().GetTokenType())
opOverload = symbol.NewOpOverload(function, ctx.LE().GetSymbol().GetTokenType())
}
if opOverload != nil {
t.at.AppendOpOverload(opOverload)
Expand Down

0 comments on commit ae729ce

Please sign in to comment.