diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/core/macros.nim | 30 | ||||
-rw-r--r-- | lib/impure/nre.nim | 8 | ||||
-rw-r--r-- | lib/impure/re.nim | 9 | ||||
-rw-r--r-- | lib/js/jsconsole.nim | 44 | ||||
-rw-r--r-- | lib/pure/future.nim | 21 | ||||
-rw-r--r-- | lib/system/hti.nim | 1 |
6 files changed, 105 insertions, 8 deletions
diff --git a/lib/core/macros.nim b/lib/core/macros.nim index 292e8dd3c..d584b1ac5 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -78,7 +78,7 @@ type nnkBreakState NimNodeKinds* = set[NimNodeKind] - NimTypeKind* = enum + NimTypeKind* = enum # some types are no longer used, see ast.nim ntyNone, ntyBool, ntyChar, ntyEmpty, ntyArrayConstr, ntyNil, ntyExpr, ntyStmt, ntyTypeDesc, ntyGenericInvocation, ntyGenericBody, ntyGenericInst, @@ -90,8 +90,8 @@ type ntyInt8, ntyInt16, ntyInt32, ntyInt64, ntyFloat, ntyFloat32, ntyFloat64, ntyFloat128, ntyUInt, ntyUInt8, ntyUInt16, ntyUInt32, ntyUInt64, - ntyBigNum, - ntyConst, ntyMutable, ntyVarargs, + ntyUnused0, ntyUnused1, ntyUnused2, + ntyVarargs, ntyUnused, ntyError, ntyBuiltinTypeClass, ntyConcept, ntyConceptInst, ntyComposite, @@ -890,6 +890,30 @@ proc boolVal*(n: NimNode): bool {.compileTime, noSideEffect.} = if n.kind == nnkIntLit: n.intVal != 0 else: n == bindSym"true" # hacky solution for now +macro expandMacros*(body: typed): untyped = + ## Expands one level of macro - useful for debugging. + ## Can be used to inspect what happens when a macro call is expanded, + ## without altering its result. + ## + ## For instance, + ## + ## .. code-block:: nim + ## import future, macros + ## + ## let + ## x = 10 + ## y = 20 + ## expandMacros: + ## dump(x + y) + ## + ## will actually dump `x + y`, but at the same time will print at + ## compile time the expansion of the ``dump`` macro, which in this + ## case is ``debugEcho ["x + y", " = ", x + y]``. + template inner(x: untyped): untyped = x + + result = getAst(inner(body)) + echo result.toStrLit + when not defined(booting): template emit*(e: static[string]): untyped {.deprecated.} = ## accepts a single string argument and treats it as nim code diff --git a/lib/impure/nre.nim b/lib/impure/nre.nim index 557bb0549..626c3fd6b 100644 --- a/lib/impure/nre.nim +++ b/lib/impure/nre.nim @@ -431,8 +431,12 @@ proc initRegex(pattern: string, flags: int, study = true): Regex = raise SyntaxError(msg: $errorMsg, pos: errOffset, pattern: pattern) if study: - # XXX investigate JIT - result.pcreExtra = pcre.study(result.pcreObj, 0x0, addr errorMsg) + var options: cint = 0 + var hasJit: cint + if pcre.config(pcre.CONFIG_JIT, addr hasJit) == 0: + if hasJit == 1'i32: + options = pcre.STUDY_JIT_COMPILE + result.pcreExtra = pcre.study(result.pcreObj, options, addr errorMsg) if errorMsg != nil: raise StudyError(msg: $errorMsg) diff --git a/lib/impure/re.nim b/lib/impure/re.nim index bd86bcdcf..f7f7c5b25 100644 --- a/lib/impure/re.nim +++ b/lib/impure/re.nim @@ -87,7 +87,12 @@ proc re*(s: string, flags = {reExtended, reStudy}): Regex = result.h = rawCompile(s, cast[cint](flags - {reStudy})) if reStudy in flags: var msg: cstring - result.e = pcre.study(result.h, 0, addr msg) + var options: cint = 0 + var hasJit: cint + if pcre.config(pcre.CONFIG_JIT, addr hasJit) == 0: + if hasJit == 1'i32: + options = pcre.STUDY_JIT_COMPILE + result.e = pcre.study(result.h, options, addr msg) if not isNil(msg): raiseInvalidRegex($msg) proc matchOrFind(s: string, pattern: Regex, matches: var openArray[string], @@ -214,7 +219,7 @@ proc find*(s: string, pattern: Regex, start = 0): int = var rtarray = initRtArray[cint](3) rawMatches = rtarray.getRawData - res = pcre.exec(pattern.h, nil, s, len(s).cint, start.cint, 0'i32, + res = pcre.exec(pattern.h, pattern.e, s, len(s).cint, start.cint, 0'i32, cast[ptr cint](rawMatches), 3) if res < 0'i32: return res return rawMatches[0] diff --git a/lib/js/jsconsole.nim b/lib/js/jsconsole.nim new file mode 100644 index 000000000..d9ced95f0 --- /dev/null +++ b/lib/js/jsconsole.nim @@ -0,0 +1,44 @@ +# +# +# Nim's Runtime Library +# (c) Copyright 2012 Andreas Rumpf +# +# See the file "copying.txt", included in this +# distribution, for details about the copyright. +# + +## Wrapper for the `console` object for the `JavaScript backend +## <backends.html#the-javascript-target>`_. + +when not defined(js) and not defined(Nimdoc): + {.error: "This module only works on the JavaScript platform".} + +import macros + +type Console* {.importc.} = ref object of RootObj + +proc convertToConsoleLoggable*[T](v: T): RootRef {.importcpp: "#".} +template convertToConsoleLoggable*(v: string): RootRef = cast[RootRef](cstring(v)) + +proc logImpl(console: Console) {.importcpp: "log", varargs.} +proc debugImpl(console: Console) {.importcpp: "debug", varargs.} +proc infoImpl(console: Console) {.importcpp: "info", varargs.} +proc errorImpl(console: Console) {.importcpp: "error", varargs.} + +proc makeConsoleCall(console: NimNode, procName: NimNode, args: NimNode): NimNode = + result = newCall(procName, console) + for c in args: result.add(c) + +macro log*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]): untyped = + makeConsoleCall(console, bindSym "logImpl", args) + +macro debug*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]): untyped = + makeConsoleCall(console, bindSym "debugImpl", args) + +macro info*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]): untyped = + makeConsoleCall(console, bindSym "infoImpl", args) + +macro error*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]): untyped = + makeConsoleCall(console, bindSym "errorImpl", args) + +var console* {.importc, nodecl.}: Console \ No newline at end of file diff --git a/lib/pure/future.nim b/lib/pure/future.nim index 67975cfcb..2a6d29933 100644 --- a/lib/pure/future.nim +++ b/lib/pure/future.nim @@ -177,3 +177,24 @@ macro `[]`*(lc: ListComprehension, comp, typ: untyped): untyped = newIdentNode("@"), newNimNode(nnkBracket))), result)))) + + +macro dump*(x: typed): untyped = + ## Dumps the content of an expression, useful for debugging. + ## It accepts any expression and prints a textual representation + ## of the tree representing the expression - as it would appear in + ## source code - together with the value of the expression. + ## + ## As an example, + ## + ## .. code-block:: nim + ## let + ## x = 10 + ## y = 20 + ## dump(x + y) + ## + ## will print ``x + y = 30``. + let s = x.toStrLit + let r = quote do: + debugEcho `s`, " = ", `x` + return r \ No newline at end of file diff --git a/lib/system/hti.nim b/lib/system/hti.nim index 984f888cb..892a209df 100644 --- a/lib/system/hti.nim +++ b/lib/system/hti.nim @@ -62,7 +62,6 @@ type tyUInt16, tyUInt32, tyUInt64, - tyBigNum, TNimNodeKind = enum nkNone, nkSlot, nkList, nkCase TNimNode {.codegenType.} = object |