Merge v0.4.4 fixes
This commit is contained in:
commit
fa1c4c636c
8 changed files with 60 additions and 62 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "0.4.3"
|
version = "0.4.4"
|
||||||
author = "genotrance"
|
author = "genotrance"
|
||||||
description = "C/C++ interop for Nim"
|
description = "C/C++ interop for Nim"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
@ -12,7 +12,7 @@ installDirs = @["nimterop"]
|
||||||
installFiles = @["config.nims"]
|
installFiles = @["config.nims"]
|
||||||
|
|
||||||
# Dependencies
|
# Dependencies
|
||||||
requires "nim >= 0.19.2", "regex >= 0.10.0", "cligen >= 0.9.17"
|
requires "nim >= 0.19.6", "regex >= 0.13.0", "cligen >= 0.9.41"
|
||||||
|
|
||||||
import nimterop/docs
|
import nimterop/docs
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ proc searchAstForNode(ast: ref Ast, node: TSNode, nimState: NimState): bool =
|
||||||
|
|
||||||
if nimState.gState.debug:
|
if nimState.gState.debug:
|
||||||
nimState.nodeBranch.add $node.tsNodeType()
|
nimState.nodeBranch.add $node.tsNodeType()
|
||||||
echo "#" & spaces(nimState.nodeBranch.len * 2) & nimState.nodeBranch[^1]
|
necho "#" & spaces(nimState.nodeBranch.len * 2) & nimState.nodeBranch[^1]
|
||||||
|
|
||||||
if ast.children.nBl:
|
if ast.children.nBl:
|
||||||
if childNames.contains(ast.regex) or
|
if childNames.contains(ast.regex) or
|
||||||
|
|
@ -111,14 +111,14 @@ proc searchAstForNode(ast: ref Ast, node: TSNode, nimState: NimState): bool =
|
||||||
result = node.saveNodeData(nimState)
|
result = node.saveNodeData(nimState)
|
||||||
else:
|
else:
|
||||||
if nimState.gState.debug:
|
if nimState.gState.debug:
|
||||||
echo "#" & spaces(nimState.nodeBranch.len * 2) & &" {ast.getRegexForAstChildren()} !=~ {childNames}"
|
necho "#" & spaces(nimState.nodeBranch.len * 2) & &" {ast.getRegexForAstChildren()} !=~ {childNames}"
|
||||||
elif node.getTSNodeNamedChildCountSansComments() == 0:
|
elif node.getTSNodeNamedChildCountSansComments() == 0:
|
||||||
result = node.saveNodeData(nimState)
|
result = node.saveNodeData(nimState)
|
||||||
|
|
||||||
if nimState.gState.debug:
|
if nimState.gState.debug:
|
||||||
discard nimState.nodeBranch.pop()
|
discard nimState.nodeBranch.pop()
|
||||||
if nimstate.nodeBranch.Bl:
|
if nimstate.nodeBranch.Bl:
|
||||||
echo ""
|
necho ""
|
||||||
|
|
||||||
proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) =
|
proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) =
|
||||||
var
|
var
|
||||||
|
|
@ -133,7 +133,7 @@ proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) =
|
||||||
if name in astTable:
|
if name in astTable:
|
||||||
for ast in astTable[name]:
|
for ast in astTable[name]:
|
||||||
if nimState.gState.debug:
|
if nimState.gState.debug:
|
||||||
echo "\n# " & nimState.getNodeVal(node).replace("\n", "\n# ") & "\n"
|
necho "\n# " & nimState.getNodeVal(node).replace("\n", "\n# ") & "\n"
|
||||||
if searchAstForNode(ast, node, nimState):
|
if searchAstForNode(ast, node, nimState):
|
||||||
ast.tonim(ast, node, nimState)
|
ast.tonim(ast, node, nimState)
|
||||||
if nimState.gState.debug:
|
if nimState.gState.debug:
|
||||||
|
|
@ -166,8 +166,8 @@ proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) =
|
||||||
if node == root:
|
if node == root:
|
||||||
break
|
break
|
||||||
|
|
||||||
proc printNimHeader*() =
|
proc printNimHeader*(gState: State) =
|
||||||
echo """# Generated at $1
|
gecho """# Generated at $1
|
||||||
# Command line:
|
# Command line:
|
||||||
# $2 $3
|
# $2 $3
|
||||||
|
|
||||||
|
|
@ -194,32 +194,32 @@ proc printNim*(gState: State, fullpath: string, root: TSNode, astTable: AstTable
|
||||||
root.searchAst(astTable, nimState)
|
root.searchAst(astTable, nimState)
|
||||||
|
|
||||||
if nimState.enumStr.nBl:
|
if nimState.enumStr.nBl:
|
||||||
echo &"{nimState.enumStr}\n"
|
necho &"{nimState.enumStr}\n"
|
||||||
|
|
||||||
nimState.constStr = nimState.getOverrideFinal(nskConst) & nimState.constStr
|
nimState.constStr = nimState.getOverrideFinal(nskConst) & nimState.constStr
|
||||||
if nimState.constStr.nBl:
|
if nimState.constStr.nBl:
|
||||||
echo &"const{nimState.constStr}\n"
|
necho &"const{nimState.constStr}\n"
|
||||||
|
|
||||||
echo &"""
|
necho &"""
|
||||||
{{.pragma: {nimState.impShort}, importc{nimState.getHeader()}.}}
|
{{.pragma: {nimState.impShort}, importc{nimState.getHeader()}.}}
|
||||||
{{.pragma: {nimState.impShort}C, {nimState.impShort}, cdecl{nimState.getDynlib()}.}}
|
{{.pragma: {nimState.impShort}C, {nimState.impShort}, cdecl{nimState.getDynlib()}.}}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
nimState.typeStr = nimState.getOverrideFinal(nskType) & nimState.typeStr
|
nimState.typeStr = nimState.getOverrideFinal(nskType) & nimState.typeStr
|
||||||
if nimState.typeStr.nBl:
|
if nimState.typeStr.nBl:
|
||||||
echo &"type{nimState.typeStr}\n"
|
necho &"type{nimState.typeStr}\n"
|
||||||
|
|
||||||
nimState.procStr = nimState.getOverrideFinal(nskProc) & nimState.procStr
|
nimState.procStr = nimState.getOverrideFinal(nskProc) & nimState.procStr
|
||||||
if nimState.procStr.nBl:
|
if nimState.procStr.nBl:
|
||||||
echo &"{nimState.procStr}\n"
|
necho &"{nimState.procStr}\n"
|
||||||
|
|
||||||
if nimState.gState.debug:
|
if nimState.gState.debug:
|
||||||
if nimState.debugStr.nBl:
|
if nimState.debugStr.nBl:
|
||||||
echo nimState.debugStr
|
necho nimState.debugStr
|
||||||
|
|
||||||
if nimState.skipStr.nBl:
|
if nimState.skipStr.nBl:
|
||||||
let
|
let
|
||||||
hash = nimState.skipStr.hash().abs()
|
hash = nimState.skipStr.hash().abs()
|
||||||
sname = getTempDir() / &"nimterop_{$hash}.h"
|
sname = getTempDir() / &"nimterop_{$hash}.h"
|
||||||
echo &"# Writing skipped definitions to {sname}\n"
|
necho &"# Writing skipped definitions to {sname}\n"
|
||||||
writeFile(sname, nimState.skipStr)
|
writeFile(sname, nimState.skipStr)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import macros, os, strutils, tables, times
|
import macros, os, strutils, tables, times
|
||||||
|
|
||||||
import compiler/[ast, astalgo, idents, options, renderer]
|
import compiler/[ast, idents, options, renderer]
|
||||||
|
|
||||||
import "."/treesitter/api
|
import "."/treesitter/api
|
||||||
|
|
||||||
|
|
@ -806,8 +806,8 @@ proc searchTree(nimState: NimState, root: TSNode) =
|
||||||
if node == root:
|
if node == root:
|
||||||
break
|
break
|
||||||
|
|
||||||
proc printNimHeader*() =
|
proc printNimHeader*(gState: State) =
|
||||||
echo """# Generated at $1
|
gecho """# Generated at $1
|
||||||
# Command line:
|
# Command line:
|
||||||
# $2 $3
|
# $2 $3
|
||||||
|
|
||||||
|
|
@ -845,4 +845,4 @@ proc printNim*(gState: State, fullpath: string, root: TSNode) =
|
||||||
tree.add nimState.typeSection
|
tree.add nimState.typeSection
|
||||||
tree.add nimState.procSection
|
tree.add nimState.procSection
|
||||||
|
|
||||||
echo tree.renderTree()
|
gecho tree.renderTree()
|
||||||
|
|
|
||||||
|
|
@ -460,7 +460,7 @@ proc configure*(path, check: string, flags = "") =
|
||||||
echo "# Running autogen.sh"
|
echo "# Running autogen.sh"
|
||||||
|
|
||||||
echo execAction(
|
echo execAction(
|
||||||
&"cd {(path / i).parentDir().sanitizePath} && bash autogen.sh").output
|
&"cd {(path / i).parentDir().sanitizePath} && bash ./autogen.sh").output
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
@ -477,7 +477,7 @@ proc configure*(path, check: string, flags = "") =
|
||||||
echo "# Running configure " & flags
|
echo "# Running configure " & flags
|
||||||
|
|
||||||
var
|
var
|
||||||
cmd = &"cd {path.sanitizePath} && bash configure"
|
cmd = &"cd {path.sanitizePath} && bash ./configure"
|
||||||
if flags.len != 0:
|
if flags.len != 0:
|
||||||
cmd &= &" {flags}"
|
cmd &= &" {flags}"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -445,20 +445,16 @@ proc printTree*(nimState: NimState, pnode: PNode, offset = "") =
|
||||||
else:
|
else:
|
||||||
stdout.write ")"
|
stdout.write ")"
|
||||||
if offset.len == 0:
|
if offset.len == 0:
|
||||||
echo ""
|
necho ""
|
||||||
|
|
||||||
template decho*(str: untyped): untyped =
|
|
||||||
if nimState.gState.debug:
|
|
||||||
echo str.getCommented()
|
|
||||||
|
|
||||||
proc printDebug*(nimState: NimState, node: TSNode) =
|
proc printDebug*(nimState: NimState, node: TSNode) =
|
||||||
if nimState.gState.debug:
|
if nimState.gState.debug:
|
||||||
echo ("Input => " & nimState.getNodeVal(node)).getCommented()
|
necho ("Input => " & nimState.getNodeVal(node)).getCommented()
|
||||||
echo nimState.gState.printLisp(node).getCommented()
|
necho nimState.gState.printLisp(node).getCommented()
|
||||||
|
|
||||||
proc printDebug*(nimState: NimState, pnode: PNode) =
|
proc printDebug*(nimState: NimState, pnode: PNode) =
|
||||||
if nimState.gState.debug:
|
if nimState.gState.debug:
|
||||||
echo ("Output => " & $pnode).getCommented()
|
necho ("Output => " & $pnode).getCommented()
|
||||||
nimState.printTree(pnode)
|
nimState.printTree(pnode)
|
||||||
|
|
||||||
# Compiler shortcuts
|
# Compiler shortcuts
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,8 @@ type
|
||||||
onSymbol*, onSymbolOverride*: OnSymbol
|
onSymbol*, onSymbolOverride*: OnSymbol
|
||||||
onSymbolOverrideFinal*: OnSymbolOverrideFinal
|
onSymbolOverrideFinal*: OnSymbolOverrideFinal
|
||||||
|
|
||||||
|
outputHandle*: File
|
||||||
|
|
||||||
NimState {.used.} = ref object
|
NimState {.used.} = ref object
|
||||||
identifiers*: TableRef[string, string]
|
identifiers*: TableRef[string, string]
|
||||||
|
|
||||||
|
|
@ -83,3 +85,19 @@ const modeDefault {.used.} = $cpp # TODO: USE this everywhere relevant
|
||||||
when not declared(CIMPORT):
|
when not declared(CIMPORT):
|
||||||
export gAtoms, gExpressions, gEnumVals, State, NimState,
|
export gAtoms, gExpressions, gEnumVals, State, NimState,
|
||||||
nBl, Bl, CompileMode, modeDefault
|
nBl, Bl, CompileMode, modeDefault
|
||||||
|
|
||||||
|
# Redirect output to file when required
|
||||||
|
template gecho*(args: string) {.dirty.} =
|
||||||
|
if gState.outputHandle.isNil:
|
||||||
|
echo args
|
||||||
|
else:
|
||||||
|
gState.outputHandle.writeLine(args)
|
||||||
|
|
||||||
|
template necho*(args: string) {.dirty.} =
|
||||||
|
let gState = nimState.gState
|
||||||
|
gecho args
|
||||||
|
|
||||||
|
template decho*(str: untyped): untyped =
|
||||||
|
if nimState.gState.debug:
|
||||||
|
let gState = nimState.gState
|
||||||
|
necho str.getCommented()
|
||||||
|
|
|
||||||
|
|
@ -735,7 +735,7 @@ proc parseGrammar*(): AstTable =
|
||||||
else:
|
else:
|
||||||
result[n].add(ast)
|
result[n].add(ast)
|
||||||
|
|
||||||
proc printGrammar*(astTable: AstTable) =
|
proc printGrammar*(gState: State, astTable: AstTable) =
|
||||||
for name in astTable.keys():
|
for name in astTable.keys():
|
||||||
for ast in astTable[name]:
|
for ast in astTable[name]:
|
||||||
echo ast.printAst()
|
gecho ast.printAst()
|
||||||
|
|
|
||||||
|
|
@ -42,11 +42,11 @@ proc process(gState: State, path: string) =
|
||||||
tree.tsTreeDelete()
|
tree.tsTreeDelete()
|
||||||
|
|
||||||
if gState.past:
|
if gState.past:
|
||||||
echo gState.printLisp(root)
|
gecho gState.printLisp(root)
|
||||||
elif gState.pnim:
|
elif gState.pnim:
|
||||||
gState.printNim(path, root)
|
gState.printNim(path, root)
|
||||||
elif gState.preprocess:
|
elif gState.preprocess:
|
||||||
echo gState.code
|
gecho gState.code
|
||||||
|
|
||||||
# CLI processing with default values
|
# CLI processing with default values
|
||||||
proc main(
|
proc main(
|
||||||
|
|
@ -99,43 +99,34 @@ proc main(
|
||||||
if pluginSourcePath.nBl:
|
if pluginSourcePath.nBl:
|
||||||
gState.loadPlugin(pluginSourcePath)
|
gState.loadPlugin(pluginSourcePath)
|
||||||
|
|
||||||
# Backup stdout
|
|
||||||
var
|
var
|
||||||
outputFile = output
|
outputFile = output
|
||||||
outputHandle: File
|
|
||||||
stdoutBackup = stdout
|
|
||||||
check = check or stub
|
check = check or stub
|
||||||
|
|
||||||
# Fix output file extention
|
# Fix output file extention for Nim mode
|
||||||
if outputFile.len != 0:
|
if outputFile.len != 0 and pnim:
|
||||||
if outputFile.splitFile().ext != ".nim":
|
if outputFile.splitFile().ext != ".nim":
|
||||||
outputFile = outputFile & ".nim"
|
outputFile = outputFile & ".nim"
|
||||||
|
|
||||||
# Check needs a file
|
# Check needs a file
|
||||||
if check and outputFile.len == 0:
|
if check and outputFile.len == 0:
|
||||||
outputFile = getTempDir() / "toast_" & ($getTime().toUnix()).addFileExt("nim")
|
outputFile = getTempDir() / "toast_" & ($getTime().toUnix()).addFileExt("nim")
|
||||||
when defined(windows):
|
|
||||||
# https://github.com/nim-lang/Nim/issues/12939
|
|
||||||
echo &"Cannot print wrapper with check on Windows, review {outputFile}\n"
|
|
||||||
|
|
||||||
# Redirect output to file
|
# Redirect output to file
|
||||||
if outputFile.len != 0:
|
if outputFile.len != 0:
|
||||||
when defined(windows):
|
doAssert gState.outputHandle.open(outputFile, fmWrite),
|
||||||
doAssert stdout.reopen(outputFile, fmWrite), &"Failed to write to {outputFile}"
|
&"Failed to write to {outputFile}"
|
||||||
else:
|
|
||||||
doAssert outputHandle.open(outputFile, fmWrite), &"Failed to write to {outputFile}"
|
|
||||||
stdout = outputHandle
|
|
||||||
|
|
||||||
if source.nBl:
|
if source.nBl:
|
||||||
# Print source after preprocess or Nim output
|
# Print source after preprocess or Nim output
|
||||||
if gState.pnim:
|
if gState.pnim:
|
||||||
printNimHeader()
|
gState.printNimHeader()
|
||||||
for src in source:
|
for src in source:
|
||||||
gState.process(src.expandSymlinkAbs())
|
gState.process(src.expandSymlinkAbs())
|
||||||
|
|
||||||
when not defined(windows):
|
# Close outputFile
|
||||||
# Restore stdout
|
if outputFile.len != 0:
|
||||||
stdout = stdoutBackup
|
gState.outputHandle.close()
|
||||||
|
|
||||||
# Check Nim output
|
# Check Nim output
|
||||||
if gState.pnim and check:
|
if gState.pnim and check:
|
||||||
|
|
@ -145,12 +136,6 @@ proc main(
|
||||||
if err != 0:
|
if err != 0:
|
||||||
# Failed check so try stubbing
|
# Failed check so try stubbing
|
||||||
if stub:
|
if stub:
|
||||||
# Close output file to prepend stubs
|
|
||||||
when not defined(windows):
|
|
||||||
outputHandle.close()
|
|
||||||
else:
|
|
||||||
stdout.close()
|
|
||||||
|
|
||||||
# Find undeclared identifiers in error
|
# Find undeclared identifiers in error
|
||||||
var
|
var
|
||||||
data = ""
|
data = ""
|
||||||
|
|
@ -177,14 +162,13 @@ proc main(
|
||||||
|
|
||||||
# Rerun nim check on stubbed wrapper
|
# Rerun nim check on stubbed wrapper
|
||||||
(check, err) = execCmdEx(&"{gState.nim} check {outputFile}")
|
(check, err) = execCmdEx(&"{gState.nim} check {outputFile}")
|
||||||
doAssert err == 0, "# Nim check with stub failed:\n\n" & check
|
doAssert err == 0, data & "\n# Nim check with stub failed:\n\n" & check
|
||||||
else:
|
else:
|
||||||
doAssert err == 0, "# Nim check failed:\n\n" & check
|
doAssert err == 0, outputFile.readFile() & "\n# Nim check failed:\n\n" & check
|
||||||
|
|
||||||
when not defined(windows):
|
# Print wrapper if temporarily redirected to file
|
||||||
# Print wrapper if temporarily redirected to file
|
if check and output.len == 0:
|
||||||
if check and output.len == 0:
|
stdout.write outputFile.readFile()
|
||||||
stdout.write outputFile.readFile()
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
# Setup cligen command line help and short flags
|
# Setup cligen command line help and short flags
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue