Enums as distinct int, always relative to project path, cmd /c for toast, escape reserved words

This commit is contained in:
Ganesh Viswanathan 2018-12-26 21:24:05 -06:00
commit c3744c0034
5 changed files with 57 additions and 22 deletions

View file

@ -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

View file

@ -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} "

View file

@ -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([(".", ""), ("-", "")])

View file

@ -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]]

View file

@ -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(("""