Skip to content

Commit

Permalink
feat(analyzer): implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
emil14 committed Oct 9, 2023
1 parent 60b634a commit daa1669
Show file tree
Hide file tree
Showing 17 changed files with 277 additions and 226 deletions.
13 changes: 7 additions & 6 deletions internal/compiler/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ func (a Analyzer) analyzePkg(pkgName string, prog src.Program) (src.Package, err
}

if err := prog[pkgName].Entities(func(entity src.Entity, entityName, fileName string) error {
scope := Scope{
prog: prog, loc: Location{
pkg: pkgName,
file: fileName,
scope := src.Scope{
Prog: prog,
Loc: src.ScopeLocation{
PkgName: pkgName,
FileName: fileName,
},
}
resolvedEntity, err := a.analyzeEntity(entity, scope)
Expand All @@ -86,12 +87,12 @@ func (a Analyzer) analyzePkg(pkgName string, prog src.Program) (src.Package, err
return resolvedPkg, nil
}

func (a Analyzer) analyzeEntity(entity src.Entity, scope Scope) (src.Entity, error) {
func (a Analyzer) analyzeEntity(entity src.Entity, scope src.Scope) (src.Entity, error) {
resolvedEntity := src.Entity{
Exported: entity.Exported,
Kind: entity.Kind,
}
isStd := strings.HasPrefix(scope.loc.pkg, "std/")
isStd := strings.HasPrefix(scope.Loc.PkgName, "std/")

switch entity.Kind {
case src.TypeEntity:
Expand Down
20 changes: 10 additions & 10 deletions internal/compiler/analyzer/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type analyzeComponentParams struct {

func (a Analyzer) analyzeComponent(
comp src.Component,
scope Scope,
scope src.Scope,
params analyzeComponentParams,
) (src.Component, error) {
resolvedInterface, err := a.analyzeInterface(comp.Interface, scope, params.iface)
Expand All @@ -39,7 +39,7 @@ func (a Analyzer) analyzeComponent(
}, nil
}

func (a Analyzer) analyzeComponentNodes(nodes map[string]src.Node, scope Scope) (map[string]src.Node, error) {
func (a Analyzer) analyzeComponentNodes(nodes map[string]src.Node, scope src.Scope) (map[string]src.Node, error) {
resolvedNodes := make(map[string]src.Node, len(nodes))
for name, node := range nodes {
resolvedNode, err := a.analyzeComponentNode(node, scope)
Expand All @@ -57,7 +57,7 @@ var (
ErrNodeInterfaceDI = errors.New("interface node cannot have dependency injection")
)

func (a Analyzer) analyzeComponentNode(node src.Node, scope Scope) (src.Node, error) {
func (a Analyzer) analyzeComponentNode(node src.Node, scope src.Scope) (src.Node, error) {
entity, _, err := scope.Entity(node.EntityRef)
if err != nil {
return src.Node{}, fmt.Errorf("entity: %w", err)
Expand Down Expand Up @@ -116,7 +116,7 @@ func (a Analyzer) analyzeComponentNet(
net []src.Connection,
compInterface src.Interface,
nodes map[string]src.Node,
scope Scope,
scope src.Scope,
) ([]src.Connection, error) {
for _, conn := range net {
senderType, err := a.getSenderType(conn.SenderSide, compInterface.IO.In, nodes, scope)
Expand All @@ -140,7 +140,7 @@ func (a Analyzer) getReceiverType(
receiverSide src.ReceiverConnectionSide,
outports map[string]src.Port,
nodes map[string]src.Node,
scope Scope,
scope src.Scope,
) (ts.Expr, error) {
if receiverSide.PortAddr.Node == "in" {
return ts.Expr{}, ErrWriteSelfIn
Expand All @@ -165,7 +165,7 @@ func (a Analyzer) getReceiverType(
func (a Analyzer) getNodeInportType(
portAddr src.PortAddr,
nodes map[string]src.Node,
scope Scope,
scope src.Scope,
) (ts.Expr, error) {
node, ok := nodes[portAddr.Node]
if !ok {
Expand All @@ -189,7 +189,7 @@ func (a Analyzer) getResolvedPortType(
params []ts.Param,
portAddr src.PortAddr,
node src.Node,
scope Scope,
scope src.Scope,
) (ts.Expr, error) {
port, ok := ports[portAddr.Port]
if !ok {
Expand Down Expand Up @@ -224,7 +224,7 @@ func (a Analyzer) getSenderType(
senderSide src.SenderConnectionSide,
inports map[string]src.Port,
nodes map[string]src.Node,
scope Scope,
scope src.Scope,
) (ts.Expr, error) {
if senderSide.ConstRef != nil {
constTypeExpr, err := a.getConstType(*senderSide.ConstRef, scope)
Expand Down Expand Up @@ -260,7 +260,7 @@ func (a Analyzer) getSenderType(
func (a Analyzer) getNodeOutportType(
portAddr src.PortAddr,
nodes map[string]src.Node,
scope Scope,
scope src.Scope,
) (ts.Expr, error) {
node, ok := nodes[portAddr.Node]
if !ok {
Expand All @@ -281,7 +281,7 @@ func (a Analyzer) getNodeOutportType(
)
}

func (a Analyzer) getConstType(ref src.EntityRef, scope Scope) (ts.Expr, error) {
func (a Analyzer) getConstType(ref src.EntityRef, scope src.Scope) (ts.Expr, error) {
entity, _, err := scope.Entity(ref)
if err != nil {
return ts.Expr{}, fmt.Errorf("prog entity: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/analyzer/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
ErrConstValuesOfDifferentTypes = errors.New("constant cannot have values of different types at once")
)

func (a Analyzer) analyzeConst(cnst src.Const, scope Scope) (src.Const, error) {
func (a Analyzer) analyzeConst(cnst src.Const, scope src.Scope) (src.Const, error) {
if cnst.Value == nil && cnst.Ref == nil {
return src.Const{}, ErrEmptyConst
}
Expand Down
8 changes: 4 additions & 4 deletions internal/compiler/analyzer/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type analyzeInterfaceParams struct {

func (a Analyzer) analyzeInterface(
def src.Interface,
scope Scope,
scope src.Scope,
params analyzeInterfaceParams,
) (src.Interface, error) {
resolvedParams, err := a.analyzeTypeParams(def.TypeParams, scope)
Expand All @@ -44,7 +44,7 @@ var (
func (a Analyzer) analyzeIO(
typeParams []ts.Param,
io src.IO,
scope Scope,
scope src.Scope,
params analyzeInterfaceParams,
) (src.IO, error) {
if !params.allowEmptyInports && len(io.In) == 0 {
Expand Down Expand Up @@ -74,7 +74,7 @@ func (a Analyzer) analyzeIO(
func (a Analyzer) analyzePorts(
params []ts.Param,
ports map[string]src.Port,
scope Scope,
scope src.Scope,
) (map[string]src.Port, error) {
resolvedPorts := make(map[string]src.Port, len(ports))
for name, port := range ports {
Expand All @@ -87,7 +87,7 @@ func (a Analyzer) analyzePorts(
return resolvedPorts, nil
}

func (a Analyzer) analyzePort(params []ts.Param, port src.Port, scope Scope) (src.Port, error) {
func (a Analyzer) analyzePort(params []ts.Param, port src.Port, scope src.Scope) (src.Port, error) {
resolvedDef, err := a.analyzeTypeDef(ts.Def{
Params: params,
BodyExpr: &port.TypeExpr,
Expand Down
6 changes: 3 additions & 3 deletions internal/compiler/analyzer/main_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
ErrMainComponentNodeNotComponent = errors.New("main component's nodes must be components only")
)

func (a Analyzer) analyzeMainComponent(cmp src.Component, pkg src.Package, scope Scope) error {
func (a Analyzer) analyzeMainComponent(cmp src.Component, pkg src.Package, scope src.Scope) error {
if len(cmp.Interface.TypeParams) != 0 {
return fmt.Errorf("%w: %v", ErrMainComponentWithTypeParams, cmp.Interface.TypeParams)
}
Expand Down Expand Up @@ -67,13 +67,13 @@ func (a Analyzer) analyzeMainComponentPort(port src.Port) error {
if port.IsArray {
return ErrMainPortIsArray
}
if !(Scope{}).IsTopType(port.TypeExpr) {
if !(src.Scope{}).IsTopType(port.TypeExpr) {
return ErrMainComponentPortTypeNotAny
}
return nil
}

func (Analyzer) analyzeMainComponentNodes(nodes map[string]src.Node, pkg src.Package, scope Scope) error {
func (Analyzer) analyzeMainComponentNodes(nodes map[string]src.Node, pkg src.Package, scope src.Scope) error {
for name, node := range nodes {
nodeEntity, _, err := scope.Entity(node.EntityRef)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions internal/compiler/analyzer/main_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func (a Analyzer) mainSpecificPkgValidation(pkg src.Package, pkgs map[string]src
return ErrMainEntityExported
}

scope := Scope{
loc: Location{
pkg: "main",
file: filename,
scope := src.Scope{
Loc: src.ScopeLocation{
PkgName: "main",
FileName: filename,
},
prog: pkgs,
Prog: pkgs,
}

if err := a.analyzeMainComponent(entityMain.Component, pkg, scope); err != nil {
Expand Down
143 changes: 0 additions & 143 deletions internal/compiler/analyzer/scope.go

This file was deleted.

7 changes: 4 additions & 3 deletions internal/compiler/analyzer/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package analyzer
import (
"fmt"

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

Expand All @@ -12,7 +13,7 @@ type analyzeTypeDefParams struct {

var ErrEmptyTypeDefBody = fmt.Errorf("type def body is empty")

func (a Analyzer) analyzeTypeDef(def ts.Def, scope Scope, params analyzeTypeDefParams) (ts.Def, error) {
func (a Analyzer) analyzeTypeDef(def ts.Def, scope src.Scope, params analyzeTypeDefParams) (ts.Def, error) {
if !params.allowEmptyBody && def.BodyExpr == nil {
return ts.Def{}, ErrEmptyTypeDefBody
}
Expand All @@ -31,15 +32,15 @@ func (a Analyzer) analyzeTypeDef(def ts.Def, scope Scope, params analyzeTypeDefP
}, nil
}

func (a Analyzer) analyzeTypeExpr(expr ts.Expr, scope Scope) (ts.Expr, error) {
func (a Analyzer) analyzeTypeExpr(expr ts.Expr, scope src.Scope) (ts.Expr, error) {
resolvedExpr, err := a.resolver.ResolveExpr(expr, scope)
if err != nil {
return ts.Expr{}, fmt.Errorf("resolve expr: %w", err)
}
return resolvedExpr, nil
}

func (a Analyzer) analyzeTypeParams(params []ts.Param, scope Scope) ([]ts.Param, error) {
func (a Analyzer) analyzeTypeParams(params []ts.Param, scope src.Scope) ([]ts.Param, error) {
resolvedParams, err := a.resolver.ResolveParams(params, scope)
if err != nil {
return nil, fmt.Errorf("resolve params: %w", err)
Expand Down
Loading

0 comments on commit daa1669

Please sign in to comment.