Add sizeof expression parser, fix nkNone issue, reduce size of typeof expressions
This commit is contained in:
parent
b51f0da6b2
commit
2fa669c75d
2 changed files with 47 additions and 38 deletions
|
|
@ -963,7 +963,7 @@ proc getTypeArray(gState: State, node: TSNode, tident: PNode, name: string): PNo
|
||||||
let
|
let
|
||||||
# Size of array could be a Nim expression
|
# Size of array could be a Nim expression
|
||||||
size = gState.getLit(gState.getNodeVal(cnode[1]), expression = true)
|
size = gState.getLit(gState.getNodeVal(cnode[1]), expression = true)
|
||||||
if size.kind != nkNilLit:
|
if size.kind != nkNone:
|
||||||
result = gState.newArrayTree(cnode, result, size)
|
result = gState.newArrayTree(cnode, result, size)
|
||||||
cnode = cnode[0]
|
cnode = cnode[0]
|
||||||
elif cnode.len == 1:
|
elif cnode.len == 1:
|
||||||
|
|
|
||||||
|
|
@ -130,9 +130,9 @@ proc processStringLiteral*(exprParser: ExprParser, node: TSNode): PNode =
|
||||||
let nodeVal = node.val
|
let nodeVal = node.val
|
||||||
result = newStrNode(nkStrLit, nodeVal[1 ..< nodeVal.len - 1])
|
result = newStrNode(nkStrLit, nodeVal[1 ..< nodeVal.len - 1])
|
||||||
|
|
||||||
proc processTSNode*(exprParser: ExprParser, node: TSNode, typeofNode: PNode = nil): PNode
|
proc processTSNode*(exprParser: ExprParser, node: TSNode, typeofNode: var PNode): PNode
|
||||||
|
|
||||||
proc processShiftExpression*(exprParser: ExprParser, node: TSNode, typeofNode: PNode = nil): PNode =
|
proc processShiftExpression*(exprParser: ExprParser, node: TSNode, typeofNode: var PNode): PNode =
|
||||||
result = newNode(nkInfix)
|
result = newNode(nkInfix)
|
||||||
let
|
let
|
||||||
left = node[0]
|
left = node[0]
|
||||||
|
|
@ -149,27 +149,26 @@ proc processShiftExpression*(exprParser: ExprParser, node: TSNode, typeofNode: P
|
||||||
|
|
||||||
let leftNode = exprParser.processTSNode(left, typeofNode)
|
let leftNode = exprParser.processTSNode(left, typeofNode)
|
||||||
|
|
||||||
var tnode = typeofNode
|
if typeofNode.isNil:
|
||||||
if tnode.isNil:
|
typeofNode = nkCall.newTree(
|
||||||
tnode = leftNode
|
exprParser.state.getIdent("typeof"),
|
||||||
|
leftNode
|
||||||
|
)
|
||||||
|
|
||||||
let rightNode = exprParser.processTSNode(right, tnode)
|
let rightNode = exprParser.processTSNode(right, typeofNode)
|
||||||
|
|
||||||
result.add leftNode
|
result.add leftNode
|
||||||
result.add nkCast.newTree(
|
result.add nkCast.newTree(
|
||||||
nkCall.newTree(
|
typeofNode,
|
||||||
exprParser.state.getIdent("typeof"),
|
|
||||||
tnode
|
|
||||||
),
|
|
||||||
rightNode
|
rightNode
|
||||||
)
|
)
|
||||||
|
|
||||||
proc processParenthesizedExpr*(exprParser: ExprParser, node: TSNode, typeofNode: PNode = nil): PNode =
|
proc processParenthesizedExpr*(exprParser: ExprParser, node: TSNode, typeofNode: var PNode): PNode =
|
||||||
result = newNode(nkPar)
|
result = newNode(nkPar)
|
||||||
for i in 0 ..< node.len():
|
for i in 0 ..< node.len():
|
||||||
result.add(exprParser.processTSNode(node[i], typeofNode))
|
result.add(exprParser.processTSNode(node[i], typeofNode))
|
||||||
|
|
||||||
proc processLogicalExpression*(exprParser: ExprParser, node: TSNode, typeofNode: PNode = nil): PNode =
|
proc processLogicalExpression*(exprParser: ExprParser, node: TSNode, typeofNode: var PNode): PNode =
|
||||||
result = newNode(nkPar)
|
result = newNode(nkPar)
|
||||||
let child = node[0]
|
let child = node[0]
|
||||||
var nimSym = ""
|
var nimSym = ""
|
||||||
|
|
@ -189,7 +188,7 @@ proc processLogicalExpression*(exprParser: ExprParser, node: TSNode, typeofNode:
|
||||||
exprParser.processTSNode(child, typeofNode)
|
exprParser.processTSNode(child, typeofNode)
|
||||||
)
|
)
|
||||||
|
|
||||||
proc processMathExpression(exprParser: ExprParser, node: TSNode, typeofNode: PNode = nil): PNode =
|
proc processMathExpression(exprParser: ExprParser, node: TSNode, typeofNode: var PNode): PNode =
|
||||||
if node.len > 1:
|
if node.len > 1:
|
||||||
# Node has left and right children ie: (2 + 7)
|
# Node has left and right children ie: (2 + 7)
|
||||||
var
|
var
|
||||||
|
|
@ -204,19 +203,18 @@ proc processMathExpression(exprParser: ExprParser, node: TSNode, typeofNode: PNo
|
||||||
res.add exprParser.state.getIdent(mathSym)
|
res.add exprParser.state.getIdent(mathSym)
|
||||||
let leftNode = exprParser.processTSNode(left, typeofNode)
|
let leftNode = exprParser.processTSNode(left, typeofNode)
|
||||||
|
|
||||||
var tnode = typeofNode
|
if typeofNode.isNil:
|
||||||
if tnode.isNil:
|
typeofNode = nkCall.newTree(
|
||||||
tnode = leftNode
|
exprParser.state.getIdent("typeof"),
|
||||||
|
leftNode
|
||||||
|
)
|
||||||
|
|
||||||
let rightNode = exprParser.processTSNode(right, tnode)
|
let rightNode = exprParser.processTSNode(right, typeofNode)
|
||||||
|
|
||||||
res.add leftNode
|
res.add leftNode
|
||||||
# res.add rightNode
|
# res.add rightNode
|
||||||
res.add nkCast.newTree(
|
res.add nkCast.newTree(
|
||||||
nkCall.newTree(
|
typeofNode,
|
||||||
exprParser.state.getIdent("typeof"),
|
|
||||||
tnode
|
|
||||||
),
|
|
||||||
rightNode
|
rightNode
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -224,10 +222,7 @@ proc processMathExpression(exprParser: ExprParser, node: TSNode, typeofNode: PNo
|
||||||
# hand argument, since some expressions return a differing
|
# hand argument, since some expressions return a differing
|
||||||
# type than the input types (2/3 == float)
|
# type than the input types (2/3 == float)
|
||||||
result = nkCall.newTree(
|
result = nkCall.newTree(
|
||||||
nkCall.newTree(
|
typeofNode,
|
||||||
exprParser.state.getIdent("typeof"),
|
|
||||||
tnode
|
|
||||||
),
|
|
||||||
res
|
res
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -248,6 +243,8 @@ proc processMathExpression(exprParser: ExprParser, node: TSNode, typeofNode: PNo
|
||||||
# so we have to make a gental cast here to coerce it to one.
|
# so we have to make a gental cast here to coerce it to one.
|
||||||
# Might be bad because we are overwriting the type
|
# Might be bad because we are overwriting the type
|
||||||
# There's probably a better way of doing this
|
# There's probably a better way of doing this
|
||||||
|
if typeofNode.isNil:
|
||||||
|
typeofNode = exprParser.state.getIdent("int64")
|
||||||
result.add nkPrefix.newTree(
|
result.add nkPrefix.newTree(
|
||||||
exprParser.state.getIdent(unarySym),
|
exprParser.state.getIdent(unarySym),
|
||||||
nkPar.newTree(
|
nkPar.newTree(
|
||||||
|
|
@ -268,7 +265,7 @@ proc processMathExpression(exprParser: ExprParser, node: TSNode, typeofNode: PNo
|
||||||
else:
|
else:
|
||||||
raise newException(ExprParseError, &"Invalid bitwise_expression \"{node.val}\"")
|
raise newException(ExprParseError, &"Invalid bitwise_expression \"{node.val}\"")
|
||||||
|
|
||||||
proc processBitwiseExpression(exprParser: ExprParser, node: TSNode, typeofNode: PNode = nil): PNode =
|
proc processBitwiseExpression(exprParser: ExprParser, node: TSNode, typeofNode: var PNode): PNode =
|
||||||
if node.len() > 1:
|
if node.len() > 1:
|
||||||
result = newNode(nkInfix)
|
result = newNode(nkInfix)
|
||||||
|
|
||||||
|
|
@ -288,24 +285,25 @@ proc processBitwiseExpression(exprParser: ExprParser, node: TSNode, typeofNode:
|
||||||
nimSym = "and"
|
nimSym = "and"
|
||||||
of "^":
|
of "^":
|
||||||
nimSym = "xor"
|
nimSym = "xor"
|
||||||
|
of "==", "!=":
|
||||||
|
nimSym = binarySym
|
||||||
else:
|
else:
|
||||||
raise newException(ExprParseError, &"Unsupported binary symbol \"{binarySym}\"")
|
raise newException(ExprParseError, &"Unsupported binary symbol \"{binarySym}\"")
|
||||||
|
|
||||||
result.add exprParser.state.getIdent(nimSym)
|
result.add exprParser.state.getIdent(nimSym)
|
||||||
let leftNode = exprParser.processTSNode(left, typeofNode)
|
let leftNode = exprParser.processTSNode(left, typeofNode)
|
||||||
|
|
||||||
var tnode = typeofNode
|
if typeofNode.isNil:
|
||||||
if tnode.isNil:
|
typeofNode = nkCall.newTree(
|
||||||
tnode = leftNode
|
exprParser.state.getIdent("typeof"),
|
||||||
|
leftNode
|
||||||
|
)
|
||||||
|
|
||||||
let rightNode = exprParser.processTSNode(right, tnode)
|
let rightNode = exprParser.processTSNode(right, typeofNode)
|
||||||
|
|
||||||
result.add leftNode
|
result.add leftNode
|
||||||
result.add nkCall.newTree(
|
result.add nkCall.newTree(
|
||||||
nkCall.newTree(
|
typeofNode,
|
||||||
exprParser.state.getIdent("typeof"),
|
|
||||||
tnode
|
|
||||||
),
|
|
||||||
rightNode
|
rightNode
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -330,7 +328,13 @@ proc processBitwiseExpression(exprParser: ExprParser, node: TSNode, typeofNode:
|
||||||
else:
|
else:
|
||||||
raise newException(ExprParseError, &"Invalid bitwise_expression \"{node.val}\"")
|
raise newException(ExprParseError, &"Invalid bitwise_expression \"{node.val}\"")
|
||||||
|
|
||||||
proc processTSNode(exprParser: ExprParser, node: TSNode, typeofNode: PNode = nil): PNode =
|
proc processSizeofExpression(exprParser: ExprParser, node: TSNode, typeofNode: var PNode): PNode =
|
||||||
|
result = nkCall.newTree(
|
||||||
|
exprParser.state.getIdent("sizeof"),
|
||||||
|
exprParser.processTSNode(node[0], typeofNode)
|
||||||
|
)
|
||||||
|
|
||||||
|
proc processTSNode(exprParser: ExprParser, node: TSNode, typeofNode: var PNode): PNode =
|
||||||
## Handle all of the types of expressions here. This proc gets called recursively
|
## Handle all of the types of expressions here. This proc gets called recursively
|
||||||
## in the processX procs and will drill down to sub nodes.
|
## in the processX procs and will drill down to sub nodes.
|
||||||
result = newNode(nkNone)
|
result = newNode(nkNone)
|
||||||
|
|
@ -351,7 +355,9 @@ proc processTSNode(exprParser: ExprParser, node: TSNode, typeofNode: PNode = nil
|
||||||
raise newException(ExprParseError, &"Node type \"{nodeName}\" has no children")
|
raise newException(ExprParseError, &"Node type \"{nodeName}\" has no children")
|
||||||
of "parenthesized_expression":
|
of "parenthesized_expression":
|
||||||
result = exprParser.processParenthesizedExpr(node, typeofNode)
|
result = exprParser.processParenthesizedExpr(node, typeofNode)
|
||||||
of "bitwise_expression":
|
of "sizeof_expression":
|
||||||
|
result = exprParser.processSizeofExpression(node, typeofNode)
|
||||||
|
of "bitwise_expression", "equality_expression":
|
||||||
result = exprParser.processBitwiseExpression(node, typeofNode)
|
result = exprParser.processBitwiseExpression(node, typeofNode)
|
||||||
of "math_expression":
|
of "math_expression":
|
||||||
result = exprParser.processMathExpression(node, typeofNode)
|
result = exprParser.processMathExpression(node, typeofNode)
|
||||||
|
|
@ -380,10 +386,13 @@ proc processTSNode(exprParser: ExprParser, node: TSNode, typeofNode: PNode = nil
|
||||||
proc codeToNode*(state: NimState, code: string): PNode =
|
proc codeToNode*(state: NimState, code: string): PNode =
|
||||||
## Convert the C string to a nim PNode tree
|
## Convert the C string to a nim PNode tree
|
||||||
result = newNode(nkNone)
|
result = newNode(nkNone)
|
||||||
|
# This is used for keeping track of the type of the first
|
||||||
|
# symbol
|
||||||
|
var tnode: PNode = nil
|
||||||
try:
|
try:
|
||||||
let exprParser = newExprParser(state, code)
|
let exprParser = newExprParser(state, code)
|
||||||
withCodeAst(exprParser):
|
withCodeAst(exprParser):
|
||||||
result = exprParser.processTSNode(root)
|
result = exprParser.processTSNode(root, tnode)
|
||||||
except ExprParseError as e:
|
except ExprParseError as e:
|
||||||
echo e.msg.getCommented
|
echo e.msg.getCommented
|
||||||
result = newNode(nkNone)
|
result = newNode(nkNone)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue