about summary refs log tree commit diff
path: root/pkg/lang/modules/module.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/lang/modules/module.go')
-rw-r--r--pkg/lang/modules/module.go104
1 files changed, 104 insertions, 0 deletions
diff --git a/pkg/lang/modules/module.go b/pkg/lang/modules/module.go
new file mode 100644
index 0000000..08b9987
--- /dev/null
+++ b/pkg/lang/modules/module.go
@@ -0,0 +1,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",
+	}
+)