Skip to content

Commit

Permalink
CORE: Fix parsing of struct initializer's type, now can include names…
Browse files Browse the repository at this point in the history
…paces
  • Loading branch information
MineGame159 committed Jan 28, 2024
1 parent c37c938 commit cd3bce0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 20 deletions.
77 changes: 57 additions & 20 deletions core/cst/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cst

import (
"fireball/core/scanner"
"fireball/core/utils"
)

var canStartExpr = []scanner.TokenKind{
Expand Down Expand Up @@ -114,24 +115,6 @@ func parsePrefixExprPratt(p *parser) Node {
return p.end()
}

if p.peek2() == scanner.LeftBrace {
p.begin(StructExprNode)

p.begin(IdentifierTypeNode)
p.advanceAddChild()
p.childAdd(p.end())

p.advanceAddChild()
if p.repeatSeparated(parseStructFieldExpr, canStartStructFieldExpr, scanner.Comma) {
return p.end()
}
if p.consume(scanner.RightBrace) {
return p.end()
}

return p.end()
}

return p.advanceGetLeaf()

case scanner.LeftParen:
Expand Down Expand Up @@ -277,6 +260,24 @@ func parsePostfixExprPratt(p *parser, op scanner.TokenKind, lhs Node) Node {

return p.end()

case scanner.LeftBrace:
p.begin(StructExprNode)

if p.childAdd(convertExprToIdentifierType(p, lhs)) {
return p.end()
}
if p.consume(scanner.LeftBrace) {
return p.end()
}
if p.repeatSeparated(parseStructFieldExpr, canStartStructFieldExpr, scanner.Comma) {
return p.end()
}
if p.consume(scanner.RightBrace) {
return p.end()
}

return p.end()

default:
p.begin(UnaryExprNode)

Expand All @@ -287,6 +288,42 @@ func parsePostfixExprPratt(p *parser, op scanner.TokenKind, lhs Node) Node {
}
}

func convertExprToIdentifierType(p *parser, node Node) Node {
p.begin(IdentifierTypeNode)

convertExprToIdentifierTypePart(p, node)

return p.end()
}

func convertExprToIdentifierTypePart(p *parser, node Node) {
//goland:noinspection GoSwitchMissingCasesForIotaConsts
switch node.Kind {
case IdentifierNode:
p.childAdd(node)
return

case BinaryExprNode:
if node.Contains(scanner.Dot) {
convertExprToIdentifierTypePart(p, node.Children[0])

p.childAdd(node.Children[1])

if len(node.Children) == 3 {
convertExprToIdentifierTypePart(p, node.Children[2])
}

return
}
}

p.reporter.Report(utils.Diagnostic{
Kind: utils.ErrorKind,
Range: node.Range,
Message: "Invalid type",
})
}

// Powers

type tokenPowers struct {
Expand Down Expand Up @@ -338,8 +375,8 @@ func init() {
prefix(scanner.Minus, scanner.Bang, scanner.PlusPlus, scanner.MinusMinus, scanner.Ampersand, scanner.Star, scanner.FuncPtr)
// x++, x--
postfix(scanner.PlusPlus, scanner.MinusMinus)
// x[], x()
postfix(scanner.LeftBracket, scanner.LeftParen)
// x[], x(), x {}
postfix(scanner.LeftBracket, scanner.LeftParen, scanner.LeftBrace)
// x.y
infix(false, scanner.Dot)
}
Expand Down
13 changes: 13 additions & 0 deletions tests/src/namespaces.fb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Tests.Namespaces;

#[Test]
func typeSimple() bool {
var _ Child.Foo;
return true;
}

#[Test]
func typeStructInit() bool {
var _ = Child.Foo {};
return true;
}
3 changes: 3 additions & 0 deletions tests/src/namespaces_child.fb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Tests.Namespaces.Child;

struct Foo {}

0 comments on commit cd3bce0

Please sign in to comment.