improved expandimportc tool

This commit is contained in:
rumpf_a@web.de
2009-12-20 23:54:19 +01:00
parent f56816def0
commit 9dda24e475
6 changed files with 83 additions and 33 deletions

4
contributors.txt Normal file
View File

@@ -0,0 +1,4 @@
Mario Ray Mahardhika
Philippe Lhoste
Alexander R<>dseth
Jonathan Plona

View File

@@ -1774,13 +1774,11 @@ proc genConstSimpleList(p: BProc, n: PNode): PRope =
app(result, '}' & tnl)
proc genConstExpr(p: BProc, n: PNode): PRope =
var
cs: TBitSet
d: TLoc
case n.Kind
of nkHiddenStdConv, nkHiddenSubConv:
result = genConstExpr(p, n.sons[1])
of nkCurly:
var cs: TBitSet
toBitSet(n, cs)
result = genRawSetData(cs, int(getSize(n.typ)))
of nkBracket, nkPar:
@@ -1788,5 +1786,6 @@ proc genConstExpr(p: BProc, n: PNode): PRope =
result = genConstSimpleList(p, n)
else:
# result := genLiteral(p, n)
var d: TLoc
initLocExpr(p, n, d)
result = rdLoc(d)

View File

@@ -11,7 +11,7 @@
## the diverse wrappers.
import
os, ropes, idents, ast, pnimsyn, rnimsyn, msgs, wordrecg, syntaxes
os, ropes, idents, ast, pnimsyn, rnimsyn, msgs, wordrecg, syntaxes, pegs
proc modifyPragmas(n: PNode, name: string) =
if n == nil: return
@@ -36,17 +36,30 @@ proc processRoutine(n: PNode) =
var name = getName(n.sons[namePos])
modifyPragmas(n.sons[pragmasPos], name)
proc processTree(n: PNode) =
proc processIdent(ident, prefix: string, n: PNode): string =
var pattern = sequence(capture(?(termIgnoreCase"T" / termIgnoreCase"P")),
termIgnoreCase(prefix), capture(*any))
if ident =~ pattern:
result = matches[0] & matches[1]
else:
result = ident
proc processTree(n: PNode, prefix: string) =
if n == nil: return
case n.kind
of nkEmpty..nkNilLit: nil
of nkProcDef, nkConverterDef: processRoutine(n)
of nkEmpty..pred(nkIdent), succ(nkIdent)..nkNilLit: nil
of nkIdent:
if prefix.len > 0:
n.ident = getIdent(processIdent(n.ident.s, prefix, n))
of nkProcDef, nkConverterDef:
processRoutine(n)
for i in 0..sonsLen(n)-1: processTree(n.sons[i], prefix)
else:
for i in 0..sonsLen(n)-1: processTree(n.sons[i])
for i in 0..sonsLen(n)-1: processTree(n.sons[i], prefix)
proc main(infile, outfile: string) =
proc main(infile, outfile, prefix: string) =
var module = ParseFile(infile)
processTree(module)
processTree(module, prefix)
renderModule(module, outfile)
if paramcount() >= 1:
@@ -54,6 +67,9 @@ if paramcount() >= 1:
var outfile = changeFileExt(infile, "new.nim")
if paramCount() >= 2:
outfile = addFileExt(paramStr(2), "new.nim")
main(infile, outfile)
var prefix = ""
if paramCount() >= 3:
prefix = paramStr(3)
main(infile, outfile, prefix)
else:
echo "usage: expand_importc filename[.nim] outfilename[.nim]"
echo "usage: expand_importc filename[.nim] outfilename[.nim] [prefix]"

View File

