implement_assembly_interpreter_by_example.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from evaluator import *
  2. DESCRIPTION = "Test if the model can implement an interpreter for a new assembly language given an example."
  3. TAGS = ['code', 'python']
  4. primes = """
  5. SET R1 2 // Starting number to check for prime
  6. start_find_primes:
  7. JMP is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result
  8. ready_prime:
  9. SET R7 1
  10. EQ R2 R7 // Check if R2 is 1 (prime)
  11. JF increment // If not prime, skip storing and increment the number
  12. // Store prime number in memory and increment count
  13. STORE R1 R8 // Store prime number at address pointed by R8
  14. INC R8 // Increment prime count
  15. // Check if 100 primes are found
  16. SET R7 100
  17. EQ R8 R7
  18. JF increment // If not 100 primes found yet, continue finding
  19. JMP end // If 100 primes found, end program
  20. increment:
  21. INC R1 // Increment number to check for prime
  22. JMP start_find_primes // Check next number
  23. is_prime:
  24. SET R2 1 // Assume number is prime initially
  25. SET R3 2 // Start divisor from 2
  26. start_loop: // Label to start the loop
  27. // Check if we have exceeded the square root of R1
  28. MUL R4 R3 R3 // R4 = R3 * R3
  29. GT R4 R1 // Set flag if R4 > R1
  30. JT is_prime_end // If not exceeded, continue; else, end loop
  31. MOD R6 R1 R3 // R6 = R1 % R3
  32. SET R7 0
  33. EQ R7 R6 // Check if R6 is 0
  34. JT not_prime // If yes, number is not prime
  35. INC R3 // Increment divisor
  36. JMP start_loop // Repeat loop
  37. not_prime:
  38. SET R2 0 // Set result to 0 (not prime)
  39. is_prime_end:
  40. JMP ready_prime
  41. end:
  42. """
  43. code = """
  44. SET R1 0
  45. SET R2 1
  46. loop:
  47. MUL R3 R2 R2
  48. STORE R3 R1
  49. INC R1
  50. INC R2
  51. SET R3 20
  52. EQ R1 R3
  53. JF loop
  54. """
  55. question = f"""Here is an example program from a new assmebly language I made up that computes primes:
  56. ```
  57. {primes}
  58. ```
  59. ite me a python interpreter `evaluate(str)` that returns the resulting memory state after running the program. For example, `evaluate(program)` should return `[2, 3, 5, 7, ...]` for the above program.
  60. """
  61. test_case, answer = make_python_test([(f'evaluate("""{code}""")[:10]', "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]"),
  62. (f'evaluate("""{primes}""")[:10]', "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]")
  63. ])
  64. TestImplementAssemblyByExample = question >> LLMRun() >> ExtractCode(lang="python") >> Echo() >> PythonRun(test_case) >> SubstringEvaluator(answer)
  65. if __name__ == "__main__":
  66. print(run_test(TestImplementAssemblyByExample))