In computer science, the expressions code as data and data as code refer to the interchangeable nature of code and data. Specifically, "code is data" refers to the idea that source code written in a programming language can be manipulated as data, such as a sequence of characters or an abstract syntax tree (AST), and it has an execution semantics only in the context of a given compiler or interpreter. The expression "data is code" refers to the idea that an arbitrary data structure such as a list of integers can be interpreted or compiled using a specialized language semantics.[1] The notions are often used in the context of Lisp-like languages that use S-expressions as their main syntax, as writing programs using nested lists of symbols makes the interpretation of the program as an AST quite transparent (a property known as homoiconicity).[2][3]
These ideas are generally used in the context of what is called metaprogramming, writing programs that treat other programs as their data.[4][5] For example, code-as-data allows the serialization of first-class functions in a portable manner.[6] Another use case is storing a program in a string, which is then processed by a compiler to produce an executable.[4] More often there is a reflection API that exposes the structure of a program as an object within the language, reducing the possibility of creating a malformed program.[7]
In computational theory, Kleene's second recursion theorem provides a form of code-is-data, by proving that a program can have access to its own source code.[8]
Code-as-data is also a principle of the Von Neumann architecture, since stored programs and data are both represented as bits in the same memory device.[4] This architecture offers the ability to write self-modifying code.[citation needed] It also opens the security risk of disguising a malicious program as user data and then using an exploit to direct execution to the malicious program.[9]
In declarative programming, the data-as-code principle is often applied. For example, configuration scripts, domain-specific languages and markup languages are cases where program execution is controlled by data elements that are not clearly sequences of commands.[citation needed]
until it is dynamically compiled, dynamic code is data. Similarly, lists in Lisp and strings in Perl are data, but they can be evaluated as code
Original source: https://en.wikipedia.org/wiki/Code as data.
Read more |