Skip to content

Commit

Permalink
refactor(ts): rename ts->typesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
emil14 committed Sep 29, 2023
1 parent 42dc888 commit 97c83c4
Show file tree
Hide file tree
Showing 22 changed files with 100 additions and 37 deletions.
70 changes: 66 additions & 4 deletions internal/compiler/analyzer/analyzer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Package analyzer implements source code analysis.
package analyzer

Check failure on line 1 in internal/compiler/analyzer/analyzer.go

View workflow job for this annotation

GitHub Actions / lint

: # github.com/nevalang/neva/internal/compiler/analyzer

import (
Expand All @@ -8,15 +7,18 @@ import (
"github.com/nevalang/neva/internal/compiler/src"
)

// TODO move to task that package must not be empty
type Analyzer struct{}

var (
ErrEmptyProgram = errors.New("empty program")
ErrMainPkgNotFound = errors.New("main package not found")
ErrEmptyProgram = errors.New("empty program")
ErrMainPkgNotFound = errors.New("main package not found")
ErrEmptyPkg = errors.New("package must not be empty")
ErrUnknownEntityKind = errors.New("unknown entity kind")
)

func (a Analyzer) Analyze(prog src.Program) (src.Program, error) {
if prog == nil {
if len(prog) == 0 {
return nil, ErrEmptyProgram
}

Expand All @@ -29,5 +31,65 @@ func (a Analyzer) Analyze(prog src.Program) (src.Program, error) {
return nil, fmt.Errorf("main specific pkg validation: %w", err)
}

for _, pkg := range prog {
if err := a.analyzePkg(pkg, prog); err != nil {
return nil, fmt.Errorf("analyze pkg: %w", err)
}
}

return nil, nil
}

func (a Analyzer) analyzePkg(pkg src.Package, prog src.Program) error {
if len(pkg) == 0 {
return ErrEmptyPkg
}

if err := pkg.Entities(func(entity src.Entity, entityName, fileName string) error {
return a.analyzeEntity(entityName, entity, pkg, prog)
}); err != nil {
return fmt.Errorf("entities: %w", err)
}

return nil
}

func (a Analyzer) analyzeEntity(entityName string, entity src.Entity, pkg src.Package, prog src.Program) error {
analyzedEntity := src.Entity{
Exported: entity.Exported,
Kind: entity.Kind,
}

switch entity.Kind {
case src.TypeEntity:
resolvedType, err := a.resolveType(entity.Type)
if err != nil {
return fmt.Errorf("resolve type: %w", err)
}
analyzedEntity.Type = resolvedType
case src.ConstEntity:
analyzedConst, err := a.analyzeConst(entity.Const)
if err != nil {
return fmt.Errorf("analyze const: %w", err)
}
analyzedEntity.Const = analyzedConst
case src.InterfaceEntity:
analyzedEntity, err := a.analyzeInterface(entity.Interface)
if err != nil {
return fmt.Errorf("analyze interface: %w", err)
}
analyzedEntity.Interface = analyzedEntity
case src.ComponentEntity:
analyzedComponent, err := a.analyzeComponent(entity.Component)
if err != nil {
return fmt.Errorf("analyze component: %w", err)
}
analyzedEntity.Component = analyzedComponent
default:
return fmt.Errorf("%w: %v", ErrUnknownEntityKind, entity.Kind)
}

pkg[entityName] = analyzedEntity

return nil
}
5 changes: 3 additions & 2 deletions internal/compiler/analyzer/main_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ func (a Analyzer) mainSpecificPkgValidation(pkg src.Package, pkgs map[string]src
return fmt.Errorf("analyze main component: %w", err)
}

if err := pkg.Entities(func(entity src.Entity, entityName, fileName string) error { // FIXME will conflict with general validation
// FIXME will conflict with general validation
if err := pkg.Entities(func(entity src.Entity, entityName, fileName string) error {
if entity.Exported {
return fmt.Errorf("%w: file %v, entity %v", ErrMainPkgExports, fileName, entityName)
}
return nil
}); err != nil {
return err
return fmt.Errorf("entities: %w", err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/parser/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

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

func (s *treeShapeListener) EnterProg(actx *generated.ProgContext) {
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/parser/listener_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package parser
import (
generated "github.com/nevalang/neva/internal/compiler/parser/generated"
"github.com/nevalang/neva/internal/compiler/src"
"github.com/nevalang/neva/pkg/ts"
ts "github.com/nevalang/neva/pkg/typesystem"
)

func parseTypeParams(params generated.ITypeParamsContext) []ts.Param {
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/src/src.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package src
import (
"fmt"

ts "github.com/nevalang/neva/pkg/ts"
ts "github.com/nevalang/neva/pkg/typesystem"
)

type Program map[string]Package
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/std/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package std

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

// New returns single-package std library with only runtime-function interfaces. TODO add HOCs.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ts
package typesystem

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ts_test
package typesystem_test

import (
"testing"

"github.com/golang/mock/gomock"
ts "github.com/nevalang/neva/pkg/ts"
ts "github.com/nevalang/neva/pkg/typesystem"
"github.com/stretchr/testify/require"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/ts/helper.go → pkg/typesystem/helper.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ts
package typesystem

// Helper is just a namespace for helper functions to avoid conflicts with entity types.
// It's a stateless type and it's safe to share it between goroutines.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//go:build integration
// +build integration

package ts_test
package typesystem_test

import (
"testing"

"github.com/stretchr/testify/assert"

ts "github.com/nevalang/neva/pkg/ts"
ts "github.com/nevalang/neva/pkg/typesystem"
)

func TestDefaultResolver(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/ts/mocks_test.go → pkg/typesystem/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/ts/resolver.go → pkg/typesystem/resolver.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ts
package typesystem

import (
"errors"
Expand Down
4 changes: 2 additions & 2 deletions pkg/ts/resolver_test.go → pkg/typesystem/resolver_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ts_test
package typesystem_test

import (
"errors"
Expand All @@ -7,7 +7,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

ts "github.com/nevalang/neva/pkg/ts"
ts "github.com/nevalang/neva/pkg/typesystem"
)

var errTest = errors.New("")
Expand Down
4 changes: 2 additions & 2 deletions pkg/ts/scope_test.go → pkg/typesystem/scope_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package ts_test
package typesystem_test

import (
"errors"

ts "github.com/nevalang/neva/pkg/ts"
ts "github.com/nevalang/neva/pkg/typesystem"
)

var ErrDefaultScope = errors.New("default scope")
Expand Down
2 changes: 1 addition & 1 deletion pkg/ts/terminator.go → pkg/typesystem/terminator.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ts
package typesystem

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package ts_test
package typesystem_test

import (
"testing"

ts "github.com/nevalang/neva/pkg/ts"
ts "github.com/nevalang/neva/pkg/typesystem"

"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/ts/trace.go → pkg/typesystem/trace.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ts
package typesystem

// Linked-list to handle recursive types
type Trace struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/ts/trace_test.go → pkg/typesystem/trace_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package ts_test
package typesystem_test

import (
"testing"

ts "github.com/nevalang/neva/pkg/ts"
ts "github.com/nevalang/neva/pkg/typesystem"
"github.com/stretchr/testify/assert"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/ts/ts.go → pkg/typesystem/ts.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package ts implements static type-system with generics and structural subtyping.
package ts
// package typesystem implements static type-system with generics and structural subtyping.
package typesystem

import "fmt"

Expand Down
4 changes: 2 additions & 2 deletions pkg/ts/ts_test.go → pkg/typesystem/ts_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package ts_test
package typesystem_test

import (
"testing"

"github.com/stretchr/testify/require"

ts "github.com/nevalang/neva/pkg/ts"
ts "github.com/nevalang/neva/pkg/typesystem"
)

var h ts.Helper
Expand Down
2 changes: 1 addition & 1 deletion pkg/ts/validator.go → pkg/typesystem/validator.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ts
package typesystem

import (
"errors"
Expand Down
4 changes: 2 additions & 2 deletions pkg/ts/validator_test.go → pkg/typesystem/validator_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package ts_test
package typesystem_test

import (
"testing"

"github.com/stretchr/testify/require"

ts "github.com/nevalang/neva/pkg/ts"
ts "github.com/nevalang/neva/pkg/typesystem"
)

func TestValidator_Validate(t *testing.T) {
Expand Down

0 comments on commit 97c83c4

Please sign in to comment.