From 5a6d4664e4417763b4a7d9f215e42102fa1b3fd4 Mon Sep 17 00:00:00 2001 From: Mel Date: Thu, 28 Jul 2022 22:11:02 +0000 Subject: Compile type declarations correctly --- pkg/lang/compiler/scope/scope_chain.go | 10 ++++++++++ pkg/lang/compiler/scope/scopes.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) (limited to 'pkg/lang/compiler/scope') diff --git a/pkg/lang/compiler/scope/scope_chain.go b/pkg/lang/compiler/scope/scope_chain.go index 1b83c75..f386017 100644 --- a/pkg/lang/compiler/scope/scope_chain.go +++ b/pkg/lang/compiler/scope/scope_chain.go @@ -72,6 +72,16 @@ func (sc *ScopeChain) EnterFunction(unit code.Marker) { sc.functionScopes = append(sc.functionScopes, NewFunctionScope(sc.CurrentScopeID(), unit)) } +func (sc *ScopeChain) EnterConstructor(unit code.Marker, thisLocal int) { + sc.Enter() + sc.functionScopes = append(sc.functionScopes, NewMethodFunctionScope(sc.CurrentScopeID(), unit, thisLocal)) +} + +func (sc *ScopeChain) EnterMethod(unit code.Marker) { + sc.Enter() + sc.functionScopes = append(sc.functionScopes, NewMethodFunctionScope(sc.CurrentScopeID(), unit, -1)) +} + func (sc *ScopeChain) EnterLoop() (code.Marker, code.Marker) { parentMarker := sc.CreateAnonymousFunctionSubUnit() diff --git a/pkg/lang/compiler/scope/scopes.go b/pkg/lang/compiler/scope/scopes.go index 7a1b20c..2a9453a 100644 --- a/pkg/lang/compiler/scope/scopes.go +++ b/pkg/lang/compiler/scope/scopes.go @@ -28,6 +28,9 @@ type FunctionScope struct { subUnitCount int outsideSymbolsInEnv []SymbolID + + isMethod bool + methodLocal int // -1 if in env 0 } func NewFunctionScope(id ScopeID, unit code.Marker) FunctionScope { @@ -40,6 +43,19 @@ func NewFunctionScope(id ScopeID, unit code.Marker) FunctionScope { } } +func NewMethodFunctionScope(id ScopeID, unit code.Marker, methodLocal int) FunctionScope { + return FunctionScope{ + id: id, + unit: unit, + subUnitCount: 0, + + outsideSymbolsInEnv: make([]SymbolID, 0), + + isMethod: true, + methodLocal: methodLocal, + } +} + func (sf FunctionScope) ID() ScopeID { return sf.id } @@ -56,6 +72,22 @@ func (sf FunctionScope) IsRootScope() bool { return sf.ID() == ScopeID(0) } +func (sf FunctionScope) IsMethod() bool { + return sf.isMethod +} + +func (sf FunctionScope) ThisLocal() (bool, int) { + if !sf.isMethod { + return false, 0 + } + + if sf.methodLocal == -1 { + return false, 0 + } + + return true, sf.methodLocal +} + type LoopScope struct { id ScopeID breakMarker code.Marker -- cgit 1.4.1