aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2018-12-26 21:24:05 -0600
committerGanesh Viswanathan <dev@genotrance.com>2018-12-26 21:24:05 -0600
commitc3744c0034d6b13081be26b13ed46014a9b0a036 (patch)
tree0428344f08bd44b7579e3b4a10c45b0bdff92439
parent9cddef831de594ebc9f72babcdfdf45a4fba87f5 (diff)
downloadnimterop-c3744c0034d6b13081be26b13ed46014a9b0a036.tar.gz
nimterop-c3744c0034d6b13081be26b13ed46014a9b0a036.zip
Enums as distinct int, always relative to project path, cmd /c for toast, escape reserved words
-rw-r--r--nimterop/ast.nim3
-rw-r--r--nimterop/cimport.nim19
-rw-r--r--nimterop/getters.nim25
-rw-r--r--nimterop/globals.nim4
-rw-r--r--nimterop/grammar.nim28
5 files changed, 57 insertions, 22 deletions
diff --git a/nimterop/ast.nim b/nimterop/ast.nim
index 105d543..dd05227 100644
--- a/nimterop/ast.nim
+++ b/nimterop/ast.nim
@@ -124,6 +124,9 @@ proc printNim*(fullpath: string, root: TSNode) =
root.searchAst()
+ if gStateRT.enumStr.nBl:
+ echo gStateRT.enumStr
+
if gStateRT.constStr.nBl:
echo "const\n" & gStateRT.constStr
diff --git a/nimterop/cimport.nim b/nimterop/cimport.nim
index d671ee4..dd51166 100644
--- a/nimterop/cimport.nim
+++ b/nimterop/cimport.nim
@@ -13,20 +13,19 @@ proc joinPathIfRel(path1: string, path2: string): string =
result = joinPath(path1, path2)
proc findPath(path: string, fail = true): string =
- # As is
- result = path.replace("\\", "/")
+ # Relative to project path
+ result = joinPathIfRel(getProjectPath(), path).replace("\\", "/")
if not fileExists(result) and not dirExists(result):
- # Relative to project path
- result = joinPathIfRel(getProjectPath(), path).replace("\\", "/")
- if not fileExists(result) and not dirExists(result):
- if fail:
- doAssert false, "File or directory not found: " & path
- else:
- return ""
+ if fail:
+ doAssert false, "File or directory not found: " & path
+ else:
+ return ""
proc getToast(fullpath: string): string =
var
- cmd = "toast --pnim --preprocess "
+ cmd = when defined(Windows): "cmd /c " else: ""
+
+ cmd &= "toast --pnim --preprocess "
for i in gStateCT.defines:
cmd.add &"--defines+={i.quoteShell} "
diff --git a/nimterop/getters.nim b/nimterop/getters.nim
index 28e1bd1..7ce16bd 100644
--- a/nimterop/getters.nim
+++ b/nimterop/getters.nim
@@ -6,12 +6,37 @@ import treesitter/runtime
import git, globals
+const gReserved = """
+addr and as asm
+bind block break
+case cast concept const continue converter
+defer discard distinct div do
+elif else end enum except export
+finally for from func
+if import in include interface is isnot iterator
+let
+macro method mixin mod
+nil not notin
+of or out
+proc ptr
+raise ref return
+shl shr static
+template try tuple type
+using
+var
+when while
+xor
+yield""".split(Whitespace)
+
proc sanitizePath*(path: string): string =
path.multiReplace([("\\\\", $DirSep), ("\\", $DirSep), ("//", $DirSep)])
proc getIdentifier*(str: string): string =
result = str.strip(chars={'_'})
+ if result in gReserved:
+ result = &"`{result}`"
+
proc getUniqueIdentifier*(exists: seq[string], prefix = ""): string =
var
name = prefix & "_" & gStateRT.sourceFile.extractFilename().multiReplace([(".", ""), ("-", "")])
diff --git a/nimterop/globals.nim b/nimterop/globals.nim
index 9ad2b99..771987e 100644
--- a/nimterop/globals.nim
+++ b/nimterop/globals.nim
@@ -23,9 +23,9 @@ type
debug*, past*, preprocess*, pnim*, pretty*: bool
- consts*, procs*, types*: seq[string]
+ consts*, enums*, procs*, types*: seq[string]
- code*, constStr*, currentHeader*, mode*, procStr*, typeStr*: string
+ code*, constStr*, currentHeader*, enumStr*, mode*, procStr*, typeStr*: string
sourceFile*: string # eg, C or C++ source or header file
ast*: Table[string, seq[ref Ast]]
diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim
index 26ce7f0..3da4cc4 100644
--- a/nimterop/grammar.nim
+++ b/nimterop/grammar.nim
@@ -176,24 +176,32 @@ proc initGrammar() =
nname = name.getIdentifier()
if nname.len == 0:
- nname = getUniqueIdentifier(gStateRT.types, "Enum")
+ nname = getUniqueIdentifier(gStateRT.enums, "Enum")
- if nname notin gStateRT.types:
- gStateRT.types.add(nname)
- gStateRT.typeStr &= &" {nname}* = enum\n"
+ if nname notin gStateRT.enums:
+ gStateRT.enums.add(nname)
+ gStateRT.enumStr &= &"\ntype {nname}* = distinct int"
+ gStateRT.enumStr &= &"\nconverter enumToInt(en: {nname}): int = en.int\n"
var
i = fstart
+ count = 0
while i < gStateRT.data.len-fend:
let
fname = gStateRT.data[i].val.getIdentifier()
- if i+1 < gStateRT.data.len-fend and gStateRT.data[i+1].name in ["math_expression", "number_literal"]:
- gStateRT.typeStr &= &" {fname} = {gStateRT.data[i+1].val}\n"
- i += 2
- else:
- gStateRT.typeStr &= &" {fname}\n"
- i += 1
+ if fname notin gStateRT.consts:
+ if i+1 < gStateRT.data.len-fend and gStateRT.data[i+1].name in ["math_expression", "number_literal"]:
+ gStateRT.constStr &= &" {fname} = {gStateRT.data[i+1].val}.{nname}\n"
+ try:
+ count = gStateRT.data[i+1].val.parseInt() + 1
+ except:
+ count += 1
+ i += 2
+ else:
+ gStateRT.constStr &= &" {fname} = {count}.{nname}\n"
+ i += 1
+ count += 1
# enum X {}
gStateRT.grammar.add(("""