Recent

Author Topic: Programs writing programs? What?? :o :o  (Read 3095 times)

OC DelGuy

  • Full Member
  • ***
  • Posts: 148
Programs writing programs? What?? :o :o
« on: January 17, 2025, 01:07:07 am »
So, I was wondering how to make an application that writes code and executes it "on the fly".

Here's my situation:
I have 64 variables in a program.  I named them all var1, var2,... var63, var64.  I want to write them all in a TMemo.  I realize I can just write 64 lines of code:
Code: Pascal  [Select][+][-]
  1.   Memo.Lines.Add(var1);
  2.   Memo.Lines.Add(var2);
  3.   ...
  4.   Memo.Lines.Add(var63);
  5.   Memo.Lines.Add(var64);
  6.  



But I'd like to know if there's a way to maybe use a loop:
Code: Pascal  [Select][+][-]
  1.   For x := 1 to 64 Do
  2.     Begin
  3.       S1 := 'Memo.Lines.Add(var' + IntToStr(x) + ');';
  4.       Execute(S1);
  5.     End;
  6.  

Substituting the line:
Code: Pascal  [Select][+][-]
  1. Execute(S1);
with something that will work.

Is there a way to do this?
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

440bx

  • Hero Member
  • *****
  • Posts: 4998
Re: Programs writing programs? What?? :o :o
« Reply #1 on: January 17, 2025, 02:28:27 am »
Could you put the variable values in an array ?  if you did that then you can use a loop to add them to the memo.

Another possibility that comes to mind is to simply generate the 64 lines required using AWK (or a tiny Pascal program would do too.)

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Wesbat

  • New Member
  • *
  • Posts: 37
    • engrams.dev
Re: Programs writing programs? What?? :o :o
« Reply #2 on: January 17, 2025, 06:56:19 am »
So, I was wondering how to make an application that writes code and executes it "on the fly".

Unless I misunderstood, It sounds like what you are describing is Pascal Script. It allows execution of a script inside your program.

I haven't used that myself, but the wiki seems a good starting point.

OC DelGuy

  • Full Member
  • ***
  • Posts: 148
Re: Programs writing programs? What?? :o :o
« Reply #3 on: January 17, 2025, 09:50:55 am »
Could you put the variable values in an array ?  if you did that then you can use a loop to add them to the memo.
If I use an Array, Then I have the same problem:  To load the array, I still need 64 lines of:
Code: Pascal  [Select][+][-]
  1. vari[1]:=var1;
  2. vari[2]:=var2;
  3. ...
  4. vari[63]:=var63;
  5. vari[64]:=var64;
  6.  
The var1 doesn't change each iteration.


Another possibility that comes to mind is to simply generate the 64 lines required using AWK (or a tiny Pascal program would do too.)
Not familiar with AWK, but Google says it a text manipulation utility.  Using AWK and writing a program means I'd have to run them every time I need to do this.
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

alpine

  • Hero Member
  • *****
  • Posts: 1349
Re: Programs writing programs? What?? :o :o
« Reply #4 on: January 17, 2025, 10:02:44 am »
Which one comes first?  :-\

This:
So, I was wondering how to make an application that writes code and executes it "on the fly".
Or that:
Here's my situation:
I have 64 variables in a program.  I named them all var1, var2,... var63, var64.  I want to write them all in a TMemo.  I realize I can just write 64 lines of code:
*snip*
Is there a way to do this?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

440bx

  • Hero Member
  • *****
  • Posts: 4998
Re: Programs writing programs? What?? :o :o
« Reply #5 on: January 17, 2025, 12:21:02 pm »
If I use an Array, Then I have the same problem:  To load the array, I still need 64 lines of:
Code: Pascal  [Select][+][-]
  1. vari[1]:=var1;
  2. vari[2]:=var2;
  3. ...
  4. vari[63]:=var63;
  5. vari[64]:=var64;
  6.  