@@ -35,38 +35,32 @@ proc getArg(n: PNode, name: string, pos: int): PNode =
return n.sons[i]
proc charArg(n: PNode, name: string, pos: int, default: Char): Char =
var x: PNode
x = getArg(n, name, pos)
var x = getArg(n, name, pos)
if x == nil: result = default
elif x.kind == nkCharLit: result = chr(int(x.intVal))
else: invalidPragma(n)
proc strArg(n: PNode, name: string, pos: int, default: string): string =
var x: PNode
x = getArg(n, name, pos)
var x = getArg(n, name, pos)
if x == nil: result = default
elif x.kind in {nkStrLit..nkTripleStrLit}: result = x.strVal
else: invalidPragma(n)
proc boolArg(n: PNode, name: string, pos: int, default: bool): bool =
var x: PNode
x = getArg(n, name, pos)
var x = getArg(n, name, pos)
if x == nil: result = default
elif (x.kind == nkIdent) and IdentEq(x.ident, "true"): result = true
elif (x.kind == nkIdent) and IdentEq(x.ident, "false"): result = false
else: invalidPragma(n)
proc filterStrip(stdin: PLLStream, filename: string, call: PNode): PLLStream =
var
line, pattern, stripped: string
leading, trailing: bool
pattern = strArg(call, "startswith", 1, "")
leading = boolArg(call, "leading", 2, true)
trailing = boolArg(call, "trailing", 3, true)
var pattern = strArg(call, "startswith", 1, "")
var leading = boolArg(call, "leading", 2, true)
var trailing = boolArg(call, "trailing", 3, true)
result = LLStreamOpen("")
while not LLStreamAtEnd(stdin):
line = LLStreamReadLine(stdin)
stripped = strip(line, leading, trailing)
var line = LLStreamReadLine(stdin)
var stripped = strip(line, leading, trailing)
if (len(pattern) == 0) or startsWith(stripped, pattern):
LLStreamWriteln(result, stripped)
else:
@@ -74,12 +68,11 @@ proc filterStrip(stdin: PLLStream, filename: string, call: PNode): PLLStream =
LLStreamClose(stdin)
proc filterReplace(stdin: PLLStream, filename: string, call: PNode): PLLStream =
var line, sub, by: string
sub = strArg(call, "sub", 1, "")
var sub = strArg(call, "sub", 1, "")
if len(sub) == 0: invalidPragma(call)
by = strArg(call, "by", 2, "")
var by = strArg(call, "by", 2, "")
result = LLStreamOpen("")
while not LLStreamAtEnd(stdin):
line = LLStreamReadLine(stdin)
var line = LLStreamReadLine(stdin)
LLStreamWriteln(result, replace(line, sub, by))
LLStreamClose(stdin)

View File

@@ -30,7 +30,7 @@ Files: "doc/*.html"
Files: "doc/*.cfg"
[Other]
Files: "readme.txt;install.txt"
Files: "readme.txt;install.txt;contributors.txt"
Files: "configure;makefile"
Files: "*.html"
Files: "*.ini"

View File

@@ -6,17 +6,55 @@ News
2009-XX-XX Version 0.8.6 released
=================================
Version 0.8.6 has been released! Get it `here <download.html>`_. The version
jump from 0.8.2 to 0.8.6 acknowledges the fact that all development of the
compiler is now done in Nimrod.
Bugfixes
--------
- The pragmas ``hint[X]:off`` and ``warning[X]:off`` now work.
- Method call syntax for iterators works again (``for x in lines.split()``).
- Many bugfixes concerning macro evaluation.
- Many bugfixes concerning compile-time evaluation.
- Fixed a typo in ``removeDir`` for POSIX that lead to an infinite recursion.
- The compiler now checks that module filenames are valid identifiers.
- Empty patterns for the ``dynlib`` pragma are now possible.
- ``os.parseCmdLine`` returned wrong results for trailing whitespace.
- Inconsequent tuple usage (using the same tuple with and without named fields)
does not crash the code generator anymore.
- A better error message is provided when the loading of a proc within a
dynamic lib fails.
Additions
---------
- Added ``system.contains`` for open arrays.
- The PEG module now supports the *search loop operator* ``@``.
- Grammar/parser: ``SAD|IND`` is allowed before any kind of closing bracket.
This allows for more flexible source code formating.
- The compiler now uses a *bind* table for symbol lookup within a ``bind``
context. (See `<manual/#templates>`_ for details.)
- ``discard """my long comment"""`` is now optimized away.
- New ``--floatChecks: on|off`` switches and pragmas for better debugging
of floating point operations. (See
`<manual/#pre-defined-floating-point-types>`_ for details.)
Changes affecting backwards compatibility
-----------------------------------------
- The compiler does not skip the linking step anymore even if no file
has changed.
- ``os.splitFile(".xyz")`` now returns ``("", ".xyz", "")`` instead of
``("", "", ".xyz")``. Filenames starting with a dot are handled
differently.
- ``strutils.split(s: string, seps: set[char])`` never yields the empty string
anymore. This behaviour is probably more appropriate for whitespace splitting.
- The compiler now stops after the ``--version`` command line switch.
- Removed support for enum inheritance in the parser; enum inheritance has
never been documented anyway.
- The ``msg`` field of ``system.E_base`` has now the type ``string``, instead
of ``cstring``. This improves memory safety.
2009-10-21 Version 0.8.2 released
@@ -144,7 +182,7 @@ Documentation
2009-06-08 Version 0.7.10 released
==================================
Nimrod version 0.7.10 has been released! Get it `here <./download.html>`_.
Nimrod version 0.7.10 has been released!
Bugfixes
--------