Reduce regex for perf, reduce globals for CT

This commit is contained in:
Ganesh Viswanathan 2020-05-18 16:39:18 -05:00
commit 6f96437f39
8 changed files with 128 additions and 115 deletions

View file

@ -30,15 +30,15 @@ proc execTest(test: string, flags = "", runDocs = true) =
mkDir docPath mkDir docPath
buildDocs(@[test], docPath, nimArgs = "--hints:off " & flags) buildDocs(@[test], docPath, nimArgs = "--hints:off " & flags)
task buildToast, "build toast":
execCmd("nim c --hints:off nimterop/toast.nim")
task buildTimeit, "build timer": task buildTimeit, "build timer":
exec "nim c --hints:off -d:danger tests/timeit" exec "nim c --hints:off -d:danger tests/timeit"
task bt, "build toast": task buildToast, "build toast":
execCmd("nim c --hints:off -d:danger nimterop/toast.nim") execCmd("nim c --hints:off -d:danger nimterop/toast.nim")
task bt, "build toast":
buildToastTask()
task btd, "build toast": task btd, "build toast":
execCmd("nim c -g nimterop/toast.nim") execCmd("nim c -g nimterop/toast.nim")

View file

@ -2,8 +2,6 @@ import hashes, macros, osproc, sets, strformat, strutils, tables
import os except findExe, sleep import os except findExe, sleep
import regex
type type
BuildType* = enum BuildType* = enum
btAutoconf, btCmake btAutoconf, btCmake
@ -995,7 +993,7 @@ macro getHeader*(header: static[string], giturl: static[string] = "", dlurl: sta
## prior to the build process. ## prior to the build process.
var var
origname = header.extractFilename().split(".")[0] origname = header.extractFilename().split(".")[0]
name = origname.replace(re"[[:^alnum:]]", "") name = origname.split(seps = AllChars-Letters-Digits).join()
# -d:xxx for this header # -d:xxx for this header
stdStr = name & "Std" stdStr = name & "Std"

View file

@ -17,8 +17,6 @@ All `{.compileTime.}` procs must be used in a compile time context, e.g. using:
import hashes, macros, os, strformat, strutils import hashes, macros, os, strformat, strutils
import regex
import "."/[build, globals, paths, types] import "."/[build, globals, paths, types]
export types export types
@ -684,7 +682,7 @@ macro c2nImport*(filename: static string, recurse: static bool = false, dynlib:
hash = output.hash().abs() hash = output.hash().abs()
hpath = getProjectCacheDir("c2nimCache", forceClean = false) / "nimterop_" & $hash & ".h" hpath = getProjectCacheDir("c2nimCache", forceClean = false) / "nimterop_" & $hash & ".h"
npath = hpath[0 .. hpath.rfind('.')] & "nim" npath = hpath[0 .. hpath.rfind('.')] & "nim"
header = ("header" & fullpath.splitFile().name.replace(re"[-.]+", "")) header = "header" & fullpath.splitFile().name.split(seps = {'-', '.'}).join()
if not fileExists(hpath) or gStateCT.nocache or compileOption("forceBuild"): if not fileExists(hpath) or gStateCT.nocache or compileOption("forceBuild"):
mkDir(hpath.parentDir()) mkDir(hpath.parentDir())
@ -715,10 +713,6 @@ macro c2nImport*(filename: static string, recurse: static bool = false, dynlib:
var var
nimout = &"const {header} = \"{fullpath}\"\n\n" & readFile(npath) nimout = &"const {header} = \"{fullpath}\"\n\n" & readFile(npath)
nimout = nimout.
replace(re"([u]?int[\d]+)_t", "$1").
replace(re"([u]?int)ptr_t", "ptr $1")
if gStateCT.debug: if gStateCT.debug:
echo nimout echo nimout

View file

@ -227,7 +227,7 @@ proc getOverride*(gState: State, name: string, kind: NimSymKind): string =
result = sym.override result = sym.override
if kind != nskProc: if kind != nskProc:
result = result.replace(re"(?m)^(.*?)$", " $1") result = " " & result.replace("\n", "\n ")
proc getOverrideFinal*(gState: State, kind: NimSymKind): string = proc getOverrideFinal*(gState: State, kind: NimSymKind): string =
# Get all unused cOverride symbols of `kind` # Get all unused cOverride symbols of `kind`

View file

@ -1,58 +1,19 @@
import sequtils, sets, tables, strutils import tables
import regex
import "."/plugin
when defined(TOAST): when defined(TOAST):
import sets, sequtils, strutils
import regex
import "."/plugin
import compiler/[ast, idents, modulegraphs, options] import compiler/[ast, idents, modulegraphs, options]
import "."/treesitter/api import "."/treesitter/api
const
gAtoms* {.used.} = @[
"field_identifier",
"identifier",
"number_literal",
"char_literal",
"preproc_arg",
"primitive_type",
"sized_type_specifier",
"type_identifier"
].toHashSet()
gExpressions* {.used.} = @[
"parenthesized_expression",
"bitwise_expression",
"shift_expression",
"math_expression",
"escape_sequence"
].toHashSet()
gEnumVals* {.used.} = @[
"identifier",
"number_literal",
"char_literal"
].concat(toSeq(gExpressions.items))
type type
Kind* = enum Feature* = enum
exactlyOne ast1, ast2
oneOrMore # +
zeroOrMore # *
zeroOrOne # ?
orWithNext # !
Ast* = object
name*: string
kind*: Kind
recursive*: bool
children*: seq[ref Ast]
when defined(TOAST):
tonim*: proc (ast: ref Ast, node: TSNode, gState: State)
regex*: Regex
AstTable* {.used.} = TableRef[string, seq[ref Ast]]
State* = ref object State* = ref object
# Command line arguments to toast - some forwarded from cimport.nim # Command line arguments to toast - some forwarded from cimport.nim
@ -79,13 +40,7 @@ type
typeMap*: TableRef[string, string] typeMap*: TableRef[string, string]
# `--typeMap | -T` to map instances of type X to Y - e.g. ABC=cint # `--typeMap | -T` to map instances of type X to Y - e.g. ABC=cint
# cimport.nim specific when defined(TOAST):
compile*: seq[string] # `cCompile()` list of files already processed
nocache*: bool # `cDisableCaching()` to disable caching of artifacts
overrides*: string # `cOverride()` code which gets added to `cPlugin()` output
pluginSource*: string # `cPlugin()` generated code to write to plugin file from
searchDirs*: seq[string] # `cSearchPath()` added directories for header search
# Data fields # Data fields
code*: string # Contents of header file currently being processed code*: string # Contents of header file currently being processed
currentHeader*: string # Const name of header being currently processed currentHeader*: string # Const name of header being currently processed
@ -102,8 +57,8 @@ type
identifiers*: TableRef[string, string] # Symbols that have been declared so far indexed by nimName identifiers*: TableRef[string, string] # Symbols that have been declared so far indexed by nimName
skippedSyms*: HashSet[string] # Symbols that have been skipped due to being unwrappable or skippedSyms*: HashSet[string] # Symbols that have been skipped due to being unwrappable or
# the user provided override is blank # the user provided override is blank
# Nim compiler objects # Nim compiler objects
when defined(TOAST):
constSection*, enumSection*, pragmaSection*, procSection*, typeSection*, varSection*: PNode constSection*, enumSection*, pragmaSection*, procSection*, typeSection*, varSection*: PNode
identCache*: IdentCache identCache*: IdentCache
config*: ConfigRef config*: ConfigRef
@ -123,11 +78,71 @@ type
commentStr*, debugStr*, skipStr*: string commentStr*, debugStr*, skipStr*: string
data*: seq[tuple[name, val: string]] data*: seq[tuple[name, val: string]]
nodeBranch*: seq[string] nodeBranch*: seq[string]
else:
# cimport.nim specific
compile*: seq[string] # `cCompile()` list of files already processed
nocache*: bool # `cDisableCaching()` to disable caching of artifacts
overrides*: string # `cOverride()` code which gets added to `cPlugin()` output
pluginSource*: string # `cPlugin()` generated code to write to plugin file from
searchDirs*: seq[string] # `cSearchPath()` added directories for header search
Feature* = enum when defined(TOAST):
ast1, ast2 const
gAtoms* {.used.} = @[
"field_identifier",
"identifier",
"number_literal",
"char_literal",
"preproc_arg",
"primitive_type",
"sized_type_specifier",
"type_identifier"
].toHashSet()
var gExpressions* {.used.} = @[
"parenthesized_expression",
"bitwise_expression",
"shift_expression",
"math_expression",
"escape_sequence"
].toHashSet()
gEnumVals* {.used.} = @[
"identifier",
"number_literal",
"char_literal"
].concat(toSeq(gExpressions.items))
type
Kind* = enum
exactlyOne
oneOrMore # +
zeroOrMore # *
zeroOrOne # ?
orWithNext # !
Ast* = object
name*: string
kind*: Kind
recursive*: bool
children*: seq[ref Ast]
tonim*: proc (ast: ref Ast, node: TSNode, gState: State)
regex*: Regex
AstTable* {.used.} = TableRef[string, seq[ref Ast]]
# Redirect output to file when required
template gecho*(args: string) =
if gState.outputHandle.isNil:
echo args
else:
gState.outputHandle.writeLine(args)
template decho*(args: varargs[string, `$`]): untyped =
if gState.debug:
gecho join(args, "").getCommented()
else:
var
gStateCT* {.compiletime, used.} = new(State) gStateCT* {.compiletime, used.} = new(State)
template nBl*(s: typed): untyped {.used.} = template nBl*(s: typed): untyped {.used.} =
@ -135,14 +150,3 @@ template nBl*(s: typed): untyped {.used.} =
template Bl*(s: typed): untyped {.used.} = template Bl*(s: typed): untyped {.used.} =
(s.len == 0) (s.len == 0)
# Redirect output to file when required
template gecho*(args: string) =
if gState.outputHandle.isNil:
echo args
else:
gState.outputHandle.writeLine(args)
template decho*(args: varargs[string, `$`]): untyped =
if gState.debug:
gecho join(args, "").getCommented()

View file

@ -4,8 +4,6 @@ import "."/treesitter/[api, c, cpp]
import "."/[ast, ast2, build, globals, getters, grammar, tshelp] import "."/[ast, ast2, build, globals, getters, grammar, tshelp]
{.passC: "-DNIMTEROP".}
proc process(gState: State, path: string, astTable: AstTable) = proc process(gState: State, path: string, astTable: AstTable) =
doAssert existsFile(path), &"Invalid path {path}" doAssert existsFile(path), &"Invalid path {path}"

View file

@ -79,6 +79,23 @@ proc getStartAtom*(node: TSNode): int =
else: else:
break break
proc getConstQualifier*(gState: State, node: TSNode): bool =
# Check if node siblings have type_qualifier = `const`
var
curr = node.tsNodePrevNamedSibling()
while not curr.isNil:
# Check previous siblings
if curr.getName() == "type_qualifier" and
gState.getNodeVal(curr) == "const":
return true
curr = curr.tsNodePrevNamedSibling()
# Check immediate next sibling
curr = node.tsNodePrevNamedSibling()
if curr.getName() == "type_qualifier" and
gState.getNodeVal(curr) == "const":
return true
proc getXCount*(node: TSNode, ntype: string, reverse = false): int = proc getXCount*(node: TSNode, ntype: string, reverse = false): int =
if not node.isNil: if not node.isNil:
# Get number of ntype nodes nested in tree # Get number of ntype nodes nested in tree

View file

@ -2,6 +2,8 @@ import macros, os, sets, strutils
import nimterop/[cimport] import nimterop/[cimport]
{.passC: "-DNIMTEROP".}
static: static:
# Skip casting on lower nim compilers because # Skip casting on lower nim compilers because
# the VM does not support it # the VM does not support it