[SOLVED] Is this possible in Free Pascal, Delphi or is it AI hallucination ? lazarus / 21-General ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2025-07-31 04:16 Hey Y'All, Was having a good session about tokenisation, AST building and expression evaluation, and on the Expression Parser unit that ChatGPT suggested I got Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  SysUtils, Classes, Variants; type  TExpressionNodeType = (entBinary, entUnary, entLiteral, entVariable);   TBinaryOperator = (boEqual, boNotEqual, boAnd, boOr, boLess, boLessEqual, boGreater, boGreaterEqual);  TUnaryOperator = (uoNot);   TExpressionNode = class    NodeType: TExpressionNodeType;    case TExpressionNodeType of      entBinary: (Op: TBinaryOperator; Left, Right: TExpressionNode);      entUnary:  (UnOp: TUnaryOperator; Operand: TExpressionNode);      entLiteral: (LiteralValue: String);      entVariable: (VariableName: String);  end; This, of course, gives an error because it's not expecting a case right there. I'm also aware that we can use a case in a record, when I changed that declaration from class to record, it complained that the record was not completely defined, which makes sense because it's referencing itself. So, my question is as follows: Is there a way to achieve this in Free Pascal, or is this a Delphi-ism, or is this just the AI hallucinating? Right now I changed it thus: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { TExpressionNode }  TExpressionNode = class  private    FNodeType: TExprNodeType;    FOp: TBinaryOperator;    FLeft: TExpressionNode;    FRight: TExpressionNode;    FUnOp: TUnaryOperator;    FOperand: TExpressionNode;    FLiteralValue: String;    FVariableName: String;  protected  public    property NodeType: TExprNodeType read FNodeType write FNodeType;    property Op: TBinaryOperator read FOp write FOp;  property Left: TExpressionNode read FLeft write FLeft;    property Right: TExpressionNode read FRight write FRight;    property UnOp: TUnaryOperator read FUnOp write FUnOp;    property Operand: TExpressionNode read FOperand write FOperand;    property LiteralValue: String read FLiteralValue write FLiteralValue;    property VariableName: String read FVariableName write FVariableName;  published  end; And it works. At least it compiles with no error and from a couple of simple tests, doesn't go boom. Cheers, Gus ---------------------------------------------------------------------- egsuh · 2025-07-31 04:49 My solution is OOP approach --- using polymorphism. TExpressionNodeType enumerates all kind of node types. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type    TExpressionNode = class         LeftChild,        RichtChild : TExpressionNode;          property NodeType : TExpressionNodeType;        function Evaluate: TExpressionNode; virtual;     end;      TVariable = class (TExpressionNode)         ...         function Evaluate: TExpressionNode; override;      end;      TOperand = class(TExpressionNode);           ....          function Evaluate: TExpressionNode; override;      end; This requires a little bit imagination. You have to decide, e.g. whether to put LiteralValue as a TExpressionNode or define a descendant like            TLiteralValue = class (TExpressionNode) depending on how you will use them. ---------------------------------------------------------------------- Thaddy · 2025-07-31 04:55 The record can be a member of the class? I expect your original proposal will work as you expected. Like so?:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$ifdef fpc}{$mode delphi}{$endif}uses classes;type  TExpressionNodeType = (entBinary, entUnary, entLiteral, entVariable);   TBinaryOperator = (boEqual, boNotEqual, boAnd, boOr, boLess, boLessEqual, boGreater, boGreaterEqual);  TUnaryOperator = (uoNot);  TLiteralValue = type ShortString;  TVariableName = type ShortString;   IExpressionNode= Interface  ['{F0DE9818-97BF-4A57-8838-67F72F402707}']  // Add methodss you need;  // Interface handles memory management  // The expressions can become quite complex  end;   TExpressionNode = class(TInterfacedObject, IExpressionNode)    NodeType:record      case TExpressionNodeType of        entBinary: (Op: TBinaryOperator; Left, Right: TExpressionNode);        entUnary:  (UnOp: TUnaryOperator;  Operand: TExpressionNode);        entLiteral: (LiteralValue: ShortString);        entVariable: (VariableName: ShortString);      end;   public      constructor Create(const operand:TBinaryOperator); overload;      constructor Create(const operand:TUnaryOperator); overload;      constructor Create(const operand:TLiteralValue);overload;      constructor Create(const operand:TVariableName);overload;      constructor Create(const operand:TBinaryOperator;const left,right:IExpressionNode); overload;    constructor Create(const operand:TUnaryOperator;const node:IExpressionNode); overload;   end; { TExpressionNode } constructor TExpressionNode.Create(const operand: TBinaryoperator);begin  Inherited create;  NodeType.OP := operand;end; constructor TExpressionNode.Create(const operand: TUnaryoperator);begin  Inherited create;  NodeType.UnOP := operand;end; constructor TExpressionNode.Create(const operand: TLiteralValue);begin  Inherited create;  Nodetype.LiteralValue := operand;end; constructor TExpressionNode.Create(const operand: TVariableName);begin  Inherited create;  Nodetype.VariableName := operand;end; constructor TExpressionNode.Create(const operand: TUnaryOperator;  const node: IExpressionNode);begin  create(operand);  // do something with nodeend; constructor TExpressionNode.Create(const operand: TBinaryOperator; const left,  right: IExpressionNode);begin  create(operand);  // do something with left, rightend; var  EN:IExpressionNode; // The interface, not the class!begin  EN:= TExpressionNode.Create(boEqual);end.I did not look at the other examples yet, but the above compiles. ---------------------------------------------------------------------- Thaddy · 2025-07-31 06:34 Added constructors and interface. ---------------------------------------------------------------------- Thaddy · 2025-07-31 07:25 Forgot to explain why I chose the interface approach. Since expressions can become quite complex, I designed it such that the memory management is done through the interface. You can then use code like this:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  EN:IExpressionNode;begin EN:= TExpressionNode.Create(uoNot,TExpressionNode.Create(boEqual) as IExpressionNode); No leaks. I like your original idea a lot. I think I use it and finish it sometime. I think I will extend it with operators, such that more complexity is hidden. ---------------------------------------------------------------------- PascalDragon · 2025-07-31 19:23 Quote from: Gustavo 'Gus' Carreno on July 31, 2025, 06:16:52 amSo, my question is as follows: Is there a way to achieve this in Free Pascal, or is this a Delphi-ism, or is this just the AI hallucinating? The provided solution as-is is a hallucination. For a proper solution see Thaddy's or egsuh's suggestions (I'd go with the polymorphism, the compiler for example does as well). ---------------------------------------------------------------------- JdeHaan · 2025-07-31 19:33 I once created a similar solution with interfaces: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program variantclassrec; {$mode objfpc}{$H+} uses //heaptrc,  {$IFDEF UNIX}  cthreads,  {$ENDIF}  Classes  { you can add units after this }; type   IExpression = interface  end;   TExpression = class(TInterfacedObject, IExpression)  public type    TExprKind = (ekNumber, ekAdd, ekMul);   public    constructor Num(const aValue: Integer);    constructor Add(Op1, Op2: IExpression);    constructor Mul(Op1, Op2: IExpression);   public    Value: record      case Kind: TExprKind of      ekNumber: (Number: Integer);        ekAdd,        ekMul:    (Op1, Op2: TExpression);      end;  end;  { TExpression } constructor TExpression.Num(const aValue: Integer);begin  Value.Kind := ekNumber;  Value.Number := aValue;end; constructor TExpression.Add(Op1, Op2: IExpression);begin  Value.Kind := ekAdd;  Value.Op1 := Op1 as TExpression;  Value.Op2 := Op2 as TExpression;end; constructor TExpression.Mul(Op1, Op2: IExpression);begin  Value.Kind := ekMul;  Value.Op1 := Op1 as TExpression;  Value.Op2 := Op2 as TExpression;end; function Evaluate(Expr: IExpression): Integer;begin  with (Expr as TExpression).Value do  case Kind of    ekNumber: Result := Number;    ekAdd: Result := Evaluate(Op1) + Evaluate(Op2);    ekMul: Result := Evaluate(Op1) * Evaluate(Op2);  end;end; var  four, five, two: IExpression;  sum, product: IExpression; begin  four := TExpression.Num(4);  five := TExpression.Num(5);   sum := TExpression.Add(four, five);   two := TExpression.Num(2);  product := TExpression.Mul(sum, two);   WriteLn(Evaluate(product));end. ---------------------------------------------------------------------- Warfley · 2025-07-31 21:12 See the example in here: https://forum.lazarus.freepascal.org/index.php/topic,68839.15.html Its not quite possible yet but I made a merge request for the FPC which allows exactly that. It should also be noted that this is exactly the kind of use case which extended pascal had on mind when extending on variant records. Extended pascal exactly defines the lifetimes of the branches through the selector Edit: my bad, after getting some sleep I saw you are using a class, not a record. I thought you were asking about being able to use string in the variant part. For classes the main polymorphic mechanism is inheritance, while for records it's variant parts. So short answer, no it doesn't work like that, but if you want something like that for locality of functionality (classes spread their polymorphism over multiple virtual functions, whereas with records you'll end up with a single case statement), use a record ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2025-08-04 01:01 Hey Y'all, Took me a while to decide to watch this: The Big OOPS due to the fact that it's 2 and 1/2 hours long. Now that I did, there is mention of this being a possible thing implemented in like the 60s or even the 50s in something called SketchPad. In the video, this stuff takes about 1% of it. So, yeah, maybe a sideways note on this, but I think we should watch that video. We are prone to be OOP beats, some of us, and we should know about the path not taken in terms of the things we now crave but were removed by someone in a very early stage. And the video is pack full of the background and the decisions that make our current programming context. This video is also something all programming language creators/maintainers should watch. It does give these peeps a bit more ideas on how to correct some of the decisions that have us kinda stuck right now. Just my humble opinion!! Cheers, Gus ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2025-08-04 03:19 Hey Y'all, BTW, I completely forgot my manners and did not thank anyone for the AWESOME solutions you peeps provided!! That's on me, and I profusely apologise for this faux pas!!! To the peeps that gave me solutions, I'm ever so thankful !!! Side note: I've discovered that we have something called TFPExpressionParser, in our included batteries. If I can solve my expression needs with that, then the AI provided expression parser is completely superfluous!!! I need to do some testing, and if it ticks all my boxes, I'll replace my AI suggested one with TFPExpressionParser. I'm hoping that it will solve the fact that the AI one is not able to understand array variables. And that is something I definitely want to be provided. Cheers, Gus ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2025-08-04 11:41 Hey Y'All, I've recently had two events that made me have a better grasp at tokenizing, parsing tokens and OOP history: In a session with ChatGPT, I finally broke my bad mental construct about tokenizing and parsing tokens: They should be 2 different steps. My previous, bad, notion was that they were one and the same and that gave me quite the paralises when attempting stuff as simple as doing a DSL for templatesI watched the talk from Casey Muratori called The Big OOPS Especially the "Big OOPS" video was the thing that gave me some background to now say with confidence: Classes do not have case, they have virtual functionsIt is Records that have the case And I now know where all that came from!! Like I mentioned in my first message on this thread, I did try to replace class with record, cuz even before the above two events, I knew that the case only made sense in a record. But then I was too lazy to suss out how to resolve the circular reference that the AI hallucinated, making the record fold on itself. I'll leave solving that problem for a future exercise. Just wanted to mention the above, and give others the pleasure of learning some history behind the entire process that gave birth to OOP as we now know it. Cheers, Gus