summary refs log tree commit diff stats
path: root/doc
diff options
context:
space:
mode:
authortreeform <starplant@gmail.com>2020-12-27 01:45:30 -0800
committerGitHub <noreply@github.com>2020-12-27 10:45:30 +0100
commit626c2bc6589101bd1b0231a2608e32b51f73abba (patch)
tree1efb0c2f77bc882dd3472a0d54ddf17c57118781 /doc
parentb57df6d0b335595d3197eb6907fb8d1a8237fc7e (diff)
downloadNim-626c2bc6589101bd1b0231a2608e32b51f73abba.tar.gz
Add docs for nnkHiddenStdConv (#16408)
Add it to devel branch this time. I hope this works.
Diffstat (limited to 'doc')
-rw-r--r--doc/astspec.txt11
1 files changed, 11 insertions, 0 deletions
diff --git a/doc/astspec.txt b/doc/astspec.txt
index c41aee96f..019b735f5 100644
--- a/doc/astspec.txt
+++ b/doc/astspec.txt
@@ -1392,6 +1392,17 @@ Macro declaration
 Macros behave like templates, but ``nnkTemplateDef`` is replaced with
 ``nnkMacroDef``.
 
+Hidden Standard Conversion
+--------------------------
+
+.. code-block:: nim
+  var f: float = 1
+
+The type of "f" is ``float`` but the type of "1" is actually ``int``. Inserting 
+``int`` into a ``float`` is a type error. Nim inserts the ``nnkHiddenStdConv``  
+node around the ``nnkIntLit`` node so that the new node has the correct type of 
+``float``. This works for any auto converted nodes and makes the conversion 
+explicit.
 
 Special node kinds
 ==================
dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#
#
#           The Nimrod Compiler
#        (c) Copyright 2011 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# This module implements Nimrod's simple filters and helpers for filters.

import
  llstream, os, wordrecg, idents, strutils, ast, astalgo, msgs, options, 
  renderer

proc filterReplace*(stdin: PLLStream, filename: string, call: PNode): PLLStream
proc filterStrip*(stdin: PLLStream, filename: string, call: PNode): PLLStream
  # helpers to retrieve arguments:
proc charArg*(n: PNode, name: string, pos: int, default: Char): Char
proc strArg*(n: PNode, name: string, pos: int, default: string): string
proc boolArg*(n: PNode, name: string, pos: int, default: bool): bool
# implementation

proc invalidPragma(n: PNode) = 
  LocalError(n.info, errXNotAllowedHere, renderTree(n, {renderNoComments}))

proc getArg(n: PNode, name: string, pos: int): PNode = 
  result = nil
  if n.kind in {nkEmpty..nkNilLit}: return 
  for i in countup(1, sonsLen(n) - 1): 
    if n.sons[i].kind == nkExprEqExpr: 
      if n.sons[i].sons[0].kind != nkIdent: invalidPragma(n)
      if IdentEq(n.sons[i].sons[0].ident, name): 
        return n.sons[i].sons[1]
    elif i == pos: 
      return n.sons[i]
  
proc charArg(n: PNode, name: string, pos: int, default: Char): Char = 
  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 = 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 = 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 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): 
    var line = LLStreamReadLine(stdin)
    var stripped = strip(line, leading, trailing)
    if (len(pattern) == 0) or startsWith(stripped, pattern): 
      LLStreamWriteln(result, stripped)
    else: 
      LLStreamWriteln(result, line)
  LLStreamClose(stdin)

proc filterReplace(stdin: PLLStream, filename: string, call: PNode): PLLStream = 
  var sub = strArg(call, "sub", 1, "")
  if len(sub) == 0: invalidPragma(call)
  var by = strArg(call, "by", 2, "")
  result = LLStreamOpen("")
  while not LLStreamAtEnd(stdin): 
    var line = LLStreamReadLine(stdin)
    LLStreamWriteln(result, replace(line, sub, by))
  LLStreamClose(stdin)