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

Improve reference expressions #1661

Merged
merged 6 commits into from Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 1 addition & 10 deletions runtime/ast/expression.go
Expand Up @@ -1878,7 +1878,6 @@ func (*DestroyExpression) precedence() precedence {

type ReferenceExpression struct {
Expression Expression
Type Type `json:"TargetType"`
StartPos Position `json:"-"`
}

Expand All @@ -1888,14 +1887,12 @@ var _ Expression = &ReferenceExpression{}
func NewReferenceExpression(
gauge common.MemoryGauge,
expression Expression,
targetType Type,
startPos Position,
) *ReferenceExpression {
common.UseMemory(gauge, common.ReferenceExpressionMemoryUsage)

return &ReferenceExpression{
Expression: expression,
Type: targetType,
StartPos: startPos,
}
}
Expand All @@ -1914,7 +1911,6 @@ func (e *ReferenceExpression) Accept(visitor Visitor) Repr {

func (e *ReferenceExpression) Walk(walkChild func(Element)) {
walkChild(e.Expression)
// TODO: walk type
}

func (e *ReferenceExpression) AcceptExp(visitor ExpressionVisitor) Repr {
Expand All @@ -1926,7 +1922,6 @@ func (e *ReferenceExpression) String() string {
}

var referenceExpressionRefOperatorDoc prettier.Doc = prettier.Text("&")
var referenceExpressionAsOperatorDoc prettier.Doc = prettier.Text("as")

func (e *ReferenceExpression) Doc() prettier.Doc {
doc := parenthesizedExpressionDoc(
Expand All @@ -1940,10 +1935,6 @@ func (e *ReferenceExpression) Doc() prettier.Doc {
prettier.Group{
Doc: doc,
},
prettier.Line{},
referenceExpressionAsOperatorDoc,
prettier.Line{},
e.Type.Doc(),
},
}
}
Expand All @@ -1953,7 +1944,7 @@ func (e *ReferenceExpression) StartPosition() Position {
}

func (e *ReferenceExpression) EndPosition(memoryGauge common.MemoryGauge) Position {
return e.Type.EndPosition(memoryGauge)
return e.Expression.EndPosition(memoryGauge)
}

func (e *ReferenceExpression) MarshalJSON() ([]byte, error) {
Expand Down
120 changes: 4 additions & 116 deletions runtime/ast/expression_test.go
Expand Up @@ -4091,12 +4091,6 @@ func TestReferenceExpression_MarshalJSON(t *testing.T) {
Pos: Position{Offset: 1, Line: 2, Column: 3},
},
},
Type: &NominalType{
Identifier: Identifier{
Identifier: "AB",
Pos: Position{Offset: 4, Line: 5, Column: 6},
},
},
StartPos: Position{Offset: 7, Line: 8, Column: 9},
}

Expand All @@ -4117,18 +4111,8 @@ func TestReferenceExpression_MarshalJSON(t *testing.T) {
"StartPos": {"Offset": 1, "Line": 2, "Column": 3},
"EndPos": {"Offset": 6, "Line": 2, "Column": 8}
},
"TargetType": {
"Type": "NominalType",
"Identifier": {
"Identifier": "AB",
"StartPos": {"Offset": 4, "Line": 5, "Column": 6},
"EndPos": {"Offset": 5, "Line": 5, "Column": 7}
},
"StartPos": {"Offset": 4, "Line": 5, "Column": 6},
"EndPos": {"Offset": 5, "Line": 5, "Column": 7}
},
"StartPos": {"Offset": 7, "Line": 8, "Column": 9},
"EndPos": {"Offset": 5, "Line": 5, "Column": 7}
"EndPos": {"Offset": 6, "Line": 2, "Column": 8}
}
`,
string(actual),
Expand All @@ -4149,14 +4133,6 @@ func TestReferenceExpression_Doc(t *testing.T) {
Value: big.NewInt(42),
Base: 10,
},
Type: &ReferenceType{
Authorized: true,
Type: &NominalType{
Identifier: Identifier{
Identifier: "Int",
},
},
},
}

assert.Equal(t,
Expand All @@ -4166,14 +4142,6 @@ func TestReferenceExpression_Doc(t *testing.T) {
prettier.Group{
Doc: prettier.Text("42"),
},
prettier.Line{},
prettier.Text("as"),
prettier.Line{},
prettier.Concat{
prettier.Text("auth "),
prettier.Text("&"),
prettier.Text("Int"),
},
},
},
expr.Doc(),
Expand All @@ -4191,22 +4159,6 @@ func TestReferenceExpression_Doc(t *testing.T) {
Value: big.NewInt(42),
Base: 10,
},
Type: &ReferenceType{
Authorized: true,
Type: &NominalType{
Identifier: Identifier{
Identifier: "AnyStruct",
},
},
},
},
Type: &ReferenceType{
Authorized: true,
Type: &NominalType{
Identifier: Identifier{
Identifier: "XYZ",
},
},
},
}

Expand All @@ -4221,25 +4173,9 @@ func TestReferenceExpression_Doc(t *testing.T) {
prettier.Group{
Doc: prettier.Text("42"),
},
prettier.Line{},
prettier.Text("as"),
prettier.Line{},
prettier.Concat{
prettier.Text("auth "),
prettier.Text("&"),
prettier.Text("AnyStruct"),
},
},
},
},
prettier.Line{},
prettier.Text("as"),
prettier.Line{},
prettier.Concat{
prettier.Text("auth "),
prettier.Text("&"),
prettier.Text("XYZ"),
},
},
},
expr.Doc(),
Expand All @@ -4264,14 +4200,6 @@ func TestReferenceExpression_Doc(t *testing.T) {
},
},
},
Type: &ReferenceType{
Authorized: true,
Type: &NominalType{
Identifier: Identifier{
Identifier: "Int",
},
},
},
}

assert.Equal(t,
Expand Down Expand Up @@ -4304,14 +4232,6 @@ func TestReferenceExpression_Doc(t *testing.T) {
},
},
},
prettier.Line{},
prettier.Text("as"),
prettier.Line{},
prettier.Concat{
prettier.Text("auth "),
prettier.Text("&"),
prettier.Text("Int"),
},
},
},
expr.Doc(),
Expand All @@ -4333,18 +4253,10 @@ func TestReferenceExpression_String(t *testing.T) {
Value: big.NewInt(42),
Base: 10,
},
Type: &ReferenceType{
Authorized: true,
Type: &NominalType{
Identifier: Identifier{
Identifier: "Int",
},
},
},
}

assert.Equal(t,
"&42 as auth &Int",
"&42",
expr.String(),
)
})
Expand All @@ -4360,27 +4272,11 @@ func TestReferenceExpression_String(t *testing.T) {
Value: big.NewInt(42),
Base: 10,
},
Type: &ReferenceType{
Authorized: true,
Type: &NominalType{
Identifier: Identifier{
Identifier: "AnyStruct",
},
},
},
},
Type: &ReferenceType{
Authorized: true,
Type: &NominalType{
Identifier: Identifier{
Identifier: "XYZ",
},
},
},
}

assert.Equal(t,
"&&42 as auth &AnyStruct as auth &XYZ",
"&&42",
expr.String(),
)
})
Expand All @@ -4403,18 +4299,10 @@ func TestReferenceExpression_String(t *testing.T) {
},
},
},
Type: &ReferenceType{
Authorized: true,
Type: &NominalType{
Identifier: Identifier{
Identifier: "Int",
},
},
},
}

assert.Equal(t,
"&(foo - bar) as auth &Int",
"&(foo - bar)",
expr.String(),
)
})
Expand Down
24 changes: 7 additions & 17 deletions runtime/parser/expression.go
Expand Up @@ -1141,27 +1141,17 @@ func definePathExpression() {
}

func defineReferenceExpression() {
setExprNullDenotation(
lexer.TokenAmpersand,
func(p *parser, token lexer.Token) ast.Expression {
p.skipSpaceAndComments(true)
expression := parseExpression(p, exprLeftBindingPowerCasting-exprBindingPowerGap)

p.skipSpaceAndComments(true)

castingExpression, ok := expression.(*ast.CastingExpression)
if !ok {
panic(fmt.Errorf("expected casting expression"))
}

defineExpr(prefixExpr{
tokenType: lexer.TokenAmpersand,
bindingPower: exprLeftBindingPowerUnaryPrefix,
nullDenotation: func(p *parser, right ast.Expression, tokenRange ast.Range) ast.Expression {
return ast.NewReferenceExpression(
p.memoryGauge,
castingExpression.Expression,
castingExpression.TypeAnnotation.Type,
token.StartPos,
right,
tokenRange.StartPos,
)
},
)
})
}

func defineMemberExpression() {
Expand Down