summaryrefslogtreecommitdiffstats
path: root/unparse.c
blob: 2302cc5522a2ef5fe11fb4f009fba2f6760a9332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
/******************************************************************************
  Copyright (c) 1992, 1995, 1996 Xerox Corporation.  All rights reserved.
  Portions of this code were written by Stephen White, aka ghond.
  Use and copying of this software and preparation of derivative works based
  upon this software are permitted.  Any distribution of this software or
  derivative works must comply with all applicable United States export
  control laws.  This software is made available AS IS, and Xerox Corporation
  makes no warranty about the software, its performance or its conformity to
  any specification.  Any person obtaining a copy of this software is requested
  to send their name and post office or electronic mail address to:
    Pavel Curtis
    Xerox PARC
    3333 Coyote Hill Rd.
    Palo Alto, CA 94304
    Pavel@Xerox.Com
 *****************************************************************************/

#include "my-ctype.h"
#include "my-stdio.h"

#include "ast.h"
#include "config.h"
#include "decompile.h"
#include "exceptions.h"
#include "functions.h"
#include "keywords.h"
#include "list.h"
#include "log.h"
#include "opcode.h"
#include "program.h"
#include "unparse.h"
#include "storage.h"
#include "streams.h"
#include "utils.h"

static Program *prog;

const char *
unparse_error(enum error e)
{
    switch (e) {
    case E_NONE:
	return "No error";
    case E_TYPE:
	return "Type mismatch";
    case E_DIV:
	return "Division by zero";
    case E_PERM:
	return "Permission denied";
    case E_PROPNF:
	return "Property not found";
    case E_VERBNF:
	return "Verb not found";
    case E_VARNF:
	return "Variable not found";
    case E_INVIND:
	return "Invalid indirection";
    case E_RECMOVE:
	return "Recursive move";
    case E_MAXREC:
	return "Too many verb calls";
    case E_RANGE:
	return "Range error";
    case E_ARGS:
	return "Incorrect number of arguments";
    case E_NACC:
	return "Move refused by destination";
    case E_INVARG:
	return "Invalid argument";
    case E_QUOTA:
	return "Resource limit exceeded";
    case E_FLOAT:
	return "Floating-point arithmetic error";
    }

    return "Unknown Error";
}

const char *
error_name(enum error e)
{
    switch (e) {
    case E_NONE:
	return "E_NONE";
    case E_TYPE:
	return "E_TYPE";
    case E_DIV:
	return "E_DIV";
    case E_PERM:
	return "E_PERM";
    case E_PROPNF:
	return "E_PROPNF";
    case E_VERBNF:
	return "E_VERBNF";
    case E_VARNF:
	return "E_VARNF";
    case E_INVIND:
	return "E_INVIND";
    case E_RECMOVE:
	return "E_RECMOVE";
    case E_MAXREC:
	return "E_MAXREC";
    case E_RANGE:
	return "E_RANGE";
    case E_ARGS:
	return "E_ARGS";
    case E_NACC:
	return "E_NACC";
    case E_INVARG:
	return "E_INVARG";
    case E_QUOTA:
	return "E_QUOTA";
    case E_FLOAT:
	return "E_FLOAT";
    }

    return "E_?";
}

struct prec {
    enum Expr_Kind kind;
    int precedence;
};

static struct prec prec_table[] =
{
    {EXPR_ASGN, 1},

    {EXPR_COND, 2},		/* the unparser for this depends on only ASGN having
				   lower precedence.  Fix that if this changes. */
    {EXPR_OR, 3},
    {EXPR_AND, 3},

    {EXPR_EQ, 4},
    {EXPR_NE, 4},
    {EXPR_LT, 4},
    {EXPR_LE, 4},
    {EXPR_GT, 4},
    {EXPR_GE, 4},
    {EXPR_IN, 4},

    {EXPR_PLUS, 5},
    {EXPR_MINUS, 5},

    {EXPR_TIMES, 6},
    {EXPR_DIVIDE, 6},
    {EXPR_MOD, 6},

    {EXPR_EXP, 7},

    {EXPR_NEGATE, 8},
    {EXPR_NOT, 8},

    {EXPR_PROP, 9},
    {EXPR_VERB, 9},
    {EXPR_INDEX, 9},
    {EXPR_RANGE, 9},

    {EXPR_VAR, 10},
    {EXPR_ID, 10},
    {EXPR_LIST, 10},
    {EXPR_CALL, 10},
    {EXPR_LENGTH, 10},
    {EXPR_CATCH, 10}
};

