Skip to content

Commit

Permalink
bugfixes: macros; splitFile; strutils.split; iterator.method
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Oct 27, 2009
1 parent c36bdec commit c5222a2
Show file tree
Hide file tree
Showing 31 changed files with 177 additions and 95 deletions.
1 change: 1 addition & 0 deletions data/magic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
'StrToIdent',
'IdentToStr',
'EqIdent',
'EqNimrodNode',
'NHint',
'NWarning',
'NError'
Expand Down
2 changes: 1 addition & 1 deletion data/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
{'errXNeedsReturnType': '$1 needs a return type'},
{'errInvalidCommandX': "invalid command: '$1'"},
{'errXOnlyAtModuleScope': "'$1' is only allowed at top level"},
{'errTemplateInstantiationTooNested': 'template instantiation too nested'},
{'errTemplateInstantiationTooNested': 'template/macro instantiation too nested'},
{'errInstantiationFrom': 'instantiation from here'},
{'errInvalidIndexValueForTuple': 'invalid index value for tuple subscript'},
{'errCommandExpectsFilename': 'command expects a filename argument'},
Expand Down
2 changes: 1 addition & 1 deletion doc/intern.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

.. contents::

Abstraction is layering ignorance on top of reality. -- unknown
"Abstraction is layering ignorance on top of reality." -- unknown


Directory structure
Expand Down
2 changes: 1 addition & 1 deletion doc/lib.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Nimrod Standard Library

..

The good thing about reinventing the wheel is that you can get a round one.
"The good thing about reinventing the wheel is that you can get a round one."

Though the Nimrod Standard Library is still evolving, it is already quite
usable. It is divided into *pure libraries*, *impure libraries* and *wrappers*.
Expand Down
4 changes: 2 additions & 2 deletions doc/manual.txt
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ An example:
nkIf # an if statement
PNode = ref TNode
TNode = object
case kind: TNodeKind # the ``kind`` field is the discriminator
case kind: TNodeKind # the ``kind`` field is the discriminant
of nkInt: intVal: int
of nkFloat: floavVal: float
of nkString: strVal: string
Expand Down Expand Up @@ -1648,7 +1648,7 @@ required. ``Elif`` parts are also allowed (but unlikely to be good
style).


Type convertions
Type conversions
~~~~~~~~~~~~~~~~
Syntactically a `type conversion` is like a procedure call, but a
type name replaces the procedure name. A type conversion is always
Expand Down
2 changes: 1 addition & 1 deletion doc/nimrodc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
:Version: |nimrodversion|

.. contents::

Introduction
============

Expand Down
8 changes: 6 additions & 2 deletions doc/tut1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ type:
var
x = 0.0 # x is of type ``float``
y = 0.0'f32 # y is of type ``float32``
z = 0.0'f64 # z is of type ``int64``
z = 0.0'f64 # z is of type ``float64``

The common operators ``+ - * / < <= == != > >=`` are defined for
floats and follow the IEEE standard.
Expand Down Expand Up @@ -1318,7 +1318,7 @@ This is best illustrated by an example:

main()


.. code-block:: nimrod
# Module B
import A # A is not parsed here! Only the already known symbols
# of A are imported.
Expand All @@ -1338,9 +1338,11 @@ imported by a third one:
# Module A
var x*: string

.. code-block:: nimrod
# Module B
var x*: int

.. code-block:: nimrod
# Module C
import A, B
write(stdout, x) # error: x is ambiguous
Expand All @@ -1357,9 +1359,11 @@ rules apply:
# Module A
proc x*(a: int): string = return $a

.. code-block:: nimrod
# Module B
proc x*(a: string): string = return $a

.. code-block:: nimrod
# Module C
import A, B
write(stdout, x(3)) # no error: A.x is called
Expand Down
13 changes: 11 additions & 2 deletions koch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# --------------------- constants ----------------------------------------

NIMROD_VERSION = '0.8.2'
NIMROD_VERSION = '0.8.3'
# This string contains Nimrod's version. It is the only place
# where the version needs to be updated. The rest is done by
# the build process automatically. It is replaced **everywhere**!
Expand Down Expand Up @@ -362,7 +362,7 @@ def cmd_rod(options):
if Exists(ExeExt("bin/nimrod")):
c.success()

