[f66b44]: pyke / krb_compiler / __init__.py Maximize Restore History

Download this file

__init__.py    192 lines (177 with data), 6.8 kB

  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
# $Id$
# coding=utf-8
#
# Copyright Š 2007 Bruce Frederiksen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from __future__ import with_statement
import os.path
import sys
# FIX: Take this out:
use_test = False
from pyke import knowledge_engine
if use_test:
from pyke.krb_compiler import compiler_test_bc
else:
from pyke.krb_compiler import compiler_bc
from pyke.krb_compiler import krbparser
#from pyke import contexts
#contexts.debug = ('patterns_out1', 'patterns_out',)
Ast_names = frozenset((
'file',
'parent',
'fc_rule',
'fc_predicate',
'assert',
'python_assertion',
'python_eq',
'python_in',
'python_check',
'counting',
'bc_rule',
'goal',
'bc_predicate',
'symbol',
'pattern_var',
'as',
'plan_spec',
'pattern_data',
'pattern_tuple',
# 'anonymous_var',
))
def dump(ast, f = sys.stderr, need_nl = False, indent = 0):
if not isinstance(ast, tuple) or len(ast) == 0:
f.write(repr(ast))
return False
if ast[0] in Ast_names:
indent += 2
if need_nl:
f.write("\n")
f.write(' ' * indent)
f.write('(%s' % ast[0])
for arg in ast[1:]:
f.write(', ')
dump(arg, f, True, indent)
f.write(')')
return True
f.write('(')
did_nl = dump(ast[0], f, False, indent)
for arg in ast[1:]:
f.write(', ')
did_nl |= dump(arg, f, did_nl, indent)
f.write(')')
return did_nl
def compile(gen_dir, gen_root_pkg, filenames):
engine = knowledge_engine.engine(compiler_bc)
for filename in filenames:
compile_file(engine, gen_dir, gen_root_pkg, filename)
def compile_file(engine, gen_dir, gen_root_pkg, filename):
rb_name = os.path.basename(filename)
if not rb_name.endswith('.krb'):
raise ValueError("compile: filename, %s, must end with .krb" % filename)
rb_name = rb_name[:-4]
if not knowledge_engine.Name_test.match(rb_name):
raise ValueError("compile: %s illegal as python identifier" % rb_name)
base_path, ignore = \
knowledge_engine._get_base_path(filename, gen_dir, gen_root_pkg, True)
fc_path = base_path + '_fc.py'
bc_path = base_path + '_bc.py'
plan_path = base_path + '_plans.py'
try:
ast = krbparser.parse(filename)
#sys.stderr.write("got ast\n")
# dump(ast)
# sys.stderr.write('\n\n')
engine.reset()
if use_test:
engine.activate('compiler_test')
(fc_lines, bc_lines, plan_lines), plan = \
engine.prove_1('compiler_test', 'compile', (rb_name, ast), 3)
else:
engine.activate('compiler')
(fc_lines, bc_lines, plan_lines), plan = \
engine.prove_1('compiler', 'compile', (rb_name, ast), 3)
krb_filename = os.path.abspath(filename)
if fc_lines:
sys.stderr.write("writing %s\n" % fc_path)
write_file(fc_lines + ("Krb_filename = '%s'" % krb_filename,),
fc_path)
elif os.path.lexists(fc_path): os.remove(fc_path)
if bc_lines:
sys.stderr.write("writing %s\n" % bc_path)
write_file(bc_lines + ("Krb_filename = '%s'" % krb_filename,),
bc_path)
elif os.path.lexists(bc_path): os.remove(bc_path)
if plan_lines:
sys.stderr.write("writing %s\n" % plan_path)
#sys.stderr.write("plan_lines:\n")
#for line in plan_lines:
# sys.stderr.write(" " + repr(line) + "\n")
write_file(plan_lines + ("Krb_filename = '%s'" % krb_filename,),
plan_path)
elif os.path.lexists(plan_path): os.remove(plan_path)
#sys.stderr.write("done!\n")
except:
if os.path.lexists(fc_path): os.remove(fc_path)
if os.path.lexists(bc_path): os.remove(bc_path)
if os.path.lexists(plan_path): os.remove(plan_path)
raise
def write_file(lines, filename):
with open(filename, 'w') as f:
indents = [0]
lineno_map = []
write_file2(lines, f, indents, lineno_map, 0)
if lineno_map:
f.write("Krb_lineno_map = (\n")
for map_entry in lineno_map:
f.write(" %s,\n" % str(map_entry))
f.write(")\n")
def write_file2(lines, f, indents, lineno_map, lineno, starting_lineno = None):
for line in lines:
if line == 'POPINDENT':
assert len(indents) > 1
del indents[-1]
elif isinstance(line, tuple):
if len(line) == 2 and line[0] == 'INDENT':
indents.append(indents[-1] + line[1])
elif len(line) == 2 and line[0] == 'STARTING_LINENO':
assert starting_lineno is None, \
"missing ENDING_LINENO for STARTING_LINENO %d" % \
starting_lineno[1]
starting_lineno = line[1], lineno + 1
elif len(line) == 2 and line[0] == 'ENDING_LINENO':
assert starting_lineno is not None, \
"missing STARTING_LINENO for ENDING_LINENO %d" % \
line[1]
lineno_map.append(((starting_lineno[1], lineno),
(starting_lineno[0], line[1])))
starting_lineno = None
else:
lineno, starting_lineno = \
write_file2(line, f, indents, lineno_map, lineno,
starting_lineno)
else:
f.write(' ' * indents[-1] + line + '\n')
lineno += 1
return lineno, starting_lineno
def test():
import doctest
sys.exit(doctest.testmod()[0])
if __name__ == "__main__":
test()