From b0107a7ade4756fa30a1baf60c96eb352defa4c9 Mon Sep 17 00:00:00 2001 From: mertcandav Date: Sat, 15 Feb 2025 21:34:51 +0300 Subject: [PATCH] compiler, std: minor refactoring --- src/julec/opt/l0.jule | 20 +- std/jule/constant/const.jule | 40 +--- std/jule/parser/expr.jule | 69 +++--- std/jule/sema/builtin.jule | 15 +- std/jule/sema/constraint.jule | 16 +- std/jule/sema/enum.jule | 14 +- std/jule/sema/eval.jule | 26 +-- std/jule/sema/scope.jule | 60 +++--- std/jule/sema/sema.jule | 19 +- std/jule/sema/trait.jule | 7 +- std/jule/sema/type.jule | 394 ++++++++++------------------------ std/jule/sema/type2.jule | 80 ++++--- 12 files changed, 249 insertions(+), 511 deletions(-) diff --git a/src/julec/opt/l0.jule b/src/julec/opt/l0.jule index 1e1be735f..60e5bfe01 100644 --- a/src/julec/opt/l0.jule +++ b/src/julec/opt/l0.jule @@ -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 } \ No newline at end of file diff --git a/std/jule/constant/const.jule b/std/jule/constant/const.jule index 4cd7f05d9..8a272f2bb 100644 --- a/std/jule/constant/const.jule +++ b/std/jule/constant/const.jule @@ -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. diff --git a/std/jule/parser/expr.jule b/std/jule/parser/expr.jule index 6cd8250e0..acc8169dc 100644 --- a/std/jule/parser/expr.jule +++ b/std/jule/parser/expr.jule @@ -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), } @@ -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) { @@ -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)) @@ -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 @@ -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 { @@ -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 @@ -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 @@ -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]) @@ -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. diff --git a/std/jule/sema/builtin.jule b/std/jule/sema/builtin.jule index a563d891f..86e0e0ec4 100644 --- a/std/jule/sema/builtin.jule +++ b/std/jule/sema/builtin.jule @@ -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), diff --git a/std/jule/sema/constraint.jule b/std/jule/sema/constraint.jule index cceef7b3d..b2985ceb4 100644 --- a/std/jule/sema/constraint.jule +++ b/std/jule/sema/constraint.jule @@ -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) } } diff --git a/std/jule/sema/enum.jule b/std/jule/sema/enum.jule index f85e248ea..3f908d35f 100644 --- a/std/jule/sema/enum.jule +++ b/std/jule/sema/enum.jule @@ -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 } } @@ -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 } } diff --git a/std/jule/sema/eval.jule b/std/jule/sema/eval.jule index cc3422fe6..f1c02f178 100644 --- a/std/jule/sema/eval.jule +++ b/std/jule/sema/eval.jule @@ -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 { @@ -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] } @@ -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 diff --git a/std/jule/sema/scope.jule b/std/jule/sema/scope.jule index 21613d175..4ca6d00c5 100644 --- a/std/jule/sema/scope.jule +++ b/std/jule/sema/scope.jule @@ -492,12 +492,9 @@ impl scopeChecker { // Just lookups current scope. fn findLabel(mut self, &ident: str): &Label { for (_, mut st) in self.scope.Stmts { - match type st { - | &Label: - mut label := (&Label)(st) - if label.Ident == ident { - ret label - } + mut label, ok := (&Label)(st) + if ok && label.Ident == ident { + ret label } } ret nil @@ -1634,31 +1631,28 @@ impl scopeChecker { ret } stmt := s.Stmts[0] - match type stmt { - | &Value: - v := (&Value)(stmt) - match type v.Model { - | &BuiltinPanicCallExpr: - mut m := (&BuiltinPanicCallExpr)(v.Model) - match type m.Expr { - | &constant::Const: - c := (&constant::Const)(m.Expr) - if !c.IsStr() { - break - } - if callToken == nil { - mut root := self.getHardRoot() - if root.calledFrom == nil { - callToken = m.Token - } else { - callToken = root.calledFrom - } - } - self.s.pushErr(callToken, build::LogMsg.ComptimePanic, c.ReadStr()) - self.stop() - } + v, mut ok := (&Value)(stmt) + if !ok { + ret + } + mut m, (ok) := (&BuiltinPanicCallExpr)(v.Model) + if !ok { + ret + } + c, (ok) := (&constant::Const)(m.Expr) + if !ok || !c.IsStr() { + ret + } + if callToken == nil { + mut root := self.getHardRoot() + if root.calledFrom == nil { + callToken = m.Token + } else { + callToken = root.calledFrom } } + self.s.pushErr(callToken, build::LogMsg.ComptimePanic, c.ReadStr()) + self.stop() } fn processConstMatch(mut &self, mut &tm: &Match, mut &m: &ast::Match) { @@ -2472,12 +2466,8 @@ fn isValidStForNextSt(&st: Stmt): bool { | &MultiAssign: ret true | &Value: - match type (&Value)(st).Model { - | &FuncCallExpr: - ret true - |: - ret false - } + _, ok := (&FuncCallExpr)((&Value)(st).Model) + ret ok |: ret false } diff --git a/std/jule/sema/sema.jule b/std/jule/sema/sema.jule index f52880104..00b670f8f 100644 --- a/std/jule/sema/sema.jule +++ b/std/jule/sema/sema.jule @@ -2372,14 +2372,8 @@ impl sema { if s.Comparable && self.meta.runtime != nil { mut decl := runtimeFindFunc(self.meta.runtime, "arrayCmp") pushArr := fn(mut t: Kind, mut token: &token::Token) { - match type t { - | &Array: - break - |: - ret - } - mut arr := (&Array)(t) - if arr == nil { + mut arr, (ok) := (&Array)(t) + if !ok || arr == nil { ret } mut f := decl.instanceForce() @@ -2566,12 +2560,9 @@ impl sema { ret } - match type sptr.Elem.Kind { - | &StructIns: - s := (&StructIns)(sptr.Elem.Kind) - if s.Source == nil && isStdPackage(s.Decl.Token.File.Path, "testing") { - ret - } + s, ok := (&StructIns)(sptr.Elem.Kind) + if ok && s.Source == nil && isStdPackage(s.Decl.Token.File.Path, "testing") { + ret } self.pushErr(f.Decl.Token, build::LogMsg.WrongTestFuncDecl) self.pushSuggestion(build::LogMsg.UseExpectedTestFuncDecl) diff --git a/std/jule/sema/trait.jule b/std/jule/sema/trait.jule index 545f2bc60..4354d2dd0 100644 --- a/std/jule/sema/trait.jule +++ b/std/jule/sema/trait.jule @@ -23,11 +23,8 @@ impl Kind for Trait { // Reports whether types are same. fn Equal(&self, other: &Type): bool { - match type other.Kind { - | &Trait: - ret self == (&Trait)(other.Kind) - } - ret false + t, ok := (&Trait)(other.Kind) + ret ok && self == t } } diff --git a/std/jule/sema/type.jule b/std/jule/sema/type.jule index a19668c2c..577f49eba 100644 --- a/std/jule/sema/type.jule +++ b/std/jule/sema/type.jule @@ -74,9 +74,8 @@ impl Kind for Type { impl Type { // Reports whether type is binded kind. fn Binded(self): bool { - match type self.Kind { - | &StructIns: - s := (&StructIns)(self.Kind) + s, ok := (&StructIns)(self.Kind) + if ok { if s.Decl.Binded { ret true } @@ -101,12 +100,9 @@ impl Type { // see developer reference (9) for details, returns the actual kind of // the source type of the struct. fn ActualKind(mut self): Kind { - match type self.Kind { - | &StructIns: - mut s := (&StructIns)(self.Kind) - if s.Source != nil { - ret s.Source.ActualKind() - } + mut s, ok := (&StructIns)(self.Kind) + if ok && s.Source != nil { + ret s.Source.ActualKind() } ret self.Kind } @@ -244,154 +240,85 @@ impl Type { // Returns primitive type if actual kind is primitive type, nil if not. fn Prim(mut self): &Prim { - mut kind := self.ActualKind() - match type kind { - | &Prim: - ret (&Prim)(kind) - |: - ret nil - } + mut p, _ := (&Prim)(self.ActualKind()) + ret p } // Returns channel type if actual kind is channel, nil if not. fn Chan(mut self): &Chan { - mut kind := self.ActualKind() - match type kind { - | &Chan: - ret (&Chan)(kind) - |: - ret nil - } + mut c, _ := (&Chan)(self.ActualKind()) + ret c } // Returns reference type if actual kind is smart pointer, nil if not. fn Sptr(mut self): &Sptr { - mut kind := self.ActualKind() - match type kind { - | &Sptr: - ret (&Sptr)(kind) - |: - ret nil - } + mut p, _ := (&Sptr)(self.ActualKind()) + ret p } // Returns pointer type if actual kind is pointer, nil if not. fn Ptr(mut self): &Ptr { - mut kind := self.ActualKind() - match type kind { - | &Ptr: - ret (&Ptr)(kind) - |: - ret nil - } + mut p, _ := (&Ptr)(self.ActualKind()) + ret p } // Returns enum type if actual kind is enum, nil if not. fn Enum(mut self): &Enum { - mut kind := self.ActualKind() - match type kind { - | &Enum: - ret (&Enum)(kind) - |: - ret nil - } + mut e, _ := (&Enum)(self.ActualKind()) + ret e } // Returns type enum if actual kind is type enum, nil if not. fn TypeEnum(mut self): &TypeEnum { - mut kind := self.ActualKind() - match type kind { - | &TypeEnum: - ret (&TypeEnum)(kind) - |: - ret nil - } + mut e, _ := (&TypeEnum)(self.ActualKind()) + ret e } // Returns array type if actual kind is array, nil if not. fn Array(mut self): &Array { - mut kind := self.ActualKind() - match type kind { - | &Array: - ret (&Array)(kind) - |: - ret nil - } + mut a, _ := (&Array)(self.ActualKind()) + ret a } // Returns slice type if actual kind is slice, nil if not. fn Slice(mut self): &Slice { - mut kind := self.ActualKind() - match type kind { - | &Slice: - ret (&Slice)(kind) - |: - ret nil - } + mut s, _ := (&Slice)(self.ActualKind()) + ret s } // Returns function type if actual kind is function, nil if not. fn Func(mut self): &FuncIns { - mut kind := self.ActualKind() - match type kind { - | &FuncIns: - ret (&FuncIns)(kind) - |: - ret nil - } + mut f, _ := (&FuncIns)(self.ActualKind()) + ret f } // Returns struct type if actual kind is structure, nil if not. fn Struct(mut self): &StructIns { - mut kind := self.ActualKind() - match type kind { - | &StructIns: - ret (&StructIns)(kind) - |: - ret nil - } + mut s, _ := (&StructIns)(self.ActualKind()) + ret s } fn softStruct(mut self): &StructIns { - match type self.Kind { - | &StructIns: - ret (&StructIns)(self.Kind) - |: - ret nil - } + mut s, _ := (&StructIns)(self.Kind) + ret s } // Returns trait type if actual kind is trait, nil if not. fn Trait(mut self): &Trait { - mut kind := self.ActualKind() - match type kind { - | &Trait: - ret (&Trait)(kind) - |: - ret nil - } + mut t, _ := (&Trait)(self.ActualKind()) + ret t } // Returns map type if actual kind is map, nil if not. fn Map(mut self): &Map { - mut kind := self.ActualKind() - match type kind { - | &Map: - ret (&Map)(kind) - |: - ret nil - } + mut m, _ := (&Map)(self.ActualKind()) + ret m } // Returns tuple type if actual kind is tuple, nil if not. fn Tuple(mut self): &Tuple { - mut kind := self.ActualKind() - match type kind { - | &Tuple: - ret (&Tuple)(kind) - |: - ret nil - } + mut t, _ := (&Tuple)(self.ActualKind()) + ret t } // Reports whether kind is comptime type. @@ -416,147 +343,83 @@ impl Type { } fn comptimeStructFields(mut self): &comptimeStructFields { - match type self.Kind { - | &comptimeStructFields: - ret (&comptimeStructFields)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeStructFields)(self.Kind) + ret ct } fn comptimeStructField(mut self): &comptimeStructField { - match type self.Kind { - | &comptimeStructField: - ret (&comptimeStructField)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeStructField)(self.Kind) + ret ct } fn comptimeEnumFields(mut self): &comptimeEnumFields { - match type self.Kind { - | &comptimeEnumFields: - ret (&comptimeEnumFields)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeEnumFields)(self.Kind) + ret ct } fn comptimeEnumField(mut self): &comptimeEnumField { - match type self.Kind { - | &comptimeEnumField: - ret (&comptimeEnumField)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeEnumField)(self.Kind) + ret ct } fn comptimeParams(mut self): &comptimeParams { - match type self.Kind { - | &comptimeParams: - ret (&comptimeParams)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeParams)(self.Kind) + ret ct } fn comptimeParam(mut self): &comptimeParam { - match type self.Kind { - | &comptimeParam: - ret (&comptimeParam)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeParam)(self.Kind) + ret ct } fn comptimeRange(mut self): &comptimeRange { - match type self.Kind { - | &comptimeRange: - ret (&comptimeRange)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeRange)(self.Kind) + ret ct } fn comptimeTypeInfos(mut self): &comptimeTypeInfos { - match type self.Kind { - | &comptimeTypeInfos: - ret (&comptimeTypeInfos)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeTypeInfos)(self.Kind) + ret ct } fn comptimeTypeInfo(mut self): &comptimeTypeInfo { - match type self.Kind { - | &comptimeTypeInfo: - ret (&comptimeTypeInfo)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeTypeInfo)(self.Kind) + ret ct } fn comptimeStatics(mut self): &comptimeStatics { - match type self.Kind { - | &comptimeStatics: - ret (&comptimeStatics)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeStatics)(self.Kind) + ret ct } fn comptimeStatic(mut self): &comptimeStatic { - match type self.Kind { - | &comptimeStatic: - ret (&comptimeStatic)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeStatic)(self.Kind) + ret ct } fn comptimeValue(mut self): &comptimeValue { - match type self.Kind { - | &comptimeValue: - ret (&comptimeValue)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeValue)(self.Kind) + ret ct } fn comptimeDecl(mut self): &comptimeDecl { - match type self.Kind { - | &comptimeDecl: - ret (&comptimeDecl)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeDecl)(self.Kind) + ret ct } fn comptimeDecls(mut self): &comptimeDecls { - match type self.Kind { - | &comptimeDecls: - ret (&comptimeDecls)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeDecls)(self.Kind) + ret ct } fn comptimeFile(mut self): &comptimeFile { - match type self.Kind { - | &comptimeFile: - ret (&comptimeFile)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeFile)(self.Kind) + ret ct } fn comptimeFiles(mut self): &comptimeFiles { - match type self.Kind { - | &comptimeFiles: - ret (&comptimeFiles)(self.Kind) - |: - ret nil - } + mut ct, _ := (&comptimeFiles)(self.Kind) + ret ct } } @@ -588,11 +451,8 @@ impl Kind for Prim { // Reports whether types are same. fn Equal(&self, other: &Type): bool { - match type other.Kind { - | &Prim: - ret self.Kind == (&Prim)(other.Kind).Kind - } - ret false + p, ok := (&Prim)(other.Kind) + ret ok && self == p } } @@ -665,12 +525,9 @@ impl Kind for Chan { // Returns smart pointer kind as string. fn Str(self): str { if self.Recv && self.Send { - match type self.Elem.Kind { - | &Chan: - c := (&Chan)(self.Elem.Kind) - if c.Recv && !c.Send { - ret "chan (" + self.Elem.Str() + ")" - } + c, ok := (&Chan)(self.Elem.Kind) + if ok && c.Recv && !c.Send { + ret "chan (" + self.Elem.Str() + ")" } ret "chan " + self.Elem.Str() } else if self.Recv { @@ -682,12 +539,8 @@ impl Kind for Chan { // Reports whether types are same. fn Equal(&self, other: &Type): bool { - match type other.Kind { - | &Chan: - c := (&Chan)(other.Kind) - ret self.Recv == c.Recv && self.Send == c.Send && self.Elem.Equal(c.Elem) - } - ret false + c, ok := (&Chan)(other.Kind) + ret ok && self.Recv == c.Recv && self.Send == c.Send && self.Elem.Equal(c.Elem) } } @@ -702,11 +555,8 @@ impl Kind for Sptr { // Reports whether types are same. fn Equal(&self, other: &Type): bool { - match type other.Kind { - | &Sptr: - ret self.Elem.Equal((&Sptr)(other.Kind).Elem) - } - ret false + p, ok := (&Sptr)(other.Kind) + ret ok && self.Elem.Equal(p.Elem) } } @@ -721,11 +571,8 @@ impl Kind for Slice { // Reports whether types are same. fn Equal(&self, other: &Type): bool { - match type other.Kind { - | &Slice: - ret self.Elem.Equal((&Slice)(other.Kind).Elem) - } - ret false + s, ok := (&Slice)(other.Kind) + ret ok && self.Elem.Equal(s.Elem) } } @@ -751,21 +598,17 @@ impl Kind for Tuple { // Reports whether types are same. fn Equal(&self, other: &Type): bool { - match type other.Kind { - | &Tuple: - tup := (&Tuple)(other.Kind) - if len(self.Types) != len(tup.Types) { + tup, ok := (&Tuple)(other.Kind) + if !ok || len(self.Types) != len(tup.Types) { + ret false + } + mut i := 0 + for i < len(self.Types); i++ { + if !self.Types[i].Equal(tup.Types[i]) { ret false } - mut i := 0 - for i < len(self.Types); i++ { - if !self.Types[i].Equal(tup.Types[i]) { - ret false - } - } - ret true } - ret false + ret true } } @@ -789,12 +632,8 @@ impl Kind for Map { // Reports whether types are same. fn Equal(&self, other: &Type): bool { - match type other.Kind { - | &Map: - m := (&Map)(other.Kind) - ret self.Key.Equal(m.Key) && self.Val.Equal(m.Val) - } - ret false + m, ok := (&Map)(other.Kind) + ret ok && self.Key.Equal(m.Key) && self.Val.Equal(m.Val) } } @@ -819,12 +658,8 @@ impl Kind for Array { // Reports whether types are same. fn Equal(&self, other: &Type): bool { - match type other.Kind { - | &Array: - arr := (&Array)(other.Kind) - ret self.N == arr.N && self.Elem.Equal(arr.Elem) - } - ret false + a, ok := (&Array)(other.Kind) + ret ok && self.N == a.N && self.Elem.Equal(a.Elem) } } @@ -844,19 +679,18 @@ impl Kind for Ptr { // Reports whether types are same. fn Equal(&self, other: &Type): bool { - match type other.Kind { - | &Ptr: - ptr := (&Ptr)(other.Kind) - match { - | self.IsUnsafe(): - ret ptr.IsUnsafe() - | ptr.IsUnsafe(): - ret self.IsUnsafe() - |: - ret self.Elem.Equal(ptr.Elem) - } + p, ok := (&Ptr)(other.Kind) + if !ok { + ret false + } + match { + | self.IsUnsafe(): + ret p.IsUnsafe() + | p.IsUnsafe(): + ret self.IsUnsafe() + |: + ret self.Elem.Equal(p.Elem) } - ret false } } @@ -1120,18 +954,12 @@ impl typeChecker { // Append to structure depend collection if oself is not exist. // If oself exist, it means type is not plain. if !oselfExist { - match type self.referencer.owner { - | &StructIns: - mut s := (&StructIns)(self.referencer.owner) - if s.Source == nil { // Play safe, ignore strict type alias structures. - match type decl { - | &StructIns: - mut ds := (&StructIns)(decl) - // Play safe, ignore strict type alias structures. - if ds.Source == nil && !s.Decl.depended(ds.Decl) { - s.Decl.Depends = append(s.Decl.Depends, ds.Decl) - } - } + mut s, (ok) := (&StructIns)(self.referencer.owner) + if ok && s.Source == nil { // Play safe, ignore strict type alias structures. + mut ds, (ok) := (&StructIns)(decl) + // Play safe, ignore strict type alias structures. + if ok && ds.Source == nil && !s.Decl.depended(ds.Decl) { + s.Decl.Depends = append(s.Decl.Depends, ds.Decl) } } } @@ -1186,9 +1014,8 @@ impl typeChecker { // Push generic reference to owner type alias. // See developer reference (3) and (4) for more information. if ta.Generic && self.referencer != nil { - match type self.referencer.owner { - | &TypeAlias: - mut rta := (&TypeAlias)(self.referencer.owner) + mut rta, ok := (&TypeAlias)(self.referencer.owner) + if ok { rta.Generics = append(rta.Generics, ta) } } @@ -1540,14 +1367,11 @@ impl typeChecker { if ident == nil { ret nil } - match type ident { - | &TypeEnum: - break - |: + mut t, ok := (&TypeEnum)(ident) + if !ok { self.pushErr(self.errorToken, build::LogMsg.InvalidSyntax) ret nil } - mut t := (&TypeEnum)(ident) mut idents := decl.Idents[1:] for i, id in idents { mut item := t.FindItem(id.Ident) diff --git a/std/jule/sema/type2.jule b/std/jule/sema/type2.jule index a6f0e8790..b25edc133 100644 --- a/std/jule/sema/type2.jule +++ b/std/jule/sema/type2.jule @@ -398,51 +398,49 @@ struct dynamicTypeAnnotation { impl dynamicTypeAnnotation { fn annotateConstraintElem[T](mut self, mut &k: &Type): bool { - match type self.c.Kind { - | &T: - mut elem := (&Type)(nil) - const match type T { - | ast::ArrayType: - elem = k.Array().Elem - | ast::SliceType: - elem = k.Slice().Elem - | ast::PtrType: - mut p := k.Ptr() - if p.IsUnsafe() { - self.e.pushErr(self.errorToken, build::LogMsg.UnsafePointerForAnnotation) - ret false - } - elem = p.Elem - | ast::SptrType: - elem = k.Sptr().Elem - | ast::ChanType: - elem = k.Chan().Elem + mut c, mut ok := (&T)(self.c.Kind) + if !ok { + ret false + } + mut elem := (&Type)(nil) + const match type T { + | ast::ArrayType: + elem = k.Array().Elem + | ast::SliceType: + elem = k.Slice().Elem + | ast::PtrType: + mut p := k.Ptr() + if p.IsUnsafe() { + self.e.pushErr(self.errorToken, build::LogMsg.UnsafePointerForAnnotation) + ret false } - self.c = (&T)(self.c.Kind).Elem - ret self.annotateConstraint(elem) + elem = p.Elem + | ast::SptrType: + elem = k.Sptr().Elem + | ast::ChanType: + elem = k.Chan().Elem } - ret false + self.c = c.Elem + ret self.annotateConstraint(elem) } fn annotateConstraintMap(mut self, mut &k: &Type): (ok: bool) { - match type self.c.Kind { - | &ast::MapType: - mut m := k.Map() - mut m2 := (&ast::MapType)(self.c.Kind) - self.c = m2.Key - if !self.annotateConstraint(m.Key) { - ret false - } - self.c = m2.Val - ret self.annotateConstraint(m.Val) + mut m2, ok := (&ast::MapType)(self.c.Kind) + if !ok { + ret } - ret false + mut m := k.Map() + self.c = m2.Key + if !self.annotateConstraint(m.Key) { + ret false + } + self.c = m2.Val + ret self.annotateConstraint(m.Val) } fn annotateConstraintTuple(mut self, mut &k: &Type): (ok: bool) { - match type self.c.Kind { - | &ast::TupleType: - mut tup := (&ast::TupleType)(self.c.Kind) + mut tup, ok := (&ast::TupleType)(self.c.Kind) + if ok { mut tup2 := k.Tuple() for (_, mut t2) in tup2.Types { for (_, mut t) in tup.Types { @@ -457,9 +455,8 @@ impl dynamicTypeAnnotation { } fn annotateConstraintFunc(mut self, mut &k: &Type): (ok: bool) { - match type self.c.Kind { - | &ast::Func: - mut pf := (&ast::Func)(self.c.Kind) + mut pf, ok := (&ast::Func)(self.c.Kind) + if ok { mut f := k.Func() for (i, mut fp) in f.Params { self.c = pf.Params[i].Kind @@ -1067,9 +1064,8 @@ impl structLitChecker { // Check duplications. dupLookup: for (_, mut expr) in exprs { - match type expr.Kind { - | &ast::FieldExprPair: - mut dpair := (&ast::FieldExprPair)(expr.Kind) + mut dpair, ok := (&ast::FieldExprPair)(expr.Kind) + if ok { match { | pair == dpair: break dupLookup