static int expr_prec[SizeOf_Expr_Kind];

struct binop {
    enum Expr_Kind kind;
    const char *string;
};

static struct binop binop_table[] =
{
    {EXPR_IN, " in "},
    {EXPR_OR, " || "},
    {EXPR_AND, " && "},
    {EXPR_EQ, " == "},
    {EXPR_NE, " != "},
    {EXPR_LT, " < "},
    {EXPR_LE, " <= "},
    {EXPR_GT, " > "},
    {EXPR_GE, " >= "},
    {EXPR_PLUS, " + "},
    {EXPR_MINUS, " - "},
    {EXPR_TIMES, " * "},
    {EXPR_DIVIDE, " / "},
    {EXPR_MOD, " % "},
    {EXPR_EXP, " ^ "},
};

static const char *binop_string[SizeOf_Expr_Kind];

static int expr_tables_initialized = 0;

static void
init_expr_tables()
{
    int i;

    for (i = 0; i < Arraysize(prec_table); i++)
	expr_prec[prec_table[i].kind] = prec_table[i].precedence;

    for (i = 0; i < Arraysize(binop_table); i++)
	binop_string[binop_table[i].kind] = binop_table[i].string;

    expr_tables_initialized = 1;
}

/********** globals *********************************/

static Unparser_Receiver receiver;
static void *receiver_data;
static int fully_parenthesize, indent_code;

/********** AST to receiver procedures **************/

static void unparse_stmt(Stmt * s, int indent);
static void unparse_expr(Stream * str, Expr * e);
static void unparse_arglist(Stream * str, Arg_List * a);
static void unparse_scatter(Stream * str, Scatter * sc);

static void
list_prg(Stmt * program, int p, int i)
{
    fully_parenthesize = p;
    indent_code = i;
    if (!expr_tables_initialized)
	init_expr_tables();
    unparse_stmt(program, 0);
}

static void
bracket_lt(Stream * str, enum Expr_Kind parent, Expr * child)
{
    if ((fully_parenthesize && expr_prec[child->kind] < expr_prec[EXPR_PROP])
	|| expr_prec[parent] > expr_prec[child->kind]) {
	stream_add_char(str, '(');
	unparse_expr(str, child);
	stream_add_char(str, ')');
    } else {
	unparse_expr(str, child);
    }
}

static void
bracket_le(Stream * str, enum Expr_Kind parent, Expr * child)
{
    if ((fully_parenthesize && expr_prec[child->kind] < expr_prec[EXPR_PROP])
	|| expr_prec[parent] >= expr_prec[child->kind]) {
	stream_add_char(str, '(');
	unparse_expr(str, child);
	stream_add_char(str, ')');
    } else {
	unparse_expr(str, child);
    }
}

static void
output(Stream * str)
{
    (*receiver) (receiver_data, reset_stream(str));
}

static void
indent_stmt(Stream * str, int indent)
{
    int i;

    if (indent_code)
	for (i = 0; i < indent; i++)
	    stream_add_char(str, ' ');
}

static void
unparse_stmt_cond(Stream * str, struct Stmt_Cond cond, int indent)
{
    Cond_Arm *elseifs;

    stream_add_string(str, "if (");
    unparse_expr(str, cond.arms->condition);
    stream_add_char(str, ')');
    output(str);
    unparse_stmt(cond.arms->stmt, indent + 2);
    for (elseifs = cond.arms->next; elseifs; elseifs = elseifs->next) {
	indent_stmt(str, indent);
	stream_add_string(str, "elseif (");
	unparse_expr(str, elseifs->condition);
	stream_add_char(str, ')');
	output(str);
	unparse_stmt(elseifs->stmt, indent + 2);
    }
    if (cond.otherwise) {
	indent_stmt(str, indent);
	stream_add_string(str, "else");
	output(str);
	unparse_stmt(cond.otherwise, indent + 2);
    }
    indent_stmt(str, indent);
    stream_add_string(str, "endif");
    output(str);
}

static void
unparse_stmt_list(Stream * str, struct Stmt_List list, int indent)
{
    stream_printf(str, "for %s in (", prog->var_names[list.id]);
    unparse_expr(str, list.expr);
    stream_add_char(str, ')');
    output(str);
    unparse_stmt(list.body, indent + 2);
    indent_stmt(str, indent);
    stream_add_string(str, "endfor");
    output(str);
}

