China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-24 16:31
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Object Oriented Batch Language DigestI View 36,490 Replies 56
Original Poster Posted 2005-08-04 19:46 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline

Object-Oriented Batch Language (Object Oriented Batch Language)


The text, code, and ideas in this article come from the following reference:
BATCH PROGRAMMING ELEMENTS (Dirk van Deun)
Chap 9: “Object Oriented Batch Language”
http://student.vub.ac.be/~dvandeun/batcoll.all
Special thanks to Dirk van Deun for his contribution!



  I came across this article two years ago, but never paid it enough attention. A few days ago I suddenly dug it out again, and only then realized the value hidden in it. In it, the implementation of “object orientation” makes an important breakthrough in batch programming ideas, and its idea of using file structures to simulate data structures was also very enlightening to me.

  The program uses file directories to simulate class libraries and object libraries, files to simulate the properties and methods owned by classes, and batch files to simulate object references, thus implementing the four major structures of OOP: classes, objects, properties, and methods, and thereby also implementing OOP features such as inheritance and overriding.

  After thoroughly studying the program, I decided to make some modifications:

  First, I rewrote all the code using internal commands, solving cross-platform portability as much as possible. At present, partial testing has been carried out and passed under MSDOS6.22/7.10 and the WindowsXP command line. My design goal is to make it run normally across the full MSDOS/ Win9x / WinNT's platform series.

  Second, I changed the way objects are defined and called; that is, objects are defined directly by class name, and properties and methods are referenced by object name, making it more in line with OOP coding habits.

  Third, I improved the scheme for using OOBL in batch files; although the original program was called OOBL, in fact it could only work at the command line and could not have all its code written into a batch file.

  Fourth, I added public/private access attributes for properties and methods, making data encapsulation possible.

  Finally, I made major changes to the strategy of loading variables before calling object methods and saving variables afterward. This change was unavoidable, because otherwise Dirk's original push/pop scheme would introduce the external program edlin.exe; but this new strategy also brings many problems: object data and class data are obviously duplicated, making data redundancy and space usage increasingly prominent.

  So far, this is still a half-finished product, and it still has rather major defects. For example, in order to keep the structure clear, no error-handling mechanism was added; as for constructors for objects in the design and polymorphic method invocation, there is still no mature solution.

  But my current state is very bad. I spent a full two weeks on it, yet progress was very slow. So I can only release this immature code. If anyone in the field is interested, you can study it and see. I hope someone can continue the unfinished work.

Below is a command-line example of OOBL:

E:\Batch\Test\OOBL>project work

"work":class man

"work":proprt Name
"work":proprt FullName private
"work":method setName
set name=%1
set fullname=%2 %3
^Z
"work":method putName
echo Name:%name%; FullName:%fullname%
^Z
"work":class employee man
"work":proprt salary private
"work":method setSal
set salary=%1
^Z
"work":method putSal
echo Name:%name%; FullName:%fullname%; Salary:%salary%;
^Z
"work":man John

"work":John name=John
"work":man fred

"work":fred name=Fred
"work":fred setName Fred Fred Ford
"work":john putName
Name:John; FullName:
"work":fred putName
Name:Fred; FullName:Fred Ford
"work":employee bill

"work":bill name=Bill
"work":bill setSal $100000
"work":bill putSal
Name:Bill; FullName:; Salary:$100000;
"work":project :end
Project is end!

E:\Batch\Test\OOB>


A batch file example of OOBL is in the attachment


[ Last edited by willsort on 2005-8-4 at 19:47 ]
Attachments
OOBL.rar (5.89 KiB, Credits to download 1 pts, Downloads: 449)
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 2 Posted 2005-08-08 15:19 ·  中国 山东 泰安 肥城市 联通
新手上路
Credits 7
Posts 5
Joined 2005-08-08 15:18
20-year member
UID 41450
Gender Male
Status Offline
Hehe, not bad!
Floor 3 Posted 2005-08-11 13:16 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
Quite a good idea for inspiring new ways of thinking.
But object orientation is mainly meant for large software development, or for writing programs that may be small but can conveniently call WIN API.
But this may be a bit mismatched with the situation of BATCH...
BATCH plus external commands only gives about 100 commands in total.
It's like bringing in an aircraft carrier and only being able to put it on an inland river... you can imagine how much use it would have... On the other hand, it would instead slow execution down,,

Also, to avoid rewriting code, BATCH already has CALL. It can also take parameters, which is a bit like functions...

Still, as pure research, that's another matter of course..
Floor 4 Posted 2005-08-11 13:56 ·  中国 湖北 武汉 联通
高级用户
★★★
Credits 587
Posts 302
Joined 2005-07-25 17:31
20-year member
UID 41046
Gender Male
Status Offline
I think WIN scripts are quite good. The weak point of batch files is not program flow control, but data types and system function calls. It can only use internal and external commands, without system-level API calls. Sometimes you have to write a program for BATCH in order to complete work that actually isn't very complicated.
As for whether OOP should be considered only after the basic functions of BATCH are improved?
You also said in the article that external commands have to be used; that's because of this too, right?
Also, using the topology of hard disk directories to simulate the hierarchy between classes, I personally feel that is not very appropriate. At the very least, could you consider using a memory virtual disk? And directory structures have limitations too.

