summary refs log tree commit diff stats
path: root/tests/macros/tmacrostmt.nim
blob: 79be5f76437ec802b327344a67dc1244a8bbcfac (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import macros
macro case_token(n: varargs[untyped]): untyped =
  # creates a lexical analyzer from regular expressions
  # ... (implementation is an exercise for the reader :-)
  nil

case_token: # this colon tells the parser it is a macro statement
of r"[A-Za-z_]+[A-Za-z_0-9]*":
  return tkIdentifier
of r"0-9+":
  return tkInteger
of r"[\+\-\*\?]+":
  return tkOperator
else:
  return tkUnknown

case_token: inc i

#bug #488

macro foo() =
  var exp = newCall("whatwhat", newIntLitNode(1))
  if compiles(getAst(exp)):
    return exp
  else: echo "Does not compute! (test OK)"

foo()

#------------------------------------
# bug #8287
type MyString = distinct string

proc `$` (c: MyString): string {.borrow.}

proc `!!` (c: cstring): int =
  c.len

proc f(name: MyString): int =
  !! $ name

macro repr_and_parse(fn: typed) =
  let fn_impl = fn.getImpl
  fn_impl.name = genSym(nskProc, $fn_impl.name)
  echo fn_impl.repr
  result = parseStmt(fn_impl.repr)

macro repr_to_string(fn: typed): string =
  let fn_impl = fn.getImpl
  result = newStrLitNode(fn_impl.repr)

repr_and_parse(f)


#------------------------------------
# bugs #8343 and #8344
proc one_if_proc(x, y : int): int =
  if x < y: result = x
  else: result = y

proc test_block(x, y : int): int =
  block label:
    result = x
    result = y

#------------------------------------
# bugs #8348

template `>`(x, y: untyped): untyped =
  ## "is greater" operator. This is the same as ``y < x``.
  y < x

proc test_cond_stmtlist(x, y: int): int =
  result = x
  if x > y:
    result = x


#------------------------------------
# bug #8762
proc t2(a, b: int): int =
  `+`(a, b)


#------------------------------------
# bug #8761

proc fn1(x, y: int):int =
  2 * (x + y)

proc fn2(x, y: float): float =
  (y + 2 * x) / (x - y)

proc fn3(x, y: int): bool =
  (((x and 3) div 4) or (x mod (y xor -1))) == 0 or y notin [1,2]

proc fn4(x: int): int =
  if x mod 2 == 0: return x + 2
  else: return 0

proc fn5(a, b: float): float =
  result = - a * a / (b * b)

proc `{}`(x: seq[float], i: int, j: int): float = 
  x[i + 0 * j]

proc `{}=`(x: var seq[float], i: int, val: float) = 
  x[i] = val

proc fn6() =
  var a = @[1.0, 2.0]
  let z = a{0, 1} 
  a{2} = 5.0


#------------------------------------
# bug #10807
proc fn_unsafeaddr(x: int): int =
  cast[int](unsafeAddr(x))

static:
  let fn1s = "proc fn1(x, y: int): int =\n  result = 2 * (x + y)\n"
  let fn2s = "proc fn2(x, y: float): float =\n  result = (y + 2 * x) / (x - y)\n"
  let fn3s = "proc fn3(x, y: int): bool =\n  result = ((x and 3) div 4 or x mod (y xor -1)) == 0 or not contains([1, 2], y)\n"
  let fn4s = "proc fn4(x: int): int =\n  if x mod 2 == 0:\n    return x + 2\n  else:\n    return 0\n"
  let fn5s = "proc fn5(a, b: float): float =\n  result = -a * a / (b * b)\n"
  let fn6s = "proc fn6() =\n  var a = @[1.0, 2.0]\n  let z = a{0, 1}\n  a{2} = 5.0\n"
  let fnAddr = "proc fn_unsafeaddr(x: int): int =\n  result = cast[int](unsafeAddr(x))\n"

  doAssert fn1.repr_to_string == fn1s
  doAssert fn2.repr_to_string == fn2s
  doAssert fn3.repr_to_string == fn3s
  doAssert fn4.repr_to_string == fn4s
  doAssert fn5.repr_to_string == fn5s
  doAssert fn6.repr_to_string == fn6s
  doAssert fn_unsafeaddr.repr_to_string == fnAddr

#------------------------------------
# bug #8763

type
  A {.pure.} = enum
    X, Y
  B {.pure.} = enum
    X, Y

proc test_pure_enums(a: B) =
  case a
    of B.X: echo B.X
    of B.Y: echo B.Y

repr_and_parse(one_if_proc)
repr_and_parse(test_block)
repr_and_parse(test_cond_stmtlist)
repr_and_parse(t2)
repr_and_parse(test_pure_enums)
committer Michael Vetter <jubalh@iodoru.org> 2020-07-07 14:18:57 +0200 Apply coding style' href='/danisanti/profani-tty/commit/src/config/files.c?id=a2726b6a7d16f5f846a882fbbe9127e4604bb8bb'>a2726b6a ^
f1141932 ^


ef942bd2 ^
f1141932 ^
ef942bd2 ^

f1141932 ^
ef942bd2 ^
a2726b6a ^
ef942bd2 ^
a2726b6a ^
ef942bd2 ^





a3a73cf0 ^

ef942bd2 ^
a2726b6a ^

f1141932 ^


ef942bd2 ^
f1141932 ^
ef942bd2 ^

f1141932 ^
ef942bd2 ^
a2726b6a ^
ef942bd2 ^
a2726b6a ^
ef942bd2 ^




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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

          
                                 
  
                                                            
                                                       
































                                                                                
                   
                 

                   
                
                         



                                              



                              

                                                     
 
                                                   
                                                     
                                                  
                                                   
                                                   
                                                         
                                               
                                                 
                                                  



























                                                                          
      
                            
 

                                                     




                                                                 
                                                    









                                          
     
                                              
 
                                                 
                     

                   






                                                                     
            
                                         
                                                              
 


                                                            
 

                                         
 
                                          
 





                                 
      
                                                    
 

                                                     

                                             
                                            





                                  
      
                                                
 

                                                 

                                             
                                            





                                  
      
                                                                                  
 

                                                        

                     
                                                       



                                             
                                              




                                    

                                
 

                                                           


                                                      
                                    
     

                                                                
                               
            
                                                             
                                                  
                                                 





                                          

                              
 

                                                       


                                                  
                                  
     

                                                            
                             
            
                                                             
                                                       
                                                  




                                          
/*
 * files.c
 * vim: expandtab:ts=4:sts=4:sw=4
 *
 * Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
 * Copyright (C) 2020 Michael Vetter <jubalh@idoru.org>
 *
 * This file is part of Profanity.
 *
 * Profanity is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Profanity is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Profanity.  If not, see <https://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give permission to
 * link the code of portions of this program with the OpenSSL library under
 * certain conditions as described in each individual source file, and
 * distribute linked combinations including the two.
 *
 * You must obey the GNU General Public License in all respects for all of the
 * code used other than OpenSSL. If you modify file(s) with this exception, you
 * may extend this exception to your version of the file(s), but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version. If you delete this exception statement from all
 * source files in the program, then also delete it here.
 *
 */
#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <glib.h>

#include "common.h"
#include "log.h"
#include "config/files.h"
#include "config/preferences.h"

static char* _files_get_xdg_config_home(void);
static char* _files_get_xdg_data_home(void);

void
files_create_directories(void)
{
    gchar* xdg_config = _files_get_xdg_config_home();
    gchar* xdg_data = _files_get_xdg_data_home();

    GString* themes_dir = g_string_new(xdg_config);
    g_string_append(themes_dir, "/profanity/themes");
    GString* icons_dir = g_string_new(xdg_config);
    g_string_append(icons_dir, "/profanity/icons");
    GString* chatlogs_dir = g_string_new(xdg_data);
    g_string_append(chatlogs_dir, "/profanity/chatlogs");
    GString* logs_dir = g_string_new(xdg_data);
    g_string_append(logs_dir, "/profanity/logs");
    GString* plugins_dir = g_string_new(xdg_data);
    g_string_append(plugins_dir, "/profanity/plugins");

    if (!mkdir_recursive(themes_dir->str)) {
        log_error("Error while creating directory %s", themes_dir->str);
    }
    if (!mkdir_recursive(icons_dir->str)) {
        log_error("Error while creating directory %s", icons_dir->str);
    }
    if (!mkdir_recursive(chatlogs_dir->str)) {
        log_error("Error while creating directory %s", chatlogs_dir->str);
    }
    if (!mkdir_recursive(logs_dir->str)) {
        log_error("Error while creating directory %s", logs_dir->str);
    }
    if (!mkdir_recursive(plugins_dir->str)) {
        log_error("Error while creating directory %s", plugins_dir->str);
    }

    g_string_free(themes_dir, TRUE);
    g_string_free(icons_dir, TRUE);
    g_string_free(chatlogs_dir, TRUE);
    g_string_free(logs_dir, TRUE);
    g_string_free(plugins_dir, TRUE);

    g_free(xdg_config);
    g_free(xdg_data);
}

gchar*
files_get_inputrc_file(void)
{
    gchar* xdg_config = _files_get_xdg_config_home();
    GString* inputrc_file = g_string_new(xdg_config);
    g_free(xdg_config);

    g_string_append(inputrc_file, "/profanity/inputrc");

    if (g_file_test(inputrc_file->str, G_FILE_TEST_IS_REGULAR)) {
        gchar* result = g_strdup(inputrc_file->str);
        g_string_free(inputrc_file, TRUE);

        return result;
    }

    g_string_free(inputrc_file, TRUE);

    return NULL;
}

char*
files_get_log_file(const char* const log_file)
{
    gchar* xdg_data = _files_get_xdg_data_home();
    GString* logfile;

    if (log_file) {
        gchar *log_path = g_path_get_dirname(log_file);
        if (!mkdir_recursive(log_path)) {
            log_error("Error while creating directory %s", log_path);
        }
        g_free(log_path);

        logfile = g_string_new(log_file);
    } else {
        logfile = g_string_new(xdg_data);
        g_string_append(logfile, "/profanity/logs/profanity");

        if (!prefs_get_boolean(PREF_LOG_SHARED)) {
            g_string_append_printf(logfile, "%d", getpid());
        }

        g_string_append(logfile, ".log");
    }

    char* result = g_strdup(logfile->str);

    free(xdg_data);
    g_string_free(logfile, TRUE);

    return result;
}

gchar*
files_get_config_path(const char* const config_base)
{
    gchar* xdg_config = _files_get_xdg_config_home();
    GString* file_str = g_string_new(xdg_config);
    g_string_append(file_str, "/profanity/");
    g_string_append(file_str, config_base);
    gchar* result = g_strdup(file_str->str);
    g_free(xdg_config);
    g_string_free(file_str, TRUE);

    return result;
}

gchar*
files_get_data_path(const char* const data_base)
{
    gchar* xdg_data = _files_get_xdg_data_home();
    GString* file_str = g_string_new(xdg_data);
    g_string_append(file_str, "/profanity/");
    g_string_append(file_str, data_base);
    gchar* result = g_strdup(file_str->str);
    g_free(xdg_data);
    g_string_free(file_str, TRUE);

    return result;
}

gchar*
files_get_account_data_path(const char* const specific_dir, const char* const jid)
{
    gchar* data_dir = files_get_data_path(specific_dir);
    GString* result_dir = g_string_new(data_dir);
    g_free(data_dir);

    gchar* account_dir = str_replace(jid, "@", "_at_");
    g_string_append(result_dir, "/");
    g_string_append(result_dir, account_dir);
    g_free(account_dir);

    gchar* result = g_strdup(result_dir->str);
    g_string_free(result_dir, TRUE);

    return result;
}

static char*
_files_get_xdg_config_home(void)
{
    gchar* xdg_config_home_env = getenv("XDG_CONFIG_HOME");
    gchar* xdg_config_home = NULL;

    if (xdg_config_home_env) {
        xdg_config_home = strdup(xdg_config_home_env);
        g_strstrip(xdg_config_home);
    }

    if (xdg_config_home && (strcmp(xdg_config_home, "") != 0)) {
        return xdg_config_home;
    } else {
        GString* default_path = g_string_new(getenv("HOME"));
        g_string_append(default_path, "/.config");
        char* result = strdup(default_path->str);
        g_string_free(default_path, TRUE);

        return result;
    }
}

static char*
_files_get_xdg_data_home(void)
{
    gchar* xdg_data_home_env = getenv("XDG_DATA_HOME");
    gchar* xdg_data_home = NULL;

    if (xdg_data_home_env) {
        xdg_data_home = strdup(xdg_data_home_env);
        g_strstrip(xdg_data_home);
    }

    if (xdg_data_home && (strcmp(xdg_data_home, "") != 0)) {
        return xdg_data_home;
    } else {
        GString* default_path = g_string_new(getenv("HOME"));
        g_string_append(default_path, "/.local/share");
        gchar* result = strdup(default_path->str);
        g_string_free(default_path, TRUE);

        return result;
    }
}