2025-09-21 21:08:30 +00:00
|
|
|
from copapy import Write, const
|
2025-10-01 21:09:49 +00:00
|
|
|
import copapy
|
2025-09-30 21:18:58 +00:00
|
|
|
import subprocess
|
2025-10-01 21:09:49 +00:00
|
|
|
import struct
|
|
|
|
|
from copapy import binwrite as binw
|
|
|
|
|
|
2025-08-29 20:58:50 +00:00
|
|
|
|
2025-09-30 21:18:58 +00:00
|
|
|
def run_command(command: list[str], encoding: str = 'utf8') -> str:
|
|
|
|
|
process = subprocess.Popen(command, stdout=subprocess.PIPE)
|
|
|
|
|
output, error = process.communicate()
|
|
|
|
|
|
|
|
|
|
assert error is None, f"Error occurred: {error.decode(encoding)}"
|
|
|
|
|
return output.decode(encoding)
|
2025-09-26 21:25:51 +00:00
|
|
|
|
2025-10-01 21:09:49 +00:00
|
|
|
|
|
|
|
|
def test_example():
|
|
|
|
|
c1 = 1.11
|
|
|
|
|
c2 = 2.22
|
2025-08-29 20:58:50 +00:00
|
|
|
|
|
|
|
|
i1 = c1 * 2
|
|
|
|
|
i2 = i1 + 3
|
|
|
|
|
|
|
|
|
|
r1 = i1 + i2
|
|
|
|
|
r2 = c2 + 4 + c1
|
|
|
|
|
|
2025-10-01 21:09:49 +00:00
|
|
|
en = {'little': '<', 'big': '>'}['little']
|
|
|
|
|
data = struct.pack(en + 'f', r1)
|
|
|
|
|
print("example r1 " + ' '.join(f'{b:02X}' for b in data))
|
|
|
|
|
|
|
|
|
|
data = struct.pack(en + 'f', r2)
|
|
|
|
|
print("example r2 " + ' '.join(f'{b:02X}' for b in data))
|
|
|
|
|
|
|
|
|
|
# assert False
|
|
|
|
|
# example r1 7B 14 EE 40
|
|
|
|
|
# example r2 5C 8F EA 40
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_compile():
|
|
|
|
|
|
|
|
|
|
print(run_command(['bash', 'build.sh']))
|
|
|
|
|
|
2025-10-02 21:10:05 +00:00
|
|
|
#c1 = const(1.11)
|
|
|
|
|
#c2 = const(2.22)
|
2025-10-01 21:09:49 +00:00
|
|
|
|
|
|
|
|
#i1 = c1 * 2
|
|
|
|
|
#i2 = i1 + 3
|
|
|
|
|
|
|
|
|
|
#r1 = i1 + i2
|
|
|
|
|
#r2 = c2 + 4 + c1
|
|
|
|
|
|
|
|
|
|
#out = [Write(r1), Write(r2)]
|
|
|
|
|
|
2025-10-02 21:10:05 +00:00
|
|
|
c1 = const(4)
|
|
|
|
|
c2 = const(2)
|
2025-10-01 21:09:49 +00:00
|
|
|
i1 = c1 * 2
|
|
|
|
|
r1 = i1 + 7
|
2025-10-02 21:10:05 +00:00
|
|
|
r2 = i1 + 9
|
|
|
|
|
out = [Write(r1), Write(r2)]
|
2025-10-01 21:09:49 +00:00
|
|
|
|
|
|
|
|
il = copapy.compile_to_instruction_list(out)
|
|
|
|
|
|
|
|
|
|
copapy.read_variable(il, r1)
|
2025-10-02 21:10:05 +00:00
|
|
|
copapy.read_variable(il, r2)
|
2025-10-01 21:09:49 +00:00
|
|
|
|
|
|
|
|
il.write_com(binw.Command.READ_DATA)
|
|
|
|
|
il.write_int(0)
|
|
|
|
|
il.write_int(36)
|
2025-08-29 20:58:50 +00:00
|
|
|
|
2025-10-01 21:09:49 +00:00
|
|
|
# run program command
|
|
|
|
|
il.write_com(binw.Command.END_PROG)
|
2025-08-29 20:58:50 +00:00
|
|
|
|
2025-09-20 21:25:07 +00:00
|
|
|
print('#', il.print())
|
2025-08-29 20:58:50 +00:00
|
|
|
|
2025-09-30 21:19:35 +00:00
|
|
|
il.to_file('test.copapy')
|
2025-09-30 21:18:58 +00:00
|
|
|
|
|
|
|
|
result = run_command(['./bin/runmem2', 'test.copapy'])
|
|
|
|
|
print(result)
|
|
|
|
|
|
2025-10-01 21:09:49 +00:00
|
|
|
assert 'Return value: 1' in result
|
2025-09-26 21:25:51 +00:00
|
|
|
|
2025-08-29 20:58:50 +00:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2025-10-01 21:09:49 +00:00
|
|
|
test_compile()
|