-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
188 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package analyzer | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/nevalang/neva/internal/compiler/src" | ||
) | ||
|
||
var ( | ||
ErrMainComponentWithTypeParams = errors.New("main component can't have type parameters") | ||
ErrMainComponentNodes = errors.New("something wrong with main component's nodes") | ||
ErrEntityNotFoundByNodeRef = errors.New("entity not found by node ref") | ||
ErrMainComponentInportsCount = errors.New("main component must have one inport") | ||
ErrMainComponentOutportsCount = errors.New("main component must have exactly one outport") | ||
ErrMainComponentWithoutEnterInport = errors.New("main component must have 'enter' inport") | ||
ErrMainComponentWithoutExitOutport = errors.New("main component must have 'exit' outport") | ||
ErrMainPortIsArray = errors.New("main component's ports cannot not be arrays") | ||
ErrMainComponentPortTypeNotAny = errors.New("main component's ports must be of type any") | ||
ErrMainComponentNodeNotComponent = errors.New("main component's nodes must be components only") | ||
) | ||
|
||
func (a Analyzer) analyzeMainComponent(cmp src.Component, pkg src.Package, pkgs map[string]src.Package) error { //nolint:funlen,unparam,lll | ||
if len(cmp.Interface.Params) != 0 { | ||
return fmt.Errorf("%w: %v", ErrMainComponentWithTypeParams, cmp.Interface.Params) | ||
} | ||
|
||
if err := a.analyzeMainComponentIO(cmp.Interface.IO); err != nil { | ||
return fmt.Errorf("main component io: %w", err) | ||
} | ||
|
||
if err := a.analyzeMainComponentNodes(cmp.Nodes, pkg, pkgs); err != nil { | ||
return fmt.Errorf("%w: %v", ErrMainComponentNodes, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a Analyzer) analyzeMainComponentIO(io src.IO) error { | ||
if len(io.Out) != 1 { | ||
return fmt.Errorf("%w: %v", ErrMainComponentOutportsCount, io.Out) | ||
} | ||
|
||
if len(io.In) != 1 { | ||
return fmt.Errorf("%w: %v", ErrMainComponentInportsCount, io.In) | ||
} | ||
|
||
enterInport, ok := io.In["enter"] | ||
if !ok { | ||
return ErrMainComponentWithoutEnterInport | ||
} | ||
|
||
if enterInport.IsArr { | ||
return ErrMainPortIsArray | ||
} | ||
|
||
if enterInport.Type != nil { | ||
return ErrMainComponentPortTypeNotAny | ||
} | ||
|
||
exitInport, ok := io.In["exit"] | ||
if !ok { | ||
return ErrMainComponentWithoutExitOutport | ||
} | ||
|
||
if exitInport.IsArr { | ||
return ErrMainPortIsArray | ||
} | ||
|
||
if exitInport.Type != nil { | ||
return ErrMainComponentPortTypeNotAny | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (Analyzer) analyzeMainComponentNodes(nodes map[string]src.Node, pkg src.Package, prog src.Program) error { | ||
for _, node := range nodes { | ||
nodeEntity, err := prog.Entity(node.EntityRef) | ||
if err != nil { | ||
return fmt.Errorf("%w: %v", ErrEntityNotFoundByNodeRef, node.EntityRef) | ||
} | ||
if nodeEntity.Kind != src.ComponentEntity { | ||
return fmt.Errorf("%w: %v", ErrMainComponentNodeNotComponent, node.EntityRef) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package analyzer | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/nevalang/neva/internal/compiler/src" | ||
) | ||
|
||
func (a Analyzer) mainSpecificPkgValidation(pkg src.Package, pkgs map[string]src.Package) error { | ||
entityMain, ok := pkg.Entity("main") | ||
if !ok { | ||
panic("analyzer: no main entity") | ||
} | ||
|
||
if entityMain.Kind != src.ComponentEntity { | ||
panic("analyzer: main entity is not a component") | ||
} | ||
|
||
if entityMain.Exported { | ||
panic("analyzer: main entity is exported") | ||
} | ||
|
||
if err := a.analyzeMainComponent(entityMain.Component, pkg, pkgs); err != nil { | ||
panic(fmt.Errorf("analyzer: %w", err)) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.