Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: import types #61

Merged
merged 4 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func (ls *LetStatement) String() string {

var out bytes.Buffer

out.WriteString("# " + ls.Type.String() + "\n")

out.WriteString(ls.Name)
out.WriteString(" = ")

Expand Down Expand Up @@ -103,8 +105,9 @@ func (cs *ConstStatement) String() string {
}

type Type struct {
Name string
List bool
Name string
Package string
List bool
}

func (t *Type) String() string {
Expand All @@ -118,6 +121,10 @@ func (types Types) String() string {
for _, t := range types {
name := t.Name

if t.Package != "" {
name = t.Package + "::" + name
}

if t.List {
name = "[]" + name
}
Expand Down
6 changes: 5 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ type lspConfig struct {
Severity []string `json:"severity"`
}

type libPath string

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

func makeConfigPath(conf string) string {
Expand All @@ -41,6 +44,7 @@ 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
60 changes: 49 additions & 11 deletions environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type Environment struct {
outer *Environment
}

var library string

func SetLibrary(path string) {
library = path
}

func Enclose(outer *Environment, t ast.Types) *Environment {
env := New()
env.returnType = t
Expand Down Expand Up @@ -67,7 +73,7 @@ var baseObjects = []string{
"impliedList",
}

func New() *Environment {
func NewGlobalEnvironment() *Environment {
v := make(map[string]Variable)
t := make(map[string]Type)
f := make(map[string]Function)
Expand All @@ -90,7 +96,7 @@ func New() *Environment {
}

for _, t := range baseTypes {
env.SetType(t, Type{Used: true, Type: []*ast.Type{{Name: t, List: false}}})
env.SetType(Type{Used: true, Name: t, Type: []*ast.Type{{Name: t, List: false}}})
}

fns, err := r.ListBaseFunctions()
Expand All @@ -109,6 +115,29 @@ func New() *Environment {
return env
}

func New() *Environment {
v := make(map[string]Variable)
t := make(map[string]Type)
f := make(map[string]Function)
c := make(map[string]Class)
m := make(map[string]Matrix)
s := make(map[string]Signature)
fct := make(map[string]Factor)
meth := make(map[string]Methods)

return &Environment{
functions: f,
variables: v,
types: t,
class: c,
matrix: m,
signature: s,
factor: fct,
method: meth,
outer: nil,
}
}

func (e *Environment) SetSignatureUsed(name string) (Signature, bool) {
obj, ok := e.signature[name]

Expand All @@ -122,11 +151,11 @@ func (e *Environment) SetSignatureUsed(name string) (Signature, bool) {
return obj, ok
}

func (e *Environment) SetTypeUsed(name string) (Type, bool) {
obj, ok := e.types[name]
func (e *Environment) SetTypeUsed(pkg, name string) (Type, bool) {
obj, ok := e.types[makeTypeKey(pkg, name)]

if !ok && e.outer != nil {
return e.outer.SetTypeUsed(name)
return e.outer.SetTypeUsed(pkg, name)
}

obj.Used = true
Expand Down Expand Up @@ -182,20 +211,29 @@ func (e *Environment) SetVariableNotMissing(name string) {
e.SetVariable(name, v)
}

func (e *Environment) GetType(name string) (Type, bool) {
obj, ok := e.types[name]
func makeTypeKey(pkg, name string) string {
if pkg == "" {
return name
}

return pkg + "::" + name
}

func (e *Environment) GetType(pkg, name string) (Type, bool) {
obj, ok := e.types[makeTypeKey(pkg, name)]
if !ok && e.outer != nil {
obj, ok = e.outer.GetType(name)
obj, ok = e.outer.GetType(pkg, name)
}

if ok {
e.SetTypeUsed(name)
e.SetTypeUsed(pkg, name)
}

return obj, ok
}

func (e *Environment) SetType(name string, val Type) Type {
e.types[name] = val
func (e *Environment) SetType(val Type) Type {
e.types[makeTypeKey(val.Package, val.Name)] = val
return val
}

Expand Down
1 change: 1 addition & 0 deletions environment/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Variable struct {
type Type struct {
Token token.Item
Type ast.Types
Package string
Used bool
Object string
Name string
Expand Down
80 changes: 79 additions & 1 deletion environment/types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package environment

import (
"errors"
"os"
"path"
"strings"

"github.com/devOpifex/vapour/ast"
"github.com/devOpifex/vapour/lexer"
"github.com/devOpifex/vapour/parser"
)

type Code struct {
Expand Down Expand Up @@ -51,7 +56,7 @@ func (e *Environment) GenerateTypes() *Code {

for i, a := range typeObject.Attributes {
sep := ""
if i > len(typeObject.Attributes)-1 {
if i < len(typeObject.Attributes)-1 {
sep = ","
}
code.add("\t" + a.Name + ": " + collaseTypes(a.Type) + sep)
Expand Down Expand Up @@ -96,3 +101,76 @@ func IsNativeObject(name string) bool {
}
return false
}

var packagesLoaded []string

func isLoaded(library string) bool {
for _, p := range packagesLoaded {
if p == library {
return true
}
}

return false
}

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

if isLoaded(pkg) {
return nil
}

packagesLoaded = append(packagesLoaded, library)

typeFile := path.Join(library, pkg, "types.vp")

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

content, err := os.ReadFile(typeFile)

if err != nil {
return err
}

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

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

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

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

// 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
}
11 changes: 10 additions & 1 deletion lexer/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,16 @@ func lexType(l *Lexer) stateFn {
return lexDefault
}

l.emit(token.ItemTypes)
if l.peek(1) == ':' && l.peek(2) == ':' {
l.emit(token.ItemTypesPkg)
l.next()
l.next()
l.emit(token.ItemNamespace)
return lexType
} else {
l.acceptRun(stringAlpha + "_.")
l.emit(token.ItemTypes)
}

if l.peek(1) == ' ' {
l.next()
Expand Down
50 changes: 50 additions & 0 deletions lexer/lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,53 @@ func TestFuncType(t *testing.T) {
}
}
}

func TestTypeImport(t *testing.T) {
code := `let x: tibble::tbl = as.tibble(cars)
let y: tibble::[]tbl | int`

l := NewTest(code)

l.Run()

if len(l.Items) == 0 {
t.Fatal("No Items where lexed")
}

tokens :=
[]token.ItemType{
token.ItemLet,
token.ItemIdent,
token.ItemColon,
token.ItemTypesPkg,
token.ItemNamespace,
token.ItemTypes,
token.ItemAssign,
token.ItemIdent,
token.ItemLeftParen,
token.ItemIdent,
token.ItemRightParen,
token.ItemNewLine,
token.ItemLet,
token.ItemIdent,
token.ItemColon,
token.ItemTypesPkg,
token.ItemNamespace,
token.ItemTypesList,
token.ItemTypes,
token.ItemOr,
token.ItemTypes,
}

for i, token := range tokens {
actual := l.Items[i].Class
if actual != token {
t.Fatalf(
"token %v expected `%v`, got `%v`",
i,
token,
actual,
)
}
}
}
Loading
Loading