I meant using the values of var1, var2...var64.

if you use the values then you could initialize the array with something like: "vari : array[1..64] of integer (or whatever other type) = (v1, v2, ... v64);"

There is another way but, it is a hack that could break in the future which is: if the variables are defined consecutively then you can define an array that overlays them.  something like:
Code: Pascal  [Select][+][-]
  1. var
  2.   v1 : integer;
  3.   v2 : integer;
  4.   v3 : integer;
  5.  
  6.   ari : array[1..3] of integer absolute v1;
  7.  
but, that's living dangerously because it is dependent on how the compiler arranges variables and it does not guarantee anything therefore the "absolute" may not yield the desired result (though, in practice, it most likely will.)

Not familiar with AWK, but Google says it a text manipulation utility.  Using AWK and writing a program means I'd have to run them every time I need to do this.
That's true but, how often do you need to do that ?  the same AWK script or Pascal program could be used when needed but, you're right, if it needs to be done several times a day most days then it isn't practical.

The question is: is the objective to dynamically execute some code or is the objective initializing a structure of some kind (likely an array) with 64 known values ?  your question does not make it clear as to which case applies.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8213
Re: Programs writing programs? What?? :o :o
« Reply #6 on: January 17, 2025, 03:05:56 pm »
Not familiar with AWK, but Google says it a text manipulation utility.  Using AWK and writing a program means I'd have to run them every time I need to do this.
That's true but, how often do you need to do that ?  the same AWK script or Pascal program could be used when needed but, you're right, if it needs to be done several times a day most days then it isn't practical.

The question is: is the objective to dynamically execute some code or is the objective initializing a structure of some kind (likely an array) with 64 known values ?  your question does not make it clear as to which case applies.

Assuming that we're talking about sourcecode preparation: don't use AWK, it's primarily for text substitution rather than for text preparation. Either use FPC itself or Perl, in either case use a makefile or pre-execute the utility using Lazarus.

/If/ we're actually talking about converting a preexisting file to a binary (either an executable or a load-on-demand library) then using PascalScript if possible would be preferred. Generating a main executable can be problematic if the original one is still being used, and while it /is/ possible to reload a dynamic library (e.g. when a new one appears, or on receipt of a signal) it's not for the faint of heart.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

alpine

  • Hero Member
  • *****
  • Posts: 1349
Re: Programs writing programs? What?? :o :o
« Reply #7 on: January 17, 2025, 03:26:14 pm »
@Mark
I'm just guessing the OP question is overstated and boils down to: How to iterate through 64 elements (ie. an array), hence my previous question. I could be wrong, of course.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MarkMLl

  • Hero Member
  • *****
  • Posts: 8213
Re: Programs writing programs? What?? :o :o
« Reply #8 on: January 17, 2025, 05:01:12 pm »
I'm just guessing the OP question is overstated and boils down to: How to iterate through 64 elements (ie. an array), hence my previous question. I could be wrong, of course.

I haven't a clue what he means, and am not sure he has either :-)

If we read OP literally, I think he might be asking /whether/ Pascal has a runtime evaluate function:


 index := 1;
 something := Eval('Var' + IntToStr(index));


to which the answer is of course a resounding NO.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 589
Re: Programs writing programs? What?? :o :o
« Reply #9 on: January 17, 2025, 05:08:12 pm »
I dimly remember a professor lecturing about self-modifying programs with horror. 
Meanwhile, copy/paste/edit are your friends.  Five steps (2-4-8-16-64) and 62 edits worst case. 
I think a lot of programming is routine like that.     

MarkMLl

  • Hero Member
  • *****
  • Posts: 8213
Re: Programs writing programs? What?? :o :o
« Reply #10 on: January 17, 2025, 06:41:57 pm »
I dimly remember a professor lecturing about self-modifying programs with horror. 
Meanwhile, copy/paste/edit are your friends.  Five steps (2-4-8-16-64) and 62 edits worst case. 
I think a lot of programming is routine like that.   

