about summary refs log tree commit diff
path: root/pkg/lang/compiler/scope/scopes.go
blob: 5cfcd5de93ddabf560d7612073733ada62188436 (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
package scope

import "jinx/pkg/lang/vm/code"

type ScopeID int

type ScopeKind int

const (
	ScopeKindNormal ScopeKind = iota
	ScopeKindFunction
	ScopeKindLoop
)

type SymbolScope struct {
	variableSymbols []Symbol[SymbolVariable]
	functionSymbols []Symbol[SymbolFunction]
}

func NewSymbolScope() SymbolScope {
	return SymbolScope{
		variableSymbols: make([]Symbol[SymbolVariable], 0),
		functionSymbols: make([]Symbol[SymbolFunction], 0),
	}
}

type FunctionScope struct {
	id           ScopeID
	unit         code.Marker
	subUnitCount int
}

func NewFunctionScope(id ScopeID, unit code.Marker) FunctionScope {
	return FunctionScope{
		id:           id,
		unit:         unit,
		subUnitCount: 0,
	}
}

func (sf FunctionScope) ID() ScopeID {
	return sf.id
}

func (sf FunctionScope) Unit() code.Marker {
	return sf.unit
}

func (sf FunctionScope) IsRootScope() bool {
	return sf.ID() == ScopeID(0)
}

type LoopScope struct {
	id             ScopeID
	breakMarker    code.Marker
	continueMarker code.Marker
}

func NewLoopScope(id ScopeID, breakMarker code.Marker, continueMarker code.Marker) LoopScope {
	return LoopScope{
		id:             id,
		breakMarker:    breakMarker,
		continueMarker: continueMarker,
	}
}

func (sl LoopScope) ID() ScopeID {
	return sl.id
}

func (sl LoopScope) BreakMarker() code.Marker {
	return sl.breakMarker
}

func (sl LoopScope) ContinueMarker() code.Marker {
	return sl.continueMarker
}