From 845acf059aad3272d4b59cd89bc21497983c88b2 Mon Sep 17 00:00:00 2001 From: mertcandav Date: Tue, 21 Jan 2025 16:28:13 +0300 Subject: [PATCH] jule: improve typed map literals --- std/jule/ast/node.jule | 11 +++++----- std/jule/parser/expr.jule | 43 +++++---------------------------------- std/jule/sema/eval.jule | 18 +++++++++------- 3 files changed, 21 insertions(+), 51 deletions(-) diff --git a/std/jule/ast/node.jule b/std/jule/ast/node.jule index f88e44498..93c63aaf1 100644 --- a/std/jule/ast/node.jule +++ b/std/jule/ast/node.jule @@ -151,7 +151,7 @@ enum ExprData: type { &VariadicExpr, &CastExpr, &FuncCallExpr, - &StructLit, + &TypedBraceLit, &BraceLit, &SlicingExpr, &SliceExpr, @@ -292,11 +292,10 @@ impl FieldExprPair { } } -// Struct literal instantiating expression. -struct StructLit { - End: &token::Token - Kind: &Type - Exprs: []&Expr // Possible types: &FieldExprPair, and other expressions. +// Typed brace instantiating expression. +struct TypedBraceLit { + Kind: &Type + Lit: &BraceLit } // Anonymous brace instantiating expression. diff --git a/std/jule/parser/expr.jule b/std/jule/parser/expr.jule index 0684f4bbe..8e214a3b1 100644 --- a/std/jule/parser/expr.jule +++ b/std/jule/parser/expr.jule @@ -5,6 +5,7 @@ use "std/jule/ast" use "std/jule/build" use "std/jule/token" +use "std/strings" struct exprBuilder { p: &parser @@ -456,35 +457,7 @@ impl exprBuilder { ret pair } - fn buildStructLitExpr(mut self, mut &tokens: []&token::Token): &ast::Expr { - mut token := tokens[0] - if token.Id == token::Id.Ident && len(tokens) > 1 { - token = tokens[1] - if token.Id == token::Id.Colon { - ret &ast::Expr{ - Token: token, - End: tokens[len(tokens)-1], - Kind: self.buildFieldExprPair(tokens), - } - } - } - ret self.buildFromTokens(tokens) - } - - fn buildStructLitExprs(mut self, mut &tokens: []&token::Token): []&ast::Expr { - mut parts := self.getBraceRangeLitExprParts(tokens) - if len(parts) == 0 { - ret nil - } - - mut pairs := make([]&ast::Expr, 0, len(parts)) - for (_, mut part) in parts { - pairs = append(pairs, self.buildStructLitExpr(part)) - } - ret pairs - } - - fn buildTypedStructLiteral(mut self, mut tokens: []&token::Token): &ast::StructLit { + fn buildTypedStructLiteral(mut self, mut tokens: []&token::Token): &ast::TypedBraceLit { mut i := 0 mut t, ok := unsafe { self.p.buildType(tokens, &i, true) } if !ok { @@ -500,11 +473,9 @@ impl exprBuilder { self.pushErr(token, build::LogMsg.InvalidSyntax) ret nil } - - ret &ast::StructLit{ - End: tokens[len(tokens)-1], + ret &ast::TypedBraceLit{ Kind: t, - Exprs: self.buildStructLitExprs(tokens), + Lit: self.buildBraceLit(tokens), } } @@ -586,8 +557,7 @@ impl exprBuilder { ret self.buildUnsafe(tokens) | token::Id.Fn: ret self.buildAnonFunc(tokens) - | token::Id.Ident - | token::Id.Cpp: + | token::Id.Map | token::Id.Ident | token::Id.Cpp: ret self.buildTypedStructLiteral(tokens) |: self.pushErr(exprTokens[0], build::LogMsg.InvalidSyntax) @@ -827,9 +797,6 @@ impl exprBuilder { } fn build(mut self, mut &tokens: []&token::Token): ast::ExprData { - if tokens[0].Id == token::Id.Map { - ret self.buildType(tokens) - } i := findLowestPrecOp(tokens) if i == -1 { ret self.buildData(tokens) diff --git a/std/jule/sema/eval.jule b/std/jule/sema/eval.jule index 09f5dea2e..3d986155a 100644 --- a/std/jule/sema/eval.jule +++ b/std/jule/sema/eval.jule @@ -1958,7 +1958,7 @@ impl eval { } fn evalStructLitExplicit(mut &self, mut s: &StructIns, - mut exprs: []&ast::Expr, mut errorToken: &token::Token): &Value { + mut lit: &ast::BraceLit, mut errorToken: &token::Token): &Value { ok := self.s.checkGenericQuantity(len(s.Decl.Generics), len(s.Generics), errorToken) if !ok { ret nil @@ -1976,7 +1976,7 @@ impl eval { errorToken: errorToken, s: s, } - slc.check(exprs) + slc.check(lit.Exprs) mut v := &Value{ Mutable: self.target.mutable, @@ -1991,7 +1991,7 @@ impl eval { ret v } - fn evalStructLit(mut &self, mut lit: &ast::StructLit): &Value { + fn evalTypedBraceLit(mut &self, mut lit: &ast::TypedBraceLit): &Value { mut t := buildType(lit.Kind) ok := self.s.checkTypeSym(t, self.lookup) if !ok { @@ -2006,12 +2006,16 @@ impl eval { goto eval } } + mut m := t.Type.Map() + if m != nil { + ret self.evalMap(m, lit.Lit) + } self.pushErr(lit.Kind.Token, build::LogMsg.InvalidSyntax) ret nil } eval: - mut v := self.evalStructLitExplicit(s, lit.Exprs, lit.Kind.Token) + mut v := self.evalStructLitExplicit(s, lit.Lit, lit.Kind.Token) match type t.Type.Kind { | &Sptr: mut model := (&StructLitExpr)(v.Model) @@ -3047,7 +3051,7 @@ impl eval { | self.prefix.Map() != nil: v = self.evalMap(self.prefix.Map(), lit) | self.prefix.Struct() != nil: - v = self.evalStructLitExplicit(self.prefix.Struct(), lit.Exprs, lit.Token) + v = self.evalStructLitExplicit(self.prefix.Struct(), lit, lit.Token) |: self.pushErr(lit.Token, build::LogMsg.InvalidSyntax) ret nil @@ -3221,8 +3225,8 @@ impl eval { ret self.evalCast((&ast::CastExpr)(kind)) | &ast::NamespaceExpr: ret self.evalNamespace((&ast::NamespaceExpr)(kind)) - | &ast::StructLit: - ret self.evalStructLit((&ast::StructLit)(kind)) + | &ast::TypedBraceLit: + ret self.evalTypedBraceLit((&ast::TypedBraceLit)(kind)) | &ast::Type: ret self.evalType((&ast::Type)(kind)) | &ast::FuncCallExpr: