Skip to content

Commit

Permalink
parser: improve handling of single short declarative-assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Aug 12, 2024
1 parent f019266 commit 4b0a4b5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 46 deletions.
24 changes: 19 additions & 5 deletions std/jule/parser/scope.jule
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ impl scopeParser {
ret lefts
}

fn buildPlainAssign(mut self, mut &tokens: []&Token): (&AssignSt, bool) {
fn buildPlainAssign(mut self, mut &tokens: []&Token): (StmtData, bool) {
mut info := self.buildAssignInfo(tokens)
if !info.ok {
ret nil, false
Expand All @@ -929,7 +929,23 @@ impl scopeParser {
assign.Declarative = true
assign.Right = self.p.buildExpr(info.r)
ok := self.buildDeclAssign1(info.l, assign)
ret assign, ok
if !ok {
ret nil, false
}
if len(assign.Left) > 1 {
ret assign, true
}
// Single left, use &VarDecl intead.
mut left := assign.Left[0]
mut decl := &VarDecl{
Token: left.Token,
Ident: left.Ident,
Mutable: left.Mutable,
Reference: left.Reference,
Scope: self.s,
Expr: assign.Right,
}
ret decl, true
}

mut parts, errs := parts(info.l, TokenId.Comma, true)
Expand Down Expand Up @@ -1025,18 +1041,16 @@ impl scopeParser {
ret assign, ok
}

fn buildAssignSt(mut self, mut &tokens: []&Token): (st: &AssignSt, ok: bool) {
fn buildAssignSt(mut self, mut &tokens: []&Token): (st: StmtData, ok: bool) {
if !checkAssignTokens(tokens) {
ret nil, false
}

match tokens[0].Id {
| TokenId.Let:
st, ok = self.buildDeclAssign(tokens)
|:
st, ok = self.buildPlainAssign(tokens)
}

ret
}

Expand Down
42 changes: 1 addition & 41 deletions std/jule/sema/scope.jule
Original file line number Diff line number Diff line change
Expand Up @@ -1517,52 +1517,12 @@ impl scopeChecker {
self.scope.Stmts = append(self.scope.Stmts, st)
}

fn singleDeclAssign(mut &self, mut &a: &AssignSt) {
mut lexpr := a.Left[0]
if self.isDuplicatedIdent(0, lexpr.Ident) {
self.s.pushErr(lexpr.Token, LogMsg.DuplicatedIdent, lexpr.Ident)
self.s.pushSuggestion(LogMsg.RenameForAvoidDuplication)
self.stop()
ret
}
if IsIgnoreIdent(lexpr.Ident) {
self.s.pushErr(lexpr.Token, LogMsg.IgnoreIdent)
}

mut r := self.s.eval(self).evalExpr(a.Right)
if r == nil {
ret
}

self.removeInteriorMutRisk(r)

// Add new variable declaration statement.
mut v := &Var{
Ident: lexpr.Ident,
Token: lexpr.Token,
Mutable: lexpr.Mutable,
Reference: lexpr.Reference,
Scope: self.scope,
Value: &Value{
Expr: a.Right,
Data: r,
},
}
self.s.checkVarValue(v)
self.table.Vars = append(self.table.Vars, v)
self.scope.Stmts = append(self.scope.Stmts, v)
}

fn checkAssignSt(mut &self, mut a: &AssignSt) {
match {
| IsPostfixOp(a.Setter.Id):
self.checkPostfix(a)
| len(a.Left) == 1:
if a.Declarative {
self.singleDeclAssign(a)
} else {
self.checkSingleAssign(a)
}
self.checkSingleAssign(a)
|:
self.checkMultiAssign(a)
}
Expand Down

0 comments on commit 4b0a4b5

Please sign in to comment.