about summary refs log tree commit diff
path: root/pkg/lang/modules/module.go
blob: 08b99875398d3894991fc416969ef0a941821d85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package modules

import (
	"fmt"
	"jinx/pkg/lang/vm/code"
)

type Module struct {
	author  string
	name    string
	code    *code.Code
	deps    []ModuleRef
	globals []string
}

func NewModule(author, name string, code *code.Code, deps []ModuleRef, globals []string) Module {
	return Module{
		author:  author,
		name:    name,
		code:    code,
		deps:    deps,
		globals: globals,
	}
}

func NewUnknownModule(code *code.Code) Module {
	return Module{
		author: "noone",
		name:   "program",
		code:   code,
		deps: []ModuleRef{
			CoreModuleRef,
		},
		globals: []string{},
	}
}

func (m Module) Author() string {
	return m.author
}

func (m Module) Name() string {
	return m.name
}

func (m Module) Code() *code.Code {
	return m.code
}

func (m Module) Deps() []ModuleRef {
	return m.deps
}

func (m Module) Globals() []string {
	return m.globals
}

func (m Module) IsCore() bool {
	return m.author == CoreModuleRef.author && m.name == CoreModuleRef.name
}

func (m Module) GetGlobal(name string) (string, error) {
	// TODO: Don't use loop
	found := false
	for _, global := range m.globals {
		if global == name {
			found = true
			break
		}
	}

	if !found {
		return "", fmt.Errorf("global %s not in module", name)
	}

	return fmt.Sprintf("%s:%s:%s", m.author, m.name, name), nil
}

type ModuleRef struct {
	author string
	name   string
}

func NewRef(author, name string) ModuleRef {
	return ModuleRef{
		author: author,
		name:   name,
	}
}

func (m ModuleRef) Author() string {
	return m.author
}

func (m ModuleRef) Name() string {
	return m.name
}

var (
	CoreModuleRef = ModuleRef{
		author: "",
		name:   "core",
	}
)