summary refs log tree commit diff stats
path: root/tests/effects/tnestedprocs.nim
blob: 125896d44286e0e9951a2cf70d0cef5a82b5e170 (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
discard """
  cmd: "nim check --hints:off $file"
  nimout: '''tnestedprocs.nim(27, 8) Error: 'inner' can have side effects
> tnestedprocs.nim(29, 13) Hint: 'inner' calls `.sideEffect` 'outer2'
>> tnestedprocs.nim(26, 6) Hint: 'outer2' called by 'inner'

tnestedprocs.nim(45, 8) Error: 'inner' can have side effects
> tnestedprocs.nim(47, 13) Hint: 'inner' calls `.sideEffect` 'outer6'
>> tnestedprocs.nim(44, 6) Hint: 'outer6' called by 'inner'

tnestedprocs.nim(58, 41) Error: type mismatch: got <proc ()> but expected 'proc (){.closure, noSideEffect.}'
  Pragma mismatch: got '{..}', but expected '{.noSideEffect.}'.
'''
  errormsg: "type mismatch: got <proc ()> but expected 'proc (){.closure, noSideEffect.}'"
"""
{.experimental: "strictEffects".}
proc outer {.noSideEffect.} =
  proc inner(p: int) =
    if p == 0:
      outer()

  inner(4)

outer()

proc outer2 =
  proc inner(p: int) {.noSideEffect.} =
    if p == 0:
      outer2()

  inner(4)

outer2()

proc outer3(p: int) {.noSideEffect.} =
  proc inner(p: int) {.noSideEffect.} =
    if p == 0:
      p.outer3()

  inner(4)

outer3(5)

proc outer6 =
  proc inner(p: int) {.noSideEffect.} =
    if p == 0:
      outer6()

  inner(4)
  echo "bad"

outer6()


proc outer4 =
  proc inner(p: int) {.noSideEffect.} =
    if p == 0:
      let x: proc () {.noSideEffect.} = outer4
      x()

  inner(4)

outer4()
11d48cc2d518'>^
3b4daca ^
1326914 ^
d441f8a ^

84875f3 ^



a6ab7a2 ^


e1c5a42 ^










a6ab7a2 ^












0fab1b4 ^
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

                  

                                                                                  
                                                
                                    
                                              
          

                                                            
                                                     

   
                                   
                   
                         

                         
                        

                         
                          

                                                                       
                        
                         



                      
 
                                
                                                                                

   
                        
                                                                 


                       
                                                               


                         
                                                                   

   
                       
                                                               

   



                                                                                              


                              










                                  












                           
   
-- Keyboard driver

Modifiers = {'lctrl', 'rctrl', 'lalt', 'ralt', 'lshift', 'rshift', 'lgui', 'rgui'}

function App.keypressed(key, scancode, isrepeat)
  if array.find(Modifiers, key) then
    -- do nothing when the modifier is pressed
    return
  end
  -- include the modifier(s) when the non-modifer is pressed
  App.keychord_press(App.combine_modifiers(key), key)
end

function App.combine_modifiers(key)
  local result = ''
  if App.ctrl_down() then
    result = result..'C-'
  end
  if App.alt_down() then
    result = result..'M-'
  end
  if App.shift_down() then
    result = result..'S-'  -- don't try to use this with letters/digits
  end
  if App.cmd_down() then
    result = result..'s-'
  end
  result = result..key
  return result
end

function App.any_modifier_down()
  return App.ctrl_down() or App.alt_down() or App.shift_down() or App.cmd_down()
end

function App.ctrl_down()
  return App.modifier_down('lctrl') or App.modifier_down('rctrl')
end

function App.alt_down()
  return App.modifier_down('lalt') or App.modifier_down('ralt')
end

function App.shift_down()
  return App.modifier_down('lshift') or App.modifier_down('rshift')
end

function App.cmd_down()
  return App.modifier_down('lgui') or App.modifier_down('rgui')
end

function App.is_cursor_movement(key)
  return array.find({'left', 'right', 'up', 'down', 'home', 'end', 'pageup', 'pagedown'}, key)
end

array = {}

function array.find(arr, elem)
  if type(elem) == 'function' then
    for i,x in ipairs(arr) do
      if elem(x) then
        return i
      end
    end
  else
    for i,x in ipairs(arr) do
      if x == elem then
        return i
      end
    end
  end
  return nil
end

function array.any(arr, f)
  for i,x in ipairs(arr) do
    local result = f(x)
    if result then
      return result
    end
  end
  return false
end