Skip to content

Commit

Permalink
compiler, std: minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Feb 15, 2025
1 parent 0c8b4a4 commit b0107a7
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 511 deletions.
20 changes: 6 additions & 14 deletions src/julec/opt/l0.jule
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,13 @@
use "std/jule/sema"

// Reports whether the expression e is optimizable array for the built-in copy function.
fn IsZCopyArray(e: sema::Expr): bool {
match type e {
| &sema::SlicingExpr:
mut ie := (&sema::SlicingExpr)(e)
ret ie.Expr.Type.Array() != nil
}
ret false
fn IsZCopyArray(mut e: sema::Expr): bool {
mut s, ok := (&sema::SlicingExpr)(e)
ret ok && s.Expr.Type.Array() != nil
}

// Reports whether the expression e is optimizable array for the built-in append function.
fn IsZAppendArray(e: sema::Expr): bool {
match type e {
| &sema::SlicingExpr:
mut ie := (&sema::SlicingExpr)(e)
ret ie.Expr.Type.Array() != nil
}
ret false
fn IsZAppendArray(mut e: sema::Expr): bool {
mut s, ok := (&sema::SlicingExpr)(e)
ret ok && s.Expr.Type.Array() != nil
}
40 changes: 10 additions & 30 deletions std/jule/constant/const.jule
Original file line number Diff line number Diff line change
Expand Up @@ -156,52 +156,32 @@ impl Const {

// Reports whether data is 64-bit signed integer.
fn IsI64(self): bool {
match type self.data {
| i64:
ret true
|:
ret false
}
_, ok := i64(self.data)
ret ok
}

// Reports whether data is 64-bit unsigned integer.
fn IsU64(self): bool {
match type self.data {
| u64:
ret true
|:
ret false
}
_, ok := u64(self.data)
ret ok
}

// Reports whether data is boolean.
fn IsBool(self): bool {
match type self.data {
| bool:
ret true
|:
ret false
}
_, ok := bool(self.data)
ret ok
}

// Reports whether data is string.
fn IsStr(self): bool {
match type self.data {
| str:
ret true
|:
ret false
}
_, ok := str(self.data)
ret ok
}

// Reports whether data is 64-bit floating-point.
fn IsF64(self): bool {
match type self.data {
| f64:
ret true
|:
ret false
}
_, ok := f64(self.data)
ret ok
}

// Reports whether data is nil.
Expand Down
69 changes: 33 additions & 36 deletions std/jule/parser/expr.jule
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl exprBuilder {
self.pushSuggestion(build::LogMsg.EmptyParentNotValid)
ret nil
}
tokens = tokens[1:len(tokens)-1] // Remove parentheses.
tokens = tokens[1 : len(tokens)-1] // Remove parentheses.
ret &ast::RangeExpr{
Expr: self.buildFromTokens(tokens),
}
Expand Down Expand Up @@ -259,7 +259,7 @@ impl exprBuilder {
self.pushErr(typeTokens[0], build::LogMsg.MissingType)
self.pushSuggestion(build::LogMsg.GiveTypeForCast)
} else {
typeTokens = typeTokens[1:len(typeTokens)-1] // Remove parentheses.
typeTokens = typeTokens[1 : len(typeTokens)-1] // Remove parentheses.
mut typeIndex := 0
mut t, ok := unsafe { self.p.buildType(typeTokens, &typeIndex, true) }
if ok && typeIndex < len(typeTokens) {
Expand Down Expand Up @@ -290,7 +290,7 @@ impl exprBuilder {
if len(tokens) < 2 {
ret nil
}
tokens = tokens[1:len(tokens)-1] // Remove parentheses.
tokens = tokens[1 : len(tokens)-1] // Remove parentheses.
mut parts, errs := parts(tokens, token::Id.Comma, true)
self.p.errors = append(self.p.errors, errs...)
mut args := make([]&ast::Expr, 0, len(parts))
Expand Down Expand Up @@ -387,7 +387,7 @@ impl exprBuilder {

mut last := 0
mut rangeN := 0
tokens = tokens[1:len(tokens)-1] // Remove parentheses.
tokens = tokens[1 : len(tokens)-1] // Remove parentheses.
for i, token in tokens {
match token.Id {
| token::Id.LBrace
Expand Down Expand Up @@ -509,22 +509,20 @@ impl exprBuilder {
if d == nil {
ret nil
}
match type d {
| &ast::FuncCallExpr:
tokens = tokens[len(exprTokens)+1:] // Get range: {...}
mut i := 0
mut rangeTokens := range(i, token::Id.LBrace, token::Id.RBrace, tokens)
mut model := (&ast::FuncCallExpr)(d)
if model.Ignored() {
self.pushErr(elseToken, build::LogMsg.InvalidSyntax)
self.pushSuggestion(build::LogMsg.JustIgnoreOrHandle)
}
model.Exception = self.p.buildScope(rangeTokens, tokens[i-1])
ret d
|:
mut model, ok := (&ast::FuncCallExpr)(d)
if !ok {
self.pushErr(exprTokens[0], build::LogMsg.InvalidSyntax)
ret nil
}
tokens = tokens[len(exprTokens)+1:] // Get range: {...}
mut i := 0
mut rangeTokens := range(i, token::Id.LBrace, token::Id.RBrace, tokens)
if model.Ignored() {
self.pushErr(elseToken, build::LogMsg.InvalidSyntax)
self.pushSuggestion(build::LogMsg.JustIgnoreOrHandle)
}
model.Exception = self.p.buildScope(rangeTokens, tokens[i-1])
ret d
}

match exprTokens[0].Id {
Expand All @@ -542,7 +540,7 @@ impl exprBuilder {

// Tokens is should be store enumerable range tokens.
fn getEnumerableParts(mut self, mut tokens: []&token::Token): [][]&token::Token {
tokens = tokens[1:len(tokens)-1] // Remove range tokens.
tokens = tokens[1 : len(tokens)-1] // Remove range tokens.
mut parts, errors := parts(tokens, token::Id.Comma, true)
self.p.errors = append(self.p.errors, errors...)
ret parts
Expand Down Expand Up @@ -573,7 +571,7 @@ impl exprBuilder {
fn buildIndexing(mut self, mut exprTokens: []&token::Token,
mut tokens: []&token::Token, mut errorToken: &token::Token): &ast::IndexingExpr {
mut end := tokens[len(tokens)-1]
tokens = tokens[1:len(tokens)-1] // Remove brackets.
tokens = tokens[1 : len(tokens)-1] // Remove brackets.
if len(tokens) == 0 {
self.pushErr(errorToken, build::LogMsg.MissingExpr)
ret nil
Expand Down Expand Up @@ -641,7 +639,7 @@ impl exprBuilder {
tokens = tokens[len(exprTokens):]

// Catch slicing expressions.
mut splitTokens := tokens[1:len(tokens)-1] // Remove brackets.
mut splitTokens := tokens[1 : len(tokens)-1] // Remove brackets.
mut start, mut to := splitDelim(splitTokens, token::Id.Colon)
if start != nil || to != nil {
ret self.buildSlicing(exprTokens, start, to, errorToken, tokens[len(tokens)-1])
Expand All @@ -661,24 +659,23 @@ impl exprBuilder {
if d == nil {
ret nil
}
match type d {
| &ast::FuncCallExpr:
mut f := (&ast::FuncCallExpr)(d)
// Catch already ignored exceptional calls.
// Like: foo()!!!!
// the ^^^ part is unnecessary and invalid
if f.Ignored() {
self.pushErr(token, build::LogMsg.InvalidSyntax)
ret nil
} else {
f.Exception = &ast::ScopeTree{
Deferred: true,
}
mut f, ok := (&ast::FuncCallExpr)(d)
if !ok {
self.pushErr(token, build::LogMsg.InvalidSyntax)
ret nil
}
// Catch already ignored exceptional calls.
// Like: foo()!!!!
// the ^^^ part is unnecessary and invalid
if f.Ignored() {
self.pushErr(token, build::LogMsg.InvalidSyntax)
ret nil
} else {
f.Exception = &ast::ScopeTree{
Deferred: true,
}
ret d
}
self.pushErr(token, build::LogMsg.InvalidSyntax)
ret nil
ret d
}

// First token should be "<-" of the tokens.
Expand Down
15 changes: 5 additions & 10 deletions std/jule/sema/builtin.jule
Original file line number Diff line number Diff line change
Expand Up @@ -1079,24 +1079,19 @@ fn builtinCallerStdComptimeTypeAlias(mut &e: &eval, mut &fc: &ast::FuncCallExpr,
e.pushErr(fc.Token, build::LogMsg.MissingExprFor, "type")
ret nil
}
match type fc.Args[0].Kind {
| &ast::IdentExpr:
break
|:
mut ie, mut ok := (&ast::IdentExpr)(fc.Args[0].Kind)
if !ok {
e.pushErr(fc.Args[0].Token, build::LogMsg.InvalidSyntax)
e.pushSuggestion(build::LogMsg.ExpectedIdentifier)
ret nil
}
mut ident := (&ast::IdentExpr)(fc.Args[0].Kind).Token
match type e.lookup {
| &scopeChecker:
break
|:
mut sc, ok := (&scopeChecker)(e.lookup)
if !ok {
e.pushErr(fc.Token, build::LogMsg.CalledOutOfScope, "TypeAlias")
ret nil
}

mut sc := (&scopeChecker)(e.lookup)
mut ident := ie.Token
mut alias := &TypeAlias{
Scope: sc.tree,
Public: mod::IsPub(ident.Kind),
Expand Down
16 changes: 6 additions & 10 deletions std/jule/sema/constraint.jule
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,14 @@ impl constraintChecker {
n := len(sema.errors)
mut kind := sema.buildTypeWithRefers(mask, sema, generics, nil)
if kind == nil {
match type mask.Kind {
| &ast::IdentType:
mut itd := (&ast::IdentType)(mask.Kind)
if len(itd.Generics) == 0 && isBuiltinConstraint(itd.Ident) {
kind = &Type{Kind: buildPrimType(itd.Ident)}
sema.errors = sema.errors[:n]
goto success
}
mut itd, ok := (&ast::IdentType)(mask.Kind)
if ok && len(itd.Generics) == 0 && isBuiltinConstraint(itd.Ident) {
kind = &Type{Kind: buildPrimType(itd.Ident)}
sema.errors = sema.errors[:n]
} else {
ret false
}
ret false
}
success:
generic.Constraint = append(generic.Constraint, kind)
}
}
Expand Down
14 changes: 4 additions & 10 deletions std/jule/sema/enum.jule
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ impl Kind for Enum {

// Reports whether types are same.
fn Equal(&self, other: &Type): bool {
match type other.Kind {
| &Enum:
ret self == (&Enum)(other.Kind)
}
ret false
e, ok := (&Enum)(other.Kind)
ret ok && self == e
}
}

Expand Down Expand Up @@ -81,11 +78,8 @@ impl Kind for TypeEnum {

// Reports whether types are same.
fn Equal(&self, other: &Type): bool {
match type other.Kind {
| &TypeEnum:
ret self == (&TypeEnum)(other.Kind)
}
ret false
e, ok := (&TypeEnum)(other.Kind)
ret ok && self == e
}
}

Expand Down
26 changes: 6 additions & 20 deletions std/jule/sema/eval.jule
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,8 @@ impl eval {

// Reports whether evaluated expression is in global scope.
fn isGlobal(self): bool {
match type self.lookup {
| &sema:
ret true
|:
ret false
}
_, ok := (&sema)(self.lookup)
ret ok
}

fn applyNumericPrefix(mut self, mut &v: &Value): bool {
Expand Down Expand Up @@ -754,18 +750,13 @@ impl eval {
mut filled := false

if len(s.Exprs) == 2 {
match type s.Exprs[1].Kind {
| &ast::VariadicExpr:
if (&ast::VariadicExpr)(s.Exprs[1].Kind).Expr != nil {
break
}
vr, ok := (&ast::VariadicExpr)(s.Exprs[1].Kind)
if ok && vr.Expr == nil {
// Filled.

if pt.Auto {
self.pushErr(s.Token, build::LogMsg.AutoSizedArrFilled)
ret nil
}

filled = true
s.Exprs = s.Exprs[:1]
}
Expand Down Expand Up @@ -3063,17 +3054,12 @@ impl eval {

mut prefix := self.prefix
for (_, mut expr) in lit.Exprs {
match type expr.Kind {
| &ast::KeyValPair:
// Ok.
break
|:
mut pair, ok := (&ast::KeyValPair)(expr.Kind)
if !ok {
self.pushErr(lit.Token, build::LogMsg.InvalidSyntax)
ret nil
}

mut pair := (&ast::KeyValPair)(expr.Kind)

self.prefix = model.Kind.Key
mut key := self.evalExpr(pair.Key)
self.prefix = prefix
Expand Down
Loading

0 comments on commit b0107a7

Please sign in to comment.