aboutsummaryrefslogtreecommitdiffhomepage
path: root/notes/pasteable_functions.txt
blob: f0e48f19883c98eefd84fe46b4bc0f18c618a729 (plain)
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
84
85
86

Use cases:
* We want to break out some code to a separate function,
  but there are outbound gotos or accesses to local variables.
* We want to inline some recurring pattern (with access to
  gotos and local variables).

Goals:
* Avoid deep indentation and large functions.
* Isolate interface/implementation so the function can be type-checked etc.
  on it's own (but not necessarily compiled, since needs to be inlined).
* Make it clear which variables are used/modified and not.

Syntax ideas:
Option 1 (simple paste):

    snippet swap_if_nonzero(int someparam)
    using var int somevar
    using var int othervar
    using goto end
    using return int {
        if someparam == 0 goto error
        if somevar == 0 return -1
        int tmp = somevar
        somevar = othervar
        othervar = tmp
    }

    func f(int x) -> int {
        var int somevar
        var int othervar
        ...
        paste swap_if_nonzero(x)
        return somevar
      error:
        ...
    }

Option 2 ("function" paste):

    snippet swap_if_nonzero(int someparam) -> int
    using var int somevar
    using var int othervar
    using goto end {
        if someparam == 0 goto error
        if somevar == 0 return -1
        int tmp = somevar
        somevar = othervar
        othervar = tmp
        return somevar
    }

    func f(int x) -> int {
        var int somevar
        var int othervar
        ...
        return paste swap_if_nonzero(x)
      error:
        ...
    }

Option 3 (typed paste):

    snippet type swapsnippet(int someparam)
    using var int somevar
    using var int othervar
    using goto end
    using return int

    snippet swapsnippet swap_if_nonzero
        if someparam == 0 goto error
        if somevar == 0 return -1
        int tmp = somevar
        somevar = othervar
        othervar = tmp
    }

    func f(int x) -> int {
        var int somevar
        var int othervar
        ...
        paste swap_if_nonzero(x)
        return somevar
      error:
        ...
    }