Enums as distinct int, always relative to project path, cmd /c for toast, escape reserved words
This commit is contained in:
parent
9cddef831d
commit
c3744c0034
5 changed files with 57 additions and 22 deletions
|
|
@ -124,6 +124,9 @@ proc printNim*(fullpath: string, root: TSNode) =
|
||||||
|
|
||||||
root.searchAst()
|
root.searchAst()
|
||||||
|
|
||||||
|
if gStateRT.enumStr.nBl:
|
||||||
|
echo gStateRT.enumStr
|
||||||
|
|
||||||
if gStateRT.constStr.nBl:
|
if gStateRT.constStr.nBl:
|
||||||
echo "const\n" & gStateRT.constStr
|
echo "const\n" & gStateRT.constStr
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,20 +13,19 @@ proc joinPathIfRel(path1: string, path2: string): string =
|
||||||
result = joinPath(path1, path2)
|
result = joinPath(path1, path2)
|
||||||
|
|
||||||
proc findPath(path: string, fail = true): string =
|
proc findPath(path: string, fail = true): string =
|
||||||
# As is
|
# Relative to project path
|
||||||
result = path.replace("\\", "/")
|
result = joinPathIfRel(getProjectPath(), path).replace("\\", "/")
|
||||||
if not fileExists(result) and not dirExists(result):
|
if not fileExists(result) and not dirExists(result):
|
||||||
# Relative to project path
|
if fail:
|
||||||
result = joinPathIfRel(getProjectPath(), path).replace("\\", "/")
|
doAssert false, "File or directory not found: " & path
|
||||||
if not fileExists(result) and not dirExists(result):
|
else:
|
||||||
if fail:
|
return ""
|
||||||
doAssert false, "File or directory not found: " & path
|
|
||||||
else:
|
|
||||||
return ""
|
|
||||||
|
|
||||||
proc getToast(fullpath: string): string =
|
proc getToast(fullpath: string): string =
|
||||||
var
|
var
|
||||||
cmd = "toast --pnim --preprocess "
|
cmd = when defined(Windows): "cmd /c " else: ""
|
||||||
|
|
||||||
|
cmd &= "toast --pnim --preprocess "
|
||||||
|
|
||||||
for i in gStateCT.defines:
|
for i in gStateCT.defines:
|
||||||
cmd.add &"--defines+={i.quoteShell} "
|
cmd.add &"--defines+={i.quoteShell} "
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,37 @@ import treesitter/runtime
|
||||||
|
|
||||||
import git, globals
|
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 =
|
proc sanitizePath*(path: string): string =
|
||||||
path.multiReplace([("\\\\", $DirSep), ("\\", $DirSep), ("//", $DirSep)])
|
path.multiReplace([("\\\\", $DirSep), ("\\", $DirSep), ("//", $DirSep)])
|
||||||
|
|
||||||
proc getIdentifier*(str: string): string =
|
proc getIdentifier*(str: string): string =
|
||||||
result = str.strip(chars={'_'})
|
result = str.strip(chars={'_'})
|
||||||
|
|
||||||
|
if result in gReserved:
|
||||||
|
result = &"`{result}`"
|
||||||
|
|
||||||
proc getUniqueIdentifier*(exists: seq[string], prefix = ""): string =
|
proc getUniqueIdentifier*(exists: seq[string], prefix = ""): string =
|
||||||
var
|
var
|
||||||
name = prefix & "_" & gStateRT.sourceFile.extractFilename().multiReplace([(".", ""), ("-", "")])
|
name = prefix & "_" & gStateRT.sourceFile.extractFilename().multiReplace([(".", ""), ("-", "")])
|
||||||
|
|
|
||||||
|
|
@ -23,9 +23,9 @@ type
|
||||||
|
|
||||||
debug*, past*, preprocess*, pnim*, pretty*: bool
|
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
|
sourceFile*: string # eg, C or C++ source or header file
|
||||||
|
|
||||||
ast*: Table[string, seq[ref Ast]]
|
ast*: Table[string, seq[ref Ast]]
|
||||||
|
|
|
||||||
|
|
@ -176,24 +176,32 @@ proc initGrammar() =
|
||||||
nname = name.getIdentifier()
|
nname = name.getIdentifier()
|
||||||
|
|
||||||
if nname.len == 0:
|
if nname.len == 0:
|
||||||
nname = getUniqueIdentifier(gStateRT.types, "Enum")
|
nname = getUniqueIdentifier(gStateRT.enums, "Enum")
|
||||||
|
|
||||||
if nname notin gStateRT.types:
|
if nname notin gStateRT.enums:
|
||||||
gStateRT.types.add(nname)
|
gStateRT.enums.add(nname)
|
||||||
gStateRT.typeStr &= &" {nname}* = enum\n"
|
gStateRT.enumStr &= &"\ntype {nname}* = distinct int"
|
||||||
|
gStateRT.enumStr &= &"\nconverter enumToInt(en: {nname}): int = en.int\n"
|
||||||
|
|
||||||
var
|
var
|
||||||
i = fstart
|
i = fstart
|
||||||
|
count = 0
|
||||||
while i < gStateRT.data.len-fend:
|
while i < gStateRT.data.len-fend:
|
||||||
let
|
let
|
||||||
fname = gStateRT.data[i].val.getIdentifier()
|
fname = gStateRT.data[i].val.getIdentifier()
|
||||||
|
|
||||||
if i+1 < gStateRT.data.len-fend and gStateRT.data[i+1].name in ["math_expression", "number_literal"]:
|
if fname notin gStateRT.consts:
|
||||||
gStateRT.typeStr &= &" {fname} = {gStateRT.data[i+1].val}\n"
|
if i+1 < gStateRT.data.len-fend and gStateRT.data[i+1].name in ["math_expression", "number_literal"]:
|
||||||
i += 2
|
gStateRT.constStr &= &" {fname} = {gStateRT.data[i+1].val}.{nname}\n"
|
||||||
else:
|
try:
|
||||||
gStateRT.typeStr &= &" {fname}\n"
|
count = gStateRT.data[i+1].val.parseInt() + 1
|
||||||
i += 1
|
except:
|
||||||
|
count += 1
|
||||||
|
i += 2
|
||||||
|
else:
|
||||||
|
gStateRT.constStr &= &" {fname} = {count}.{nname}\n"
|
||||||
|
i += 1
|
||||||
|
count += 1
|
||||||
|
|
||||||
# enum X {}
|
# enum X {}
|
||||||
gStateRT.grammar.add(("""
|
gStateRT.grammar.add(("""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue