Skip to content

Commit

Permalink
fix: struct builders with sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
emil14 committed Jan 12, 2024
1 parent a331509 commit e3c71ab
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 27 deletions.
9 changes: 9 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@
"cwd": "${workspaceFolder}/examples",
"args": ["struct_builder/verbose"]
},
{
"name": "Interpreter: struct_builder/with_sugar",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/interpreter",
"cwd": "${workspaceFolder}/examples",
"args": ["struct_builder/with_sugar"]
},
// === Other ===
{
"name": "antlr_000_empty.neva",
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
"minimap.background": "#00000000",
"scrollbar.shadow": "#00000000"
},
"svg.preview.background": "dark-transparent"
"svg.preview.background": "dark-transparent",
"vsicons.presets.angular": true
}
2 changes: 1 addition & 1 deletion examples/struct_builder/with_sugar/main.neva
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ components {
Main(enter) (exit) {
nodes {
builder StructBuilder<User>
print Print<str>
print Print<User>
}
net {
$age -> builder.age
Expand Down
8 changes: 7 additions & 1 deletion internal/compiler/analyzer/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ func (a Analyzer) analyzeComponent( //nolint:funlen
}
}

analyzedNet, err := a.analyzeComponentNetwork(component.Net, analyzedInterface, analyzedNodes, nodesIfaces, scope)
analyzedNet, err := a.analyzeComponentNetwork(
component.Net,
analyzedInterface,
analyzedNodes,
nodesIfaces,
scope,
)
if err != nil {
return src.Component{}, compiler.Error{
Location: &scope.Location,
Expand Down
26 changes: 11 additions & 15 deletions internal/compiler/analyzer/component_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,13 @@ func (a Analyzer) resolveReceiverType(
return outport.TypeExpr, nil
}

nodeInportType, err := a.getNodeInportType(receiverSide.PortAddr, nodes, scope)
nodeInportType, err := a.getNodeInportType(receiverSide.PortAddr, nodes, nodesIfaces, scope)
if err != nil {
return ts.Expr{}, compiler.Error{
return ts.Expr{}, &compiler.Error{
Err: err,
Location: &scope.Location,
Meta: &receiverSide.PortAddr.Meta,
}.Merge(err)
}
}

return nodeInportType, nil
Expand All @@ -263,33 +264,28 @@ func (a Analyzer) resolveReceiverType(
func (a Analyzer) getNodeInportType(
portAddr src.PortAddr,
nodes map[string]src.Node,
nodesIfaces map[string]src.Interface,
scope src.Scope,
) (ts.Expr, *compiler.Error) {
node, ok := nodes[portAddr.Node]
if !ok {
return ts.Expr{}, &compiler.Error{
Err: fmt.Errorf("%w '%v'", ErrNodeNotFound, portAddr.Node),
Err: fmt.Errorf("Node not found '%v'", portAddr.Node),
Location: &scope.Location,
Meta: &portAddr.Meta,
}
}

entity, location, err := scope.Entity(node.EntityRef)
if err != nil {
iface, ok := nodesIfaces[portAddr.Node]
if !ok {
return ts.Expr{}, &compiler.Error{
Err: err,
Err: fmt.Errorf("%w '%v'", ErrNodeNotFound, portAddr.Node),
Location: &scope.Location,
Meta: &portAddr.Meta,
}
}

var iface src.Interface
if entity.Kind == src.ComponentEntity {
iface = entity.Component.Interface
} else { // we assume that nodes are already validated so if it's not component then it's interface
iface = entity.Interface
}

// TODO optimize: we can resolve every node's interface just once before processing the network
typ, aerr := a.getResolvedPortType(
iface.IO.In,
iface.TypeParams.Params,
Expand All @@ -299,7 +295,7 @@ func (a Analyzer) getNodeInportType(
)
if aerr != nil {
return ts.Expr{}, compiler.Error{
Location: &location,
Location: &scope.Location,
Meta: &portAddr.Meta,
}.Merge(aerr)
}
Expand Down
18 changes: 10 additions & 8 deletions internal/compiler/analyzer/component_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ func (a Analyzer) getResolvedNodeInterface( //nolint:funlen
return iface, nil
}

// handle struct inports directive case:

if len(iface.IO.In) != 0 {
return src.Interface{}, &compiler.Error{
Err: ErrNormalInportsWithStructInportsDirective,
Expand Down Expand Up @@ -252,14 +250,18 @@ func (a Analyzer) getResolvedNodeInterface( //nolint:funlen
}
}

iface = src.Interface{
return src.Interface{
TypeParams: iface.TypeParams,
IO: src.IO{
In: inports,
Out: iface.IO.Out,
In: inports,
Out: map[string]src.Port{
"v": { // TODO refactor (it's not good to know exact interface here)
TypeExpr: resolvedNodeArg,
IsArray: false,
Meta: iface.IO.Out["v"].Meta,
},
},
},
Meta: iface.Meta,
}

return iface, nil
}, nil
}
22 changes: 22 additions & 0 deletions internal/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package compiler

import (
"context"
"fmt"
"strings"

"github.com/nevalang/neva/pkg/ir"
Expand Down Expand Up @@ -64,6 +65,27 @@ func (c Compiler) CompileToIR(
return nil, err
}

// FIXME no structBuilder func call in resulting irprog

fmt.Println(
JSONDump(parsedBuild.Modules[desugaredBuild.EntryModRef].Packages["struct_builder/with_sugar"]),
)
fmt.Println()
fmt.Println()
fmt.Println(
JSONDump(analyzedBuild.Modules[desugaredBuild.EntryModRef].Packages["struct_builder/with_sugar"]),
)
fmt.Println()
fmt.Println()
fmt.Println(
JSONDump(desugaredBuild.Modules[desugaredBuild.EntryModRef].Packages["struct_builder/with_sugar"]),
)
fmt.Println()
fmt.Println()
fmt.Println(
JSONDump(irProg),
)

return irProg, nil
}

Expand Down
3 changes: 2 additions & 1 deletion internal/compiler/desugarer/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ func (d Desugarer) desugarComponent( //nolint:funlen
// finally replace component ref for this current node with the ref to newly created local builder variation
desugaredNodes[nodeName] = src.Node{
EntityRef: src.EntityRef{
Name: localBuilderName,
Pkg: "builtin",
Name: "StructBuilder",
},
Directives: node.Directives,
TypeArgs: node.TypeArgs,
Expand Down
1 change: 1 addition & 0 deletions internal/runtime/event_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "fmt"
type EmptyListener struct{}

func (l EmptyListener) Send(event Event, msg Msg) Msg {
fmt.Println(event, msg)
return msg
}

Expand Down

0 comments on commit e3c71ab

Please sign in to comment.