From a8b47fbcac2ba5c13b70c2c7491101991946ede9 Mon Sep 17 00:00:00 2001
From: Romain Pesche <romain@pesche.fr>
Date: Tue, 22 Oct 2019 22:36:04 +0200
Subject: [PATCH 1/2] make it flake8 compatible (ignoring Warning)

---
 disassembler.py          | 47 +++++++++++++++++++------------------
 setup.cfg                |  5 ++++
 test_execute_programs.py |  3 ---
 tma_16_assembler.py      | 50 ++++++++++++++++++++++++++++------------
 4 files changed, 65 insertions(+), 40 deletions(-)
 create mode 100644 setup.cfg

diff --git a/disassembler.py b/disassembler.py
index 181eb4a..efb736d 100644
--- a/disassembler.py
+++ b/disassembler.py
@@ -6,7 +6,7 @@ import sys
 filename = ""
 no_file = True
 for arg in sys.argv:
-    if arg[-4:] == ".tmx": # extension for TMA executables
+    if arg[-4:] == ".tmx":  # extension for TMA executables
         filename = arg
         no_file = False
 if no_file:
@@ -15,6 +15,7 @@ if no_file:
 file_context = open(filename, "rb").read()
 instructions = bytearray(file_context)
 
+
 # This function takes two 8-bit bytes, interpreting the first as the
 # most significant and the second as the least significant portions
 # of a single 16-bit value.
@@ -22,6 +23,7 @@ def combine_bytes(byte_0, byte_1):
     byte_0_shifted = byte_0 << 8
     return byte_0_shifted | byte_1
 
+
 def parse_reg_id(hex_val):
     if hex_val == 0x0A:
         return "ra"
@@ -36,6 +38,7 @@ def parse_reg_id(hex_val):
     else:
         return f"; unexpected byte {hex(hex_val)} found; might be a bug in the disassembler"
 
+
 i = 0
 while i < len(instructions):
     if instructions[i] == 0x01:
@@ -47,7 +50,7 @@ while i < len(instructions):
               + parse_reg_id(instructions[i + 2]) + " "
               + hex(instructions[i + 3])
               + hex(instructions[i + 4])[2:]
-        )
+              )
         i += 5
     elif instructions[i] == 0x03:
         print("jgr "
@@ -55,64 +58,64 @@ while i < len(instructions):
               + parse_reg_id(instructions[i + 2]) + " "
               + hex(instructions[i + 3])
               + hex(instructions[i + 4])[2:]
-        )
+              )
         i += 5
     elif instructions[i] == 0x04:
         print("add "
               + parse_reg_id(instructions[i + 1]) + " "
               + parse_reg_id(instructions[i + 2])
-        )
+              )
         i += 3
     elif instructions[i] == 0x05:
         print("sub "
               + parse_reg_id(instructions[i + 1]) + " "
               + parse_reg_id(instructions[i + 2])
-        )
+              )
         i += 3
     elif instructions[i] == 0x06:
         print("read "
               + parse_reg_id(instructions[i + 1]) + " "
               + hex(instructions[i + 2])
               + hex(instructions[i + 3])[2:]
-        )
+              )
         i += 4
     elif instructions[i] == 0x07:
         print("write "
               + parse_reg_id(instructions[i + 1]) + " "
               + hex(instructions[i + 2])
               + hex(instructions[i + 3])[2:]
-        )
+              )
         i += 4
     elif instructions[i] == 0x08:
         print("movr "
               + parse_reg_id(instructions[i + 1]) + " "
               + parse_reg_id(instructions[i + 2])
-        )
+              )
         i += 3
     elif instructions[i] == 0x09:
         print("movl "
               + parse_reg_id(instructions[i + 1]) + " "
               + hex(instructions[i + 2])
               + hex(instructions[i + 3])[2:]
-        )
+              )
         i += 4
     elif instructions[i] == 0x0A:
         print("and "
               + parse_reg_id(instructions[i + 1]) + " "
               + parse_reg_id(instructions[i + 2])
-        )
+              )
         i += 3
     elif instructions[i] == 0x0B:
         print("or "
               + parse_reg_id(instructions[i + 1]) + " "
               + parse_reg_id(instructions[i + 2])
-        )
+              )
         i += 3
     elif instructions[i] == 0x0C:
         print("xor "
               + parse_reg_id(instructions[i + 1]) + " "
               + parse_reg_id(instructions[i + 2])
-        )
+              )
         i += 3
     elif instructions[i] == 0x0D:
         print("not " + parse_reg_id(instructions[i + 1]))
@@ -127,7 +130,7 @@ while i < len(instructions):
         print("push " + parse_reg_id(instructions[i + 1]))
         i += 2
     elif instructions[i] == 0x11:
