예제 #1
0
 def visit_identifier(self, node):
     if node.name in symboltable.funcTable:
         entry = symboltable.funcTable[node.name]
         ret = dict(entry)
         ret['const'] = False
         if 'json' in entry:
             ret['json'] = {
                 "name": node.name,
                 "collapse": True,
                 "children": []
             }
         else:
             ret["json"] = {"name": node.name + " = " + str(entry['value'])}
         return ret
     elif node.name in parser._functions:
         return {
             "type": "function",
             "value": node.name,
             "const": False,
             "json": {
                 "name": node.name + " = function"
             }
         }
     else:
         parser.clean()
         raise Exception(node.name + " is not defined")
예제 #2
0
def build():
    inputFileName = sys.argv[1]
    inputFile = open(inputFileName, 'r', encoding='UTF-8')
    program = Program()

    for lineNumber, line in enumerate(inputFile, 1):
        line = clean(line)

        if line:
            try:
                if (line.startswith(common.PREFIX_LABEL)):
                    program.addLabel(parseLabel(line))
                else:
                    program.addInstruction(parseInstruction(line))
            except (ParserException, ProgramException) as e:
                print('Error on line: %s' % lineNumber)
                print('Content: %s' % line)
                print('Message: %s' % e)
                sys.exit(1)

    if re.search(r'\.asm$', inputFileName):
        outputFileName = re.sub(r'\.asm$', '.hack', inputFileName)
    else:
        outputFileName = inputFileName + '.hack'

    outputFile = open(outputFileName, 'w')
    outputFile.write(program.getBinary())
    outputFile.write('\n')
예제 #3
0
def pass2(file_name):
    #first address for symbol memory
    with open(file_name, 'r') as asm_file:
        address = 16
        command_list = []
        for command in asm_file:
            current_command = parser.clean(command)
            #skip further processing if line made up of only spaces and/or comments
            if current_command:
                if parser.command_type(current_command) == 'C_Command':
                    dest = parser.dest(current_command)
                    comp = parser.comp(current_command)
                    jump = parser.jump(current_command)
                    binary_repr = code.binary_code(dest, comp, jump)
                    command_list.append(binary_repr)
                elif parser.command_type(current_command) == 'A_Command':
                    symbol = parser.symbol(current_command)
                    #if symbol doesn't begin with a numeric value, check symbol table for
                    #correct value. Add new symbol if necessary
                    try:
                        int_symbol = int(symbol)
                    except ValueError:
                        if not sym_table.contains(symbol):
                            sym_table.add_entry(symbol, address)
                            address += 1

                        symbol = sym_table.get_address(symbol)

                    #convert decimal symbol to binary and add to command_list
                    binary_symbol = "{0:016b}".format(int(symbol))
                    command_list.append(binary_symbol)

    return command_list
예제 #4
0
def get_response(query, userinfo):
    """Return the appropriate response for the query and user info."""

    m = match(clean(query))
    if not m:
        return None
    data, method = m
    return method.method(data, userinfo)
예제 #5
0
def get_words(file_name):
    # Extract words from a text file. Clean the words by removing surrounding
    # punctuation and whitespace, and convert the word to singular.
    reader = open(file_name)
    words = reader.read()
    reader.close()
    words = words.replace("\n", " ")
    words = parser.convert_abbreviations(words)
    words = words.split(" ")
    words = parser.remove_blanks(words)
    for i in range(0, len(words)):
        words[i] = parser.clean(words[i])
    return words
예제 #6
0
def score(sentence, word_scores):
    # The scoring algorithm.
    denominator = 1.0
    score = 0.0
    words = sentence.split(" ")
    for word in words:
        if word not in word_scores:
            continue
        if sentence.count(word) == 1:
            denominator += 1.0
        word = parser.clean(word)
        score += word_scores.get(word)
    return score / denominator
예제 #7
0
def expr(pattern, method):
    clean_pattern = clean(pattern, expression=True)
    parsed_pattern = []
    for token in clean_pattern:
        if not isinstance(token, str):
            parsed_pattern.append(token)
            continue
        if token.startswith("{{") and token.endswith("}}"):
            t = PlaceholderToken(token[2:-2])
            parsed_pattern.append(t)
            continue

        parsed_pattern.append(token)

    return Expression(parsed_pattern, method=method, raw=pattern)
예제 #8
0
def pass1(file_name, sym_table):
    with open(file_name, 'r') as asm_file:
        program_counter = 0

        #Go through file once, find the loops and store them in the symbol table
        for command in asm_file:
            current_command = parser.clean(command)
            #skip further processing if line made up of only spaces and/or comments
            if current_command:
                if parser.command_type(current_command) == 'L_Command':
                    loop_symbol = parser.symbol(current_command)
                    sym_table.add_entry(loop_symbol, program_counter)

                else:
                    program_counter += 1
