summary refs log tree commit diff stats
path: root/tests/accept/compile/tnewuns.nim
blob: 5181e467c1af298e2021591a3efb5dad3cc0f907 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
# test the new unsigned operations:

import
  strutils

var
  x, y: int

x = 1
y = high(int)

writeln(stdout, $ ( x +% y ) )
cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #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.
#

proc isExpr(n: PNode): bool = 
  # returns true if ``n`` looks like an expression
  case n.kind
  of nkIdent..nkNilLit: 
    result = true
  of nkCall..nkPassAsOpenArray: 
    for i in countup(0, sonsLen(n) - 1): 
      if not isExpr(n.sons[i]): 
        return false
    result = true
  else: result = false
  
proc isTypeDesc(n: PNode): bool = 
  # returns true if ``n`` looks like a type desc
  case n.kind
  of nkIdent, nkSym, nkType: 
    result = true
  of nkDotExpr, nkBracketExpr: 
    for i in countup(0, sonsLen(n) - 1): 
      if not isTypeDesc(n.sons[i]): 
        return false
    result = true
  of nkTypeOfExpr..nkEnumTy: 
    result = true
  else: result = false
  
proc evalTemplateAux(c: PContext, templ, actual: PNode, sym: PSym): PNode = 
  case templ.kind
  of nkSym: 
    var p = templ.sym
    if (p.kind == skParam) and (p.owner.id == sym.id): 
      result = copyTree(actual.sons[p.position])
    else: 
      result = copyNode(templ)
  of nkNone..nkIdent, nkType..nkNilLit: # atom
    result = copyNode(templ)
  else: 
    result = copyNode(templ)
    newSons(result, sonsLen(templ))
    for i in countup(0, sonsLen(templ) - 1): 
      result.sons[i] = evalTemplateAux(c, templ.sons[i], actual, sym)
  
var evalTemplateCounter: int = 0
  # to prevend endless recursion in templates instantation

proc evalTemplateArgs(c: PContext, n: PNode, s: PSym): PNode = 
  var 
    f, a: int
    arg: PNode
  f = sonsLen(s.typ) 
  # if the template has zero arguments, it can be called without ``()``
  # `n` is then a nkSym or something similar
  case n.kind
  of nkCall, nkInfix, nkPrefix, nkPostfix, nkCommand, nkCallStrLit: 
    a = sonsLen(n)
  else: a = 0
  if a > f: LocalError(n.info, errWrongNumberOfArguments)
  result = copyNode(n)
  for i in countup(1, f - 1): 
    if i < a: arg = n.sons[i]
    else: arg = copyTree(s.typ.n.sons[i].sym.ast)
    if arg == nil or arg.kind == nkEmpty: 
      LocalError(n.info, errWrongNumberOfArguments)
    elif not (s.typ.sons[i].kind in {tyTypeDesc, tyStmt, tyExpr}): 
      # concrete type means semantic checking for argument:
      # XXX This is horrible! Better make semantic checking use some kind
      # of fixpoint iteration ...
      arg = fitNode(c, s.typ.sons[i], semExprWithType(c, arg))
    addSon(result, arg)

proc evalTemplate(c: PContext, n: PNode, sym: PSym): PNode = 
  var args: PNode
  inc(evalTemplateCounter)
  if evalTemplateCounter <= 100: 
    # replace each param by the corresponding node:
    args = evalTemplateArgs(c, n, sym)
    result = evalTemplateAux(c, sym.ast.sons[codePos], args, sym)
    dec(evalTemplateCounter)
  else:
    GlobalError(n.info, errTemplateInstantiationTooNested)
    result = n

proc symChoice(c: PContext, n: PNode, s: PSym): PNode = 
  var 
    a: PSym
    o: TOverloadIter
    i: int
  i = 0
  a = initOverloadIter(o, c, n)
  while a != nil: 
    a = nextOverloadIter(o, c, n)
    inc(i)
  if i <= 1: 
    result = newSymNode(s)
    result.info = n.info
    markUsed(n, s)
  else: 
    # semantic checking requires a type; ``fitNode`` deals with it
    # appropriately
    result = newNodeIT(nkSymChoice, n.info, newTypeS(tyNone, c))
    a = initOverloadIter(o, c, n)
    while a != nil: 
      addSon(result, newSymNode(a))
      a = nextOverloadIter(o, c, n)