Actually, it would be better if DOS supported REXX.

I don't know too much about this, so please forgive me if I said anything wrong^^

[ Last edited by fdsiuha on 2005-8-20 at 21:54 ]
欢迎造访DOS的小屋!
http://risky.ik8.com
Floor 5 Posted 2005-08-14 20:06 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re GOTOmsdos & fdsiuha:

Just as I said in the top post, this program is only a half-finished product in development, so naturally it has many defects and problems. But it introduces a new idea into batch programming, and that is naturally very helpful for broadening our thinking. And our task is to improve it step by step, so that it gradually reaches practical needs.

Re GOTOmsdos:

This is the first time I've heard that OOP is for conveniently calling Win API, and I don't know what that is based on.

As for the problem of there being few batch commands, this is how I understand it: all languages operate through basic syntax keywords together with support libraries. C has only a little over a dozen keywords too; everything else depends on functions and macros in libraries. And batch keywords (special command keywords) are also only a little over a dozen, while its support library is all executable programs under DOS. Of course, many programs do not have very good interfaces.

And the implementation of OOP is not only for avoiding code rewriting; it also has many other characteristics, such as the data encapsulation I mentioned above.

Re fdsiuha:

Win scripts are good, but they are not batch files. Although they are called “batch files under windows,” they are another kind of script. REXX is the same. They have many advantages and can easily complete many tasks that batch files find difficult.

But we can develop and strengthen batch files in every possible direction. The basic functions of batch files do need improvement, such as variable types (though most of the time I am satisfied with this Basic-like generic design), error handling (I have already done some work on this), text processing (I used debug and edlin to make some simple implementations), system calls, and referencing external support libraries (this is already close to maturity).

As for a memory virtual disk, that is still not within the scope of what we can consider now, because it still cannot become large enough to affect system performance. When this problem becomes prominent enough that we can no longer tolerate it, it will not be too late to discuss it then.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 6 Posted 2005-08-19 13:08 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
This is the first time I've heard that OOP is for conveniently calling Win API, and I don't know what that is based on.


Oh, what I said was mainly aimed at the situation of writing WIN programs; of course I didn't mean all of OOP..
Also, the main idea of OOP is not encapsulation, but first making reusable classes so they can be called. That's why there are class libraries. For WIN, there is ready-made MFC, so when writing programs you call them.
Conversely, without class invocation, OOP has no meaning..
But you need a large number of functions before encapsulation becomes necessary..
DOS BATCH doesn't have that.. Even if some function-like small blocks can be made, the quantity is hard to scale up..

I wonder whether other scripting languages involve classes? For example UNIX SHELL and so on, which are much richer than DOS BATCH...

[ Last edited by GOTOmsdos on 2005-8-19 at 13:15 ]
Floor 7 Posted 2005-08-19 13:36 ·  中国 四川 德阳 电信
元老会员
★★
联盟“管理员”
Credits 608
Posts 157
Joined 2002-10-18 00:00
23-year member
UID 20
Gender Male
Status Offline
This...
it's better to use a real scripting language like perl
instead
La guerre, c'est le pax
Freedom is Slavery
无知就是力量
Floor 8 Posted 2005-08-19 21:15 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re GOTOmsdos:

It seems, brother, that you still have not understood my original meaning above.

In OOP, data encapsulation and reusability are equally important features; there is no distinction of which comes first. Comparatively speaking, the innovativeness of OOP's data encapsulation is somewhat stronger, because the reusability of functions in procedural programming (POP) is not bad either.

As for the number of functions, I have already said that “function” is only a specialized term in high-level languages. In a broad sense, it refers to the runtime support library. This is similar to the standard library of C++, the MFC of VS, the VCL of Delphi, and the J2SDK of Java. The runtime library of batch files consists of all the internal and external commands of the platform it runs on, all third-party programs, tools, and software (whether command-line programs or not), and all code and programs that batch files can call directly or indirectly. Of course, their interfaces may not necessarily be standard or friendly, and that is precisely what shows the necessity of encapsulation.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 9 Posted 2005-08-20 18:24 ·  中国 山东 济宁 电信
中级用户
★★★
痴迷DOS者
Credits 456
Posts 570
Joined 2004-10-09 00:00
21-year member
UID 32281
Gender Male
Status Offline
API calls can be made with the rundll32 command
DOS不是万能的,没有DOS是万万不能的
自古系统谁无死?留取胆清照汗青!uploadImages/20035317345478982.png
Floor 10 Posted 2005-08-20 22:18 ·  中国 湖北 武汉 联通
高级用户
★★★
Credits 587
Posts 302
Joined 2005-07-25 17:31
20-year member
UID 41046
Gender Male
Status Offline
These past few days I've been thinking that if a batch file is made too complex, that's a case of losing sight of the essentials. Simply doing “technology for technology's sake” has no practical value. Maybe GOTOmsdos was thinking along those lines too. Fully implementing OOP in existing batch files and internal commands would not bring benefits to ordinary users; on the contrary, it would make this kind of “secondary development” batch file hard to understand, let alone practical.