static void
unparse_stmt_range(Stream * str, struct Stmt_Range range, int indent)
{
    stream_printf(str, "for %s in [", prog->var_names[range.id]);
    unparse_expr(str, range.from);
    stream_add_string(str, "..");
    unparse_expr(str, range.to);
    stream_add_char(str, ']');
    output(str);
    unparse_stmt(range.body, indent + 2);
    indent_stmt(str, indent);
    stream_add_string(str, "endfor");
    output(str);
}

static void
unparse_stmt_fork(Stream * str, struct Stmt_Fork fork_stmt, int indent)
{
    if (fork_stmt.id >= 0)
	stream_printf(str, "fork %s (", prog->var_names[fork_stmt.id]);
    else
	stream_add_string(str, "fork (");
    unparse_expr(str, fork_stmt.time);
    stream_add_char(str, ')');
    output(str);
    unparse_stmt(fork_stmt.body, indent + 2);
    indent_stmt(str, indent);
    stream_add_string(str, "endfork");
    output(str);
}

static void
unparse_stmt_catch(Stream * str, struct Stmt_Catch catch, int indent)
{
    Except_Arm *ex;

    stream_add_string(str, "try");
    output(str);
    unparse_stmt(catch.body, indent + 2);
    for (ex = catch.excepts; ex; ex = ex->next) {
	indent_stmt(str, indent);
	stream_add_string(str, "except ");
	if (ex->id >= 0)
	    stream_printf(str, "%s ", prog->var_names[ex->id]);
	stream_add_char(str, '(');
	if (ex->codes)
	    unparse_arglist(str, ex->codes);
	else
	    stream_add_string(str, "ANY");
	stream_add_char(str, ')');
	output(str);
	unparse_stmt(ex->stmt, indent + 2);
    }
    indent_stmt(str, indent);
    stream_add_string(str, "endtry");
    output(str);
}

static void
unparse_stmt(Stmt * stmt, int indent)
{
    Stream *str = new_stream(100);

    while (stmt) {
	indent_stmt(str, indent);
	switch (stmt->kind) {
	case STMT_COND:
	    unparse_stmt_cond(str, stmt->s.cond, indent);
	    break;
	case STMT_LIST:
	    unparse_stmt_list(str, stmt->s.list, indent);
	    break;
	case STMT_RANGE:
	    unparse_stmt_range(str, stmt->s.range, indent);
	    break;
	case STMT_FORK:
	    unparse_stmt_fork(str, stmt->s.fork, indent);
	    break;
	case STMT_EXPR:
	    unparse_expr(str, stmt->s.expr);
	    stream_add_char(str, ';');
	    output(str);
	    break;
	case STMT_WHILE:
	    if (stmt->s.loop.id == -1)
		stream_add_string(str, "while (");
	    else
		stream_printf(str, "while %s (",
			      prog->var_names[stmt->s.loop.id]);
	    unparse_expr(str, stmt->s.loop.condition);
	    stream_add_char(str, ')');
	    output(str);
	    unparse_stmt(stmt->s.loop.body, indent + 2);
	    indent_stmt(str, indent);
	    stream_add_string(str, "endwhile");
	    output(str);
	    break;
	case STMT_RETURN:
	    if (stmt->s.expr) {
		stream_add_string(str, "return ");
		unparse_expr(str, stmt->s.expr);
	    } else
		stream_add_string(str, "return");
	    stream_add_char(str, ';');
	    output(str);
	    break;
	case STMT_TRY_EXCEPT:
	    unparse_stmt_catch(str, stmt->s.catch, indent);
	    break;
	case STMT_TRY_FINALLY:
	    stream_add_string(str, "try");
	    output(str);
	    unparse_stmt(stmt->s.finally.body, indent + 2);
	    indent_stmt(str, indent);
	    stream_add_string(str, "finally");
	    output(str);
	    unparse_stmt(stmt->s.finally.handler, indent + 2);
	    indent_stmt(str, indent);
	    stream_add_string(str, "endtry");
	    output(str);
	    break;
	case STMT_BREAK:
	case STMT_CONTINUE:
	    {
		const char *kwd = (stmt->kind == STMT_BREAK ? "break"
				   : "continue");

		if (stmt->s.exit == -1)
		    stream_printf(str, "%s;", kwd);
		else
		    stream_printf(str, "%s %s;", kwd,
				  prog->var_names[stmt->s.exit]);
		output(str);
	    }
	    break;
	default:
	    errlog("UNPARSE_STMT: Unknown Stmt_Kind: %d\n", stmt->kind);
	    stream_add_string(str, "?!?!?!?;");
	    output(str);
	    break;
	}
	stmt = stmt->next;
    }

    free_stream(str);
}

