Skip to content

Commit

Permalink
ast,transpile: add support for ConstantExpr (#850)
Browse files Browse the repository at this point in the history
c2go is now capable of parsing stddef.h (of Clang) on Linux.

Fixes #829
Fixes #848 
Superseeds #830
  • Loading branch information
mewmew authored Mar 17, 2020
1 parent 48da8dc commit 6feef47
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func Parse(fullline string) Node {
return parseConstAttr(line)
case "ConstantArrayType":
return parseConstantArrayType(line)
case "ConstantExpr":
return parseConstantExpr(line)
case "ContinueStmt":
return parseContinueStmt(line)
case "CompoundAssignOperator":
Expand Down
46 changes: 46 additions & 0 deletions ast/constant_expr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ast

// ConstantExpr is a constant expression
type ConstantExpr struct {
Addr Address
Pos Position
Type string
ChildNodes []Node
}

func parseConstantExpr(line string) *ConstantExpr {
groups := groupsFromRegex(
"<(?P<position>.*)> '(?P<type>.*?)'",
line,
)

return &ConstantExpr{
Addr: ParseAddress(groups["address"]),
Pos: NewPositionFromString(groups["position"]),
Type: groups["type"],
ChildNodes: []Node{},
}
}

// AddChild adds a new child node. Child nodes can then be accessed with the
// Children attribute.
func (n *ConstantExpr) AddChild(node Node) {
n.ChildNodes = append(n.ChildNodes, node)
}

// Address returns the numeric address of the node. See the documentation for
// the Address type for more information.
func (n *ConstantExpr) Address() Address {
return n.Addr
}

// Children returns the child nodes. If this node does not have any children or
// this node does not support children it will always return an empty slice.
func (n *ConstantExpr) Children() []Node {
return n.ChildNodes
}

// Position returns the position in the original source code.
func (n *ConstantExpr) Position() Position {
return n.Pos
}
18 changes: 18 additions & 0 deletions ast/constant_expr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ast

import (
"testing"
)

func TestConstantExpr(t *testing.T) {
nodes := map[string]Node{
`0x5558ffde68f0 <col:34, col:55> 'unsigned long'`: &ConstantExpr{
Addr: 0x5558ffde68f0,
Pos: NewPositionFromString("col:34, col:55"),
Type: "unsigned long",
ChildNodes: []Node{},
},
}

runNodeTests(t, nodes)
}
2 changes: 2 additions & 0 deletions ast/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ func setPosition(node Node, position Position) {
n.Pos = position
case *ConstAttr:
n.Pos = position
case *ConstantExpr:
n.Pos = position
case *ContinueStmt:
n.Pos = position
case *CompoundAssignOperator:
Expand Down
12 changes: 12 additions & 0 deletions transpiler/literals.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,15 @@ func transpileCompoundLiteralExpr(n *ast.CompoundLiteralExpr, p *program.Program
expr, t, _, _, err := transpileToExpr(n.Children()[0], p, false)
return expr, t, err
}

func transpileConstantExpr(n *ast.ConstantExpr, p *program.Program) (goast.Expr, string, error) {
children := n.Children()
expr, t, _, _, err := transpileToExpr(children[0], p, false)
if len(children) != 1 {
p.AddMessage(p.GenerateWarningMessage(fmt.Errorf("ConstantExpr has %d children, expected 1 child", len(children)), n))
}
if len(n.Type) > 0 {
t = n.Type
}
return expr, t, err
}
3 changes: 3 additions & 0 deletions transpiler/transpiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ func transpileToExpr(node ast.Node, p *program.Program, exprIsStmt bool) (
exprType = "double"
err = nil

case *ast.ConstantExpr:
expr, exprType, err = transpileConstantExpr(n, p)

case *ast.PredefinedExpr:
expr, exprType, err = transpilePredefinedExpr(n, p)

Expand Down

0 comments on commit 6feef47

Please sign in to comment.