-        print("pop "  + parse_reg_id(instructions[i + 1]))
+        print("pop " + parse_reg_id(instructions[i + 1]))
         i += 2
     elif instructions[i] == 0x12:
         print("ovrf " + parse_reg_id(instructions[i + 1]))
@@ -137,9 +140,9 @@ while i < len(instructions):
         i += 1
     elif instructions[i] == 0x14:
         print("readr "
-             + parse_reg_id(instructions[i + 1]) + " "
-             + parse_reg_id(instructions[i + 2])
-        )
+              + parse_reg_id(instructions[i + 1]) + " "
+              + parse_reg_id(instructions[i + 2])
+              )
         i += 3
     elif instructions[i] == 0x15:
         print("inc " + parse_reg_id(instructions[i + 1]))
@@ -149,21 +152,21 @@ while i < len(instructions):
         i += 2
     elif instructions[i] == 0x17:
         print("writr "
-             + parse_reg_id(instructions[i + 1]) + " "
-             + parse_reg_id(instructions[i + 2])
-        )
+              + parse_reg_id(instructions[i + 1]) + " "
+              + parse_reg_id(instructions[i + 2])
+              )
         i += 3
     elif instructions[i] == 0x18:
         print("bsl "
               + parse_reg_id(instructions[i + 1])
-        )
+              )
         i += 2
     elif instructions[i] == 0x19:
         print("bsr "
               + parse_reg_id(instructions[i + 1])
-        )
+              )
         i += 2
 
     else:
-        print(f"pb {hex(combine_bytes(instructions[i], instructions[i+1]))}") # XXX: allow putting single bytes?
+        print(f"pb {hex(combine_bytes(instructions[i], instructions[i+1]))}")  # XXX: allow putting single bytes?
         i += 2
diff --git a/setup.cfg b/setup.cfg
new file mode 100644
index 0000000..203400b
--- /dev/null
+++ b/setup.cfg
@@ -0,0 +1,5 @@
+
+
+[flake8]
+max-line-length = 120
+ignore = W
diff --git a/test_execute_programs.py b/test_execute_programs.py
index af9a3ad..0bf3109 100644
--- a/test_execute_programs.py
+++ b/test_execute_programs.py
@@ -19,8 +19,6 @@ def check_vm_executable():
         pytest.skip("No rust VM available, please run :  cd tma-16-rs && cargo build")
 
 
-
-
 def launch_asm(source_file):
 
     output_file = tempfile.NamedTemporaryFile(suffix=".tmx")
@@ -34,7 +32,6 @@ def launch_asm(source_file):
         '--report'], capture_output=True)
     assert res
 
-
     stdout = res.stdout.decode('utf-8')
     stderr = res.stderr.decode('utf-8')
     return stdout, stderr
diff --git a/tma_16_assembler.py b/tma_16_assembler.py
index 4ec5ad1..1b083fb 100644
--- a/tma_16_assembler.py
+++ b/tma_16_assembler.py
@@ -4,6 +4,7 @@
 import sys
 import re
 
+
 # First we remove all the comments from the file
 def strip_comments(line):
     if ";" in line:
@@ -12,14 +13,15 @@ def strip_comments(line):
     else:
         return line
 
+
 # expand the macros
 def expand_macro_defs(token_list):
-    for i in range (0, len(token_list)):
+    for i in range(0, len(token_list)):
         try:
             if token_list[i] == "#define":
                 macro_name = token_list[i + 1]
-                macro_val  = token_list[i + 2]
-                for j in range (0, len(token_list)):
+                macro_val = token_list[i + 2]
+                for j in range(0, len(token_list)):
                     if token_list[j] == macro_name:
                         token_list[j] = macro_val
                 token_list[i] = ""
@@ -28,6 +30,7 @@ def expand_macro_defs(token_list):
         except IndexError:
             continue
 
+
 # this part is mostly copied from Stack Overflow
 # https://stackoverflow.com/questions/32834963/most-significant-byte-calculation
 def binary(i):
@@ -43,33 +46,49 @@ def binary(i):
         i >>= 1
         count -= 1
     return s
+
+
 def most_sig_8_bits(val_16_bits):
     a = binary(val_16_bits)
     b = a[0:9]
-    c = int(b,2)
+    c = int(b, 2)
     return c
+
+
 def least_sig_8_bits(val_16_bits):
     a = binary(val_16_bits)
     b = a[9:17]
-    c = int(b,2)
+    c = int(b, 2)
     return c
 
+
 def is_int_literal(token):
     return re.match(r"^[0-9]+$", token)
+
+
 def is_hex_literal(token):
     return re.match(r"^0x[0-9a-fA-F]+$", token)
+
+
 def is_bin_literal(token):
     return re.match(r"^0b[0-1]+$", token)