The advantage of batch files is that every line is an internal command / batch command, or an external command / application program. That way, as long as someone is somewhat familiar with DOS and uses certain commands fairly often, they can use batch files. No need to learn data structures, no need to understand OOP, and no need to learn a specific language.

Another advantage is that text is easy to modify, flexible, and convenient. Ordinary languages need a compiler or interpreter to run (COMMAND.com here is equivalent to an interpreter). Why shouldn't I just casually use some OOP language, all of which have the ability to call SHELL command lines, to replace batch files? And their OOP features are all complete, and they are also easier to understand than DOS batch files.

Therefore I do not agree that OOP is a direction for the development of batch files, and batch files should not be made more complex. But I absolutely do not mean that the OP's idea is bad. As a vehicle for studying and discussing the implementation of OOP, it is indeed worth researching. But the object of study is not batch files themselves, but OOP. It is like using self-hosting techniques to write the compiler for that language itself; in fact it does not have much practical value, but at least it can prove that such a thing is feasible.

PS: If DOS internally supported structured and object-oriented batch files, that would also be good and possible, but the COMMAND.COM file would need to gain a bit of “weight,” and the system overhead would also be a bit larger.

[ Last edited by fdsiuha on 2005-8-20 at 22:48 ]
欢迎造访DOS的小屋!
http://risky.ik8.com
Floor 11 Posted 2005-08-21 17:19 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re fdsiuha:

Two years ago, my thinking was the same as yours — “batch files should not be made more complex.” Because simplicity and ease of use are the greatest advantages of batch files, just like DOS, the host platform they live on. So I did not pay much attention to the reference mentioned in the top post.

But now my thinking has changed somewhat — “batch files should not be made overly complex when there is no need.” Because over these two years my horizons have broadened. I have discovered many batch programs of considerable complexity, and I have also written some myself. As the saying goes, “what exists is reasonable”; whether it should or should not exist, it has already appeared.

Nowadays, it is difficult to say that DOS still has room to survive simply because of its “simplicity” — many users are already complaining, “Why is DOS so hard to learn?!” From another angle, it may itself be simple, in that its size is far smaller than comparable systems like Windows and Linux; however, the scale of difficulty is relative and shifting. Structural simplicity does not mean ease of use. For Windows to achieve a light mouse click, the price paid is a huge increase in size and performance consumption.

The phrase "Complex makes simple" has considerable validity. So in order to make batch files simple, it is necessary to do some complex work. Just as Java, in order to simplify object recycling, adopted the quite costly automatic garbage collection mechanism. Shifting complexity away from the user and onto the producer is an inevitable trend.

Doesn't your idea about DOS internally supporting OOP have a similar motivation?
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 12 Posted 2006-10-13 23:50 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Such a good thread shouldn't sink. Actually Microsoft's idea was to use WSH to replace bat, but vbs wasn't command-oriented enough, so later they also came out with PowerShell with reference to UNIX. There are too many new technologies; what matters is the ideas behind them.

willsort has already reached a truly masterful level, bumping the thread again in remembrance

[ Last edited by electronixtar on 2006-10-13 at 23:51 ]

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 13 Posted 2006-10-14 00:46 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
This is another innovation and step forward :)
Huge bump!!!
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 14 Posted 2006-10-14 01:07 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Bump~~
This “@echo %dbg% off” is really interesting. When you want to debug, you don't even need to modify the batch file anymore :)


:: Sample.bat - Sample of OOBL
:: Will Sort - 2005-7-31
@echo %dbg% off
call project Work
.....



This method is nice~:)
Set dbg=on ,
then after entering just this one line, running any batch file with a command like @echo %dbg% off will first execute the %dbg% parameter before it, haha......
Bump~


C:\TEMP\Oop>set dbg=on

C:\TEMP\Oop>project.bat MyProj
on off

C:\TEMP\Oop>if "MyProj" == ":end" goto proje

C:\TEMP\Oop>prompt "MyProj":

"MyProj":md MyProj


And this prompt really gives people a bit of the feeling of entering the dot prompt era of Dbase,
as if you're really interactively entering commands in a development environment!

[ Last edited by redtek on 2006-10-14 at 01:11 ]
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 15 Posted 2006-10-14 01:09 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Bump~~~ Didn't expect batch files could be object-oriented too...
Forum Jump: