Skip to content

Commit

Permalink
refactor: get library from libpaths
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCoene committed Sep 15, 2024
1 parent 1394354 commit 1b7fc9a
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 53 deletions.
6 changes: 1 addition & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ type lspConfig struct {
Severity []string `json:"severity"`
}

type libPath string

type Config struct {
Lsp *lspConfig `json:"lsp"`
Library libPath `json:"library"`
Lsp *lspConfig `json:"lsp"`
}

func makeConfigPath(conf string) string {
Expand All @@ -44,7 +41,6 @@ func hasConfig(conf string) bool {

func ReadConfig() *Config {
configuration := &Config{
Library: "",
Lsp: &lspConfig{
When: []string{"open", "save", "close", "text"},
Severity: []string{"fatal", "warn", "info", "hint"},
Expand Down
6 changes: 3 additions & 3 deletions environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ type Environment struct {
outer *Environment
}

var library string
var library []string

func SetLibrary(path string) {
library = path
func SetLibrary(paths []string) {
library = paths
}

func Enclose(outer *Environment, t ast.Types) *Environment {
Expand Down
86 changes: 43 additions & 43 deletions environment/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,63 +114,63 @@ func isLoaded(library string) bool {
return false
}

func (env *Environment) LoadPackageTypes(pkg string) error {
if library == "" {
return nil
func (env *Environment) LoadPackageTypes(pkg string) {
if len(library) == 0 {
return
}

if isLoaded(pkg) {
return nil
return
}

packagesLoaded = append(packagesLoaded, library)
packagesLoaded = append(packagesLoaded, pkg)

typeFile := path.Join(library, pkg, "types.vp")
for _, lib := range library {
typeFile := path.Join(lib, pkg, "types.vp")

if _, err := os.Stat(typeFile); errors.Is(err, os.ErrNotExist) {
return err
}
if _, err := os.Stat(typeFile); errors.Is(err, os.ErrNotExist) {
continue
}

content, err := os.ReadFile(typeFile)
content, err := os.ReadFile(typeFile)

if err != nil {
return err
}
if err != nil {
continue
}

// lex
l := lexer.NewCode(typeFile, string(content))
l.Run()
// lex
l := lexer.NewCode(typeFile, string(content))
l.Run()

if l.HasError() {
return errors.New("failed to lex types file")
}
if l.HasError() {
continue
}

// parse
p := parser.New(l)
prog := p.Run()
// parse
p := parser.New(l)
prog := p.Run()

if p.HasError() {
return errors.New("failed to lex types file")
}
if p.HasError() {
continue
}

// range over the Statements
// these should all be type declarations
for _, p := range prog.Statements {
switch node := p.(type) {
case *ast.TypeStatement:
env.SetType(
Type{
Token: node.Token,
Type: node.Type,
Attributes: node.Attributes,
Object: node.Object,
Name: node.Name,
Package: pkg,
Used: true,
},
)
// range over the Statements
// these should all be type declarations
for _, p := range prog.Statements {
switch node := p.(type) {
case *ast.TypeStatement:
env.SetType(
Type{
Token: node.Token,
Type: node.Type,
Attributes: node.Attributes,
Object: node.Object,
Name: node.Name,
Package: pkg,
Used: true,
},
)
}
}
}

return nil
}
24 changes: 24 additions & 0 deletions r/libpath.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package r

import (
"encoding/json"
)

func LibPath() []string {
var paths []string
output, err := Callr(`paths <- paste0(.libPaths(), collapse = "\",\"")
paths <- paste0("[\"", paths, "\"]")
cat(paths)`)

if err != nil {
return paths
}

err = json.Unmarshal(output, &paths)

if err != nil {
return paths
}

return paths
}
3 changes: 2 additions & 1 deletion run.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/devOpifex/vapour/devtools"
"github.com/devOpifex/vapour/environment"
"github.com/devOpifex/vapour/lsp"
"github.com/devOpifex/vapour/r"
)

func run(code string) {
Expand All @@ -34,7 +35,7 @@ func run(code string) {
func (v *vapour) Run(args cli.CLI) {
v.config = config.ReadConfig()

environment.SetLibrary(string(v.config.Library))
environment.SetLibrary(r.LibPath())

if *args.Indir != "" {
ok := v.transpile(args)
Expand Down
2 changes: 1 addition & 1 deletion walker/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ func (w *Walker) walkInfixExpressionNS(node *ast.InfixExpression, operator strin
)
}

_ = w.env.LoadPackageTypes(ln.Item().Value)
w.env.LoadPackageTypes(ln.Item().Value)

if node.Right == nil {
w.addFatalf(
Expand Down
30 changes: 30 additions & 0 deletions walker/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"testing"

"github.com/devOpifex/vapour/diagnostics"
"github.com/devOpifex/vapour/environment"
"github.com/devOpifex/vapour/lexer"
"github.com/devOpifex/vapour/parser"
"github.com/devOpifex/vapour/r"
)

func (w *Walker) testDiagnostics(t *testing.T, expected diagnostics.Diagnostics) {
Expand Down Expand Up @@ -1239,3 +1241,31 @@ print(c)

w.testDiagnostics(t, expected)
}

func TestTypeImport(t *testing.T) {
code := `
let x: vape::user = vape::user(id = 1)
let y: vape::user = vape::user(id = "char")
let w: vape::user = vape::user(wrong = 2)
`

l := lexer.NewTest(code)

l.Run()
p := parser.New(l)

prog := p.Run()

environment.SetLibrary(r.LibPath())
w := New()

w.Run(prog)

expected := diagnostics.Diagnostics{
{Severity: diagnostics.Fatal},
{Severity: diagnostics.Fatal},
}

w.testDiagnostics(t, expected)
}

0 comments on commit 1b7fc9a

Please sign in to comment.