8 static void indent(tsd_t
*TSD
)
12 for (i
=0; i
<TSD
->indentsize
; i
++)
17 static void tabin(tsd_t
*TSD
)
19 TSD
->indentsize
+= 3 ;
23 static void tabout(tsd_t
*TSD
)
25 TSD
->indentsize
-= 3 ;
31 printf( "require \"r2perl.pl\" ;\n\n" ) ;
35 static void output( const char *cptr
)
37 printf( "%s", cptr
) ;
41 static void outint( int num
)
47 static void outstr( const streng
*str
)
51 for (i
=0; i
<str
->len
; i
++)
52 putchar( str
->value
[i
] ) ;
55 static void expression( nodeptr
this )
74 expression( this->p
[0] ) ;
77 case X_EQUAL
: output( "==" ) ; break ;
78 case X_GT
: output( "==" ) ; break ;
79 case X_LT
: output( "==" ) ; break ;
81 expression( this->p
[1] ) ;
91 expression( this->p
[0] ) ;
95 case X_MODULUS
: output( "%" ) ; break ;
96 case X_DEVIDE
: output( "/" ) ; break ;
97 case X_MULT
: output( "*" ) ; break ;
98 case X_MINUS
: output( "-" ) ; break ;
99 case X_PLUSS
: output( "+" ) ; break ;
101 expression( this->p
[1] ) ;
107 outstr( this->name
) ;
109 for (tptr
=this->p
[0]; tptr
; tptr
=tptr
->p
[1])
112 expression( tptr
->p
[0] ) ;
123 outstr( this->name
) ;
129 outstr( this->name
) ;
134 expression( this->p
[0] ) ;
135 if (this->type
== X_SPACE
)
136 output( ",\" \"," ) ;
139 expression( this->p
[1] ) ;
152 void translate( tsd_t
*TSD
, nodeptr
this )
161 switch ( this->type
)
167 translate( TSD
, this->p
[0] ) ;
174 if ((!this->p
[0]) && (!this->p
[1]))
179 else if (this->p
[0] && !this->p
[0]->name
)
181 output( "for ($loopcnt_" ) ;
182 outint( TSD
->loopcnt
) ;
183 output( "=0; $loopcnt_" ) ;
184 outint( TSD
->loopcnt
) ;
186 expression( this->p
[0]->p
[1]->p
[0] ) ;
187 output( "; $loopcnt_" ) ;
188 outint( TSD
->loopcnt
++ ) ;
197 outstr( this->p
[0]->name
) ;
199 expression( this->p
[0]->p
[0] ) ;
204 if (this->p
[0]->p
[i
] && this->p
[0]->p
[i
]->type
== X_DO_TO
)
207 outstr( this->p
[0]->name
) ;
209 expression( this->p
[0]->p
[i
]->p
[0] ) ;
217 if (this->p
[0]->p
[i
] && this->p
[0]->p
[i
]->type
== X_DO_BY
)
220 outstr( this->p
[0]->name
) ;
222 expression( this->p
[0]->p
[i
]->p
[0] ) ;
232 if (this->p
[1] && this->p
[1]->type
== X_WHILE
)
236 expression( this->p
[1]->p
[0] ) ;
237 output( ") break ;\n" ) ;
240 translate( TSD
, this->p
[2] ) ;
242 if (this->p
[1] && this->p
[1]->type
== X_UNTIL
)
246 expression( this->p
[1]->p
[0] ) ;
247 output( ") break ;\n" ) ;
259 expression( this->p
[0] ) ;
264 translate( TSD
, this->p
[1] ) ;
275 translate( TSD
, this->p
[2] ) ;
286 outstr( this->name
) ;
288 expression( this->p
[0] ) ;
297 output( expression( this->p[0] ) ) ;
304 output( "else { die( "WHEN or OTHERWISE expected" ) ; }" ) ;
312 nstack[nstackptr++] = this->next ;
313 nstack[nstackptr++] = this->p[1] ;
321 if (str_true(TSD,tptr=evaluate(TSD,this->p[0])))
323 Free_stringTSD( tptr ) ;
324 nstackptr-- ; / * kill the OTHERWISE on the stack * /
328 Free_stringTSD( tptr ) ;
339 expression( this->p
[0] ) ;
342 output( "\"\\n\" ;\n" ) ;
355 expression( this->p
[0] ) ;
363 output( "system(" ) ;
364 expression( this->p
[0] ) ;
368 case X_ADDR_N: / * ADDRESS environment [expr] * /
370 streng *envir, *tmp, *rc ;
376 tmp = evaluate(TSD, this->p[0]);
377 rc = perform(tmp, envir, this->lineno);
378 Free_stringTSD( tmp ) ;
379 setvalue( TSD, &RC_name, rc ) ;
383 Free_stringTSD( TSD->currlevel->prev_env ) ;
384 TSD->currlevel->prev_env = TSD->currlevel->environment ;
385 TSD->currlevel->environment = Str_dupTSD(envir) ;
391 case X_ADDR_V: / * ADDRESS [VALUE] expr * /
395 cptr = evaluate(TSD, this->p[0]) ;
396 Free_stringTSD( TSD->currlevel->prev_env ) ;
397 TSD->currlevel->prev_env = TSD->currlevel->environment ;
398 TSD->currlevel->environment = cptr ;
403 case X_ADDR_S: / * ADDRESS * /
407 tptr = TSD->currlevel->environment ;
408 TSD->currlevel->environment = TSD->currlevel->prev_env ;
409 TSD->currlevel->prev_env = tptr ;
417 for (nptr=this->p[0]; nptr; nptr=nptr->p[0] )
419 if (nptr->type == X_SIM_SYMBOL)
420 drop_var( TSD, nptr->name ) ;
421 else if (nptr->type == X_IND_SYMBOL)
424 streng *name,*value = shortcut(TSD,nptr) ;
426 /* Chop space separated words and drop them one by one */
429 begin
= end
; /* end of last word processed + 1 */
430 while ((begin
< Str_len(value
)) &&
431 isspace(value
->value
[begin
]))
433 if (begin
== Str_len(value
))
435 end
= begin
+ 1; /* find next separator */
436 while ((end
< Str_len(value
)) &&
437 !isspace(value
->value
[end
]))
439 /* end now on space after word or past end of string */
441 name
= Str_makeTSD(end
- begin
);
442 name
->len
= end
- begin
;
443 memcpy(name
->value
, value
->value
+ begin
, Str_len(name
));
447 drop_var( TSD
, name
) ;
448 Free_stringTSD( name
) ;
458 trap
*traps
= gettraps( TSD
, TSD
->currlevel
) ;
460 / * which kind of condition is
this? * /
461 type
= identify_trap( this->p
[1]->type
) ;
463 / * We always set
this * /
464 traps
[type
].invoked
= (this->type
== X_SIG_SET
) ;
465 traps
[type
].delayed
= 0 ;
466 traps
[type
].on_off
= (this->p
[0]->type
== X_ON
) ;
468 / * set the name of the variable to work on
* /
469 FREE_IF_DEFINED( traps
[type
].name
) ;
471 traps
[type
].name
= Str_dupTSD( this->name
) ;
472 else if (this->p
[0]->type
== X_ON
)
473 traps
[type
].name
= Str_creTSD( signalnames
[type
] ) ;
483 cptr
= (this->name
) ? Str_dupTSD(this->name
) : evaluate( TSD
, this->p
[0] ) ;
485 for (;stackptr
>0;stackptr
--)
487 if (stack
[stackptr
-1].increment
)
489 free_a_descr(TSD
,stack
[stackptr
-1].increment
) ;
490 stack
[stackptr
-1].increment
= NULL
;
493 if (stack
[stackptr
-1].stopval
)
495 free_a_descr(TSD
, stack
[stackptr
-1].stopval
) ;
496 stack
[stackptr
-1].stopval
= NULL
;
500 setvalue( TSD
, var_sigl
, int_to_streng( TSD
, this->lineno
)) ;
501 entry
= getlabel( TSD
, cptr
) ;
504 Free_stringTSD( cptr
) ;
506 if ((entry
)==NULL
) exiterror(16) ;
512 if (TSD
->currlevel
->varflag
)
513 exiterror( ERR_UNEXPECTED_PROC
, 0 ) ;
515 for (ptr
=this->p
[0];(ptr
);ptr
=ptr
->p
[0])
517 expose_var(TSD
,ptr
->name
) ;
519 exiterror( ERR_INTERPRETER_FAILURE
, 0 ) ;
521 expose_var(TSD
,NULL
) ;
526 this->u
.node
= getlabel(TSD
,this->name
) ;
527 this->type
= (this->u
.node
) ? X_IS_INTERNAL
: X_IS_BUILTIN
;
536 setvalue( TSD
, var_sigl
, int_to_streng( TSD
, this->lineno
)) ;
538 no_next_interactive
= 1 ;
539 targs
= initplist( TSD
, this ) ;
540 oldlevel
= TSD
->currlevel
;
541 TSD
->currlevel
= newlevel( TSD
, TSD
->currlevel
) ;
542 TSD
->currlevel
->args
= targs
;
543 stackmark
= pushcallstack( this ) ;
545 result
= interpret( TSD
, this->u
.node
) ;
547 popcallstack( stackmark
) ;
548 removelevel( TSD
, TSD
->currlevel
) ;
549 TSD
->currlevel
= oldlevel
;
550 TSD
->currlevel
->next
= NULL
;
553 setvalue( TSD
, RESULT_name
, result
) ;
555 drop_var( TSD
, RESULT_name
) ;
563 if (!(result
= buildtinfunc( TSD
, this )))
564 exiterror( ERR_ROUTINE_NOT_FOUND
, 0 ) ;
567 setvalue( TSD
, RESULT_name
, result
) ;
569 drop_var( TSD
, RESULT_name
) ;
576 args
= TSD
->currlevel
->args
->next
;
577 (void)parseargtree( TSD
, this, args
, this->type
!=X_PARSE_ARG
) ;
583 switch (this->p
[0]->type
) {
585 source
= Str_dupTSD(shortcut( TSD
, this->p
[0] )) ;
586 / * source
= Str_dupTSD(getvalue( TSD
, this->p
[0]->name
, 1 )) ; * /
590 source
= evaluate(TSD
, this->p
[0]->p
[0]);
594 source
= popline( TSD
) ;
598 source
= Str_creTSD(PARSE_VERSION_STRING
) ;
602 source
= readkbdline( TSD
) ;
606 origfile
= TSD
->systeminfo
->called_as
;
607 inpfile
= TSD
->systeminfo
->input_file
;
608 source
= Str_makeTSD(15+Str_len(origfile
)+Str_len(inpfile
)) ;
610 Str_catstrTSD(source
,"UNIX COMMAND ") ;
611 Str_catTSD(source
,inpfile
) ;
612 Str_catstrTSD(source
," ") ;
613 Str_catTSD(source
,origfile
) ;
617 if (this->type
==X_PARSE_U
)
618 (void)upcase(source
) ;
620 doparse( TSD
, source
, this->p
[1] ) ;
621 Free_stringTSD( source
) ;
627 output( "push( @rx_array, " ) ;
628 expression( this->p
[0] ) ;
634 output( "pop( @rx_array, " ) ;
635 expression( this->p
[0] ) ;
641 output( "unshift( @rx_array, " ) ;
642 expression( this->p
[0] ) ;
649 output( "return( " ) ;
651 expression( this->p
[0] ) ;
661 output( "warn( \"symbolname ignored in LEAVE\n\") ;\n" ) ;
664 output( "break ;\n" ) ;
671 output( "warn( \"symbolname ignored in ITERATE\n\") ;\n" ) ;
674 output( "continue ;\n" ) ;
680 streng *cptr = evaluate( TSD, this->p[0] ) ;
681 TSD->currlevel->currnumsize = atopos( TSD, cptr ) ;
682 Free_stringTSD( cptr ) ;
688 streng *cptr = evaluate( TSD, this->p[0] ) ;
689 TSD->currlevel->numfuzz = atozpos( TSD, cptr ) ;
690 Free_stringTSD( cptr ) ;
696 if (this->p[0]->type == X_NUM_SCI)
697 TSD->currlevel->numform = NUM_FORM_SCI ;
698 else if (this->p[0]->type == X_NUM_ENG)
699 TSD->currlevel->numform = NUM_FORM_ENG ;
701 exiterror( ERR_INTERPRETER_FAILURE, 0 ) ;