예제 #9
0
    def visit_application(self, node):
        memoFlag = False
        args = []

        while type(node) is Application:
            if node.arg != None:
                args = [node.arg.visit(BuildD3Json())] + args
            node = node.func

        if node in symboltable.funcTable:
            if symboltable.funcTable[node]['type'] == "function":
                funcName = node
                node = symboltable.funcTable[node]['value']
                funcName = "(" + funcName + " = " + node + ")"
            else:
                parser.clean()
                raise Exception(node + " is not a function")
        else:
            funcName = node

        if len(args) > len(parser._args[node]):
            parser.clean()
            raise Exception(
                node +
                " is called with more arguments ({}) than what is defined ({})"
                .format(len(args), len(parser._args[node])))
        if len(args) < len(parser._args[node]):
            parser.clean()
            raise Exception(
                node +
                " is called with less arguments ({}) than what is defined ({})"
                .format(len(args), len(parser._args[node])))

        symboltable.getTable(node)
        k = 0
        while k < len(args):
            entry = dict(args[k])
            argName = parser._args[node][k]
            symboltable.funcTable[argName] = entry
            k += 1

        if node in parser._lambda_closure:
            closure = parser._lambda_closure[node]
            for argName in closure:
                entry = symboltable.getHighterValues(argName)
                if entry:
                    symboltable.funcTable[argName] = entry

        variables = []
        global _prop
        global _argMap
        global _fold
        _argMap = {}
        tam = len(parser._whereDict[node])
        for i in range(tam):
            entry = parser._whereDict[node][i]
            result = entry['expression'].visit(BuildD3Json())
            if result['const'] and _prop:
                _argMap[entry['var']] = result
                for j in range(i + 1, tam):
                    parser._whereDict[node][j][
                        'expression'] = parser._whereDict[node][j][
                            'expression'].visit(PropSearch())
            else:
                symboltable.funcTable[entry['var']] = result
                variables += [(entry['var'], result)]

        if _prop:
            parser._functions[node] = parser._functions[node].visit(
                PropSearch())
        global _memo
        if _memo:
            memoKey = node + ' ' + ' '.join(
                ["({} {})".format(arg['type'], arg['value']) for arg in args])
            global memoized
            if memoKey not in memoized:
                memoized[memoKey] = parser._functions[node].visit(
                    BuildD3Json())
            else:
                memoFlag = True

            exec_tree = memoized[memoKey]
        else:
            exec_tree = parser._functions[node].visit(BuildD3Json())

        symboltable.deleteTable(node)

        if memoFlag:
            exec_tree['json']['collapse'] = True
            if 'children' in exec_tree['json']:
                del exec_tree['json']['children']

            args_tree = {
                "name": "(memoized) {}".format(memoKey),
                "children": [exec_tree['json']]
            }
        else:
            args_string = ""
            for arg in parser._args[node]:
                args_string += " " + arg
            exec_tree['json']['collapse'] = funcName in specialNames
            if funcName in specialNames and 'children' in exec_tree['json']:
                del exec_tree['json']['children']

            args_tree = {
                "name": funcName + args_string + " = ",
                "children": [exec_tree['json']]
            }

        for arg in args:
            args_tree = {"name": " ", "children": [args_tree, arg['json']]}

        for v in variables:
            if not (_fold and v[1]['const']):
                args_tree = {
                    "name":
                    "",
                    "children": [
                        args_tree, {
                            "name": "where " + v[0] + " = ",
                            "children": [v[1]['json']]
                        }
                    ]
                }
            else:
                args_tree = {
                    "name":
                    "",
                    "children": [
                        args_tree, {
                            "name": "where " + v[0] + " = ",
                            "children": [{
                                "name": v[1]['json']['name']
                            }]
                        }
                    ]
                }

        ret = dict(exec_tree)
        ret['json'] = args_tree
        ret['const'] = False

        return ret
