Skip to content

Commit

Permalink
fix: infer return of the left hand of assignment operator
Browse files Browse the repository at this point in the history
  • Loading branch information
lavyyy committed Oct 1, 2024
1 parent c6e456d commit 2db59a8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @lunarclient/molang

## 1.0.10

### Patch Changes

- fix: infer return of the left hand of assignment operator

## 1.0.9

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion __tests__/custom/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,6 @@ test('Custom syntax', () => {
`return ({t.__scvar0.length=0;t.__scvar1=t.__scvar0;}+t.__scvar1);`
)
expect(customMolang.transform('f.test_no_return()')).toBe(
`return ({t.__scvar0=0;});`
`return ({t.__scvar0=0;}+t.__scvar1);`
)
})
8 changes: 7 additions & 1 deletion __tests__/inferReturn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ test('Infer return at end of statement with function', () => {
test('Infer return at end of statement with negative variable', () => {
const statement = `v.y = 10;`

expect(molang.execute(statement)).toBe(0)
expect(molang.execute(statement)).toBe(10)

const statement2 = `-v.y;`

expect(molang.execute(statement2)).toBe(-10)
})

test('Infer return at end of assignment statement', () => {
const statement = `v.z = 10;`

expect(molang.execute(statement)).toBe(10)
})
6 changes: 6 additions & 0 deletions lib/parser/expressions/genericOperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ export class GenericOperatorExpression extends Expression {
return `${this.left.toString()}${this.operator}${this.right.toString()}`
}
}

export function isGenericOperatorExpression(
expr: IExpression
): expr is GenericOperatorExpression {
return expr.type === 'GenericOperatorExpression'
}
27 changes: 20 additions & 7 deletions lib/parser/parselets/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IInfixParselet } from './infix'
import { StatementExpression } from '../expressions/statement'
import { StaticExpression } from '../expressions/static'
import { ReturnExpression } from '../expressions'
import { isGenericOperatorExpression } from '../expressions/genericOperator'

export class StatementParselet implements IInfixParselet {
constructor(public precedence = 0) {}
Expand Down Expand Up @@ -78,14 +79,26 @@ export class StatementParselet implements IInfixParselet {

// Infer when statement should be returned
if (
!expressions.some((expr) => expr.isReturn) &&
!expressions[expressions.length - 1].isReturn &&
expressions[expressions.length - 1].type !==
'GenericOperatorExpression'
(!expressions.some((expr) => expr.isReturn) ||
!expressions[expressions.length - 1].isReturn) &&
!parser.match('CURLY_RIGHT', false)
) {
expressions[expressions.length - 1] = new ReturnExpression(
expressions[expressions.length - 1]
)
const lastExpression = expressions[expressions.length - 1]

// If the last expression is an assignment then append a return expression
if (
isGenericOperatorExpression(lastExpression) &&
lastExpression.operator === '='
) {
expressions.push(
new ReturnExpression(lastExpression.allExpressions[0])
)
} else {
// Anything else we just return the last expression
expressions[expressions.length - 1] = new ReturnExpression(
lastExpression
)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunarclient/molang",
"version": "1.0.9",
"version": "1.0.10",
"description": "Lunar Client's fork of bridge-core's molang parser",
"main": "lib/main.ts",
"files": [
Expand Down

0 comments on commit 2db59a8

Please sign in to comment.