Skip to content

Commit

Permalink
jule: improve typed map literals
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Jan 21, 2025
1 parent 3dcf34c commit 845acf0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 51 deletions.
11 changes: 5 additions & 6 deletions std/jule/ast/node.jule
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ enum ExprData: type {
&VariadicExpr,
&CastExpr,
&FuncCallExpr,
&StructLit,
&TypedBraceLit,
&BraceLit,
&SlicingExpr,
&SliceExpr,
Expand Down Expand Up @@ -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.
Expand Down
43 changes: 5 additions & 38 deletions std/jule/parser/expr.jule
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use "std/jule/ast"
use "std/jule/build"
use "std/jule/token"
use "std/strings"

struct exprBuilder {
p: &parser
Expand Down Expand Up @@ -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 {
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 11 additions & 7 deletions std/jule/sema/eval.jule
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -1976,7 +1976,7 @@ impl eval {
errorToken: errorToken,
s: s,
}
slc.check(exprs)
slc.check(lit.Exprs)

mut v := &Value{
Mutable: self.target.mutable,
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 845acf0

Please sign in to comment.