ch3njust1n / smart

Self-modifying code at runtime with Large Language Models
MIT License
4 stars 0 forks source link

[LLM] Check syntax of generated code #43

Closed ch3njust1n closed 1 year ago

ch3njust1n commented 1 year ago

Use python's ast package to check the syntax of generated code at runtime.

import ast

def is_code_correct(code):
    try:
        ast.parse(code)
        return True
    except SyntaxError:
        return False

# test
print(is_code_correct('print("Hello, world!")'))  # True
print(is_code_correct('print("Hello, world!'))  # False - missing closing quote