proc resolveTemplateParams(c: PContext, n: PNode, withinBind: bool, 
                           toBind: var TIntSet): PNode = 
  var s: PSym
  case n.kind
  of nkIdent: 
    if not withinBind and not Contains(toBind, n.ident.id): 
      s = SymTabLocalGet(c.Tab, n.ident)
      if (s != nil): 
        result = newSymNode(s)
        result.info = n.info
      else: 
        result = n
    else: 
      Incl(toBind, n.ident.id)
      result = symChoice(c, n, lookup(c, n))
  of nkEmpty, nkSym..nkNilLit:         # atom
    result = n
  of nkBind: 
    result = resolveTemplateParams(c, n.sons[0], true, toBind)
  else: 
    result = n
    for i in countup(0, sonsLen(n) - 1): 
      result.sons[i] = resolveTemplateParams(c, n.sons[i], withinBind, toBind)
  
proc transformToExpr(n: PNode): PNode = 
  var realStmt: int
  result = n
  case n.kind
  of nkStmtList: 
    realStmt = - 1
    for i in countup(0, sonsLen(n) - 1): 
      case n.sons[i].kind
      of nkCommentStmt, nkEmpty, nkNilLit: 
        nil
      else: 
        if realStmt == - 1: realStmt = i
        else: realStmt = - 2
    if realStmt >= 0: result = transformToExpr(n.sons[realStmt])
    else: n.kind = nkStmtListExpr
  of nkBlockStmt: 
    n.kind = nkBlockExpr
    #nkIfStmt: n.kind := nkIfExpr; // this is not correct!
  else: 
    nil

proc semTemplateDef(c: PContext, n: PNode): PNode = 
  var s: PSym
  if c.p.owner.kind == skModule: 
    s = semIdentVis(c, skTemplate, n.sons[0], {sfExported})
    incl(s.flags, sfGlobal)
  else:
    s = semIdentVis(c, skTemplate, n.sons[0], {})
  # check parameter list:
  pushOwner(s)
  openScope(c.tab)
  n.sons[namePos] = newSymNode(s) # check that no pragmas exist:
  if n.sons[pragmasPos].kind != nkEmpty: 
    LocalError(n.info, errNoPragmasAllowedForX, "template") 
  # check that no generic parameters exist:
  if n.sons[genericParamsPos].kind != nkEmpty: 
    LocalError(n.info, errNoGenericParamsAllowedForX, "template")
  if n.sons[paramsPos].kind == nkEmpty: 
    # use ``stmt`` as implicit result type
    s.typ = newTypeS(tyProc, c)
    s.typ.n = newNodeI(nkFormalParams, n.info)
    addSon(s.typ, newTypeS(tyStmt, c))
    addSon(s.typ.n, newNodeIT(nkType, n.info, s.typ.sons[0]))
  else: 
    semParamList(c, n.sons[ParamsPos], nil, s)
    if n.sons[paramsPos].sons[0].kind == nkEmpty: 
      # use ``stmt`` as implicit result type
      s.typ.sons[0] = newTypeS(tyStmt, c)
      s.typ.n.sons[0] = newNodeIT(nkType, n.info, s.typ.sons[0])
  addParams(c, s.typ.n)       # resolve parameters:
  var toBind = initIntSet()
  n.sons[codePos] = resolveTemplateParams(c, n.sons[codePos], false, toBind)
  if not (s.typ.sons[0].kind in {tyStmt, tyTypeDesc}): 
    n.sons[codePos] = transformToExpr(n.sons[codePos]) 
    # only parameters are resolved, no type checking is performed
  closeScope(c.tab)
  popOwner()
  s.ast = n
  result = n
  if n.sons[codePos].kind == nkEmpty: 
    LocalError(n.info, errImplOfXexpected, s.name.s)
  # add identifier of template as a last step to not allow recursive templates:
  addInterfaceDecl(c, s)