Compiles a program from "Assembly" folder into "Program" folder.
Can be executed directly by double-click or on the command line.
Give name of *.WSA file without extension (example: stack_calc).
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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | #! /usr/bin/env python
"""Assembler.py
Compiles a program from "Assembly" folder into "Program" folder.
Can be executed directly by double-click or on the command line.
Give name of *.WSA file without extension (example: stack_calc)."""
################################################################################
__author__ = 'Stephen "Zero" Chappell <Noctis.Skytower@gmail.com>'
__date__ = '14 March 2010'
__version__ = '$Revision: 3 $'
################################################################################
import string
from Interpreter import INS, MNEMONIC
################################################################################
def parse(code):
program = []
process_virtual(program, code)
process_control(program)
return tuple(program)
def process_virtual(program, code):
for line, text in enumerate(code.split('\n')):
if not text or text[0] == '#':
continue
if text.startswith('part '):
parse_part(program, line, text[5:])
elif text.startswith(' '):
parse_code(program, line, text[5:])
else:
syntax_error(line)
def syntax_error(line):
raise SyntaxError('Line ' + str(line + 1))
################################################################################
def process_control(program):
parts = get_parts(program)
names = dict(pair for pair in zip(parts, generate_index()))
correct_control(program, names)
def get_parts(program):
parts = []
for ins in program:
if isinstance(ins, tuple):
ins, arg = ins
if ins == INS.PART:
if arg in parts:
raise NameError('Part definition was found twice: ' + arg)
parts.append(arg)
return parts
def generate_index():
index = 1
while True:
yield index
index *= -1
if index > 0:
index += 1
def correct_control(program, names):
for index, ins in enumerate(program):
if isinstance(ins, tuple):
ins, arg = ins
if ins in HAS_LABEL:
if arg not in names:
raise NameError('Part definition was never found: ' + arg)
program[index] = (ins, names[arg])
################################################################################
def parse_part(program, line, text):
if not valid_label(text):
syntax_error(line)
program.append((INS.PART, text))
def valid_label(text):
if not between_quotes(text):
return False
label = text[1:-1]
if not valid_name(label):
return False
return True
def between_quotes(text):
if len(text) < 3:
return False
if text.count('"') != 2:
return False
if text[0] != '"' or text[-1] != '"':
return False
return True
def valid_name(label):
valid_characters = string.ascii_letters + string.digits + '_'
valid_set = frozenset(valid_characters)
label_set = frozenset(label)
if len(label_set - valid_set) != 0:
return False
return True
################################################################################
from Interpreter import HAS_LABEL, Program
NO_ARGS = Program.NO_ARGS
HAS_ARG = Program.HAS_ARG
TWO_WAY = tuple(set(NO_ARGS) & set(HAS_ARG))
################################################################################
def parse_code(program, line, text):
for ins, word in enumerate(MNEMONIC):
if text.startswith(word):
check_code(program, line, text[len(word):], ins)
break
else:
syntax_error(line)
def check_code(program, line, text, ins):
if ins in TWO_WAY:
if text:
number = parse_number(line, text)
program.append((ins, number))
else:
program.append(ins)
elif ins in HAS_LABEL:
text = parse_label(line, text)
program.append((ins, text))
elif ins in HAS_ARG:
number = parse_number(line, text)
program.append((ins, number))
elif ins in NO_ARGS:
if text:
syntax_error(line)
program.append(ins)
else:
syntax_error(line)
def parse_label(line, text):
if not text or text[0] != ' ':
syntax_error(line)
text = text[1:]
if not valid_label(text):
syntax_error(line)
return text
################################################################################
def parse_number(line, text):
if not valid_number(text):
syntax_error(line)
return int(text)
def valid_number(text):
if len(text) < 2:
return False
if text[0] != ' ':
return False
text = text[1:]
if '+' in text and '-' in text:
return False
if '+' in text:
if text.count('+') != 1:
return False
if text[0] != '+':
return False
text = text[1:]
if not text:
return False
if '-' in text:
if text.count('-') != 1:
return False
if text[0] != '-':
return False
text = text[1:]
if not text:
return False
valid_set = frozenset(string.digits)
value_set = frozenset(text)
if len(value_set - valid_set) != 0:
return False
return True
################################################################################
################################################################################
from Interpreter import partition_number
VMC_2_TRI = {
(INS.PUSH, True): (0, 0),
(INS.COPY, False): (0, 2, 0),
(INS.COPY, True): (0, 1, 0),
(INS.SWAP, False): (0, 2, 1),
(INS.AWAY, False): (0, 2, 2),
(INS.AWAY, True): (0, 1, 2),
(INS.ADD, False): (1, 0, 0, 0),
(INS.SUB, False): (1, 0, 0, 1),
(INS.MUL, False): (1, 0, 0, 2),
(INS.DIV, False): (1, 0, 1, 0),
(INS.MOD, False): (1, 0, 1, 1),
(INS.SET, False): (1, 1, 0),
(INS.GET, False): (1, 1, 1),
(INS.PART, True): (2, 0, 0),
(INS.CALL, True): (2, 0, 1),
(INS.GOTO, True): (2, 0, 2),
(INS.ZERO, True): (2, 1, 0),
(INS.LESS, True): (2, 1, 1),
(INS.BACK, False): (2, 1, 2),
(INS.EXIT, False): (2, 2, 2),
(INS.OCHR, False): (1, 2, 0, 0),
(INS.OINT, False): (1, 2, 0, 1),
(INS.ICHR, False): (1, 2, 1, 0),
(INS.IINT, False): (1, 2, 1, 1)
}
################################################################################
def to_trinary(program):
trinary_code = []
for ins in program:
if isinstance(ins, tuple):
ins, arg = ins
trinary_code.extend(VMC_2_TRI[(ins, True)])
trinary_code.extend(from_number(arg))
else:
trinary_code.extend(VMC_2_TRI[(ins, False)])
return tuple(trinary_code)
def from_number(arg):
code = [int(arg < 0)]
if arg:
for bit in reversed(list(partition_number(abs(arg), 2))):
code.append(bit)
return code + [2]
return code + [0, 2]
to_ws = lambda trinary: ''.join(' \t\n'[index] for index in trinary)
def compile_wsa(source):
program = parse(source)
trinary = to_trinary(program)
ws_code = to_ws(trinary)
return ws_code
################################################################################
################################################################################
import os
import sys
import time
import traceback
def main():
name, source, command_line, error = get_source()
if not error:
start = time.clock()
try:
ws_code = compile_wsa(source)
except:
print('ERROR: File could not be compiled.\n')
traceback.print_exc()
error = True
else:
path = os.path.join('Programs', name + '.ws')
try:
open(path, 'w').write(ws_code)
except IOError as err:
print(err)
error = True
else:
div, mod = divmod((time.clock() - start) * 1000, 1)
args = int(div), '{:.3}'.format(mod)[1:]
print('DONE: Compiled in {}{} ms'.format(*args))
handle_close(error, command_line)
def get_source():
if len(sys.argv) > 1:
command_line = True
name = sys.argv[1]
else:
command_line = False
try:
name = input('Source File: ')
except:
return None, None, False, True
print()
path = os.path.join('Assembly', name + '.wsa')
try:
return name, open(path).read(), command_line, False
except IOError as err:
print(err)
return None, None, command_line, True
def handle_close(error, command_line):
if error:
usage = 'Usage: {} <assembly>'.format(os.path.basename(sys.argv[0]))
print('\n{}\n{}'.format('-' * len(usage), usage))
if not command_line:
time.sleep(10)
################################################################################
if __name__ == '__main__':
main()
|
Tags: assembler, whitespace