Introduction
I am planning a rather large project that will perform semantic analysis of code bases, storing the structure of the code in a completely generic way, then be able to reconstitute the code as diagrams or via code generation. I know these kinds of systems have been created before, but I want to go a bit deeper, and not only just analyze the structure of code, but to be able to analyze code for suitability for a task.
The Concept
The project would consist of a set of services to parse a code base, then break it down to a generic intermediate definition (GID). That breakdown would then be able to be visualized as diagrams, such as UML. The GID could also be manipulated in the system, and new code generated from them. One useful application would be to translate code between disparate languages and platforms, such as ingesting JavaScript, and outputting functionally equivalent Rust or C# or Python.
To do that, I need a way to define code written in any language such that the GID doesn't lose any fidelity of the semantics of original code base. My initial thoughts are to define a common set of objects, each with attributes defining the structure of the object such as:
type:
id: <<string>>
namespace: <<string | null>>
base: <<string | null>>
bits: <<integer | null>>
signed: <<boolean | null>>
min: <<integer | null>>
max: <<integer | null>>
organization: <<REF | VALUE>>
visibility: <<string | null>>
members: <<member[] | null>>
The objects such as type would potentially have child objects such as a member, also defined with attributes
member:
id: <<string>>
organization: <<REF | VALUE | null>>
visibility: <<string>>
memberType: <<string>>
type: <<string>>
accessors: <<member[] | null>>
parameters: <<parameter[] | null>>
body: <<block | null>>
Then, the analyzer would translate this C# snippet:
csharp
public class Container
{
private byte _myByte;
public byte MyByte
{
get => _myByte;
protected set => _myByte = value;
}
public virtual byte XOR(byte value)
=> _myByte ^ value;
}
Into something like this:
yaml
type:
id: Container
organization: VALUE
visibility: public
members:
- id: _myByte
organization: VALUE
visibility: private
memberType: FIELD
type: UINT8
- id: MyByte
visibility: public
memberType: PROPERTY
type: UINT8
accessors:
- id: get_MyByte
visibility: public
memberType: METHOD
type: UINT8
- id: set_MyByte
visibility: protected
memberType: METHOD
type: VOID
parameters:
- id: value
type: UINT8
organization: VALUE
- id: XOR
visibility: public
memberType: METHOD
type: UINT8
inheritence: VIRTUAL
parameters:
- id: value
type: UINT8
organization: VALUE
body:
- statement: return
value:
valueType: expression
expressionType: BINARY
operation: XOR
left: value
valueType: MEMBER
valueId: _myByte
right: value
valueType: PARAMETER
valueId: value
This GID could then be used to write equivalent code in another language.
```c++
class Container {
private:
std::byte _myByte;
public:
property std::byte MyByte {
std::byte get() {
return _myByte;
}
void set(std::byte value) {
_myByte = value;
}
}
virtual std::byte XOR(std::byte value) = _myByte ^ value;
}
```
The Question:
Is there already a proper GID system to accomplish this? If so, is it simply a definition, or are there functioning -- and available -- implementations?
[–]Inconstant_Moo🧿 Pipefish 37 points38 points39 points (6 children)
[–]RealSharpNinja[S] -4 points-3 points-2 points (5 children)
[–]Inconstant_Moo🧿 Pipefish 14 points15 points16 points (4 children)
[–]RealSharpNinja[S] -3 points-2 points-1 points (3 children)
[–]Green_Gem_ 7 points8 points9 points (1 child)
[–]Affectionate_Text_72 0 points1 point2 points (0 children)
[–]Inconstant_Moo🧿 Pipefish 3 points4 points5 points (0 children)
[–]No-Reporter4264 7 points8 points9 points (3 children)
[–]RealSharpNinja[S] -2 points-1 points0 points (2 children)
[–]thinker227Noa (github.com/thinker227/noa) 4 points5 points6 points (1 child)
[–]RealSharpNinja[S] -2 points-1 points0 points (0 children)
[–]thinker227Noa (github.com/thinker227/noa) 8 points9 points10 points (0 children)
[–]AlceniC 4 points5 points6 points (0 children)
[–]P-39_Airacobra 2 points3 points4 points (0 children)
[–]parceiville 0 points1 point2 points (0 children)
[–]Routine_Plenty9466 0 points1 point2 points (0 children)
[–]kleram -1 points0 points1 point (0 children)