Skip to content

Commit

Permalink
refactor(ts)
Browse files Browse the repository at this point in the history
  • Loading branch information
emil14 committed Sep 28, 2023
1 parent da13e97 commit f27f699
Show file tree
Hide file tree
Showing 32 changed files with 1,306 additions and 234 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ linters-settings:
issues:
exclude-rules:
- source: "^//go:generate"
linters: [funlen , lll , varnamelen, err113]
linters: [funlen , lll , varnamelen, err113, dupword]
- path: _test\.go
linters: [funlen , lll , varnamelen, err113]
linters: [funlen , lll , varnamelen, err113, dupword]
2 changes: 1 addition & 1 deletion cmd/vm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"os"

"github.com/nevalang/neva/internal/vm/repo/disk"
"github.com/nevalang/neva/internal/runtime"
"github.com/nevalang/neva/internal/runtime/funcs"
"github.com/nevalang/neva/internal/vm"
"github.com/nevalang/neva/internal/vm/decoder/proto"
"github.com/nevalang/neva/internal/vm/repo/disk"
)

func main() {
Expand Down
18 changes: 14 additions & 4 deletions internal/compiler/analyzer/analyzer.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
// Package analyzer implements source code analysis.
package analyzer

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

"github.com/nevalang/neva/internal/compiler/src"
)

type Analyzer struct{}

var (
ErrEmptyProgram = errors.New("empty program")
ErrMainPkgNotFound = errors.New("main package not found")
)

