Skip to content

Commit

Permalink
feat(analyzer): remove support for const ref in analyzer cuz it expec…
Browse files Browse the repository at this point in the history
…ts desugared form now
  • Loading branch information
emil14 committed Dec 21, 2023
1 parent d4d61c3 commit 889ff4d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 48 deletions.
48 changes: 2 additions & 46 deletions internal/compiler/analyzer/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ var (
ErrUnusedNode = errors.New("Unused node found")
ErrUnusedNodeInport = errors.New("Unused node inport found")
ErrUnusedNodeOutport = errors.New("Unused node outport found")
ErrSenderConstRefEntityKind = errors.New("Sender in network with entity reference can only refer to a constant")
ErrSenderIsEmpty = errors.New("Sender in network must either refer to some port address or constant")
ErrSenderIsEmpty = errors.New("Sender in network must refer to some port address")
ErrReadSelfOut = errors.New("Component cannot read from self outport")
ErrWriteSelfIn = errors.New("Component cannot write to self inport")
ErrInportNotFound = errors.New("Referenced inport not found in component's interface")
Expand Down Expand Up @@ -451,24 +450,14 @@ func (a Analyzer) getSenderType(
nodesIfaces map[string]src.Interface,
scope src.Scope,
) (ts.Expr, *Error) {
if senderSide.ConstRef != nil {
constTypeExpr, err := a.getConstType(*senderSide.ConstRef, scope)
if err != nil {
return ts.Expr{}, Error{
Location: &scope.Location,
Meta: &senderSide.ConstRef.Meta,
}.Merge(err)
}
return constTypeExpr, nil
}

if senderSide.PortAddr == nil {
return ts.Expr{}, &Error{
Err: ErrSenderIsEmpty,
Location: &scope.Location,
Meta: &senderSide.Meta,
}
}

if senderSide.PortAddr.Node == "out" {
return ts.Expr{}, &Error{
Err: ErrReadSelfOut,
Expand Down Expand Up @@ -542,36 +531,3 @@ func (a Analyzer) getNodeOutportType(

return typ, err
}

func (a Analyzer) getConstType(ref src.EntityRef, scope src.Scope) (ts.Expr, *Error) {
entity, _, err := scope.Entity(ref)
if err != nil {
return ts.Expr{}, &Error{
Err: err,
Location: &scope.Location,
Meta: &ref.Meta,
}
}

if entity.Kind != src.ConstEntity {
return ts.Expr{}, &Error{
Err: fmt.Errorf("%w: %v", ErrSenderConstRefEntityKind, entity.Kind),
Location: &scope.Location,
Meta: entity.Meta(),
}
}

if entity.Const.Ref != nil {
expr, err := a.getConstType(*entity.Const.Ref, scope)
if err != nil {
return ts.Expr{}, Error{
Location: &scope.Location,
Meta: entity.Meta(),
}.Merge(err)
}

return expr, nil
}

return entity.Const.Value.TypeExpr, nil
}
1 change: 1 addition & 0 deletions internal/compiler/analyzer/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

// Error is custom error interface implementation that allows to keep track of code location.
// TODO move this to compiler or src package
type Error struct {
Err error
Location *src.Location
Expand Down
55 changes: 54 additions & 1 deletion internal/compiler/desugarer/desugarer.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,62 @@
package desugarer

import src "github.com/nevalang/neva/internal/compiler/sourcecode"
import (
"errors"
"fmt"

"github.com/nevalang/neva/internal/compiler/analyzer"
src "github.com/nevalang/neva/internal/compiler/sourcecode"
ts "github.com/nevalang/neva/pkg/typesystem"
)

var ErrConstSenderEntityKind = errors.New("Entity that is used as a const reference in component's network must be of kind constant") //nolint:lll

type Desugarer struct{}

func (d Desugarer) Desugar(mod src.Module) (src.Module, error) {
return mod, nil
}

// if senderSide.ConstRef != nil {
// constTypeExpr, err := d.getConstType(*senderSide.ConstRef, scope)
// if err != nil {
// return ts.Expr{}, Error{
// Location: &scope.Location,
// Meta: &senderSide.ConstRef.Meta,
// }.Merge(err)
// }
// return constTypeExpr, nil
// }

func (d Desugarer) getConstType(ref src.EntityRef, scope src.Scope) (ts.Expr, *analyzer.Error) {
entity, _, err := scope.Entity(ref)
if err != nil {
return ts.Expr{}, &analyzer.Error{
Err: err,
Location: &scope.Location,
Meta: &ref.Meta,
}
}

if entity.Kind != src.ConstEntity {
return ts.Expr{}, &analyzer.Error{
Err: fmt.Errorf("%w: %v", ErrConstSenderEntityKind, entity.Kind),
Location: &scope.Location,
Meta: entity.Meta(),
}
}

if entity.Const.Ref != nil {
expr, err := d.getConstType(*entity.Const.Ref, scope)
if err != nil {
return ts.Expr{}, analyzer.Error{
Location: &scope.Location,
Meta: entity.Meta(),
}.Merge(err)
}

return expr, nil
}

return entity.Const.Value.TypeExpr, nil
}
2 changes: 1 addition & 1 deletion internal/compiler/sourcecode/src.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ type ReceiverConnectionSide struct {
// SenderConnectionSide unlike ReceiverConnectionSide could refer to constant.
type SenderConnectionSide struct {
PortAddr *PortAddr `json:"portAddr,omitempty"`
ConstRef *EntityRef `json:"constRef,omitempty"`
ConstRef *EntityRef `json:"constRef,omitempty"` // Only sugared form
Selectors []string `json:"selectors,omitempty"`
Meta Meta `json:"meta,omitempty"`
}
Expand Down

0 comments on commit 889ff4d

Please sign in to comment.