about summary refs log tree commit diff
path: root/pkg/lang/compiler/scope/scope_chain.go
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-08-11 01:25:47 +0000
committerMel <einebeere@gmail.com>2022-08-11 01:25:47 +0000
commit86f31acf6789be116dcc54ed85b069a37c0f7aa8 (patch)
treebc7afd6a8c340825996d29c6cfd392ae42b4fbd5 /pkg/lang/compiler/scope/scope_chain.go
parentc46b2bc7ce6df1f2c6c9494ef08015ec29992da5 (diff)
downloadjinx-86f31acf6789be116dcc54ed85b069a37c0f7aa8.tar.zst
jinx-86f31acf6789be116dcc54ed85b069a37c0f7aa8.zip
Actual modules and core
Diffstat (limited to 'pkg/lang/compiler/scope/scope_chain.go')
-rw-r--r--pkg/lang/compiler/scope/scope_chain.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/pkg/lang/compiler/scope/scope_chain.go b/pkg/lang/compiler/scope/scope_chain.go
index f386017..0c8f5cf 100644
--- a/pkg/lang/compiler/scope/scope_chain.go
+++ b/pkg/lang/compiler/scope/scope_chain.go
@@ -160,6 +160,35 @@ func (sc *ScopeChain) DeclareTemporary() int {
 	return len(sc.Current().variableSymbols) // :)
 }
 
+func (sc *ScopeChain) DeclareGlobal(name, module, author string) bool {
+	if _, ok := sc.nameToSymbol[name]; ok {
+		return false
+	}
+
+	current := sc.Current()
+	indexInScope := len(current.globalSymbols)
+
+	symbolID := SymbolID{
+		symbolKind:   SymbolKindGlobal,
+		scopeID:      sc.CurrentScopeID(),
+		indexInScope: indexInScope,
+	}
+
+	globalID := fmt.Sprintf("%s:%s:%s", author, module, name)
+
+	// Declare the symbol in the current scope.
+	current.globalSymbols = append(current.globalSymbols, Symbol[SymbolGlobal]{
+		name: name,
+		data: SymbolGlobal{
+			id: globalID,
+		},
+	})
+
+	sc.nameToSymbol[name] = symbolID
+
+	return true
+}
+
 func (sc *ScopeChain) CreateAnonymousFunctionSubUnit() code.Marker {
 	fnScope := sc.CurrentFunction()
 
@@ -242,3 +271,11 @@ func (sc *ScopeChain) GetEnv(id SymbolID) Symbol[SymbolEnv] {
 		},
 	}
 }
+
+func (sc *ScopeChain) GetGlobal(id SymbolID) Symbol[SymbolGlobal] {
+	if id.symbolKind != SymbolKindGlobal {
+		panic("incorrect symbol id kind given")
+	}
+
+	return sc.symbolScopes[id.scopeID].globalSymbols[id.indexInScope]
+}