'nimrod pretty' usable

This commit is contained in:
Araq
2013-07-31 20:35:04 +02:00
parent 805959378d
commit 731c6f9083
5 changed files with 55 additions and 63 deletions

View File

@@ -1,7 +1,7 @@
#
#
# The Nimrod Compiler
# (c) Copyright 2012 Andreas Rumpf
# (c) Copyright 2013 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
@@ -10,48 +10,35 @@
# This module handles the conditional symbols.
import
ast, astalgo, hashes, platform, strutils, idents
strtabs, platform, strutils, idents
var gSymbols*: TStrTable
# We need to use a PStringTable here as defined symbols are always guaranteed
# to be style insensitive. Otherwise hell would break lose.
var gSymbols: PStringTable
proc DefineSymbol*(symbol: string) =
var i = getIdent(symbol)
var sym = StrTableGet(gSymbols, i)
if sym == nil:
new(sym) # circumvent the ID mechanism
sym.kind = skConditional
sym.name = i
StrTableAdd(gSymbols, sym)
sym.position = 1
gSymbols[symbol] = "true"
proc UndefSymbol*(symbol: string) =
var sym = StrTableGet(gSymbols, getIdent(symbol))
if sym != nil: sym.position = 0
proc isDefined*(symbol: PIdent): bool =
var sym = StrTableGet(gSymbols, symbol)
result = sym != nil and sym.position == 1
gSymbols[symbol] = "false"
proc isDefined*(symbol: string): bool =
result = isDefined(getIdent(symbol))
if gSymbols.hasKey(symbol):
result = gSymbols[symbol] == "true"
proc isDefined*(symbol: PIdent): bool = isDefined(symbol.s)
iterator definedSymbolNames*: string =
var it: TTabIter
var s = InitTabIter(it, gSymbols)
while s != nil:
if s.position == 1: yield s.name.s
s = nextIter(it, gSymbols)
for key, val in pairs(gSymbols):
if val == "true": yield key
proc countDefinedSymbols*(): int =
var it: TTabIter
var s = InitTabIter(it, gSymbols)
result = 0
while s != nil:
if s.position == 1: inc(result)
s = nextIter(it, gSymbols)
for key, val in pairs(gSymbols):
if val == "true": inc(result)
proc InitDefines*() =
initStrTable(gSymbols)
gSymbols = newStringTable(modeStyleInsensitive)
DefineSymbol("nimrod") # 'nimrod' is always defined
# for bootstrapping purposes and old code:
DefineSymbol("nimhygiene")
@@ -66,7 +53,7 @@ proc InitDefines*() =
of cpuI386: DefineSymbol("x86")
of cpuIa64: DefineSymbol("itanium")
of cpuAmd64: DefineSymbol("x8664")
else: nil
else: discard
case targetOS
of osDOS:
DefineSymbol("msdos")
@@ -92,12 +79,10 @@ proc InitDefines*() =
DefineSymbol("macintosh")
DefineSymbol("unix")
DefineSymbol("posix")
else: nil
else: discard
DefineSymbol("cpu" & $cpu[targetCPU].bit)
DefineSymbol(normalize(endianToStr[cpu[targetCPU].endian]))
DefineSymbol(cpu[targetCPU].name)
DefineSymbol(platform.os[targetOS].name)
if platform.OS[targetOS].props.contains(ospLacksThreadVars):
DefineSymbol("emulatedthreadvars")

View File