static int
ok_identifier(const char *name)
{
    const char *p = name;

    if (*p != '\0' && (isalpha(*p) || *p == '_')) {
	while (*++p != '\0' && (isalnum(*p) || *p == '_'));
	if (*p == '\0' && !find_keyword(name))
	    return 1;
    }
    return 0;
}

static void
unparse_name_expr(Stream * str, Expr * expr)
{
    /*
     * Handle the right-hand expression in EXPR_PROP and EXPR_VERB.
     * If it's a simple string literal with the syntax of an identifier,
     * just print the name.  Otherwise, use parens and unparse the
     * expression normally.
     */

    if (expr->kind == EXPR_VAR && expr->e.var.type == TYPE_STR
	&& ok_identifier(expr->e.var.v.str)) {
	stream_add_string(str, expr->e.var.v.str);
	return;
    }
    /* We need to use the full unparser */
    stream_add_char(str, '(');
    unparse_expr(str, expr);
    stream_add_char(str, ')');
}

static void
unparse_expr(Stream * str, Expr * expr)
{
    switch (expr->kind) {
    case EXPR_PROP:
	if (expr->e.bin.lhs->kind == EXPR_VAR
	    && expr->e.bin.lhs->e.var.type == TYPE_OBJ
	    && expr->e.bin.lhs->e.var.v.obj == 0
	    && expr->e.bin.rhs->kind == EXPR_VAR
	    && expr->e.bin.rhs->e.var.type == TYPE_STR
	    && ok_identifier(expr->e.bin.rhs->e.var.v.str)) {
	    stream_add_char(str, '$');
	    stream_add_string(str, expr->e.bin.rhs->e.var.v.str);
	} else {
	    bracket_lt(str, EXPR_PROP, expr->e.bin.lhs);
	    if (expr->e.bin.lhs->kind == EXPR_VAR
		&& expr->e.bin.lhs->e.var.type == TYPE_INT)
		/* avoid parsing digits followed by dot as floating-point */
		stream_add_char(str, ' ');
	    stream_add_char(str, '.');
	    unparse_name_expr(str, expr->e.bin.rhs);
	}
	break;

    case EXPR_VERB:
	if (expr->e.verb.obj->kind == EXPR_VAR
	    && expr->e.verb.obj->e.var.type == TYPE_OBJ
	    && expr->e.verb.obj->e.var.v.obj == 0
	    && expr->e.verb.verb->kind == EXPR_VAR
	    && expr->e.verb.verb->e.var.type == TYPE_STR
	    && ok_identifier(expr->e.verb.verb->e.var.v.str)) {
	    stream_add_char(str, '$');
	    stream_add_string(str, expr->e.verb.verb->e.var.v.str);
	} else {
	    bracket_lt(str, EXPR_VERB, expr->e.verb.obj);
	    stream_add_char(str, ':');
	    unparse_name_expr(str, expr->e.verb.verb);
	}
	stream_add_char(str, '(');
	unparse_arglist(str, expr->e.verb.args);
	stream_add_char(str, ')');
	break;

    case EXPR_INDEX:
	bracket_lt(str, EXPR_INDEX, expr->e.bin.lhs);
	stream_add_char(str, '[');
	unparse_expr(str, expr->e.bin.rhs);
	stream_add_char(str, ']');
	break;

    case EXPR_RANGE:
	bracket_lt(str, EXPR_RANGE, expr->e.range.base);
	stream_add_char(str, '[');
	unparse_expr(str, expr->e.range.from);
	stream_add_string(str, "..");
	unparse_expr(str, expr->e.range.to);
	stream_add_char(str, ']');
	break;

	/* left-associative binary operators */
    case EXPR_PLUS:
    case EXPR_MINUS:
    case EXPR_TIMES:
    case EXPR_DIVIDE:
    case EXPR_MOD:
    case EXPR_AND:
    case EXPR_OR:
    case EXPR_EQ:
    case EXPR_NE:
    case EXPR_LT:
    case EXPR_GT:
    case EXPR_LE:
    case EXPR_GE:
    case EXPR_IN:
	bracket_lt(str, expr->kind, expr->e.bin.lhs);
	stream_add_string(str, binop_string[expr->kind]);
	bracket_le(str, expr->kind, expr->e.bin.rhs);
	break;

	/* right-associative binary operators */
    case EXPR_EXP:
	bracket_le(str, expr->kind, expr->e.bin.lhs);
	stream_add_string(str, binop_string[expr->kind]);
	bracket_lt(str, expr->kind, expr->e.bin.rhs);
	break;

    case EXPR_COND:
	bracket_le(str, EXPR_COND, expr->e.cond.condition);
	stream_add_string(str, " ? ");
	unparse_expr(str, expr->e.cond.consequent);
	stream_add_string(str, " | ");
	bracket_le(str, EXPR_COND, expr->e.cond.alternate);
	break;

    case EXPR_NEGATE:
	stream_add_char(str, '-');
	bracket_lt(str, EXPR_NEGATE, expr->e.expr);
	break;

    case EXPR_NOT:
	stream_add_char(str, '!');
	bracket_lt(str, EXPR_NOT, expr->e.expr);
	break;

    case EXPR_VAR:
	stream_add_string(str, value_to_literal(expr->e.var));
	break;

    case EXPR_ASGN:
	unparse_expr(str, expr->e.bin.lhs);
	stream_add_string(str, " = ");
	unparse_expr(str, expr->e.bin.rhs);
	break;

    case EXPR_CALL:
	stream_add_string(str, name_func_by_num(expr->e.call.func));
	stream_add_char(str, '(');
	unparse_arglist(str, expr->e.call.args);
	stream_add_char(str, ')');
	break;

    case EXPR_ID:
	stream_add_string(str, prog->var_names[expr->e.id]);
	break;

    case EXPR_LIST:
	stream_add_char(str, '{');
	unparse_arglist(str, expr->e.list);
	stream_add_char(str, '}');
	break;

    case EXPR_SCATTER:
	stream_add_char(str, '{');
	unparse_scatter(str, expr->e.scatter);
	stream_add_char(str, '}');
	break;

    case EXPR_CATCH:
	stream_add_string(str, "`");
	unparse_expr(str, expr->e.catch.try);
	stream_add_string(str, " ! ");
	if (expr->e.catch.codes)
	    unparse_arglist(str, expr->e.catch.codes);
	else
	    stream_add_string(str, "ANY");
	if (expr->e.catch.except) {
	    stream_add_string(str, " => ");
	    unparse_expr(str, expr->e.catch.except);
	}
	stream_add_string(str, "'");
	break;

    case EXPR_LENGTH:
	stream_add_string(str, "$");
	break;

    default:
	errlog("UNPARSE_EXPR: Unknown Expr_Kind: %d\n", expr->kind);
	stream_add_string(str, "(?!?!?!?!?)");
	break;
    }
}