+
+
 def is_char_literal(token):
     return re.match(r"^'.'|'\\[nstr0]'$", token)
+
+
 def is_chword_literal(token):
     return re.match(r'^".."|".\\[nstr0]"|"\\[nstr0]."|"\\[nstr0]\\[nstr0]"$', token)
+
+
 def is_reg_literal(token):
     if token in ["ra", "rb", "rc", "rd", "ip"]:
         return True
     else:
         return False
 
+
 def assemble(input_file, output_file=None):
 
     file_lines = open(input_file).read().split('\n')
@@ -84,13 +103,12 @@ def assemble(input_file, output_file=None):
             if token != '':
                 tokens.append(token)
 
-
     expand_macro_defs(tokens)
 
     # then we turn it into TMA-16 machine code
     machine_code_bytes = []
 
-    i = 0 # index of currently selected token
+    i = 0  # index of currently selected token
     for token in tokens:
         # The instruction set of the TMA-16 is based on a
         # pen-and-paper model I made earlier called the TMA-8. I
@@ -242,7 +260,7 @@ def assemble(input_file, output_file=None):
             reg_bytes = reg_token_to_bytes[token]
             machine_code_bytes.append(reg_bytes)
 
-        elif token == "alloc": # allocate (assembler directive to allocate a certain amount of memory)
+        elif token == "alloc":  # allocate (assembler directive to allocate a certain amount of memory)
             mem_amount = 0
             if is_int_literal(tokens[i + 1]):
                 mem_amount = int(tokens[i + 1])
@@ -254,9 +272,9 @@ def assemble(input_file, output_file=None):
                 print(f"error: {input_file}: invalid literal `{tokens[i + 1]}`")
                 exit(1)
 
-            tokens[i + 1] = "" # so we don't have a stray number in the code
+            tokens[i + 1] = ""  # so we don't have a stray number in the code
 
-            for i in range (0, mem_amount):
+            for i in range(0, mem_amount):
                 machine_code_bytes.append(0x00)
 
         else:
@@ -285,10 +303,11 @@ def assemble(input_file, output_file=None):
                 'inc': 0x15,     # increment
                 'dec': 0x16,     # increment
                 'writr': 0x17,   # write to an address from a register
-                'bsl':  0x18,    # bitshift left
-                'bsr':  0x19,    # bitshift right
+                'bsl': 0x18,    # bitshift left
+                'bsr': 0x19,    # bitshift right
                 'get': 0x20,     # get input
-                'pb':  None,     # put byte (not an instruction, just an assembler directive to write a byte at that position)
+                'pb': None,     # put byte
+                                 # (not an instruction, just an assembler directive to write a byte at that position)
             }
 
             try:
@@ -300,8 +319,7 @@ def assemble(input_file, output_file=None):
             if byte_token:
                 machine_code_bytes.append(byte_token)
 
-        i += 1 # update index counter
-
+        i += 1  # update index counter
 
     # now create the executable
     new_file_name = output_file or input_file[:-4] + ".tmx"
@@ -324,6 +342,7 @@ def assemble(input_file, output_file=None):
                 line_bytes += 1
         print("")
 
+
 def main():
     if len(sys.argv) < 2:
         print("Error! No assembly files input")
@@ -334,5 +353,6 @@ def main():
     output_file = sys.argv[2] if len(sys.argv) == 3 else None
     assemble(input_file, output_file)
 
+
 if __name__ == '__main__':
     main()
-- 
GitLab


From edece4148bc2bf0752d7d87db87bf48551f053df Mon Sep 17 00:00:00 2001
From: Romain Pesche <romain@pesche.fr>
Date: Tue, 22 Oct 2019 22:37:19 +0200
Subject: [PATCH 2/2] add flake8 pipeline

---
 .circleci/config.yml | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/.circleci/config.yml b/.circleci/config.yml
index d6d1326..ddeaa54 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -1,12 +1,10 @@
 version: 2
 jobs:
-  build:
+  tests:
     docker:
     - image: rust:latest
-
     steps:
       - checkout
-
       - run:
           name: install dependencies
           command: |
@@ -14,8 +12,28 @@ jobs:
               cargo build
               apt update
               apt install python3-pytest -y
-
       - run:
           name: run tests
           command: |
             python3 -m pytest test_execute_programs.py
+
+  lint:
+    docker:
+    - image: python:3.7
+    steps:
+      - checkout
+      - run:
+          name: install dependencies
+          command: pip install flake8
+      - run:
+          name: flake8 lint
+          command: flake8
+
+workflows:
+  version: 2
+  test_and_lint:
+    jobs:
+      - lint
+      - tests:
+          requires:
+              - lint
-- 
GitLab