Skip to content

Commit

Permalink
Bunch of updates, however need to deal with references as they suck
Browse files Browse the repository at this point in the history
  • Loading branch information
0x19 committed Aug 15, 2023
1 parent 3d05a12 commit e32084f
Show file tree
Hide file tree
Showing 16 changed files with 482 additions and 30 deletions.
11 changes: 10 additions & 1 deletion abi/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,20 @@ func (r *Root) GetEntryName() string {
return r.EntryContractName
}

// GetContracts returns the map of contracts in the ABI.
// GetContracts returns the map of contracts.
func (r *Root) GetContracts() map[string]*Contract {
return r.Contracts
}

// GetContractsAsSlice returns the slice contracts.
func (r *Root) GetContractsAsSlice() []*Contract {
toReturn := make([]*Contract, 0)
for _, c := range r.Contracts {
toReturn = append(toReturn, c)
}
return toReturn
}

// GetContractByName retrieves a contract by its name from the ABI.
// Returns nil if the contract is not found.
func (r *Root) GetContractByName(name string) *Contract {
Expand Down
121 changes: 121 additions & 0 deletions ast/and.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package ast

import (
ast_pb "github.com/txpull/protos/dist/go/ast"
"github.com/txpull/solgo/parser"
)

type AndOperation struct {
*ASTBuilder

Id int64 `json:"id"`
NodeType ast_pb.NodeType `json:"node_type"`
Src SrcNode `json:"src"`
Expressions []Node[NodeType] `json:"expressions"`
TypeDescriptions []*TypeDescription `json:"type_descriptions"`
}

func NewAndOperationExpression(b *ASTBuilder) *AndOperation {
return &AndOperation{
ASTBuilder: b,
Id: b.GetNextID(),
NodeType: ast_pb.NodeType_AND_OPERATION,
TypeDescriptions: make([]*TypeDescription, 0),
}
}

// SetReferenceDescriptor sets the reference descriptions of the AndOperation node.
func (b *AndOperation) SetReferenceDescriptor(refId int64, refDesc *TypeDescription) bool {
return false
}

func (f *AndOperation) GetId() int64 {
return f.Id
}

func (f *AndOperation) GetType() ast_pb.NodeType {
return f.NodeType
}

func (f *AndOperation) GetSrc() SrcNode {
return f.Src
}

func (f *AndOperation) GetTypeDescription() *TypeDescription {
return f.TypeDescriptions[0]
}

func (f *AndOperation) GetNodes() []Node[NodeType] {
toReturn := []Node[NodeType]{}
toReturn = append(toReturn, f.Expressions...)
return toReturn
}

func (f *AndOperation) GetExpressions() []Node[NodeType] {
return f.Expressions
}

func (f *AndOperation) ToProto() NodeType {
/* proto := ast_pb.AndOperation{
Id: f.GetId(),
NodeType: f.GetType(),
Src: f.GetSrc().ToProto(),
LeftExpression: f.GetLeftExpression().ToProto().(*v3.TypedStruct),
RightExpression: f.GetRightExpression().ToProto().(*v3.TypedStruct),
TypeDescription: f.GetTypeDescription().ToProto(),
}
*/
return NewTypedStruct(nil, "AndOperation")
}

func (f *AndOperation) Parse(
unit *SourceUnit[Node[ast_pb.SourceUnit]],
contractNode Node[NodeType],
fnNode Node[NodeType],
bodyNode *BodyNode,
vDeclar *VariableDeclaration,
expNode Node[NodeType],
ctx *parser.AndOperationContext,
) Node[NodeType] {
f.Id = f.GetNextID()
f.Src = SrcNode{
Id: f.GetNextID(),
Line: int64(ctx.GetStart().GetLine()),
Column: int64(ctx.GetStart().GetColumn()),
Start: int64(ctx.GetStart().GetStart()),
End: int64(ctx.GetStop().GetStop()),
Length: int64(ctx.GetStop().GetStop() - ctx.GetStart().GetStart() + 1),
ParentIndex: func() int64 {
if vDeclar != nil {
return vDeclar.GetId()
}

if expNode != nil {
return expNode.GetId()
}

if bodyNode != nil {
return bodyNode.GetId()
}

if fnNode != nil {
return fnNode.GetId()
}

return contractNode.GetId()
}(),
}

expression := NewExpression(f.ASTBuilder)

for _, expr := range ctx.AllExpression() {
parsedExp := expression.Parse(unit, contractNode, fnNode, bodyNode, vDeclar, f, expr)
f.Expressions = append(
f.Expressions,
parsedExp,
)
f.TypeDescriptions = append(f.TypeDescriptions, parsedExp.GetTypeDescription())
}

return f
}
123 changes: 123 additions & 0 deletions ast/conditional.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package ast

import (
ast_pb "github.com/txpull/protos/dist/go/ast"
"github.com/txpull/solgo/parser"
)

type Conditional struct {
*ASTBuilder

Id int64 `json:"id"`
NodeType ast_pb.NodeType `json:"node_type"`
Src SrcNode `json:"src"`
Expressions []Node[NodeType] `json:"right_expression"`
TypeDescriptions []*TypeDescription `json:"type_descriptions"`
}

func NewConditionalExpression(b *ASTBuilder) *Conditional {
return &Conditional{
ASTBuilder: b,
Id: b.GetNextID(),
NodeType: ast_pb.NodeType_CONDITIONAL_EXPRESSION,
TypeDescriptions: make([]*TypeDescription, 0),
}
}

// SetReferenceDescriptor sets the reference descriptions of the Conditional node.
func (b *Conditional) SetReferenceDescriptor(refId int64, refDesc *TypeDescription) bool {
return false
}

func (f *Conditional) GetId() int64 {
return f.Id
}

func (f *Conditional) GetType() ast_pb.NodeType {
return f.NodeType
}

func (f *Conditional) GetSrc() SrcNode {
return f.Src
}

func (f *Conditional) GetTypeDescription() *TypeDescription {
return f.TypeDescriptions[0]
}

func (f *Conditional) GetNodes() []Node[NodeType] {
toReturn := []Node[NodeType]{}
for _, exp := range f.Expressions {
toReturn = append(toReturn, exp)
}
return toReturn
}

func (f *Conditional) GetExpressions() []Node[NodeType] {
return f.Expressions
}

func (f *Conditional) ToProto() NodeType {
/* proto := ast_pb.Conditional{
Id: f.GetId(),
NodeType: f.GetType(),
Src: f.GetSrc().ToProto(),
LeftExpression: f.GetLeftExpression().ToProto().(*v3.TypedStruct),
RightExpression: f.GetRightExpression().ToProto().(*v3.TypedStruct),
TypeDescription: f.GetTypeDescription().ToProto(),
}
*/
return NewTypedStruct(nil, "Conditional")
}

func (f *Conditional) Parse(
unit *SourceUnit[Node[ast_pb.SourceUnit]],
contractNode Node[NodeType],
fnNode Node[NodeType],
bodyNode *BodyNode,
vDeclar *VariableDeclaration,
expNode Node[NodeType],
ctx *parser.ConditionalContext,
) Node[NodeType] {
f.Id = f.GetNextID()
f.Src = SrcNode{
Id: f.GetNextID(),
Line: int64(ctx.GetStart().GetLine()),
Column: int64(ctx.GetStart().GetColumn()),
Start: int64(ctx.GetStart().GetStart()),
End: int64(ctx.GetStop().GetStop()),
Length: int64(ctx.GetStop().GetStop() - ctx.GetStart().GetStart() + 1),
ParentIndex: func() int64 {
if vDeclar != nil {
return vDeclar.GetId()
}

if expNode != nil {
return expNode.GetId()
}

if bodyNode != nil {
return bodyNode.GetId()
}

if fnNode != nil {
return fnNode.GetId()
}

return contractNode.GetId()
}(),
}

expression := NewExpression(f.ASTBuilder)

for _, expr := range ctx.AllExpression() {
parsedExp := expression.Parse(unit, contractNode, fnNode, bodyNode, vDeclar, f, expr)
f.Expressions = append(
f.Expressions,
parsedExp,
)
f.TypeDescriptions = append(f.TypeDescriptions, parsedExp.GetTypeDescription())
}

return f
}
17 changes: 12 additions & 5 deletions ast/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

ast_pb "github.com/txpull/protos/dist/go/ast"
"github.com/txpull/solgo/parser"
"go.uber.org/zap"
)

