Skip to content

Commit

Permalink
compiler: improve handling of ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Jul 30, 2024
1 parent b0804eb commit da7515f
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 382 deletions.
18 changes: 8 additions & 10 deletions std/jule/importer/directive_eval.jule
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ impl directiveEval {
let mut last = 0
for i < len(tokens)-1; i++ {
let b = tokens[i]
if b.Id == TokenId.Range {
match b.Kind {
| TokenKind.LParent:
rangeN++
continue
| TokenKind.RParent:
rangeN--
}
match b.Id {
| TokenId.LParent:
rangeN++
continue
| TokenId.RParent:
rangeN--
}
if rangeN > 0 || b.Id != op {
continue
Expand Down Expand Up @@ -117,11 +115,11 @@ impl directiveEval {

for (_, mut andPart) in logicalAndParts {
let first = andPart[0]
if first.Id == TokenId.Range && first.Kind == TokenKind.LParent {
if first.Id == TokenId.LParent {
let end = andPart[len(andPart)-1]

// Missing close.
if end.Id != TokenId.Range || end.Kind != TokenKind.RParent {
if end.Id != TokenId.RParent {
self.pushErr(first, LogMsg.WaitCloseParent)
ret false
}
Expand Down
13 changes: 6 additions & 7 deletions std/jule/lex/lex.jule
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ static basicOps: [...]kindPair = [
{TokenKind.Gt, TokenId.Gt},
{TokenKind.Eq, TokenId.Eq},
{TokenKind.Hash, TokenId.Hash},
{TokenKind.LBrace, TokenId.LBrace},
{TokenKind.RBrace, TokenId.RBrace},
{TokenKind.LBracket, TokenId.LBracket},
{TokenKind.RBracket, TokenId.RBracket},
{TokenKind.LParent, TokenId.LParent},
{TokenKind.RParent, TokenId.RParent},
]

fn makeErr(row: int, col: int, &f: &File, fmt: LogMsg, args: ...any): Log {
Expand Down Expand Up @@ -718,7 +724,6 @@ impl lex {
ret true
}
}

ret false
}

Expand Down Expand Up @@ -777,12 +782,6 @@ impl lex {
| bytesHasPrefix(txt, TokenKind.RangLComment):
self.lexRangeComment(t)
ret t
| self.isOp(txt, TokenKind.LParent, TokenId.Range, t)
| self.isOp(txt, TokenKind.RParent, TokenId.Range, t)
| self.isOp(txt, TokenKind.LBrace, TokenId.Range, t)
| self.isOp(txt, TokenKind.RBrace, TokenId.Range, t)
| self.isOp(txt, TokenKind.LBracket, TokenId.Range, t)
| self.isOp(txt, TokenKind.RBracket, TokenId.Range, t)
| self.lexBasicOps(txt, t):
// Pass.
break
Expand Down
9 changes: 7 additions & 2 deletions std/jule/lex/token.jule
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ enum Ident: str {
enum TokenId: uint {
Na,
Ident,
Range,
Ret,
Semicolon,
Lit,
Expand Down Expand Up @@ -194,6 +193,12 @@ enum TokenId: uint {
Lt,
Gt,
Eq,
LBrace,
RBrace,
LParent,
RParent,
LBracket,
RBracket,
}

// Token kinds.
Expand Down Expand Up @@ -474,7 +479,7 @@ fn IsAssign(id: TokenId): bool {
id == TokenId.Let ||
id == TokenId.Mut ||
id == TokenId.Self ||
id == TokenId.Range ||
id == TokenId.LParent ||
id == TokenId.Star)
}

Expand Down
18 changes: 9 additions & 9 deletions std/jule/parser/assign.jule
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ fn checkAssignTokens(&tokens: []&Token): bool {
}
let mut braceN = 0
for _, t in tokens {
if t.Id == TokenId.Range {
match t.Kind {
| TokenKind.LBrace
| TokenKind.LBracket
| TokenKind.LParent:
braceN++
|:
braceN--
}
match t.Id {
| TokenId.LBrace
| TokenId.LBracket
| TokenId.LParent:
braceN++
| TokenId.RBrace
| TokenId.RBracket
| TokenId.RParent:
braceN--
}
match {
| braceN < 0:
Expand Down
Loading

0 comments on commit da7515f

Please sign in to comment.