Newsgroups: comp.lang.c Path: nuchat!menudo.uh.edu!swrinde!elroy.jpl.nasa.gov!usc!howland.reston.ans.net!newsserver.jvnc.net!udel!princeton!hart!mg From: mg@hart.Princeton.EDU (Michael Golan) Subject: Re: C Language Interpreter Message-ID: <1993Oct18.073424.26049@Princeton.EDU> Originator: news@nimaster Sender: news@Princeton.EDU (USENET News System) Nntp-Posting-Host: hart.princeton.edu Organization: Princeton University References: <29k8vr$ckj@cnj.digex.com> <29kmo6$9i5@electra.saaf.se> <29m7oeINN26mj@rs1.rrz.Uni-Koeln.DE> Date: Mon, 18 Oct 1993 07:34:24 GMT Lines: 192 aeg03@rrz.uni-koeln.de (Jan T. Kim) writes: >In <29kmo6$9i5@electra.saaf.se> pausch@electra.saaf.se (Paul Schlyter) writes: >Being able to run a program without any delay caused by compiling >and linking is one advantage of interpreter, but not the only >one. It's also very convenient to call functions in direct mode >from an interpreter, obtaining such functionality with a compiler >setup requires writing a stupid input-call main() wrapper for >each function to be tested. Indeed, it is nice to have more than an expression-evaluator under the debugger, which seems to be the principle difference between a fast compile-debug cycle and an interpreter with fast pcode. If you use the DUEL extension to gdb ( can ftp it from ftp.cs.princeton.edu:/duel), you get most of the useful capabilities of a C interpreter under gdb, e.g., to print out the positive elements of x[100]: (gdb) duel int i; for(i=0; i<100; i++) if(x[i]>0) printf("x[%d]=%d\n",i,x[i]); Using gdb with C and need this thing? it exists! its free (public domain)! it should be part of gdb, but the GNU people won't incorporate it due to "political problems" :-( [you can send mail to gnu@cygnus.com and ask them to incorporate the code] Plus, you can use the generators functionality of DUEL to test your functions much faster, e.g., the code (gdb) dl my_test_func(1..5,("hello","world")) will call my_test_func with (1,"hello"), (1,"world"), (2,"hello") ... etc. you can then check and display the results with Duel, e.g., (gdb) dl x[..100] >? 0 will show which elements of array x[100] are positive - compare this short code to the big C line above - they do the exact same thing! if you are using gdb and doing C programming, and still haven't tried Duel, you should. One user went as far as to say: ``Personally, I think it's the greatest thing since source-code debuggers,'' here is more tempting info for you: ----------------------------------------------------------------------------- DUEL - A high level language for debugging C programs. ----------------------------------------------------------------------------- Duel is a special purpose language designed for concise state exploration of debugged C programs under existing debuggers. It lets you to explore the program's state better than either the debugger's commands, a C interpreter, or print statements added to your code. The debugger is extended with the new 'dl' command for Duel expressions, e.g., gdb> dl x[0..10].level >? 5 x[3].level = 14 x[8].level = 6 prints the "level" fields of array elements x[0] to x[10] that are greater than 5. The output includes the values 14 and 6, as well as their symbolic representation "x[3].level" and "x[8].level". Linked list and other complex data structures are just as easy. Most of C (e.g. for, if) is also supported, e.g., int i; for(i=0; i<11; i++) if(x[i].level>5) x[i]. Duel is debugger-independent, but the current distribution interfaces only with gdb. You will need the source for gdb-4.6, or above. All supported gdb environments should work with Duel. It was tried at least on dec/mips, sparc, sgi/iris, sony, hp9000, aix/rs6000, and sysV. Duel is public domain code. Do whatever you like with it - add it to commercial debuggers, to dbx/sdb or even make it part of the GNU project. No part of this distribution contains any copyright or derived code, (i.e. no GPL code, either). No disclaimer - free public domain software don't need one! Even if you don't normally use debuggers, but you are programming in C, give Duel a try! The man page & help even include a summary of useful gdb commands. Duel is available for anonymous ftp from ftp.cs.princeton.edu, in /duel/duel.tar.Z. A "tar.Z" format means you should use the command "zcat duel.tar.Z | tar xf -" to unpack the files. The Usenix Jan/93 paper about Duel is also available as tech report 399, in /reports/1992/399.ps.Z on ftp.cs.princeton.edu. My PhD thesis dealing with debuggers & duel will be available within a month - send me email if you would like a copy. DUEL is "Debugging U (might) Even Like" or "Don't Use this Exotic Language" you should judge which is better! Michael Golan mg@cs.princeton.edu ----------------------------------------------------------------------------- The tutorial part from the manual page follows: Duel is based on expressions which return multiple values. The x..y operator returns the integers from x to y; the x,y operator returns x and then y, e.g. gdb> dl (1,9,12..15,22) prints 1, 9, 12, 13, 14, 15 and 22. Such expressions can be used wher- ever a single value is used, e.g. gdb> dl x[0..99]=0 ; assigns zero to the first 100 elements of array x. The semantics of x[i] are the same as in C. They are applied for each of the values returned by 0..99, which can be thought of as an implied external loop. The trailing semicolon indicates evaluation for side-effects only, with no output. Duel incorporates C operators, casts C statements as expres- sions, and supports limited variable declaration: gdb> dl int i;for(i=0;i<100;i++) if(x[i]<0) printf("x[%d]=%d\n",i,x[i]); x[7] = -4 The semicolon prevents Duel output; only output from printf is printed. Aliases are defined with x:=y and provide an alternative to variable declaration. We could also return x[i] instead of using printf: gdb> dl if(x[i:=0..99]<0) x[i] x[i] = -4 The symbolic output "x[i]" can be fixed by surrounding i with {}, i.e. gdb> dl if(x[i:=0..99]<0) x[{i}] x[7] = -4 The {} are like (), but force the symbolic evaluation to use i's value, instead of "i". You can usually avoid this altogether with direct Duel operators: gdb> dl x[..100] =?y, etc., operators compare their left side operand to their right side operand as in C, but return the left side value if the comparison result is true. Otherwise, they look for the next values to compare, without returning anything. Duel's x.y and x->y allow an expression y, evaluated under x's scope: gdb> dl emp[..100].(if(code>400) (code,name)) emp[46].code = 682 emp[46].name = "Ela" The if() expression is evaluated under the scope of each element of emp[], an array of structures. In C terms, we had to write: gdb> dl int i; for(i=0; i<100 ; i++) if(emp[i].code>400) emp[{i}].code,emp[{i}].name A useful alternative to loops is the x=>y operator. It returns y for each value of x, setting `_' to reference x's value, e.g. gdb> ..100 => if(emp[_].code>400) emp[_].code,emp[_].name Using `_' instead of `i' also avoids the need for {i}. Finally, the x-->y operator expands lists and other data structures. If head points to a linked list threaded through the next field, then: gdb> dl head-->next->data head->data = 12 head->next->data = 14 head-->next[[2]]->data = 20 head-->next[[3]]->data = 26 produce the data field for each node in the list. x-->y returns x, x->y, x->y->y, x->y->y->y, ... until a NULL is found. The symbolic output "x-->y[[n]]" indicates that ->y was applied n times. x[[y]] is also the selection operator: gdb> dl head-->next[[50..60]]->data return the 50th through the 60th elements in the list. The #/x operator counts the number of values, so gdb> dl #/( head-->next->data >? 50 ) counts the number of data elements over 50 on the list. [There is a lot more in the full manual. Cyclic lists, strings, arrays like argv, etc] --------------------------------------------------------------------------- -- Michael Golan | Duel, PD add-on to gdb, let x[..100].val >? 0 mg@cs.princeton.edu | show the positive val elements of array x[100] in the | debugger, etc. annon ftp ftp.cs.princeton.edu:/duel