func (a Analyzer) Analyze(prog src.Program) (src.Program, error) {
if prog == nil {
panic("analyzer: nil program")
return nil, ErrEmptyProgram
}

mainPkg, ok := prog["main"]
if !ok {
panic("analyzer: no main package")
return nil, ErrMainPkgNotFound
}

if err := a.mainSpecificPkgValidation(mainPkg, prog); err != nil {
panic(err)
return nil, fmt.Errorf("main specific pkg validation: %w", err)
}

return nil, nil
Expand Down
2 changes: 1 addition & 1 deletion 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, pkgs map[string]src.Package) error { //nolint:funlen,unparam,lll
func (a Analyzer) analyzeMainComponent(cmp src.Component, pkg src.Package, pkgs map[string]src.Package) error { //nolint:unparam,lll
if len(cmp.Interface.Params) != 0 {
return fmt.Errorf("%w: %v", ErrMainComponentWithTypeParams, cmp.Interface.Params)
}
Expand Down
15 changes: 11 additions & 4 deletions internal/compiler/analyzer/main_pkg.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package analyzer

import (
"errors"
"fmt"

"github.com/nevalang/neva/internal/compiler/src"
)

var (
ErrMainEntityNotFound = errors.New("entity main is not found")
ErrMainEntityIsNotComponent = errors.New("main entity is not a component")
ErrMainEntityExported = errors.New("main entity is exported")
)

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")
return ErrMainEntityNotFound
}

if entityMain.Kind != src.ComponentEntity {
panic("analyzer: main entity is not a component")
return ErrMainEntityIsNotComponent
}

if entityMain.Exported {
panic("analyzer: main entity is exported")
return ErrMainEntityExported
}

if err := a.analyzeMainComponent(entityMain.Component, pkg, pkgs); err != nil {
panic(fmt.Errorf("analyzer: %w", err))
return fmt.Errorf("analyze main component: %w", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions internal/compiler/irgen/irgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type (
}
)

func (g Generator) processComponentNode(
func (g Generator) processComponentNode( //nolint:funlen
ctx context.Context,
nodeCtx nodeContext,
pkgs map[string]src.File,
Expand Down Expand Up @@ -211,7 +211,7 @@ func (g Generator) processNet(
return result, nil
}

func (g Generator) processSenderSide(
func (g Generator) processSenderSide( //nolint:funlen
pkgs map[string]src.File,
nodeCtx nodeContext,
senderSide src.SenderConnectionSide,
Expand Down
5 changes: 2 additions & 3 deletions internal/compiler/parser/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func (s *treeShapeListener) EnterUseStmt(actx *generated.UseStmtContext) {
func (s *treeShapeListener) EnterImportDef(actx *generated.ImportDefContext) {
path := actx.ImportPath().GetText()
alias := path
id := actx.IDENTIFIER()
if id != nil {
if id := actx.IDENTIFIER(); id != nil {
alias = actx.IDENTIFIER().GetText()
}
s.file.Imports[alias] = path
Expand All @@ -39,7 +38,7 @@ func (s *treeShapeListener) EnterTypeDef(actx *generated.TypeDefContext) {
Kind: src.TypeEntity,
Type: ts.Def{
Params: parseTypeParams(actx.TypeParams()),
BodyExpr: *parseTypeExpr(actx.TypeExpr()),
BodyExpr: parseTypeExpr(actx.TypeExpr()),
},
}
s.file.Entities[name] = result
Expand Down
18 changes: 7 additions & 11 deletions internal/compiler/parser/listener_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func parseTypeParams(params generated.ITypeParamsContext) []ts.Param {
parsedParamExpr := parseTypeExpr(typeParam.TypeExpr())
result = append(result, ts.Param{
Name: paramName,
Constr: *parsedParamExpr,
Constr: parsedParamExpr,
})
}

Expand All @@ -41,7 +41,7 @@ func parseTypeInstExpr(instExpr generated.ITypeInstExprContext) ts.Expr {

ref := instExpr.IDENTIFIER().GetText()
result := ts.Expr{
Inst: ts.InstExpr{
Inst: &ts.InstExpr{
Ref: ref,
},
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func parseInterfaceDef(actx generated.IInterfaceDefContext) src.Interface {
}
}

func parseNodes(actx []generated.ICompNodesDefContext) map[string]src.Node {
func parseNodes(actx []generated.ICompNodesDefContext) map[string]src.Node { //nolint:funlen
result := map[string]src.Node{}

for _, nodesDef := range actx {
Expand All @@ -102,7 +102,7 @@ func parseNodes(actx []generated.ICompNodesDefContext) map[string]src.Node {
name string
node src.Node
)
if abs != nil {
if abs != nil { //nolint:nestif
name = abs.IDENTIFIER().GetText()
expr := parseTypeInstExpr(abs.TypeInstExpr())
node = src.Node{
Expand All @@ -113,9 +113,7 @@ func parseNodes(actx []generated.ICompNodesDefContext) map[string]src.Node {
name = concrete.IDENTIFIER().GetText()
concreteNodeInst := concrete.ConcreteNodeInst()

var (
pkg, nodeRef string
)
var pkg, nodeRef string
nodePath := concreteNodeInst.EntityRef().AllIDENTIFIER()
if len(nodePath) == 2 {
pkg = nodePath[0].GetText()
Expand Down Expand Up @@ -154,9 +152,7 @@ func parseNodes(actx []generated.ICompNodesDefContext) map[string]src.Node {
}

func parseConcreteNode(nodeInst generated.IConcreteNodeInstContext) src.Node {
var (
pkg, nodeRef string
)
var pkg, nodeRef string
nodePath := nodeInst.EntityRef().AllIDENTIFIER()
if len(nodePath) == 2 {
pkg = nodePath[0].GetText()
Expand Down Expand Up @@ -188,7 +184,7 @@ func parseTypeExprs(in []generated.ITypeExprContext) []ts.Expr {
return result
}

func parseNet(actx []generated.ICompNetDefContext) []src.Connection {
func parseNet(actx []generated.ICompNetDefContext) []src.Connection { //nolint:funlen
result := []src.Connection{}

for _, connDefs := range actx {
Expand Down
3 changes: 1 addition & 2 deletions internal/compiler/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"context"

"github.com/antlr4-go/antlr/v4"
"golang.org/x/sync/errgroup"

generated "github.com/nevalang/neva/internal/compiler/parser/generated"
"github.com/nevalang/neva/internal/compiler/src"
"golang.org/x/sync/errgroup"
)

type treeShapeListener struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/parser/tests/smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

"github.com/antlr4-go/antlr/v4"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

generated "github.com/nevalang/neva/internal/compiler/parser/generated"
"github.com/stretchr/testify/require"
)

type TreeShapeListener struct {
Expand Down
42 changes: 21 additions & 21 deletions internal/compiler/std/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func New() src.File { //nolint:funlen
Out: map[string]src.Port{
"v": {
Type: &ts.Expr{
Inst: ts.InstExpr{Ref: "str"},
Inst: &ts.InstExpr{Ref: "str"},
},
},
},
Expand All @@ -43,14 +43,14 @@ func New() src.File { //nolint:funlen
In: map[string]src.Port{
"v": {
Type: &ts.Expr{
Inst: ts.InstExpr{Ref: "t1"},
Inst: &ts.InstExpr{Ref: "t1"},
},
},
},
Out: map[string]src.Port{
"v": {
Type: &ts.Expr{
Inst: ts.InstExpr{Ref: "t1"},
Inst: &ts.InstExpr{Ref: "t1"},
},
},
},
Expand All @@ -70,15 +70,15 @@ func New() src.File { //nolint:funlen
In: map[string]src.Port{
"v": {
Type: &ts.Expr{
Inst: ts.InstExpr{Ref: "t1"},
Inst: &ts.InstExpr{Ref: "t1"},
},
},
"sig": {},
},
Out: map[string]src.Port{
"v": {
Type: &ts.Expr{
Inst: ts.InstExpr{Ref: "t1"},
Inst: &ts.InstExpr{Ref: "t1"},
},
},
},
Expand All @@ -99,7 +99,7 @@ func New() src.File { //nolint:funlen
Out: map[string]src.Port{
"v": {
Type: &ts.Expr{
Inst: ts.InstExpr{Ref: "t1"},
Inst: &ts.InstExpr{Ref: "t1"},
},
},
},
Expand All @@ -115,17 +115,17 @@ func New() src.File { //nolint:funlen
Params: []ts.Param{
{
Name: "t1",
Constr: ts.Expr{
Lit: ts.LitExpr{
Constr: &ts.Expr{
Lit: &ts.LitExpr{
Union: []ts.Expr{
{
Inst: ts.InstExpr{Ref: "int"},
Inst: &ts.InstExpr{Ref: "int"},
},
{
Inst: ts.InstExpr{Ref: "float"},
Inst: &ts.InstExpr{Ref: "float"},
},
{
Inst: ts.InstExpr{Ref: "str"},
Inst: &ts.InstExpr{Ref: "str"},
},
},
},
Expand All @@ -136,19 +136,19 @@ func New() src.File { //nolint:funlen
In: map[string]src.Port{
"a": {
Type: &ts.Expr{
Inst: ts.InstExpr{Ref: "t1"},
Inst: &ts.InstExpr{Ref: "t1"},
},
},
"b": {
Type: &ts.Expr{
Inst: ts.InstExpr{Ref: "t1"},
Inst: &ts.InstExpr{Ref: "t1"},
},
},
},
Out: map[string]src.Port{
"v": {
Type: &ts.Expr{
Inst: ts.InstExpr{Ref: "t1"},
Inst: &ts.InstExpr{Ref: "t1"},
},
},
},
Expand All @@ -164,14 +164,14 @@ func New() src.File { //nolint:funlen
Params: []ts.Param{
{
Name: "t1",
Constr: ts.Expr{
Lit: ts.LitExpr{
Constr: &ts.Expr{
Lit: &ts.LitExpr{
Union: []ts.Expr{
{
Inst: ts.InstExpr{Ref: "int"},
Inst: &ts.InstExpr{Ref: "int"},
},
{
Inst: ts.InstExpr{Ref: "float"},
Inst: &ts.InstExpr{Ref: "float"},
},
},
},
Expand All @@ -182,19 +182,19 @@ func New() src.File { //nolint:funlen
In: map[string]src.Port{
"v": {
Type: &ts.Expr{
Inst: ts.InstExpr{Ref: "str"},
Inst: &ts.InstExpr{Ref: "str"},
},
},
},
Out: map[string]src.Port{
"v": {
Type: &ts.Expr{
Inst: ts.InstExpr{Ref: "t1"},
Inst: &ts.InstExpr{Ref: "t1"},
},
},
"err": {
Type: &ts.Expr{
Inst: ts.InstExpr{Ref: "str"},
Inst: &ts.InstExpr{Ref: "str"},
},
},
},
Expand Down
Loading

0 comments on commit f27f699

Please sign in to comment.