static void
unparse_arglist(Stream * str, Arg_List * args)
{
    while (args) {
	if (args->kind == ARG_SPLICE)
	    stream_add_char(str, '@');
	unparse_expr(str, args->expr);
	if (args->next)
	    stream_add_string(str, ", ");
	args = args->next;
    }
}

static void
unparse_scatter(Stream * str, Scatter * sc)
{
    while (sc) {
	switch (sc->kind) {
	case SCAT_REST:
	    stream_add_char(str, '@');
	    /* fall thru to ... */
	case SCAT_REQUIRED:
	    stream_add_string(str, prog->var_names[sc->id]);
	    break;
	case SCAT_OPTIONAL:
	    stream_printf(str, "?%s", prog->var_names[sc->id]);
	    if (sc->expr) {
		stream_add_string(str, " = ");
		unparse_expr(str, sc->expr);
	    }
	}
	if (sc->next)
	    stream_add_string(str, ", ");
	sc = sc->next;
    }
}

void
unparse_program(Program * p, Unparser_Receiver r, void *data,
		int fully_parenthesize, int indent_lines, int f_index)
{
    Stmt *stmt = decompile_program(p, f_index);

    prog = p;
    receiver = r;
    receiver_data = data;
    list_prg(stmt, fully_parenthesize, indent_lines);
    free_stmt(stmt);
}

static void
print_line(void *data, const char *line)
{
    FILE *fp = data;

    fprintf(fp, "%s\n", line);
}

void
unparse_to_file(FILE * fp, Program * p, int fully_parenthesize, int indent_lines,
		int f_index)
{
    unparse_program(p, print_line, fp, fully_parenthesize, indent_lines,
		    f_index);
}

void
unparse_to_stderr(Program * p, int fully_parenthesize, int indent_lines,
		  int f_index)
{
    unparse_to_file(stderr, p, fully_parenthesize, indent_lines, f_index);
}

