<commit_before>#!/usr/local/bin/python2.7
import sys


class Cmd_arithmetic:
    def __init__(self, code):
        self.code = code

    def generate(self):
        return self.code


class Cmd_push:
    def __init__(self, code):
        self.code = code
        self.segment = 'constant'
        self.index = 0

    def generate(self):
        code = self.code
        if self.segment == 'constant':
            code = '@' + str(self.index) + '\n' + self.code
        return code


cmd_add = Cmd_arithmetic(
    """@SP
M=M-1
A=M
D=M	// D is value at top of the stack
@SP
A=M-1	// Point to next highest on stack
M=D+M  // Add D to that guy
"""
)

cmd_push = Cmd_push(
    """D=A
@SP
A=M
M=D
@SP
M=M+1
"""
)


def parse(line):
    # strip comments
    no_comments = line.split('#', 1)[0]
    no_comments = no_comments.split('//', 1)[0]
    tokens = no_comments.split()
    return (tokens)


def writecode(tokens):
    if not tokens:
        return
    asm_file.write('//')
    for token in tokens:
        asm_file.write(' ' + str(token))
    asm_file.write('\n')
    if tokens[0] == 'add':
        asm_file.write(cmd_add.generate())
    if tokens[0] == 'push':
        cmd_push.segment = tokens[1]
        cmd_push.index = tokens[2]
        asm_file.write(cmd_push.generate())


cmd_name = sys.argv[0].split('/')[-1]

if len(sys.argv) != 2:
    sys.exit(cmd_name + " error: pass me one arg, the name of the file to compile")

intermediate_filename = sys.argv[1]

try:
    intermediate_code = open(intermediate_filename, "r")
except:
    sys.exit(cmd_name + " error. I couldn't open " + intermediate_filename + " for reading!")

asm_filename = intermediate_filename.replace('.vm', '.asm')
<commit_msg>Assembler is indented as a prelude to looping<commit_after>#!/usr/local/bin/python2.7
import sys


class Cmd_arithmetic:
    def __init__(self, code):
        self.code = code

    def generate(self):
        return self.code


class Cmd_push:
    def __init__(self, code):
        self.code = code
        self.segment = 'constant'
        self.index = 0

    def generate(self):
        code = self.code
        if self.segment == 'constant':
            code = '    @' + str(self.index) + '\n' + self.code
        return code


cmd_add = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M     // D is value at top of the stack
    @SP
    A=M-1   // Point to next highest on stack
    M=D+M   // Add D to that guy
"""
)

cmd_sub = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M     // D is value at top of the stack
    @SP
    A=M-1   // Point to next highest on stack
    M=D-M   // Sub that guy from D
"""
)

cmd_neg = Cmd_arithmetic(
    """    @SP
    A=M
    D=-M    // D is value at top of the stack
    M=D
"""
)

cmd_eq = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M     // D is value at top of the stack
    @SP
    A=M-1   // Point to next highest on stack
    A=M
    D;JEQ
"""
)

cmd_push = Cmd_push(
    """    D=A
    @SP
    A=M
    M=D
    @SP
    M=M+1
"""
)

def parse(line):
    # strip comments
    no_comments = line.split('#', 1)[0]
    no_comments = no_comments.split('//', 1)[0]
    tokens = no_comments.split()
    return (tokens)


def writecode(tokens):
    if not tokens:
        return
    asm_file.write('//')
    for token in tokens:
        asm_file.write(' ' + str(token))
    asm_file.write('\n')
    if tokens[0] == 'add':
        asm_file.write(cmd_add.generate())
    if tokens[0] == 'sub':
        asm_file.write(cmd_sub.generate())
    if tokens[0] == 'neg':
        asm_file.write(cmd_neg.generate())
    if tokens[0] == 'eq':
        asm_file.write(cmd_eq.generate())
    if tokens[0] == 'push':
        cmd_push.segment = tokens[1]
        cmd_push.index = tokens[2]
        asm_file.write(cmd_push.generate())


cmd_name = sys.argv[0].split('/')[-1]

if len(sys.argv) != 2:
    sys.exit(cmd_name + " error: pass me one arg, the name of the file to compile")

intermediate_filename = sys.argv[1]

try:
    intermediate_code = open(intermediate_filename, "r")
except:
    sys.exit(cmd_name + " error. I couldn't open " + intermediate_filename + " for reading!")

asm_filename = intermediate_filename.replace('.vm', '.asm')

{'fraction_non_alphanumeric': 0.14561487130600573, 'fraction_numerical': 0.010247855100095328, 'mean_word_length': 2.2995283018867925, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1947932', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1513, 'n_words': 385}