Json Schema for LLMs

This commit is contained in:
Michael Schimmel
2026-01-06 20:55:11 +01:00
parent 4618573d6c
commit 8a29cf7f74
8 changed files with 263 additions and 179 deletions
+5 -10
View File
@@ -98,7 +98,8 @@ type
function Run(const ANode: IAstNode; const Params: TArray<IIdentifierNode> = []; const Args: TArray<TDataValue> = []): TDataValue;
procedure Define(const Name: String; const AScript: IAstNode);
// UPDATED: Now supports Documentation
procedure Define(const Name: String; const AScript: IAstNode; const Doc: string = '');
property Environment: IEnvironment read FEnvironment;
property RootScope: IExecutionScope read GetRootScope;
@@ -261,11 +262,12 @@ begin
Result := FEnvironment.CreateEnvironment;
end;
procedure TAstEnvironment.Define(const Name: String; const AScript: IAstNode);
procedure TAstEnvironment.Define(const Name: String; const AScript: IAstNode; const Doc: string);
begin
var compiled := Compile(AScript);
var linked := Link(compiled);
RootScope.Define(Name, linked.Func([]), compiled.StaticType);
// Pass the documentation to the scope
RootScope.Define(Name, linked.Func([]), compiled.StaticType, Doc);
end;
function TAstEnvironment.ExpandMacros(const Node: IAstNode): IAstNode;
@@ -425,9 +427,6 @@ begin
if lg.HasErrors then
raise ECompilationFailed.Create(lg.GetEntries);
// Note: If types are Unknown but no errors were logged (edge case), we proceed.
// But Binder/TypeChecker logic ensures errors are logged for Unknowns that matter.
var specialized := Specialize(typedNode);
Result := specialized.AsLambdaExpression;
@@ -471,7 +470,6 @@ begin
var cExecutionStrategy := FExecutionStrategy;
// The Glue: This callback performs the full compilation pipeline for a macro argument expression.
// It binds the node to the layout of the temporary macro scope and then executes it.
var macroEvaluator: TMacroEvaluatorProc :=
function(const Scope: IExecutionScope; const ANode: IAstNode): TDataValue
var
@@ -485,15 +483,12 @@ begin
tempLog := TCompilerLog.Create;
// 1. Binding
// Note: Scope is dynamic (from TMacroExpander), so Descriptor.Layout describes current variables.
boundSubAst := TAstBinder.Bind(Scope.Descriptor.Layout, ANode, tmpLayout, tempLog);
// Macro execution is strictly fail-fast. If we can't bind arguments, we can't run the macro.
if tempLog.HasErrors then
raise EMacroException.Create('Macro Argument Error: ' + tempLog.GetEntries[0].Message);
// 2. Scope Matching
// Create a child scope to ensure correct depth resolution (Binder sees Layout at depth 0 relative to itself)
scratchScope := TScope.CreateScope(Scope, nil, nil);
// 3. Execution