char rcsid_unparse[] = "$Id$";

/* 
 * $Log$
 * Revision 1.3  1998/12/14 13:19:12  nop
 * Merge UNSAFE_OPTS (ref fixups); fix Log tag placement to fit CVS whims
 *
 * Revision 1.2  1997/03/03 04:19:34  nop
 * GNU Indent normalization
 *
 * Revision 1.1.1.1  1997/03/03 03:45:01  nop
 * LambdaMOO 1.8.0p5
 *
 * Revision 2.5  1996/02/18  23:21:00  pavel
 * Fixed unparsing of expression `X.Y', where X is an integer, to add a space
 * before the dot to keep it from looking like a floating-point literal.
 * Release 1.8.0beta3.
 *
 * Revision 2.4  1996/02/08  06:43:40  pavel
 * Added support for E_FLOAT, exponentiation expression, named WHILE loop,
 * BREAK and CONTINUE statements.  Added unparse_to_file() and
 * unparse_to_stderr().  Renamed err/logf() to errlog/oklog().  Updated
 * copyright notice for 1996.  Release 1.8.0beta1.
 *
 * Revision 2.3  1996/01/16  07:13:16  pavel
 * Add EXPR_SCATTER handling.  Release 1.8.0alpha6.
 *
 * Revision 2.2  1995/12/31  03:13:56  pavel
 * Added EXPR_LENGTH case to unparse_expr().  Release 1.8.0alpha4.
 *
 * Revision 2.1  1995/12/11  07:56:52  pavel
 * Added support for `$foo(...)' syntax.
 *
 * Release 1.8.0alpha2.
 *
 * Revision 2.0  1995/11/30  04:43:14  pavel
 * New baseline version, corresponding to release 1.8.0alpha1.
 *
 * Revision 1.18  1992/10/23  23:03:47  pavel
 * Added copyright notice.
 *
 * Revision 1.17  1992/10/23  22:23:09  pavel
 * Eliminated all uses of the useless macro NULL.
 *
 * Revision 1.16  1992/10/21  03:02:35  pavel
 * Converted to use new automatic configuration system.
 *
 * Revision 1.15  1992/10/17  20:56:00  pavel
 * Global rename of strdup->str_dup, strref->str_ref, vardup->var_dup, and
 * varref->var_ref.
 *
 * Revision 1.14  1992/09/23  01:15:05  pjames
 * Now prints out hot PC and fork_vector for non-found linenumbers.
 *
 * Revision 1.13  1992/09/23  01:00:45  pjames
 * When get_lineno doesn't find a linenumber, all program info is put
 * into the errlog.
 *
 * Revision 1.12  1992/09/23  00:22:09  pavel
 * Fixed up missing RCS change log.
 *
 * Revision 1.11  1992/09/04  01:23:50  pavel
 * Added support for the `f' (for `fertile') bit on objects.
 *
 * Revision 1.10  1992/08/31  22:24:49  pjames
 * Changed some `char *'s to `const char *'
 *
 * Revision 1.9  1992/08/28  23:17:52  pjames
 * Added ASGN_RANGE unparsing code.
 * Changed OP_LISTSET to OP_INDEXSET.
 *
 * Revision 1.8  1992/08/28  16:19:59  pjames
 * Changed vardup to varref.
 *
 * Revision 1.7  1992/08/14  00:26:51  pavel
 * Fixed missing `static' in declarations of stmt2ast and expr2ast.
 *
 * Revision 1.6  1992/08/12  01:52:17  pjames
 * Optimized negative numbers are now translated to negative literals,
 * instead of negated positive literals.
 *
 * Revision 1.5  1992/08/10  16:41:46  pjames
 * Changed ip to pc.  Updated #includes.
 *
 * Revision 1.4  1992/07/30  21:28:42  pjames
 * Completely redone.  Converts Bytecodes to AST, then using code from
 * version 1.3 of the server, computes the line number or generates
 * source code.
 *
 * Revision 1.3  1992/07/27  18:15:06  pjames
 * Changed name of ct_env to var_names, const_env to literals, and
 * f_vectors to fork_vectors.
 *
 * Revision 1.2  1992/07/21  00:07:36  pavel
 * Added rcsid_<filename-root> declaration to hold the RCS ident. string.
 *
 * Revision 1.1  1992/07/20  23:23:12  pavel
 * Initial RCS-controlled version.
 */