# ------------------- constants -----------------------------------------------
# -----------------------------------------------------------------------------

HELP = Subs("""\
+-----------------------------------------------------------------+
Expand Down Expand Up @@ -426,6 +426,7 @@ def main(args):
elif cmd == "inno": cmd_inno()
elif cmd == "csource": cmd_csource(join(args[i+1:]))
elif cmd == "install": cmd_install() # for backwards compability
#elif cmd == "llvmdebug": cmd_llvm(debug=true)
else: Error("illegal command: " + cmd)

def cmd_csource(args):
Expand All @@ -443,6 +444,14 @@ def cmd_inno():

def cmd_install():
Exec("sh ./build.sh")

def cmd_llvm(debug=true):
if not debug: release = "--enable-optimized"
else: release = ""
Exec(Subs("./configure --enable-bindings $1 --enable-shared" +
" --enable-targets=host", release))
Exec("make")
Echo("Type [sudo] make install!")

# -------------------------- bootstrap ----------------------------------------

Expand Down
5 changes: 4 additions & 1 deletion lib/pure/macros.nim
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ proc `!` *(s: string): TNimrodIdent {.magic: "StrToIdent".}
proc `$`*(i: TNimrodIdent): string {.magic: "IdentToStr".}
## converts a Nimrod identifier to a string
proc `==`* (a, b: TNimrodIdent): bool {.magic: "EqIdent".}
proc `==`* (a, b: TNimrodIdent): bool {.magic: "EqIdent", noSideEffect.}
## compares two Nimrod identifiers
proc `==`* (a, b: PNimrodNode): bool {.magic: "EqNimrodNode", noSideEffect.}
## compares two Nimrod nodes
proc len*(n: PNimrodNode): int {.magic: "NLen".}
## returns the number of children of `n`.
Expand Down
30 changes: 17 additions & 13 deletions lib/pure/os.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
## This module contains basic operating system facilities like
## retrieving environment variables, reading command line arguments,
## working with directories, running shell commands, etc.
## This module is -- like any other basic library -- platform independant.
{.deadCodeElim: on.}

{.push debugger: off.}
Expand Down Expand Up @@ -384,10 +383,13 @@ proc SplitPath*(path: string): tuple[head, tail: string] {.noSideEffect.} =
## Splits a directory into (head, tail), so that
## ``JoinPath(head, tail) == path``.
##
## Example: After ``SplitPath("usr/local/bin", head, tail)``,
## `head` is "usr/local" and `tail` is "bin".
## Example: After ``SplitPath("usr/local/bin/", head, tail)``,
## `head` is "usr/local/bin" and `tail` is "".
## Examples:
## .. code-block:: nimrod
## SplitPath("usr/local/bin") -> ("usr/local", "bin")
## SplitPath("usr/local/bin/") -> ("usr/local/bin", "")
## SplitPath("bin") -> ("", "bin")
## SplitPath("/bin") -> ("", "bin")
## SplitPath("") -> ("", "")
var
sepPos = -1
for i in countdown(len(path)-1, 0):
Expand Down Expand Up @@ -431,8 +433,9 @@ proc normExt(ext: string): string =
else: result = extSep & ext

proc searchExtPos(s: string): int =
# BUGFIX: do not search until 0! .DS_Store is no file extension!
result = -1
for i in countdown(len(s)-1, 0):
for i in countdown(len(s)-1, 1):
if s[i] == extsep:
result = i
break
Expand All @@ -447,10 +450,11 @@ proc splitFile*(path: string): tuple[dir, name, ext: string] {.noSideEffect.} =
## Example:
##
## .. code-block:: nimrod
## var (dir, name, ext) = splitFile("usr/local/nimrodc.html")
## assert dir == "usr/local"
## assert name == "nimrodc"
## assert ext == ".html"
## var (dir, name, ext) = splitFile("usr/local/nimrodc.html")
## assert dir == "usr/local"
## assert name == "nimrodc"
## assert ext == ".html"
##
## If `path` has no extension, `ext` is the empty string.
## If `path` has no directory component, `dir` is the empty string.
## If `path` has no filename component, `name` and `ext` are empty strings.
Expand All @@ -461,7 +465,7 @@ proc splitFile*(path: string): tuple[dir, name, ext: string] {.noSideEffect.} =
var dotPos = path.len
for i in countdown(len(path)-1, 0):
if path[i] == ExtSep:
if dotPos == path.len: dotPos = i
if dotPos == path.len and i > 0: dotPos = i
elif path[i] in {dirsep, altsep}:
sepPos = i
break
Expand Down Expand Up @@ -877,9 +881,9 @@ iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string =
## filter meaning
## --------------------- ---------------------------------------------
## ``pcFile`` yield real files
## ``pcLinkToFile`` yield symbol links to files
## ``pcLinkToFile`` yield symbolic links to files
## ``pcDir`` follow real directories
## ``pcLinkToDir`` follow symbol links to directories
## ``pcLinkToDir`` follow symbolic links to directories
## --------------------- ---------------------------------------------
##
var stack = @[dir]
Expand Down
9 changes: 4 additions & 5 deletions lib/pure/strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,14 @@ iterator split*(s: string, seps: set[char] = Whitespace): string =
## writeln(stdout, word)
##
## produces the same output.
var
first: int = 0
last: int = 0
var last = 0
assert(not ('\0' in seps))
while last < len(s):
while s[last] in seps: inc(last)
first = last
var first = last
while last < len(s) and s[last] not_in seps: inc(last) # BUGFIX!
yield copy(s, first, last-1)
if first <= last-1:
yield copy(s, first, last-1)

iterator split*(s: string, sep: char): string =
## Splits the string `s` into substrings.
Expand Down
6 changes: 4 additions & 2 deletions nim/ast.pas
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ interface
mNAddMultiple, mNDel, mNKind, mNIntVal, mNFloatVal, mNSymbol,
mNIdent, mNGetType, mNStrVal, mNSetIntVal, mNSetFloatVal, mNSetSymbol,
mNSetIdent, mNSetType, mNSetStrVal, mNNewNimNode, mNCopyNimNode, mNCopyNimTree,
mStrToIdent, mIdentToStr, mEqIdent, mNHint, mNWarning, mNError
mStrToIdent, mIdentToStr, mEqIdent, mEqNimrodNode, mNHint, mNWarning,
mNError
//[[[end]]]
);

Expand Down Expand Up @@ -521,7 +522,8 @@ TObjectSet = record
'NAddMultiple', 'NDel', 'NKind', 'NIntVal', 'NFloatVal', 'NSymbol',
'NIdent', 'NGetType', 'NStrVal', 'NSetIntVal', 'NSetFloatVal', 'NSetSymbol',
'NSetIdent', 'NSetType', 'NSetStrVal', 'NNewNimNode', 'NCopyNimNode', 'NCopyNimTree',
'StrToIdent', 'IdentToStr', 'EqIdent', 'NHint', 'NWarning', 'NError'
'StrToIdent', 'IdentToStr', 'EqIdent', 'EqNimrodNode', 'NHint', 'NWarning',
'NError'
//[[[end]]]
);

Expand Down
2 changes: 1 addition & 1 deletion nim/cgen.pas
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ implementation
cfsProcs, // section for C procs that are not inline
cfsTypeInit1, // section 1 for declarations of type information
cfsTypeInit2, // section 2 for initialization of type information
cfsTypeInit3, // section 3 for init of type information
cfsTypeInit3, // section 3 for initialization of type information
cfsDebugInit, // section for initialization of debug information
cfsDynLibInit, // section for initialization of dynamic library binding
cfsDynLibDeinit // section for deinitialization of dynamic libraries
Expand Down
4 changes: 2 additions & 2 deletions nim/commands.pas
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ function processPath(const path: string): string;

procedure processCompile(const filename: string);
var
found, trunc, ext: string;
found, trunc: string;
begin
found := findFile(filename);
if found = '' then found := filename;
splitFilename(found, trunc, ext);
trunc := changeFileExt(found, '');
extccomp.addExternalFileToCompile(trunc);
extccomp.addFileToLink(completeCFilePath(trunc, false));
end;
Expand Down
6 changes: 3 additions & 3 deletions nim/depends.pas
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
//
// The Nimrod Compiler
// (c) Copyright 2008 Andreas Rumpf
// (c) Copyright 2009 Andreas Rumpf
//
// See the file "copying.txt", included in this
// distribution, for details about the copyright.
Expand Down Expand Up @@ -51,12 +51,12 @@ function addDotDependency(c: PPassContext; n: PNode): PNode;
case n.kind of
nkImportStmt: begin
for i := 0 to sonsLen(n)-1 do begin
imported := extractFileTrunk(getModuleFile(n.sons[i]));
imported := splitFile(getModuleFile(n.sons[i])).name;
addDependencyAux(g.module.name.s, imported);
end
end;
nkFromStmt: begin
imported := extractFileTrunk(getModuleFile(n.sons[0]));
imported := splitFile(getModuleFile(n.sons[0])).name;
addDependencyAux(g.module.name.s, imported);
end;
nkStmtList, nkBlockStmt, nkStmtListExpr, nkBlockExpr: begin
Expand Down
27 changes: 22 additions & 5 deletions nim/evals.pas
Original file line number Diff line number Diff line change
Expand Up @@ -994,9 +994,11 @@ function evalMagicOrCall(c: PEvalContext; n: PNode): PNode;
result := evalAux(c, n.sons[2]);
if result.kind = nkExceptBranch then exit;
k := getOrdValue(result);
if (k >= 0) and (k < sonsLen(a))
and not (a.kind in [nkEmpty..nkNilLit]) then
result := a.sons[int(k)]
if not (a.kind in [nkEmpty..nkNilLit]) and (k >= 0)
and (k < sonsLen(a)) then begin
result := a.sons[int(k)];
if result = nil then result := newNode(nkEmpty)
end
else begin
stackTrace(c, n, errIndexOutOfBounds);
result := emptyNode
Expand All @@ -1013,8 +1015,10 @@ function evalMagicOrCall(c: PEvalContext; n: PNode): PNode;
if result.kind = nkExceptBranch then exit;
k := getOrdValue(b);
if (k >= 0) and (k < sonsLen(a))
and not (a.kind in [nkEmpty..nkNilLit]) then
a.sons[int(k)] := result
and not (a.kind in [nkEmpty..nkNilLit]) then begin
if result.kind = nkEmpty then a.sons[int(k)] := nil
else a.sons[int(k)] := result
end
else
stackTrace(c, n, errIndexOutOfBounds);
result := emptyNode;
Expand Down Expand Up @@ -1205,6 +1209,19 @@ function evalMagicOrCall(c: PEvalContext; n: PNode): PNode;
if (a.kind = nkIdent) and (b.kind = nkIdent) then
if a.ident.id = b.ident.id then result.intVal := 1
end;
mEqNimrodNode: begin
result := evalAux(c, n.sons[1]);
if result.kind = nkExceptBranch then exit;
a := result;
result := evalAux(c, n.sons[2]);
if result.kind = nkExceptBranch then exit;
b := result;
result := newNodeIT(nkIntLit, n.info, n.typ);
if (a = b)
or (b.kind in [nkNilLit, nkEmpty])
and (a.kind in [nkNilLit, nkEmpty]) then
result.intVal := 1
end;
mNHint: begin
result := evalAux(c, n.sons[1]);
if result.kind = nkExceptBranch then exit;
Expand Down
8 changes: 4 additions & 4 deletions nim/extccomp.pas
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ function getCompileCFileCmd(const cfilename: string;
begin
c := ccompiler;
options := compileOptions;
trunk := extractFileTrunk(cfilename);
trunk := splitFile(cfilename).name;
if optCDebug in gGlobalOptions then begin
key := trunk + '.debug';
if existsConfigVar(key) then
Expand Down Expand Up @@ -594,15 +594,15 @@ procedure CallCCompiler(const projectfile: string);

if optGenDynLib in gGlobalOptions then begin
exefile := format(platform.os[targetOS].dllFrmt,
[extractFileTrunk(projectFile)]);
[splitFile(projectFile).name]);
buildDll := cc[c].buildDll;
end
else begin
exefile := extractFileTrunk(projectFile) +{&} platform.os[targetOS].exeExt;
exefile := splitFile(projectFile).name +{&} platform.os[targetOS].exeExt;
buildDll := '';
end;
if targetOS = platform.hostOS then
exefile := joinPath(extractDir(projectFile), exefile);
exefile := joinPath(splitFile(projectFile).dir, exefile);
exefile := quoteIfContainsWhite(exefile);

it := PStrEntry(toLink.head);
Expand Down
Loading

0 comments on commit c5222a2

Please sign in to comment.