Depends: there's (at least) two cases. First

Quote
[…] It was “syntactically mutating small Lisp programs”. Rather than disdaining it for that reason, we saw that that was its salvation, its chief source of power, the reason it had such a high hit rate: AM was exploiting the natural tie between Lisp and mathematics.

That's from discussion of Doug Lenat's recently-rediscovered corpus.

Now I'm not necessarily saying you're wrong, and I'm not necessarily saying that (self-)modifying code is desirable. But Lenat was one of the core workers on "classic" AI which sought to reason, and so anything that offsets the current emphasis on "spicy pattern matching" should not be rejected out of hand. Furthermore, that kind of "suck it and see" approach is the core of the Monte Carlo Method, many genetic algorithms (used for e.g. database query optimisation) and... well, much more: it's fine provided that your approach prevents syntax errors and hallucinations from being accepted as ex-cathedra revelations of divine intent.

Second, there's plenty of cases which could fit within OP's somewhat loosely-phrased question. As a particular example, a high proportion of compilers start off with a programming language defined in a metalanguage, which is used to generate a lexer, parser, and (sometimes) even a code generator.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

gidesa

  • Full Member
  • ***
  • Posts: 160
Re: Programs writing programs? What?? :o :o
« Reply #11 on: January 17, 2025, 08:34:20 pm »
Hello,
Pascal is not an interpreted (*) language, so you can do generating those lines with an external tool, as suggested, Awk or whatever.
(*) But this is not totally true. There is the RTTI (Run Time Type Information?), using that you could dynamically execute some code at run time.
So, instead of having 64 separate variables, you could define variables as (public) fields in a record (or public properties in a class):
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyRecord = record
  3.     var1: string;  // or whatever type
  4.     var2: string;
  5.     ......
  6.     var64: string;
  7.   end;
  8.  
Then using RTTI you can read every field of record, and have the loop as requested:
Code: Pascal  [Select][+][-]
  1. // pseudo code
  2. For field in record Do
  3.     Begin
  4.       Memo.Lines.Add(field);
  5.      End;
  6.  

I think this work only in FPC 3.3.1 or newer.


Curt Carpenter

  • Hero Member
  • *****
  • Posts: 589
Re: Programs writing programs? What?? :o :o
« Reply #12 on: January 18, 2025, 12:06:18 am »

Depends: there's (at least) two cases. First...


I would identify the two cases as:  case 1:  exploring self-modifying programs as an area of research; and 2: being tasked with debugging and maintaining programs which modify themselves  :)  1 I would imagine would be pretty exciting.  Two would be "what fresh new hell is this?"

MarkMLl

  • Hero Member
  • *****
  • Posts: 8213
Re: Programs writing programs? What?? :o :o
« Reply #13 on: January 18, 2025, 08:58:04 am »
Pascal is not an interpreted (*) language,

Most of the early implementations were interpreted. Anything that targets .Net or Javascript arguably still is.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 16523
  • Kallstadt seems a good place to evict Trump to.
Re: Programs writing programs? What?? :o :o
« Reply #14 on: January 18, 2025, 09:15:14 am »
Pascal is not an interpreted (*) language,

Most of the early implementations were interpreted. Anything that targets .Net or Javascript arguably still is.

MarkMLl
P-code is NOT the same as interpreted. Early Pascal started life as a p-code compiler often mistakenly referred to as P-code interpreter, which is merely the runtime. Think of p-code (or java byte code and wasm for that matter) as compiling for a virtual CPU. P-code has many traits of natively compiled code and nothing to do with dynamic, true interpreted languages. Things like reflection for compiled languages(.net, java, pascal rtti) were not yet known at the time.
« Last Edit: January 18, 2025, 09:23:44 am by Thaddy »
But I am sure they don't want the Trumps back...

 

TinyPortal © 2005-2018