2025-10-09 20:50:57 +00:00
|
|
|
from copapy import Write, CPVariable
|
2025-10-08 20:47:49 +00:00
|
|
|
import copapy
|
|
|
|
|
import subprocess
|
|
|
|
|
import struct
|
|
|
|
|
from copapy import binwrite
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
def function(c1):
|
|
|
|
|
r1 = c1 / 2
|
|
|
|
|
|
|
|
|
|
return [r1]
|
|
|
|
|
|
|
|
|
|
def test_compile():
|
|
|
|
|
|
2025-10-09 20:50:57 +00:00
|
|
|
c1 = CPVariable(16)
|
2025-10-08 20:47:49 +00:00
|
|
|
|
|
|
|
|
ret = function(c1)
|
|
|
|
|
|
|
|
|
|
out = [Write(r) for r in ret]
|
|
|
|
|
|
|
|
|
|
il, _ = copapy.compile_to_instruction_list(out, copapy.generic_sdb)
|
|
|
|
|
|
|
|
|
|
# run program command
|
|
|
|
|
il.write_com(binwrite.Command.RUN_PROG)
|
|
|
|
|
|
|
|
|
|
il.write_com(binwrite.Command.READ_DATA)
|
|
|
|
|
il.write_int(0)
|
|
|
|
|
il.write_int(36)
|
|
|
|
|
|
2025-10-12 21:21:34 +00:00
|
|
|
il.write_com(binwrite.Command.END_COM)
|
2025-10-08 20:47:49 +00:00
|
|
|
|
|
|
|
|
print('* Data to runner:')
|
|
|
|
|
il.print()
|
|
|
|
|
|
|
|
|
|
il.to_file('bin/test.copapy')
|
|
|
|
|
|
|
|
|
|
result = run_command(['bin/coparun', 'bin/test.copapy'])
|
|
|
|
|
print('* Output from runner:')
|
|
|
|
|
print(result)
|
|
|
|
|
|
|
|
|
|
assert 'Return value: 1' in result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
#test_example()
|
|
|
|
|
test_compile()
|