예제 #10
0
    def visit_binop(self, node):
        left = node.left.visit(BuildD3Json())
        right = node.right.visit(BuildD3Json())

        exprFromKey = None
        newType = None

        arithmetic_op = {"float", "int", "bool", "long"}
        lr_types_union = {left['type'], right['type']}

        if node.op == '+':
            if lr_types_union == {"list"}:
                exprFromKey = {}
                for key in left['exprFromKey']:
                    exprFromKey[key] = left['exprFromKey'][key]
                for key in right['exprFromKey']:
                    exprFromKey[len(left['value']) +
                                key] = right['exprFromKey'][key]

            elif not lr_types_union < arithmetic_op and lr_types_union != {
                    "str"
            }:
                parser.clean()
                raise Exception(
                    "Error: invalid operation \'{}\' between \'{}:{}\' and \'{}:{}\'"
                    .format(node.op, left['value'], left['type'],
                            right['value'], right['type']))
            value = left['value'] + right['value']

        if node.op == '-':
            if not lr_types_union < arithmetic_op:
                parser.clean()
                raise Exception(
                    "Error: invalid operation \'{}\' between \'{}:{}\' and \'{}:{}\'"
                    .format(node.op, left['value'], left['type'],
                            right['value'], right['type']))
            value = left['value'] - right['value']

        if node.op == '*':
            if not lr_types_union < arithmetic_op:
                parser.clean()
                raise Exception(
                    "Error: invalid operation \'{}\' between \'{}:{}\' and \'{}:{}\'"
                    .format(node.op, left['value'], left['type'],
                            right['value'], right['type']))
            value = left['value'] * right['value']

        if node.op == '/':
            if not lr_types_union < arithmetic_op:
                parser.clean()
                raise Exception(
                    "Error: invalid operation \'{}\' between \'{}:{}\' and \'{}:{}\'"
                    .format(node.op, left['value'], left['type'],
                            right['value'], right['type']))
            if right['value'] == 0:
                parser.clean()
                raise Exception(
                    "Error: division by zero between \'{}:{}\' and \'{}:{}\'".
                    format(left['value'], left['type'], right['value'],
                           right['type']))
            value = left['value'] / right['value']

        if node.op == 'and':
            if lr_types_union == {"structure"}:
                value = {}
                for key in left['value']:
                    if key in right['value']:
                        value[key] = right['value'][key]

                exprFromKey = {}
                for key in left['exprFromKey']:
                    if key in right['exprFromKey']:
                        exprFromKey[key] = right['exprFromKey'][key]

            else:
                value = left['value'] and right['value']

        if node.op == 'xor':
            if lr_types_union == {"structure"}:
                value = {}
                for key in left['value']:
                    if key not in right['value']:
                        value[key] = left['value'][key]

                for key in right['value']:
                    if key not in left['value']:
                        value[key] = right['value'][key]

                exprFromKey = {}
                for key in left['exprFromKey']:
                    if key not in right['exprFromKey']:
                        exprFromKey[key] = left['exprFromKey'][key]

                for key in right['exprFromKey']:
                    if key not in left['exprFromKey']:
                        exprFromKey[key] = right['exprFromKey'][key]

            else:
                value = (left['value']
                         and not right['value']) or (not left['value']
                                                     and right['value'])

        if node.op == 'ior':
            if lr_types_union == {"structure"}:
                value = {}
                for key in left['value']:
                    value[key] = left['value'][key]
                for key in right['value']:
                    value[key] = right['value'][key]

                exprFromKey = {}
                for key in left['exprFromKey']:
                    exprFromKey[key] = left['exprFromKey'][key]
                for key in right['exprFromKey']:
                    exprFromKey[key] = right['exprFromKey'][key]

            else:
                value = left['value'] or right['value']

        if node.op == '==':
            value = left['value'] == right['value']

        if node.op == '!=':
            value = left['value'] != right['value']

        if node.op == '>=':
            value = left['value'] >= right['value']

        if node.op == '<=':
            value = left['value'] <= right['value']

        if node.op == '>':
            value = left['value'] > right['value']

        if node.op == '<':
            value = left['value'] < right['value']

        relOps = {"==", "!=", ">=", "<=", ">", "<"}

        ret = {"type": type(value).__name__, "value": value}

        if newType:
            ret['type'] = newType
        elif node.op in relOps:
            ret['type'] = "bool"
        elif left['type'] == right['type']:
            ret['type'] = left['type']

        if exprFromKey != None:
            ret['exprFromKey'] = exprFromKey

        global _fold
        if _fold:
            ret['const'] = left['const'] and right['const']
        else:
            ret['const'] = False

        ret["json"] = {"name": "({}) {}".format(ret['type'], ret['value'])}

        if not (_fold and ret['const']):
            ret["json"]["children"] = [{
                "name":
                " ",
                "children": [{
                    "name": node.op
                }, left['json']]
            }, right['json']]

        return ret
예제 #11
0
from parser import clean

GARBAGE_EXAMPLES = {
    "<>": 0,
    "<random characters>": 17,
    "<<<<>": 3,
    "<{!>}>": 2,
    "<!!>": 0,
    "<!!!>>": 0,
    "<{o\"i!a,<{i<a>": 10,
}

for g, c in GARBAGE_EXAMPLES.items():
    assert clean(g) == ('', c)