File: tcheckedfield1.nim

package info (click to toggle)
nim 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,911,644 kB
  • sloc: sh: 24,603; ansic: 1,761; python: 1,492; makefile: 1,013; sql: 298; asm: 141; xml: 13
file content (61 lines) | stat: -rw-r--r-- 1,165 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
discard """
  nimout: "tcheckedfield1.nim(39, 6) Warning: cannot prove that field 'x.s' is accessible [ProveField]"
  action: run
  output: "abc abc"
"""

import strutils

{.warning[ProveField]: on.}
{.experimental: "notnil".}
type
  TNodeKind = enum
    nkBinary, nkTernary, nkStr
  PNode = ref TNode not nil
  TNode = object
    case k: TNodeKind
    of nkBinary, nkTernary: a, b: PNode
    of nkStr: s: string

  PList = ref object
    data: string
    next: PList

proc getData(x: PList not nil) =
  echo x.data

var head: PList

proc processList() =
  var it = head
  while it != nil:
    getData(it)
    it = it.next

proc toString2(x: PNode): string =
  if x.k < nkStr:
    toString2(x.a) & " " & toString2(x.b)
  else:
    x.s

proc toString(x: PNode): string =
  case x.k
  of nkTernary, nkBinary:
    toString(x.a) & " " & toString(x.b)
  of nkStr:
    x.s

proc toString3(x: PNode): string =
  if x.k <= nkBinary:
    toString3(x.a) & " " & toString3(x.b)
  else:
    x.s # x.k in {nkStr}  --> fact:  not (x.k <= nkBinary)

proc p() =
  var x: PNode = PNode(k: nkStr, s: "abc")

  let y = x
  if not y.isNil:
    echo toString(y), " ", toString2(y)

p()