@@ -11,8 +11,11 @@
## to convert Nimrod code into a consistent style.
import
strutils, os, options, ast, astalgo, msgs, ropes, idents, passes, pegs,
strutils, os, options, ast, astalgo, msgs, ropes, idents, passes,
intsets, strtabs
const
removeTP = false # when true, "nimrod pretty" converts TTyp to Typ.
type
TGen = object of TPassContext
@@ -43,10 +46,14 @@ proc loadFile(info: TLineInfo) =
proc overwriteFiles*() =
for i in 0 .. high(gSourceFiles):
if not gSourceFiles[i].dirty: continue
var f = open(gSourceFiles[i].fullpath.changeFileExt(".pretty.nim"), fmWrite)
for line in gSourceFiles[i].lines:
f.writeln(line)
f.close
let newFile = gSourceFiles[i].fullpath.changeFileExt(".pretty.nim")
try:
var f = open(newFile, fmWrite)
for line in gSourceFiles[i].lines:
f.writeln(line)
f.close
except EIO:
rawMessage(errCannotOpenFile, newFile)
proc beautifyName(s: string, k: TSymKind): string =
result = newStringOfCap(s.len)
@@ -54,8 +61,9 @@ proc beautifyName(s: string, k: TSymKind): string =
case k
of skType, skGenericParam:
# skip leading 'T'
if s[0] == 'T' and s[1] in {'A'..'Z'}:
i = 1
when removeTP:
if s[0] == 'T' and s[1] in {'A'..'Z'}:
i = 1
result.add toUpper(s[i])
of skConst, skEnumField:
# for 'const' we keep how it's spelt; either upper case or lower case:
@@ -138,10 +146,11 @@ proc processSym(c: PPassContext, n: PNode): PNode =
let last = first+identLen(line, first)-1
if last-first+1 != newName.len or differ(line, first, last, newName):
var x = line.subStr(0, first-1) & newName & line.substr(last+1)
# the WinAPI module is full of 'TX = X' which after the substitution
# becomes 'X = X'. We remove those lines:
if x.match(peg"\s* {\ident} \s* '=' \s* y$1 ('#' .*)?"):
x = ""
when removeTP:
# the WinAPI module is full of 'TX = X' which after the substitution
# becomes 'X = X'. We remove those lines:
if x.match(peg"\s* {\ident} \s* '=' \s* y$1 ('#' .*)?"):
x = ""
system.shallowCopy(gSourceFiles[n.info.fileIndex].lines[n.info.line-1], x)
gSourceFiles[n.info.fileIndex].dirty = true
@@ -156,14 +165,16 @@ proc myOpen(module: PSym): PPassContext =
result = g
if rules.isNil:
rules = newStringTable(modeStyleInsensitive)
let path = joinPath([getPrefixDir(), "config", "rename.rules.cfg"])
for line in lines(path):
if line.len > 0:
let colon = line.find(':')
if colon > 0:
rules[line.substr(0, colon-1)] = line.substr(colon+1)
else:
rules[line] = line
when removeTP:
# XXX activate when the T/P stuff is deprecated
let path = joinPath([getPrefixDir(), "config", "rename.rules.cfg"])
for line in lines(path):
if line.len > 0:
let colon = line.find(':')
if colon > 0:
rules[line.substr(0, colon-1)] = line.substr(colon+1)
else:
rules[line] = line
const prettyPass* = makePass(open = myOpen, process = processSym)

View File

@@ -45,14 +45,10 @@ proc addStmt(w: PRodWriter, n: PNode)
proc writeRod(w: PRodWriter)
proc getDefines(): string =
var it: TTabIter
var s = InitTabIter(it, gSymbols)
result = ""
while s != nil:
if s.position == 1:
if result.len != 0: add(result, " ")
add(result, s.name.s)
s = nextIter(it, gSymbols)
for d in definedSymbolNames():
if result.len != 0: add(result, " ")
add(result, d)
proc fileIdx(w: PRodWriter, filename: string): int =
for i in countup(0, high(w.files)):

View File

@@ -431,7 +431,7 @@ proc semConst(c: PContext, n: PNode): PNode =
var b = newNodeI(nkConstDef, a.info)
if importantComments(): b.comment = a.comment
addSon(b, newSymNode(v))
addSon(b, ast.emptyNode) # no type description
addSon(b, a.sons[1])
addSon(b, copyTree(def))
addSon(result, b)

View File

@@ -601,7 +601,7 @@ proc paramTypeClass(c: PContext, paramType: PType, procKind: TSymKind):
result.typ = newTypeS(tyExpr, c)
result.typ.sons = paramType.sons
of tyTypeDesc:
if tfInstantiated notin paramType.flags:
if tfInstantiated notin paramType.flags:
result.typ = newTypeS(tyTypeDesc, c)
result.typ.sons = paramType.sons
of tyDistinct:
@@ -719,8 +719,8 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
if skipTypes(typ, {tyGenericInst}).kind == tyEmpty: continue
for j in countup(0, length-3):
var arg = newSymG(skParam, a.sons[j], c)
var finalType = liftParamType(c, kind, genericParams, typ,
arg.name.s, arg.info).skipIntLit
var finalType = liftParamType(c, kind, genericParams, typ, arg.name.s,
a.sons[length-2].info).skipIntLit
arg.typ = finalType
arg.position = counter
arg.constraint = constraint