Skip to content

Commit

Permalink
sema: add more metadata for scope handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Sep 12, 2024
1 parent 0473d39 commit d3896fc
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion std/jule/sema/scope.jule
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@ struct Scope {
Unsafety: bool
Deferred: bool
Stmts: []Stmt

// Data of the child scope starting from the root scope.
// For the root scope, counting starts from 0. So the root scope will be 0.
// It increases by one for each child scope.
ChildIndex: int

// If the scope is a child scope,
// it contains the data of the statement in which it was appear.
StmtIndex: int
}

// Chain conditional node.
Expand Down Expand Up @@ -348,12 +357,16 @@ struct BreakSt {
// Label.
struct Label {
Ident: str
Scope: &Scope // Owner scope.
Index: int // Index of statement.
}

// Goto statement.
struct GotoSt {
Ident: str
Label: &Label
Scope: &Scope // Owner scope.
Index: int // Index of statement.
}

// Postfix assignment.
Expand Down Expand Up @@ -448,7 +461,7 @@ struct scopeChecker {
captured: &[]&Var // Anonymous function's captured variables will be stored here.
labels: &[]&scopeLabel // All labels of all scopes.
gotos: &[]&scopeGoto // All gotos of all scopes.
i: int
i: int // Index of current statement.
}

impl Lookup for scopeChecker {
Expand Down Expand Up @@ -792,6 +805,8 @@ impl scopeChecker {
}

fn checkChildSsc(mut &self, mut &tree: &ScopeTree, mut &s: &Scope, mut &ssc: &scopeChecker) {
s.StmtIndex = self.i
s.ChildIndex = ssc.childIndex
ssc.parent = self
ssc.check(tree, s)
}
Expand Down Expand Up @@ -1229,6 +1244,8 @@ impl scopeChecker {

mut label := &Label{
Ident: l.Ident,
Scope: self.scope,
Index: self.i,
}

self.scope.Stmts = append(self.scope.Stmts, label)
Expand All @@ -1243,6 +1260,8 @@ impl scopeChecker {
fn pushGoto(mut &self, mut gt: &ast::GotoSt) {
mut st := &GotoSt{
Ident: gt.Label.Kind,
Scope: self.scope,
Index: self.i,
}
self.scope.Stmts = append(self.scope.Stmts, st)

Expand Down

0 comments on commit d3896fc

Please sign in to comment.