summary refs log tree commit diff stats
path: root/tests/caas/idetools_api.nim
blob: 7d27ffdf8292f555180230a73e7d306b7fc40661 (plain) (blame)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import unicode, sequtils, macros

proc test_enums() =
  var o: Tfile
  if o.open("files " & "test.txt", fmWrite):
    o.write("test")
    o.close()

proc test_iterators(filename = "tests.nim") =
  let
    input = readFile(filename)
    letters = toSeq(runes(string(input)))
  for letter in letters: echo int(letter)

const SOME_SEQUENCE = @[1, 2]
type
  bad_string = distinct string
  TPerson = object of TObject
    name*: bad_string
    age: int

proc adder(a, b: int): int =
  result = a + b

type
  PExpr = ref object of TObject ## abstract base class for an expression
  PLiteral = ref object of PExpr
    x: int
  PPlusExpr = ref object of PExpr
    a, b: PExpr

# watch out: 'eval' relies on dynamic binding
method eval(e: PExpr): int =
  # override this base method
  quit "to override!"

method eval(e: PLiteral): int = e.x
method eval(e: PPlusExpr): int = eval(e.a) + eval(e.b)

proc newLit(x: int): PLiteral = PLiteral(x: x)
proc newPlus(a, b: PExpr): PPlusExpr = PPlusExpr(a: a, b: b)

echo eval(newPlus(newPlus(newLit(1), newLit(2)), newLit(4)))

proc findVowelPosition(text: string) =
  var found = -1
  block loops:
    for i, letter in pairs(text):
      for j in ['a', 'e', 'i', 'o', 'u']:
        if letter == j:
          found = i
          break loops # leave both for-loops
  echo found

findVowelPosition("Zerg") # should output 1, position of vowel.

macro expect*(exceptions: varargs[expr], body: stmt): stmt {.immediate.} =
  ## Expect docstrings
  let exp = callsite()
  template expectBody(errorTypes, lineInfoLit: expr,
                      body: stmt): PNimrodNode {.dirty.} =
    try:
      body
      assert false
    except errorTypes:
      nil

  var body = exp[exp.len - 1]

  var errorTypes = newNimNode(nnkBracket)
  for i in countup(1, exp.len - 2):
    errorTypes.add(exp[i])

  result = getAst(expectBody(errorTypes, exp.lineinfo, body))

proc err =
  raise newException(EArithmetic, "some exception")

proc testMacro() =
  expect(EArithmetic):
    err()

testMacro()