type Expression struct {
Expand Down Expand Up @@ -84,12 +85,18 @@ func (e *Expression) Parse(
case *parser.ExpOperationContext:
expOperation := NewExprOperationExpression(e.ASTBuilder)
return expOperation.Parse(unit, contractNode, fnNode, bodyNode, vDecar, exprNode, ctxType)
case *parser.ConditionalContext:
conditional := NewConditionalExpression(e.ASTBuilder)
return conditional.Parse(unit, contractNode, fnNode, bodyNode, vDecar, exprNode, ctxType)
case *parser.AndOperationContext:
andOperation := NewAndOperationExpression(e.ASTBuilder)
return andOperation.Parse(unit, contractNode, fnNode, bodyNode, vDecar, exprNode, ctxType)
default:
panic(
fmt.Sprintf(
"Expression type not supported @ Expression.Parse: %T",
ctx,
),
zap.L().Warn(
"Expression type not supported @ Expression.Parse",
zap.String("type", fmt.Sprintf("%T", ctx)),
)
}

return nil
}
3 changes: 3 additions & 0 deletions ast/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ func (f *Function) buildTypeDescription() *TypeDescription {
typeIdentifiers := make([]string, 0)

for _, paramType := range f.GetParameters().GetParameterTypes() {
if paramType == nil {
f.dumpNode(f)
}
typeStrings = append(typeStrings, paramType.TypeString)
typeIdentifiers = append(typeIdentifiers, "$_"+paramType.TypeIdentifier)
}
Expand Down
Loading

0 comments on commit e32084f

Please sign in to comment.