-
Notifications
You must be signed in to change notification settings - Fork 1
/
msgs.py
115 lines (92 loc) · 2.96 KB
/
msgs.py
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
#
# Copyright (c) 2015-2016 Sam Horlbeck Olsen
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Yes, yes, I know this code is horrible... It has lots of duplication
# (woo copy paste!) and it generally could be structured waaaaaay better.
# This was just a hack to move the source over to having string literal
# messages stored in a central location in the code. I have much higher
# standards for the code in the compiler itself. Maybe someday I will
# return to this code.
#
# Say what you will about how ugly it is; writing this code and replacing
# ALL of the die() and warn() string literals in the compiler took just
# over an hour.
import sys
import re
var = ''
def prompt(text):
sys.stdout.write(text + ' ')
sys.stdout.flush()
return raw_input()
externs = []
code = []
prefix = 'msgerr_'
prefixwarn = 'msgwarn_'
fname = prompt('File:')
fulltext = ''
with open(fname, 'r') as f:
fulltext = f.read()
maxName = 0
def print_spaces(f, name):
f.write(' ' * (1 + maxName - len(name)))
instances = re.findall(r'die\(.*(".*")', fulltext)
varnames = []
ignore = set()
for i in instances:
print i
var = prefix + '_'.join(prompt('Err > ').split(' '))
print ''
if ':' in var:
var = var.split(':')[0]
ignore.add(var)
if len(var) > maxName:
maxName = len(var)
varnames.append(var)
for i in range(len(instances)):
fulltext = re.sub(r'(die\(.*)".*"', '\\1' + varnames[i], fulltext, 1)
instanceswarn = re.findall(r'warn\(.*(".*")', fulltext)
varnameswarn = []
for i in instanceswarn:
print i
var = prefixwarn + '_'.join(prompt('Warn > ').split(' '))
print ''
if ':' in var:
var = var.split(':')[0]
ignore.add(var)
if len(var) > maxName:
maxName = len(var)
varnameswarn.append(var)
for i in range(len(instanceswarn)):
fulltext = re.sub(r'(warn\(.*)".*"', '\\1' + varnameswarn[i], fulltext, 1)
with open(fname, 'w') as f:
f.write(fulltext)
with open('src/core/messages.h', 'a') as f:
f.write('// ' + fname + '\n')
for v in varnames:
if v in ignore:
continue
f.write('extern const char *' + v + ';\n')
for v in varnameswarn:
if v in ignore:
continue
f.write('extern const char *' + v + ';\n')
f.write('\n')
with open('src/core/messages.c', 'a') as f:
f.write('// ' + fname + '\n')
for i in range(len(instances)):
if varnames[i] in ignore:
continue
f.write('const char *' + varnames[i])
print_spaces(f, varnames[i])
f.write('= ' + instances[i] + ';\n')
for i in range(len(instanceswarn)):
if varnameswarn[i] in ignore:
continue
f.write('const char *' + varnameswarn[i])
print_spaces(f, varnameswarn[i])
f.write('= ' + instanceswarn[i] + ';\n')
f.write('\n')