f~oyecto de investioaci~n148.206.53.84/tesiuami/uam8534.pdf · c 1 generacion de codigo intermedio...

104
.. UNIVERSIDAD AUTONOMA METROPOLITANA.-% . c0-3- F~OYECTO DE INVESTIOACI~N 1-11 LIC. EN COMPUTACI~N INFORME FINAL /' SEPTIEMI~RE '89

Upload: vancong

Post on 22-Oct-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

..

UNIVERSIDAD AUTONOMA METROPOLITANA.-% . c0-3-

F~OYECTO DE INVESTIOACI~N 1-11

LIC. EN COMPUTACI~N

INFORME FINAL

/'

SEPTIEMI~RE '89

1

D i s e ñ a r un l e n g u a j e de p r o g r a m a c i ó n q u e p e r m i t a 1 a d e f i n i c i ó n y m a n i p u l a c i ó n de g r i f i c a s por computadora.

I m p l a n t a r el l e n g u a j e p a r a o p e r a r l o e n u n a c o m p u t a d o r a t i p o PC.

, . ,. 2

La idea d e d i s e ñ a r e i m p l a n t a r un l e n g u a j e p a r a m a n i p u l a r g r á f i c o s s u r g i ó a r a í z d e l i n t e r é s d e c o m b i n a r tanto el manejo de 1 engua jes d e p r o g r a m a c i ó n como 1 a g r a f i c a c i ó n p o r c o m p u t a d o r a , t d p i c o q u e a c t u a l m e n t e h a i d o a l c a n z a n d o g r a n i m p o r t a n c i a e n el campo d e l a c o m p u t a c i ó n .

L a g r a f i c a c i ó n por computadora es un medio de. c o m u n i c a c i ó n b a s t a n t e efectivo e n t r e el hombre y l a máquina: el o j o humano puede absorber con mayor f aci 1 i dad una imágen que una s e r i e d e e c u a c i ones o t a b 1 as de números.

En 'la a c t u a l i d a d , p r á c t i c a m e n t e n o existe n i n g u n a á r e a d e l q u e h a c e r humano donde no puedan u t i l i zarse los d e s p l i e g u e s g r á f i c o s c o n a l g u n a v e n t a j a . Hoy d í a , se puede a d v e r t i r q u e éstas g r á f i c a s se u t i l i z a n e n áreas t a n I

d i v e r s a s como l a a d m i n i s t r a c i ó n , el a r t e , l a p u b l i c i d a d , l a e d u c a c i ó n y l a m e d i c i n a , p o r c i t a r a l g u n a s .

E l p r e s e n t e p r o y e c t o h a si d o c o n c e b i d o de t a l forma que e n g l o b a l o s p r i n c i p i o s b d s i c o s d e l a g e n e r a c i ó n d e c o m p i l a d o r e s así como los. procedimientos minimos con los q u e d e b e c o n t a r c u a l q u i e r sistema o r i e n t a d o a l a mani pul aci ón ,de g r áf i cas.

Es a s i .como 1 os comandos de programación para d e s p l e g a r y m a n i p u l a r s a l i d a s d e g r á f f c a a e s t a n d i s e ñ a d o s bajo una e s t r u c t u r a s i m i l a r a l a q u e p r e s e n t a n a l g u n o s l e n g u a j e s e x i s t e n t e s , tomando en cuenta que l a f a c i l i d a d d e s u u t i l i z a c i ó n es un p u n t o b a s t a n t e i m p o r t a n t e q u e n o d e b e ser pasado por a l t o .

3

D e n t r o d e l a p r o g r a m a c i d n d e sistemas, un c o m p i l a d o r es aquel programa que 1 ee un texto e s c r i t o e n un 1 engua j e d e p r o g r a m a c i ó n d e t e r m i n a d o C l e n g u a j e f uente3 y l o t r a d u c e a un texto e q u i v a l e n t e e s c r i t o e n otro l e n g u a j e d e menor n i vel C 1 engua j e d e s t i no3 . Una p a r t e muy i m p o r t a n t e d e l p r o c e s o d e t r a d u c c i ó n es l a n o t i f i c a c i d n a l u s u a r i o d e l a p r e s e n c i a d e errores e n el programa fuente.

PAdemos o b s e r v a r dos clases d e compi 1 a d o r e s :

a3 Aquel los q u e t r a d u c e n a un l e n g u a j e d e bajo n i v e l C l e n g u a j e objeto3, t a l como un ensamblador, y

b3 A q u e l l o s q u e t r a d u c e n a un l e n g u a j e s i m p l i f i c a d o l l a m a d o c ó d i g o . i n t e r m e d i o , el c u a l p u e d e s e r e j e c u t a d o d i r e c t a m e n t e p o r un p r o g r a m a l l a m a d o i n t é r p r e t e .

E s p r e c i s a m e n t e &te u l t i m o caso el que h a s i d o d e s a r r o l l a d o , d e tal forma que l a e s t r u c t u r a d e l c o m p i l a d o r es l a s i g u i e n t e :

P r o g r a m a f u e n t e - A n a l i z a d o r L é x i c o Anal i zador Si n t á c t i co G e n e r a d o r d e c d d i go i n t e r medi o O p t i m i z a d o r d e c ó d i go

1 J.

C ó d i g o o p t i m i z a d o

1 J.

I n t é r p r e t e

4

.

A) ANALIZADOR LEXICO E l a n a l i z a d o r léxico d e t e c t a e i d e n t i f i c a los

componentes lexicales o t o k e n s d e n t r o d e l p r o g r a m a f u e n t e , r e c o n o c i e n d o u n a s e c u e n c i a d e c a r a c t e r e s d e l i m i t a d a ya sea por un e s p a c i o e n b l a n c o , el c a r á c t e r d e c o n t r o l d e f i n d e l í n e a o el c a r á c t e r de c o n t r o l de f i n d e a r c h i v o . D i c h o r e c o n o c i m i e n t o se l o g r a a p a r t i r d e l t i p o d e c a r á c t e r q u e c o r r e s p o n d e al primero de l a s e c u e n c i a . De ésta forma se l l e g a a d e t e r m i n a r si el t o k e n d e t e c t a d o es u n a p a l a b r a r e s e r v a d a , un s í m b o l o e s p e c i a l , un i d e n t i f i c a d o r , un número, un c o m e n t a r i o , una cadena o un símbolo d e s c o n o c i d o .

P a r a t a l efecto se u t i l i z a n 2 t a b l a s como s o p o r t e , l a s c u a l e s e s t á n a l m a c e n a d a s e n a r r e g l o s . Una d e ellas c o n t i e n e e n o r d e n a l f a b 6 t i c o t o d a s 1 a s p a l a b r a s r e s e r v a d a s d e q u e c o n s t a el 1 e n g u a j e d e p r o g r a m a c i d n d i s e ñ a d o ; la o t r a c o n t i e n e t o d o s los símbolos e s p e c i a l e s q u e p u e d e n s e r u t i 1 i z a d o s d e n t r o d e l p r o g r a m a ,

f u e n t e . La búsqueda en ambas t a b l a s se ha implementado como una b ú s q u e d a b i n a r i a c o n el o b . j e t o d e a g i l i z a r el a n á l i s i s e i n c r e m e n t a r s u e f i c i e n c i a . En el a p é n d i c e I se muestra el c o n t e n i d o d e c a d a u n a d e las t a b 1 as.

Un e l e m e n t o muy i m p o r t a n t e d e n t r o d e l a n a l i z a d o r léxico es l a l l a m a d a ' t a b l a d e i d e n t i f i c a d o r e s , l a c u a l se u t i l i z a p a r a a l m a c e n a r t o d a l a i n f o r m a c i ó n d e t e c t a d a e n c u a n t o a l a d e c l a r a c i ó n , u s o y manejo de los i d e n t i f i c a d o r e s e n el pr ogr ama f u e n t e . Con el objeto de minimizar l a c a n t i d a d d e memoria r e q u e r i d a p a r a d i c h o a l m a c e n a m i e n t o , a c o n t i n u a c i ó n se d e t a l l a n l a s e s t r u c t u r a s d e d a L o s q u e han s i d o ' e m p l e a d a s :

Una l i s t a l i g a d a c u y o n o d o t i e n e una c a p a c i d a d máxima d e 20 c a r a c t e r e s y que es u t i l i z a d a p a r a a l m a c e n a r c a d a u n o d e l o s i d e n t i f i c a d o r e s .

Una l i s t a l i g a d a p a r a a l m a c e n a r l a i n f o r m a c i ó n b á s i c a d e c a d a u n o de los i d e n t i f i c a d o r e s , a s a b e r :

b. 13

b. 21

b. 33

El apuntador a l a l i s t a donde se i n i c i a el al macenami e n t o f í si co d e l i d e n t i f i c a d o r .

E l l u g a r d e n t r o d e d i c h a l i s t a donde se i n i c i a el almacenamiento a s í como 1 a 1 o n g i t u d e n c a r a c t e r e s d e l i d e n t i f i c a d o r .

El apuntador a 1 a 1 i S t a d o n d e se almacenan los números d e renglón donde se h a c e r e f e r e n c i a a l i d e n t i f i c a d o r .

b. 43

b. 6 3

b. 6?

b. 7 3

E l t i p o d e l i d e n t i f i c a d o r : Prugrama. Definicidn, Entero o No Identificado.

E l número d e r e n g l ó n d e n t r o d e l c ó d i g o f i n a l donde se i n i c i a un p r o c e d i m i e n t o Define, si' pr eci samente el i d e n t i f i c a d o r c o r r e s p o n d e a una D e f i n i c i ó n .

L a clave C c r L i a s I c o n q u e será manejado d i c h o i d e n t i f i c a d o r d e n t r o d e 1 a i n t e r pr etaci 6n.

Y los a p u n t a d o r e s a l a s listas locales d e i d e n t i f i c a d o r es Cal macenami e n t o f í s i c o e i n f o r m a c i ó n básica) e n caso d e que el i denti f i c a d o r p r e s e n t e c o r r e s p o n d a a una Definicich.

ALmasenamiento F ~ S ~ C O

1

i j k t r i a n g u l o

Información eámica

Lirta de Renglones

€j: prog nada; entero i,j,k; d e f i n e t r i a n g d o ; comienza

i :r o; 9

i :m i + i; . . .

medida que se v a n d e t e c t a n d o los i d e n t i f i c a d o r e s e n el ama f u e n t e , se a c c e s a n l a s t a b l a s c o r r e s p o n d i e n t e s a c t u a l i z a r l a i n f o r m a c i ó n q u e sea p e r t i n e n t e .

C a b e a c l a r a r que cuando un i d e n t i f i c a d o r n o se localiza en ninguna t a b l a d e i d e n t i f i c a d o r e s , se p r o c e d e a ser a g r e g a d o d e a c u e r d o a l a s s i g u i e o t e s r e g l a s :

a> Si es l a d e c l a r a c i d n d e l i d e n t i f i c a d o r , se a g r e g a a l a t a b l a local e n caso d e q u e d i c h a d e c l a r a c i ó n esté i n c l u í d a d e n t r o d e l c u e r p o d e l p r o g r a m a p r i n c i p a l ; d e o t r o modo, se a g r e g a a l a t a b l a l o c a l c o r r e s p o n d i e n t e al p r o c e d i m i e n t o Define d o n d e h a s i d o d e c l a r a d o .

b)' Si el i d e n t i f i c a d o r se d e t e c t ó e n u s o d e n t r o d e l c u e r p o d e l p r o g r a m a p r i n c i p a l , se a g r e g a a l a t a b l a g l obal .

c) Si el i d e n t i f i c a d o r se d e t e c t ó e n u s o d e n t r o d e l . cuer'po de un p r o c e d i m i e n t o Define, se a g r e g a a l a

t a b 1 a 1 oca1 c o r r e s p o n d i e n t e a d i cho pr o c e d i m i e n t o .

Así mismo, d e n t r o d e l a n a l i z a d o r l é x i c o , se d e t e c t a n e i d e n t i f i c a n l o s s i g u i e n t e s t i p o s d e e r r o r e s :

a3 I d e n t i f i c a d o r es n o d e f i n i d o s . b.3 I d e n t i f i c a d o r e s r e d e f i n i d o s . c l E x c e s o - d e c a r a c t e r e s e n u n a c a d e n a . d l Uso , i n c o r r e c t o d e un i d e n t i f i c a d o r d e un

el D e t e c c i ó n d e un símbolo d e s c o n o c i d o . p r o c e d i m i e n t o Define.

7

B f ANALIZADOR SlNTACTlCO T o d o l e n g u a j e d e p r o g r a m a c i ó n t i e n e r e g l a s q u e

p r e s c r i b e n l a e s t r u c t u r a s i n t á c t i c a d e los programas bien e s c r i t o s .

L a s i n t á x i s p a r a l a c o n s t r u c c i ó n d e un l e n g u a j e d e p r o g r a m a c i ó n p u e d e s e r d e s c r i t a p o r u n a G r a m á t i c a L i b r e d e C o n t e x t o o b a j o una n o t a c i ó n BNF CBackus - Naur Form>, p r o p o r c i o n a n d o u n a e s p e c i f i c a c i b n s i n t á c t i c a p r e c i s a y f á c i l d e e n t e n d e r y u t i l i z a r .

L a g r a m á t i c a d e l l e n g u a j e d e m a n i p u l a c i c h d e g r á f i c o s q u e h a s i d o d i s e ñ a d o se p r e s e n t a e n el a p é n d i c e I1 u t i l i z a n d o l a n o t a c i 6 n BNF. Como t o d a g r a m á t i c a l i b r e d e c o n t e x t o , l a mostrada cuenta c o n c u a t r o e l e m e n t o s b á s i c o s :

1. Un c o n j u n t o d e t o k e n s c o n o c i d o s como sí mbol os . t e r d n a l e s C s u b r a y a d o s 3 .

2. Un c o n j u n t o d e no t e r m i n a l e s C < --- > 3 .

3 . Un c o n j u n t o d e r e g l a s d e p r o d u c c i ó n .

4. L a d e s i g n a c i 6 n d e uno d e los n o t e r m i n a l e s como s í m b o l o i n i c i a l C <programa> 3 .

C a d a r e g 1 a d e p r o d u c c i on está s e p a r a d a e n d o s 1 ados por el metasímbolo '-+', el c u a l p u e d e l e e r s e como ' c o n s i s t e e n ' . Además, se han u t i l i z a d o c o r c h e t e s y l l a v e s como m e t a s í m b o l o s p a r a i n d i c a r q u e l a s e c u e n c i a q u e e n c i e r r a n es o p c i o n a l o p u e d e o c u r r i r c u a l q u i e r número d e veces en s u c e s i ó n r e s p e c t i v a m e n t e .

Ahora bien, el o b j e ? t i v o p r i n c i p a l d e l a n a l i z a d o r si n t . a c t i c o es o b t e n e r una s e r i e de taskens del a n a l i z a d o r léxico y v e r i f i c a r q u e d i c h a s e r i e p u e d a ser genorada por l a g r a m á t i c a establecida p a r a el l e n g u a j s f u e n t a e , r e p o r t a n d o a q u e l los s r r o r e s d e s i n t á x i s e n q u e se ha i ncur r i do. Como e s t r a t e g i a s e n l a r e c u p e r a c i ó n d e l o s e r r o r e s se han i mplementado las 1 1 amadas Modo d e P á n i c o y N i vel d e F r a s e .

E l a n a l i z a d o r s i n t á c t i c o d e s a r r o l l a d o es d e l t i p o p r e d i c t i v o p o r d e s c e n s o r e c u r s i v o , por lo q u e c u e n t a c o n un p r o c e d i m i e n t o e s p e c i f i c o p a r a c a d a símbolo n o t e r m i n a l . De éSta f o r m a , c a d a p r o c e d i m i e n t o d e c i d e q u e p r o d u c c i ó n u t i l i z a r d e a c u e r d o a l s i g u i e n t e t o k e n r e p o r t a d o por el a n a l i z a d o r l d x i c o y , una vez q u e se d e t e r m i n a d i c h a p r o d u c c i ó n , se t r a t a d e i m i t a r d e manera que l a s e r i e d e t o k e n s c o i n c i d a c o n d i c h a p r o d u c c i ó n . De no e x i s t i r coi n c i d e n c i a a l g u n a , se p r o c e d e a n o t i f i c a r el c o r r e s p o n d i e n t e error d e s intáxis.

8

Como p a r t e f i n a l d e l a n a l i z a d o r s i n t á c t i c o se e n c u e n t r a l a i m p r e s i ó n d e r e f e r e n c i a s c r u z a d a s , l a c u a l c o n s i s t e u n i c a m e n t e e n v a c i a r el c o n t e n i d o de l a s tab1 as d e i d e n t i f i c a d o r e s global y 1 oca1 es generadas por el a n a l i z a d o r l6xico.

C 1 GENERACION DE CODIGO INTERMEDIO Como p a r t e i n d i v i s i b l e d e l a n a l i z a d o r s i n t á c t i c o se

e n c u e n t r a l a g e n e r a c i ó n d e c ó d i g o i n t e r m e d i o .

El código g e n e r a d o es d e l ti PO d e t r e s d i r e c c i o n e s :

Operador C Destino, Operando r, Operando 2)

C a b e a c l a r a r q u e , d a d o q u e 1 as p r o p o s i c i o n e s Rec tangulo y Sernielipse r e q u i e r e n d e c u a t r o p a r á m e t r o s o b l i g a t o r i o s p a r a s u i m p l e m e n t a c i 6 n , el c d d i g n g e n e r a d o p a r a ambas c o n s i s t e d e dos l í n e a s , c a d a u n a d e e l l a s c o n d o s par dmet.r os.

%be h a c e r s e n o t a r q u e como l a g e n e r a c i ó n d e código está i n t a g r a d a a l a n a l i z a d o r s i n t i c t i c o , u n a vez q u e se d e t e c t a un e r r o r d e l i x i c o o s i n t á x i s , se i n t e r r u m p e d i c h a g e n e r a c i ó n .

Todas l a s p r o p o s i c i o n e s d e l l e n g u a j e f u e n t e d e t i p o c o n d i c i o n a l C Para, Mientras, etc . 3 r e q u i e r e n p a r a s u c o d i f i caci ó n d e e t i q u e t a s si mból i cas d e sal to. P a r a el m a n e j o d e d i c h a s e t i q u e t a s se han implementado dos pr o c e d i m i e n t o s e s p e c í f i cos:

i3 genera-etiqueta C n J , el c u a l g e n e r a t a n t a s e t i q u e t a s como sea el val or de n y l a s mete e n u n a p i l a , y

i i 3 saca-etiqueta Cn), el c u a l saca n e t i q u e t a s d e l a p i l a d e e t i q u e t a s . .

Por o t r a p a r t e , e n el c ó d i g o g e n e r a d o n o se u t i 1 i z a n l o s í d e n t i f i c a d o r e s d e f i n i d o s p o r el u s u a r i o ; e n l u g a r d e el l o se emplean números c lave <alias3 q u e p e r m i t e n un mejor c o n t r o l d e l a s v a r i a b l e s y l o s accesos a l o s p r o c e d i m i e n t o s d e D e f i n i c i ó n . Así mismo, se h a c e u s o d e v a r i a b l e s t e m p o r a l e s como s o p o r t e e n 1 a g e n e r a c i c j n d e l c ó d i g o d e al gunas pr oposi c i o n e s C p . e. AS i gnac i c;n3 .

E l c ó d i g o i n t e r m e d i o está c o m p u e s t o d e 47 p s e u d o i n s t r u c c i o n e s d i f e r e n t e s , l a s c u a l e s p u e d e n a g r u p a r s e e n seis + . i p o s d i s t i n t o s , t a l como se m u e s t r a e n el a p é n d i c e 111.

D 1 OPTIMZADOR DE CODIGO Es c o n v e n i e n t e q u e el c ó d i g o i n t e r m e d i o se o p t í m i ze d e

manera tal q u e el t r a b a j o d e l i n t é r p r t e se m i n i m i z e .

Por e l l o , d u r a n t e esta fase, se p r o c e d e a l a e l i m i n a c i d n d e t o d a s 1 a s e t i q u e t a s simbólicas d e s a l t o d e l a r c h i v o d e c o d i go i n t e r medi o g e n e r a d o c o n a n t e r i or i d a d , c o n t a b i 1 i zando el número de ' i n s t r u c c i o n e s ' C l i n e a s 3 r e a l d e q u e c o n s t a , d e f o r m a q u e se p u e d a d e t e r m i n a r c o n p r e c i s i ó n l a p o s i c i ó n q u e o c u p a l a i n s t r u c c i ó n i n m e d i a t a s i g u i e n t e a l a Cs3 e t i q u e t a <s3 . e l i m i n a d a Cs3.

P o s t e r i o r m e n t e , e n un s e g u n d o p a s o , t o d a s l a s r e f e r e n c i a s h e c h a s p o r a l g u n a p s e u d o i n s t r u c c i ó n a c u a l q u i e r e t i q u e t a s o n r e e m p l a z a d a s p o r l a p o s i c i 6 n c o r r e s p o n d i e n t e , a f i n d e mantener l a c o n s i s t e n c i a d e l a i n f o r m a c i ó n .

P a r a e f e c t . u a r éSta 1 abor , el o p t i m i z a d o r se v a l e d e dos e s t r u c t u r a s d e d a t o s e s p e c í f i c a s :

il Una p i l a p a r a a l m a c e n a r t,odas las e t i q u e t . a s c o n s e c u t i v a s Q n c o n t r a d a s e n el a r c h i v o d e c o d i g o i n t e r medi o, y

i i > Una l i s t a l i g a d a p a r a a l m a c e n a r l o s e t i q u e t a s q u e s e r á n r e e m p l a z a d a s , i n c l u y e n d o el número d e 1 i nea o p o s i c i ó n q u e a h o r a d e b e r á s e r r e f e r e n c i a d o p o r 1 as p s e u d o i n s t r u c c i o n e s q u e así l o r e q u i e r a n .

P u e s t o q u e e n esta f a s e d e o p t i m i z a c i ó n d e c ó d i g o se determinan los numeros d e l í n e a d o n d e i n i c i a n t a n t o el programa pr i n c i p a l como I os pr o c e d i m i e n t o s d e d e f i n i c i cin , se accesa 1 a t a b l a d e i d e n t i f i c a d o r e s global a f i n de a c t u a l i z a r l a i n f o r m a c i ó n c o r r e s p o n d i e n t e , l o g r a n d o c o n esto q u e se m a n t e n g a u n a p e r f e c t a c o o r d i n a c i d n e n t r e l a i n f o r m a c i ó n c o n t e n i d a e n l a tabla y el a r c h i v o f i n a l d e c ó d i go.

1 0

E 1 INTERPRETE L a f u n c i d n b á s i c a d e l i n t i r p r e t e d e l c ó d i g o i n t e r m e d i o

es e f e c t u a r las o p e r a c i o n e s e s p e c i f i c a d a s por 1 a s p s e u d o i n s t r u c c i o n e s i n c l u i d a s d e n t r o d e l a r ' c h i v o d e c ó d i g o .

Para e l l o se u t i l i z a un c o n t a d o r d e programa CPC3 que p e r m i t e c o n o c e r l a s i g u i e n t e p s e u d o i n s t r u c c i ó n a ser e j e c u t a d a . I n i c i a l mente, el PC t o m el va1 or c o r r e s p o n d i e n t e a l pr i mer par ámetr o d e 1 a p s e u d o i n t r u c c i ó n Hal t , el G u a l i n d i c a l a l í n e a d e c ó d i g o d o n d e i n i c i a el programa pr i n c i p a l .

Es conveniente mencionar que a r a i z d e l e s f u e r z o d e v a r i a s or g a n i zaci o n e s i n t e r n a c i o n a l es y n a c i o n a l es, se ha l l e g a d o a u n a e s t a n d a r i z a c i ó n d e los sistemas o r i e n t a d o s a l a g r a f i c a c i ó n ; d i c h o e s t d n d a r es c o n o c i d o como GKS C Si stem K e r n e 1 d e G r á f i c a s ] . A l g u n a s d e l a s p s e u d o i n s t r u c c i o n e s q u e se han i mplementado . e n el i n t é r p r e t e c o r r e s p o n d e n a l a s e s p e c i f i c a c i o n e s d e f u n c i o n e s b á s i c a s d e g r á f i c a s d i c t a d a s pro el mismo GKS.

El i n t é r p r e t e está a d a p t a d o d e t a l manera que sólo puede u t i l i z a r s e m e d i a r e s o l u c i ó n e n v i d e o C 3 2 0 * ,200 p i x e l e s ] a t r a v é s d e un manejador d e gráficos CGA CAdaptador de g r á f i c o s e.n color para IBM P C 3 .

Es por el lo que 1 as c o o r d e n a d a s d e los p u n t o s a s e r axhi b i d o s e n a l m o n i t o r d e b e r á n e s t a r e n el i n t é r v a l o C O , 0 3 a C 31Q, 18Q3, c o r r e s p o n d i e n d o el p u n t o C O , 0 3 a l a e s q u i n a i n f e r i o r i z q u i e r d a d e l m o n i t o r d e v i d e o , y al p u n t o C319,1993 a l a e s q u i n a s u p e r i o r d e r e c h a d e l mismo.

Todos 1 os p u n t o s g e n e r a d o s a par t i r d e 1 a s p s e u d o i n s t r u c c i o n e s d e g r a f i c a c i ó n se almacenan en una e s t r u c t u r a e s p e c í f i c a l l a m a d a Napa de Bits.

11

E l mapa está o r g a n i z a d o como trn a r r e g l o b i d i m e n s i o n a l d e b y t e s d e 40 * 200 e l e m e n t o s , d o n d e c a d a e l e m e n t o Cun b y t e 1 r e p r e s e n t a l a i n f o r m a c i ó n d e 8 p i x e l e s , e q d e c i r , c a d a b i t , c o r r e s p o n d e a u n a p o s i c i l j n c o o r d e n a d a e n e s p e c i a l d e l m o n i t o r d e v i d e o .

B a j o la c o n s i d e r a c i j n d e q u e c a d a p r o c e d i m i e n t o d e d e f i n i c i ó n a s í como el p r o g r a m a p r i n c i p a l r e q u i e r e n d e un mapa e x c l u s i v o , se ha implementado una l i s t a l i g a d a q u e p e r m i t e c o n t r o l a r t a n t o los p r o c e d i m i e n t o s q u e i n t e r v i e n e n e n el programa como s u s c o r r e s p o n d i e n t e s mapas d e b i t s . E l n o d o d e d i c h a l i s t a c o n t i e n e i n f o r m a c i ó n como:

al I d e n t i f i c a d o r d e l p r o c e d i m i e n t o .

b3 ' V a r i a b l e de t i p o B d o l e a n p a r a e s t a b l e c e r si ha s i d o o u t i l i z a d o el mapa.

c3 El apuntador a l mapa r e s p e c t i v o .

De esta manera, cuando se e j e c u t a u n a p s e u d o i n s t r u c c i ó n g r á f i c a , se accesa el nodo d e l a l i s t a de p r o c e d i m i e n t o s c o r r e s p o n d i e n t e y t o d o s los p u n t o s g e n e r a d o s se almacenan e n el mapa apuntado por el p r o c e d i m i e n t o .

E s i mpor t a n t e s e í í a l ar q u e a q u e l 1 as p s e u d o i n s t r u c c i o n e s d e t r a n s f o r m a c i ó n CRotactdn, Escalacidn, Traslacidn y Ref¿exi&d d b l i g a n a l a c r e a c i ó n d e un nuevo mapa q u e r e e m p l a z a r á a l q u e e-Aste c o n a n t e r i o r i d a d , l o g r a n d o c o n esto q u e l o s n u e v o s p u n t o s c a l c u l a d o s n o se s o b r e p o n g a n con los ya e x i s t e n t e s .

P a r a n o r a s L r i n g i r a xn número l i m i t a d o l a c a n t i d a d d e v a r i a b l e s q u e p u e d e d e c l a r a r y manejar e1 u s u a r i o , se h a i mplementado una 1 i s t a 1 i g a d a d e v a r i a b l e s c o n c a p a c i d a d d e h a s t a 32 v a r i a b 1 es por nodo.

En l a fase d e g e n e r a c i ó n d e c ó d i g o , cada v a r i a b l e d e f i n i d a p o r el u s u a r i o es renombrada bajo un número c l a v e CaliOrs3. E s t e número c lave es q u i é n d e t e r m i n a el a c c e s o a l nodo y a l a v a r i a b l e e n s i , ya s e a p a r a a s i g n a r l e un v a l o r o p a r a c o n o c e r s u c o n t e n i d o .

E l c o c i e n t e d e d i v i d i r el número c lave e n t r e 32 nos p r o p o r c i o n a el i d e n t i f i c a d o r d e l nodo d e l a l i s t a , m i e n t r a s q u e el r e s i d u o n o s i n d i c a l a p o s i c i b n q u e o c u p a l a v a r i a b l e d e n t r o d e l nodo.

12

Con a1 o b j e t o d e f aci 1 i t-.a.r el acceso a l a r c h i v o d e c ó d i g o , p u e s t o q u e el i n t é r p r e t e i r a a c c e s a n d o a q u e l l a l í n e a a p u n t a d a p o r el P C , d i c h o a r c h i v o se lee d e l d i s c o y se a l m a c e n a e n un a r r e g l o d e c a d e n a s o s t r i n g s c o n c a p a c i d a d máxima d e 256 1 í n e a s , d e modo q u e el acceso a c a d a l í n e a se h a c e s i m p l e m e n t e c o n s i d e r a n d o a l PC como el í n d i c e d l s n t r o d e l a r r e g l o .

T a l como p u e d e a p r e c i a r s e e n el a p i n d i c e 111 , e x i s t e n seis g r u p o s d i s t i n t o s d e p s e r r d o i n s t r u c c i o n e s , mismos q u e ser i n . t r a t a d o s por s e p a r a d o .

A3 P s e u d o i n s t r u c c i o n e s A r i t m é t , i c a s y de Asignacicjn.

Cada una de estas p s e u d o i n s t r u c c i o n e s o b l i g a a l a e j e c u c i ó n r e a l d e l a o p e r a c i d n i n d i c a d a , a l o j a n d o el r e s u l t a d 0 e n 1 a p o s i c i ó n d e n t r o d e l n o d o d e v a r i a b l e s q u e c o r r e s p o n d a .

B> P s e u d o i n s t r u c c i o n e s L ó g i c a s .

E s t a s p s e u d o i n s t r u c c i o n e s e f e c t ú a n l a comparación i ndi cada e n t r e 1 os oper andos 1 y 2. Si el r e s u l t a d o es v e r d a d e r o , se a s i g n a un ' 1 ' a l a v a r i a b l e c o r r e s p o n d i e n t e ; e n caso c o n t r a r i o , se a s i g n a un ' O ' .

P s e u d o i n s t r u c c i o n e s d e E n t r a d a Sal i d a .

L a o p e r a c i o n d e d e s p l i e g u e C D I S P 3 p r e s e n t a e n el moni t,or el v a l o r d e la v a r i a b l e o l a c a d e n a i n d i c a d a , d e a c u e r d o a 1 as c o o r d e n a d a s a c t u a l es X , Y .

Las o p e r a c i o n e s d e 1 e c t . u r a y e s c r i t u r a CLEE, ESCR3 l e e n o e s c r i b e n el c o n t e n i d o d e una v a r i a b l e d e s d e el t e c l a d o o h a c i a el m o n i t o r r e s p e c t i v a m e n t e .

D> P s e u d o i n s t r u c c i ó n de Error.

E s t a p s e u d o i n s t r u c c i 6 n CXXX,W se emplea con el o b j e t o d e p o d e r d e t e r m i n a r a l g u n a f a l l a e n el c o n t e n i d o d e l a r c h i v o d e c b d i g o . d e manera que cuando es e j e c u t a d a se n o t i f i c a a l u s u a r i o un m e n s a j e d e e r r o r y se f i n a l i z a l a e j e c u c i ó n d e l i n t é r p r e t e .

13

E3 P s e u d o i n s t r u c c i o n e s d e C o n t r o l d e F l u j o .

E. 1> S a l t o C o n d i c i G n a l e I n c o n d i c i o n a l C S S I F , SAL73.

Ambas p s e u d o i n s t r u c c i o n e s modi f i c a n el val or a c t u a l d e l P C , c o n 1 a sal vedad d e q u e SSIF 1 o h a c e s d l o si el v a l o r d e l a v a r i a b l e i n d i c a d a es i g u a l a c e r o .

E. 2 3 F i n d e e j e c u c i ó n CHAL13.

E s t a p s e u d o i n s t r u c c i ó n f i n a l i za 1 a e j e c u c i 6 n d e l i n t d r p r e t e , p r e s e n t a n d o n u e v a m e n t e el menú pr i n c i p a l d e i compi 1 a d o r .

E. 33 LLamado a un p r o c e d i m i e n t o CLLAPO.

E s t a p s e u d o i n s t . r rrcci cjn modi f i c a el val or a c t u a l d e l PC de a c u e r d o a l valor d e l parámetro e s p e c i f i c a d o , el c u a l i n d i c a el i n i c i o d e un p r o c e d i m i e n t o d e t e r m i n a d o .

El v a l o r a n t e r i o r d e l PC así como el mapa a c t u a l se almacenan en una pi1 a e o n el o b j e t o d e retomar d i c h a i n f o r m a c i ó n u n a vez q u e sea d e t e c t a d a l a p s e u d o i n s t r u c c i ó n d e r e t o r n o .CREGR3. A d e d s se accesa el mapa c o r r e s p o n d i e n t e a l p r o c e d i m i e n t o a e j e c u t a r s e a f i n de p o d e r a l o . j a r adecuadamente 1 a i n f o r m a c i d n g r á f i c a q u e sea g e n e r a d a .

E. 43 R e t o r n o d e s d e un p r o c e d i m i e n t o ( : R E G = .

Modifica los v a l o r e s a c t u a l e s d e l PC j r ifel a p u n t a d o r a l mapa r e s t a u r á n d o l os c o n a q u e l 1 os v a l o r e s q u e se t e n í a n a n t e s d e h a b e r s e e f e c t . u a d o el 1 1 amado a l p r o c e d i m i e n t o . E s t o se l o g r a a txavés d e u n a r u t i n a q u e saca ambos v a l o r e s d e l a p i l a u t i l i z a d a p a r a t a l efecto.

F> P s e u d o i n s t r u c c i o n e s Gr áf i cas.

T a l véz este g r u p o de p s e u d o i n s t r u c c i o n e s c o n s t i t , u y a n l a p a r t e m e d u l a r d e l i n t é r p r e t e , p u e s t o q u e u n o d e los o b j e t i v o s p r i n c i p a l e s d e l c o m p i l a d o r es ¡a g e n e r a c i ó n g r á f i cos.

De a c u e r d o c o n l a s normas d e GKS, todo sistema g r a f i c a c i ó n debe c o n t a r c o n c i e r t a s f u n c i o n e s p r i m i t i v a s s a l i d a , tales como l a g e n e r a c i ó n d e l í n e a s , c u r v a s pol í g o n o s .

14

de

d e de

Y

i

Esp i r'al : ,. - - : - =l. 3

!?3sas :

P e r m i t e l a sscrrtura e n el monitor de rma variable, u n a c c n s t a n t e o una c a d e n a , .do acuerdo a l o que sea especificado.

*.- .~ , . . ._ . .. . . . .... . .~ . ._ . __ . . . . . . . , . . . . . . ""I_

G r a f i c a un circulo d e l r a d i o i n d i c a d o . Si se c m i Len 1 as c o o r d e n a d a s i .Y, y3 d e l c e n t . r o , se asumen las c o o r d e n a d a s actilales d e l c u r s o r .

I

G r a f i c a un arce elí p L i c o con d n g u l o s i n i c i a l f i n a l ang-inic j r . z n g f i n r e s p e c t . i v a m e n t e , serniejes horizont,ai ?x y vert,i cal ry, y c o o r d e n a d a s d e l c e n t r o e n Cx, y>.

x7. j RECTANGULC

Para cada :na. de +:st.as p r q x s i ci ones, .-e - graf i c a 1 a f i grur a s e ñ a i a d a , d e b i g n d o s e i ndi car c0rrectament.e el c, los p a r i m e t r o s r a d i a l e s . En Lodos ~ G S , casc~s, si l a s coordenadas C x, y3 d e l centro no se e s p e c i f i c a n , se asumen las del c u r s o r act.ual .

26

i

i g u a l e s , l a e s c a l a c i ó n será

27

uni f o r me

i

26) REFLEJA

Si se elige ia u1 t.1ma npcic jn , se abandona la e j e c u c i on del conpi 1 ador y se regresa. a l Si st,sma O p e r a t i v o .

i

29

I . TABLAS DE PALABRAS RESERVADAS Y SiMBOLUS ESPECIALES.

A C L S A BORRA C I RCLJLO D E F I NE E J ECUTA EJEX ENTERO ESCRI BE FI N S I HASTA'QUE I NI !:I 0 L I SA MI ENTRAS aTRO FI VOTE FROG RECTAEJGULO ROSA3 ROSAS 23. TRASLADA

ARCO CARACOL COMI ENZA DESDE E J E 1 EJEY ENTONCES ESP1 RuL HAC1 A HAZ LEE LLAMA MUESTRA PARA POLI GONO PUEJTEADA REFLEJA POSA4 ROTA TERMI NA '*'EA

30

:: programa> "+

I I

I

. . - . ..

090994

Los comentar i OS pueden i ncl u i rse s n c : . ~ ~ l q u i e r 1 ugar d e l pr ogr ama f u e n t e .

i

33

3:

J.

ASTR

BORR

CARA

CARD

CI RC

DI BU

ELI P

ESCA

ESP1

i:;OT

LI ?!E

LLEPl

MOVE

-

PI '/,e

POLI

PUNT

RECT

REFL

ROS3

R O S

ROS5

R O S 8

ROTA

SEMI

TRAS

- XXXX Código de error. F i n de e j e c u c i ó n

35

IV. LISTADO DEL PROYECTO.

36

OR-) {Range checking off3 {%Bt> {Boolwn complete evaluation on) {$St) {Stack checking on) {$It> W O checking on3 '

{%Ut> Ckmeric c rocessor) {$Et> CEmuletion%73 CUI 6552Qr16384,655360) {Turbo 3 default stack and heap)

Uses Crt, U n i t found in TURBO+TR3 GLOBhLV, INITIAL, LEXSINT , OPTIHIZ, INTERPR 1

beqin inic-tablas; uhi le not f ingraceso do begin

sec-inicial l i f op-entr = F2 then begin

lee-lineal programa; i f hay-error then begin

uindou (1,f78Q,25)t erose (salcod); end

else begin q t i n i w ; interpreta; end ;

erase (codint) 1 close (Entrada); close (Salcod); end

i f op-entr = F5 then begin else

interpreta; close (salcod); end ;

end ; windou (1,1,80,25!1 clrscr;

end

M Propraw Principal $1

{SR-3 Chnqe checlcinq off) OEt3 CBooletln complete evaluation on) {$St3 {Stack checking on) <$I+> CI/O checking on3 {$N-3 Ctk, nuaeric coprocessor)

Unit GLOBALU 1

(X (t !t

Proyecto de Investigocion I1

Seccion I: VQriQbles Globales

Interface

Uses

const ESC = t27i F2 = #hat F5 = M 3 1

type st2 = string 121; 5 t3 = string E31t S t 4 = string E47; st12 = string C123t st20 = string C201;

C tlefinicion de los teclas ESC, F2 y F5 3

I Declaracion del tipo que puede tener une 3 deterainada secuencia de curacteres 3

llpunl = *nodo1 t nodo1 = record (t Def para alnacenar t)

infor: st20; (t los identificadores t) l i g l : opunlt

end I listo1 = record

p r i d apunl; u1t: Clpunlt

end t

appun2 = %do2t .. a

........... ................ "_ ... .. I . . . ........ ....... . .

nodo2

lista2

, apun3

lista3

nod03

apun t nodo

lista

app i 1 nodpl

stclck

"d

= record (1 Def para almacenar los t) infrg: inteqer; (t nuleros de renglones X) lig2: apun2;

end 1 = record

prie: apun?; ult: apun2;

end t

= % d o 3 ; (1 Def para le tabla de $) ($ identificadores $1

= record prim: apun3; u1 t : apun3 I

end ;

= record 1 is : apunl I inic: inteqer; long: integer; rengll lista21 defin: t i p ; indef: integer; dins: integer; lig3: apun3; varloc: lista3; lisloc: listal;

end ;

= = record

etiq: integer; direc: integer; liga: apunt!

end t = record

end prim: 2punt;

= "nodpl; record

info: integer; l iga : a p p i l ;

end; = record

end i tope: appil;

= (DEFINICION,USO);

(t Ref para la listo de 49 (t etiquetas $1

(X Def para l a pila de $1 ($ etiquetas $1

va r entrada: text; codint,salcod: text; tab-reserv: array CO,+613 o f tab-1; tab-simesp: array Cl, ,171 o f tab-2; tab-cod: army C0,,461 o f st4; lincod: string U331 codfin: string 1683 t vrlr-cadena: strin E6011 ah-simbo: l istal ; tab-ident: lista3; pila: stack : hay-error: boolean; f ing mesa: boolean 1 *-en t r: char; cont-iup: byte;

'F.! ~ ~ ~ __ _ . . , _ l__ ._ .__ l

.I

. a , *

" . . "" - .. __n I" -,". ,

cant-lin: integer; noubre: PQthStri

c"""""""""""""""""""~"""""""""""--"--------- "~"""""""__""""""""""""""""""""-"-----"-- 1

[splesentation

End +

C%R-) CRanqe checking off) C$Bt> {Boolean coqlete evaluat ion on} <SS+) <Stack checking on) {$It) W O checkinq on) {%N-) {No nuaeric coprocessor)

Unit INITIhL;

(1 Proyecto de Investigacion 11 (It (t Seccion 11: Inic ial izacion

Interface

Uses DOSF C r t l {Unit fuund i n T U R B O t T P L )

aoBnLu;

It (t (t

%) k t i f i c a c i m de los procedimientos de interfase $1

%)

procedure inic-tablas;

procedure sec- ipic ial l

procedure inic-tPblasi C In ic ia l i zac ion de las tablas de palabras reservadas, 3 { de sinbolos especiales y de pseudoinstrucciones del > c

begin

f ingroceso := frllse;

tab-reserv COltnomb := 'MMRA'; tab-reserv C2Itnoab := 'hSTROIE' ; tab-reserv C41,noub := 'CARACOL'; tab-reserv E6 lmab := 'CIRCULO'; tab-reserv C83+nonb := 'CON'; tab-reserv C10l4nwrb := 'DESDE'; tab-reserv CI31,nomb := 'EJEI'; tab-resew El5l.nwb := 'EJEX' ; tab-reserv C123mmb := 'EJECUTh' f tab-reserv C 183 *nomb : = 'ENTERO' tab-resew C203,norb := 'ESCALA'; tab-reserv C221,nwrb := 'ESPIRhL't tab-reserv E241,noab := 'FINSI' I tab-reserv C253,noab := 'HACIA'; tab-reserv E271,nmb := 'HASTAM' f tab-reserv C291.nomb := 'INCR': tab-reserv t311,noab := 'LEE'; tab-reserv C331 m a b : = 'LI!%' ; tab-reserv E351,nomb :=' 'LLENh'; tab-reserv t 3 7 3 m m b := 'MSlM'; tab-resew C391,nolb := 'OTRO'; tab-resew C411,nolb := 'PMI';

cadi90 in tened io 1

fob-resew E11,noab := 'ARCO'; tab-reserv 131,noab .:= 'BORRA' ; tab-reserv C51,noab := 'CMDIOIDE'; tab-resew C71,no~b := 'COHIEWZh' ; tab-reserv E91 ,noab := 'DEFINE' t tab-reserv E l l l t no lb := 'DIBUJA'; tub-resew 114l.nonb := 'EJEU'; tab-resew E1611nmb := 'EJEY'I tub-reserv E171 ,nolb := 'ELIPSE' t tab-reserv 1191,norb := 'ENTONCES': tab-:eserv C211 m n b := 'ESCRIBE' ; tab-resew C23l+nwb := 'FIN';

tab-resew E261,nwb := 'HIISTA'; tub-resew C283+noab := 'HAZ'; tab-reserv C301,nwb := 'INICIO'; tab-resew 1323,noab := 'LINEA'; tab-resew t343,nmb := 'LLMA'; tab-resew C363.nolb := 'HIENTRM' ; tab-resew C381,nmb := 'MIEVE'; tab-resew E401,1t0&1 := '$M"; tab-resew C42ltn#b := 'PIVOTE';

t.ab-reserv t431 ,nomb := 'POLIGONO';. tab-resew C451tnoab := 'PROG' I tab-reserv C471,nomb := 'RAYADA' ; tab-reserv C W , ~ O I B ~ := 'REFLEJA'; tab-reserv 151ltnolb := 'ROSA3'I tab-reserv C531,nomb := 'ROSAS'; tab-reserv [S51 ,nolb := 'ROTA' ; tab-reserv E561 tnorb := 'SEHIELIPSE' t tab-reserv C58Itnomb := 'TERHINA' C tab-reserv C601,norb := 'TRASLADA' ;

tab-sinesp tlI,sin~b := " ' * tab-simesp 131,sinb := '1 '; tab-simesp CSI,sia~b := 'f '; tab-sirnesp C71,simb := '- '* tab-simesp t91tsiab := '1 '* tab-simesp C11I.siab := '; '; tab-simesp C131.sinb := 'W; tab-simesp CISltsimb := '= 'I tab-silaesp 1173,sinb := '>=';

t

* f

tab-cod COI := 'ACLA'; tab-cod 131 := 'ASTR't tab-cod 163 := 'CARD'; tab-cod 191 := 'DIFE' ; tab-cod C123 := '€LIP'; tab-cod E153 := 'ESPI'I tab-cod 1181 := 'IGUA'I tab-cod 1213 := 'LLhH't tQb-cod 1241 := 'MYI'; tab-cod C271 := 'ROVE'; tab-cod C301 := 'PDLI'; tab-cod 1331 := 'REFL' ; kb-cod C361 := 'ROS3't tab-cod C391 := ' ROW1 tab-cod C421 := '!iEtfI'; tab-cod C4Sl := 'TRAS';

end ;

procedure ser-inicial;

tab-reserv C441 ,nolb : = 'POWCIHTO' ; tab-reserv ;461+nmb := 'PUNTEhDA' tab-resew E483,nonb := 'RECTANGULO'; tab-reserv I503 + n w b := 'REPITE' ; tab-reserv 152I*nomb := 'ROSM'; trlb-reserv 1543 ,nomb := '80SAa';

tab-reserv 1573,noinb := 'SI'; tab-reserv 2593,nmb := 'TIPOL'; tab-resew Cbl1,noab := 'VEA';

tab-simp 12Itsieb := ' ( '1 tab-simesp E41,siillb := 'a " t

tab-simesp 161,siab := ', ' C tab-siaesp C81,5imb := ' + ' : tab-siaesp 1101,siub := I : = ' ;

tab-sinesp E121,simb := 'Z, '; tab-simp Il4Lsinb := '0'; tab-siaesp Cl61,siab := '> ';

tab-cod E11 := 'ARCO'; tab-cod 141 := 'BORR'; tab-cod C71 := 'CIRC' ; tab-cod El01 := 'DISP'; tab-cod 1131 := 'ESCh'; tab-cod C163 := 'GOT '; tab-cod I193 := 'LEE ' t tab-cod 1221 := 'LLEW'I tab-cod C251 := 'HEN 'I tab-cod 1281 := 'NUL7'; tllb-cod 1311 := 'PUNT'; tab-cod 1341 := 'REGR'; tllb-cod t371 := 'ROS4'; tab-cod C401 := 'ROTA'; tab-cod 11433 := 'SSIF'; tab-cod 1461 := 'XXXX';

tab-cod E21 := 'ASIG'; tab-cod [SI := 'CARA': tab-cod E81 := 'DIBU'; tab-cod 1111 := 'DIVI') tab-cod E141 := 'ESCR'; tab-cod t173 := 'HMT'; tab-cod E201 := 'LIME' ; tab-cod 1233 := 'MY ' t tab-cod C261 := 'HENI'; tab-cod t291 := 'PIUO' f tab-cod 1323 := 'RECT'; tab-cod 1351 := 'REST': tab-cod 1381 := 'ROSS' ; tab-cod C413 := 'SALT'; tab-cod E441 := 'SWA';

i Control de la presentacion inicial 1

C Presenkcion del marco de l a ventana 1 procedure marco (xi,yi,xf,yf: integer); var i: integer; begin

HighUidea; qotoxy ( x i t y i ) ; write (C201); Cor i := (xi t 1) to (xf - 1) do

write (t187); f o r i := ( v i t 1) to (v f - 1) do begin

write 0205);

gotoxy (x i urite (t186); gDt?xy (xf , i ) ; write ( tJ85); end

qotoxy ( x i ,y? ) ; write (#200); for i := ( x i t 1) t o (xf - 1) do

write (t188); LowVideo;

write ( t205);

end;

I

procedure encabezadol (1 Presentacion del encabezado t) {t Y lectura de opcion inicial t?

begin clrscrl marco (2,&79,?4) t window (4,4,77,??)t

urite ('UNIVERSIDAD hUToN0nA RTROPOLITANA') I

write !'PROYECTO DE IMSTIGACIDN 11');

write ('CWILADM GRAFICO');

urite l'<F2>: Cmpilacion Y Corrida');

urite !'<F52.! Corrida unicamente');

write ('(ESC>.: Fin de sesion'); repeat

qotoxy (21,41i

gotpxy (2416);

gotoxy ( 2 9 ~ 8 ) ;

gotoxy (24,101 1

gotoxy (24p11);

gotoxy (24,121 ;

op-entr := ReadKey; if op-entr = #O then

op-entr := ReadKeyl until op-entr in CESCpF2,F5I; i f op-entr = ESC then fingroceso := true;

end I

procedure inicia-edl C Inicializacion de las tablas de identificadores { (informion f i s h e inforuacion basicd asi 3 C cm0 de la pila utilizada en la generacion de 1

etiquetas 3

var j: integer; begin

hay-error := false; aln-sinbo,prim := nil; cllm-simbo,ult := nil; tab-ident,prim := nil; tab-ident+ult := nil; pila.tope := nil;

end;

procedure lee-archivo (tip-ar: string); (X Identifitacion del archivo que t) var i: byte; (t contiene e l program fuente t) begin ($ o e l programa objeto, segun t)

i := O; (1 sea el caso t) repeat

í$I-> i := i t 1; i f i < 4 then begin

gotoxy (3,14); delline; urite ( ' N o l b r e del progr~ma ',tip-ar,'t '11 readln (noabre) f if op-entr = F2 then begin

assign (entrada,nonbre) I reset (entrada): end

else begin assign (ralcod,norbre); reset (solcod);

end I end ;

{$It> until (IORESIKT = O ) or ( i =. 4); if i = 4 then begin

lotoxy (3,161; write ('Tres intentos es mas que suficiente'); urite (#7); delay !2000); op-entr := ESC; window (1,1,80,25); end ;

end t

procedure ver-result;

vnr d: DirStr; n : NaneSt r ; e: ExtStr;

I$ Prepamion del archivo de codiqo intermedio $1 (% y del archivo teqorcll de codiqo t)

b

beqin Fsplit (nombre,d,n,e); e := ',CGF'; nombre := d t n t e; assign (Salcod,norbre); rewrite (Salcwl);

assign (codint,'TEMP+TtlP'); rewrite (codint)

end ;

b e g i n cont-imp := 0; cant-lin := O; encabezado i if o p n t r = F1 then b e g i n

inicia-edf lee-archivo ('fuente'); i f op-entr <? ESC then

end

it op-entr = F5 then

ver-result;

else

lee-archivo ('objeto'); clrscr;

end I

M Control del procedimiento inicial $1

- ,

C$R-l <Range checking off) {$Et> {300leQn complete evaluation on) <$St3 CStctck checking on) OItl W O checking on) {$N-) CNo numeric coprocessor)

Unit LEXSINT:

Proyecto de Investigacion I1

Seccion 11': Conpilador

Interface

Uses Crt, {Unit found i n TURBOtTPL3 GLoanLu;

procedure wrif ica-impr ;

procedure lee-linea;

procedure r-error Cj: integer);

procedure lex (var T: Tip);

procedure programa;

Implementation

C Declaracion de variables solo utilizadas 3 C en los andizadores lexico y sintactico >

var rengl: string 1801; longl: integer; error-id: boolean; tok,estaba: boolean: agregue-local: boolean; cadena,c: st20 t id-exp: st201 pos,p,ind,l: inteper; f ,tok-ont: t i p ; in-id: integer t dir-def: integer: dir-dib: integer; dir-temp: integer i apf ,apl: apunl i ah-def: apun3; regreso-t~k: booleani dib,def-dib: boolean; uso-dib: boolean i id-def: boolean t doy-error: boolean; pr-lec,pr-ex: boolean:

integer I !os-t: ip-dat: t i p i rWd0: A-d I

siab : st2 : p-as,p-loc: integer; cont-etiq: integer; alias,id-alias: inteqeri

{SI lexical + inc)

CCI sintect inc3

End

Proyecto de Investigacion 11

Seccion 111: hnalizador Lexicografico

C Verifica el numero de lineas que han sido impresrls 3 procedure verifica-iaprt var c : char; begin

qotoxy (50919); HighUideo; write ('<RETURN> para continuar'); c := ReodKey ) dellinei

cont-imp := 0 ; LowUideol

gotoxy ( 1 ~ 1 9 ) ;

end ;

procedure LEE-LINEA;

begin i f n o t €OF (entrada) then b e g i n

if cont-iwp >= 18 then

rwdln (entrada,RMGL) ) LONGL : = LENGTH (RENTL) 1 CONT-LIH := CONT-IIN t !; POS := 1; L d i d e o t uriteln (CONT-LIN:2r' ' ,REM) ; cont-imp := cont-imp t 1; end f

verifica-ieprl

end t

(Y Lectura de una linw $1 ($ del proqrow fuente 8)

procedure R-ERROR INTEGER): (t lnpresion de los errores detectados t)

1d0p E: STRING r501; beqin if DOY-ERROR then begin E := ' '; CASE J OF

1 : E := 'Se espera la palabra PRo6' ; 2 : E := 'Se espera un IDENTIFICfiWIR'; 3 : E := 'Se espera la palabra FIN'; 4 ! E := 'Se espera un PUNTO'; 5 : E := 'Se espera que sea fin del programa' ) 6 : E := 'Se espera simbolo de punto Y coma'; 7 : E := 'Se espera la clauwla ENTERO'; 8 : E := 'Se espera operador de ASIGUMION'; 9 : E := 'Se espera l o palabra ENTOWCES'I 10: E := 'Se espera la palabra FINSI'; 11: E := 'Se espera un parentesis izquierdo') 12: E := 'Se espera un parentesis derecho'; 13: E := 'Se espera la palabra HAZ'; 14: E := 'Se espera la palabra HEISTME'; 15: E := 'Se espera la palabra TERHII' i 16: E := 'Se espera un identificador o.un nuuero') 17: E := 'Identificador EDEFINIWI'; 18: E := 'Identificador NO DEFINIDO'; 19: E := 'Sinbolo desconocido; se espera olquno valido';

20: E := 'Falta f i n de comentario; se espera ')"; 21: E := 'Faltan digitos adicionales'; 22: E := 'Se espera alguno de los sinbolos: (,>,<=,>=,=,O'; 23: E := 'Se espera que continue e l programa'; 24: E := 'Se espera alguna sentencia valida'; 25: E := 'Parentesis derecho desapareado'; 26: E := 'No se esperaba rirnbolo de punto Y coma'; 27: E := 'Se espera l a palabra INICIO'; 28: E := 'Se espera l a palobro EJECUTA'; 29: E := 'Se espera l a palabra HASTA'; 30: E := 'Se espera s i l bo lo de corn'; 31: E := 'Se esperan las clausulas HACIA o INCR'; 32: E := 'Se esperan las clausulas LISA, PUNTEADA o RAYADA'; 33: E := 'Se espera l a clausula CON'; 34: E := 'El ident i f icador no corresponde a un MFIHE'; 35: E := 'Se espero l a palabra COHIENZh't 56: E := 'Se espera un nuaero entero'; 37: E := 'Se espera sinbolo de co l i l l a ' ; 38: E := 'Se espera alguna de las clausulas EJE {I , U, X, Y)'; 39: E := 'Cadena detnasiado extensa, Excede 66 caracteres';

end C hay-errar := true; i f cont imp >.= 18 then

veritica-impr; wr i te (t7!; pos-t := pos-t t 3; HighUideo; wr i te ln ( '*' :pos-t,E); cont-imp := cont-imp t 1; deluy (1000) ;

end ; end;

procedure LEX (var T: TIP) ; (t Control del Analisis Lexicografico t)

procedure r-uarningt iS Inpresion del nensaie de precaucion que $1 begin (t indica l a truncacion de un ident i f icador $1

i f cont-imp >.= 18 then

wr i te ( t7 ) ; pos-t := pos-t t 5; I HighUideo; writeln ('*':~os_t,'Precaucion~ Se trunca Q 20 caracteres1');

* cont-imp := cont-imp t 1: delay f 10001 ;

verif ica-inprt

end ;

function TIPO (var CM: CHAR): TIP; (S,Deteninacion del tipo de caracter t)

begin TIPO := ESPhCIO; i f tok ant O M S T R A then car := Upcase (car); i f (CAR = t2bl or ((Em (entrada)) and (Pos :> longl)) then

i f (CAR = #13) or (Pos > Long11 then TIPO := FIN-LINEA else

i f C M I N C'O',,'9'3 then TIPO := DIGIT0 else if CAR IN ['t','-',',',';','?','/','(',')'l then TIPO := SIIIEOLO else CASE CAR OF

TIW := FIN-ARCH else

' i f CAR IN C'A',,'Z'I then TIW := LETRA else

'Y: TIPO := m y m ; '<': TIPO := Em:

' = f : TIPO := IGUAL; ' , ' ! TIPO := PLINTO; '{': TIPO != LLAUE-IZO; 'Y: TIPO := LLAVE-DER; ' ' : TIPO != ESPACIO1 ' : ! TIPO := DOS PUNTOS;

else TIPO := SIHBOLO; 'Y TIPO := confiur

end 1 end ;

else if tab merv ~mitad14?10M < CAD then

liminf := mitad t 1

encontro := true; else

end i : twfwnM := encontro;

end ; IND := nitad;

C Busqueda de un identificador en l a tabla 3 C local o global, segun Sea el valor de TR 1

procedure busqueda ITR: a p u d t CANST20; var EST:ROOLEPM; var hP:hPU#3; var D1B:EOOLEM);

var 1,J: INTEGER; x: n m 1 ;

1 begin I := 1; J := TR"r1NIC;

' X := TR"+LIS; DIB := false; dir-dib := O ; uhile (TR 0 NIL) and (not EST) do begin

if XA,INFOR CJI <> GAD I11 then beqin

>

TR := TRA+LIG3;

end else

if TRA+lONG = I then if TRA.LONG = L then b e g i n

EST := true; dir-dib := trA,indef I id-alias := tP.alias; if TRA,DEFIH = DEF-FIfM4 then DIB := true;

end else begin

TR := TRA,LIG3; J := TRA+INIC: I := 1 ; X := TRA.LIS; end

else begin I := I t 1; J := J t 1; i f J > 20 then begin

X := XA+LIG1; J := 1; end f

end ; end 1

PP := TR; end ;

procedure WS-CAD (CANST'LO; var EST:BOOLEAN; wr AP:AWN3; var DIB:BODLEMLN); vnr TR: opun3; begin It Control de l a buqueda de $!

if (def-dib) and not (id-def) then (t identif icadores en la t) if Nodo = KFINICION then begin (rt tab la loca l o globalt $1

TR := rll#_defA+varloctRrilC (t asignandole a TR e l valor 8) busqueda ITR, m d t est, ap, d i b ) ; ( 8 que sea conveniente $1 end

else begin TR := alm-defA,varloc*prial busqueda (TRt cadt est, apt dib)f if not est then begin

TR := Tab-idenLprirn; . busqueda (TRY cad, es t t apt d ib ) ; end;

end else begin

Y := TAB-IDENT,PRIH; Susqueda -!TI?? cad, estt apr dib); end ;

end;

procedure DAIEE_NOWIl (var Ut AWN1): (t Creation de un nodo para almacenar $1 ($ los caracteres del identif icador $1

begin NEW (u); U"+INFOR := ' '; Y"SLIG1 := NIL;

end t

(t Creacion de un nodo para almcenar t) (t un identificador en la tabla $1 (t que corresponda t)

var Z: 4PUN2; begin

NEY (Y!; YA+LIG3 := NIL; Y",LIS := APL; YAtINIC := IN-ID; Y",LONG := L; NEU (2); ZA,INFRG := CONT-LINt ZA,LIG2 := NIL; YA,RENGLoPRIn := Z; YA,RENGL+ULT := 2; if Id-Def then begin

ah-def := y ; YA,DEFIN := KF-FIGURh; YA,INDEF := n1R-W; yAtalias := O ; end

else begin Y A , DEFIN : = TIP-BAT ;

aiias := alias + 1; yA,alios := alias; end t

vA,varlac,prim := nil; yA,varloctult := nil; y", lislac ,prim := nil; yA,lisloc+ult := nil; if agregue-local then

YAcINDEF := O ;

if alm-deC*,varloc*prin = nil then begin aln-defA,varloc+prin := y; ala-defA,varloctult := Y; end

else beqin rlla-defA.varloc,ultA,lig3 := y ; alm-defA,wrloctult *- ,- y: end

(t S i se agrego un identif t) (8 1 ~ 0 1 , se actualiza la t) (t tabla correspondiente t)

else if ThB_IDENT,PRIH = NIL then begin (t Se actualiza l a tabla globo

TAB-IDENT,PRIH := Y; !t par haberse agregado una ThB-IDENT+ULT := Y; ('4 variable de t ipo global end

else begin ThB-IDENT+ULT',LIGJ := Y ; TM-IDENT,ULT := Y; end ;

eqd:

procedure INSERTA (CAD: ST20; var EST: BOOLEAN; var DIB: BOOLEAN);

var J: INTEGER; W: APuN1; hP,Y,t: nPlw3;

L := LENGTH (CAD): EST := false;

if EST then

else begin

beg in (t Insercion de un identificador fj

BUS-CAD (GAD,EST,AP,DIE);

AGR-REIIGL ( hP 1

agregue-local := false; if (def-dib) and not (id-def) then begin

agregue-local := true: if aln-defA,lisloc,prirn = NIL then begin

DANE-NOW1 (Y) 1 alm-defA,lislac+pria := U; ula-defA+lisloc+ult := U; end t

U := all-defA+lisloc,ult; FOR J := 1 TO L do begin

if p-loc i' 21 then

else begin

p-loc := p-loc + 1 ;

u*,infor Cp-locl := cad CJI

MNE-NOW 1 (U 1 I all-defA,lisloc,ultA,ligl := bJ; rlln-defA,lisloc ,ult := u; p-loc := 1; UA,INFOR Cp-10~1 := Cad CJI; end t

apl := u; if J = 1 then beqin

in-id := p-loci end

end end

else begin if dLH-SIHBO+pRIH = NIL then begin

MHE-NoDOl (Y 1 ; PILH-SInBOtPRIH := u: nLn-sIwBo,uu := u: end ;

U := ALNSIttB0,M.T; FOR J := 1 TO L do begin

i? P-hS < 21 then

else begin

P AS := p-As t t ; .

lP,INFMI CP-AS1 := CAD CJI

MI!E-NoDOl (U) t ALH-SIflBO,ULTA+LIG1 := M; dtH-SIHBO+ULT := U; P-ns := 1; WNFOR CP-~SI := CnD CJI; end ;

APL := Y:

end t

if J = 1 then b e q i n

IN-ID := P-ns: end ;

9ANE-NODO3 (7) ; end t

end ;

end;

procedure R-LETRA; (It Reconociliento de unrt letra $1

begin P := 1: . uhile ((TIPO [REMI. CPOQ) = LETRA) M1 (TIPO IRENGL tWS'I) = DIGITO)) do

begin if p < 21 then begin

INSERT (m CWSI,CADEN~,P~; P := P t 1 ; end;

Pos := POS t 1 ;

if p :. 20 then r-warning; i f RESERVADA (CADEM) then

T := TIP (IND) else begin

T := Ill'ENTIFICrrllORI

if ESTABA then b e g i n

e n d 1

INSERTA (ChDD(CI,ESTAEA,DIB);

i f Mow) = MFINICION then

else R-ERROR ( 17)

if (USO-DISI and (not DIB) then R-ERRWI (34 )

end

if HOD0 = Uso then R-ERROR (18):

else

end ; end C

procedure R-HAYOR; (t Reconocimiento de un simbolo de myor $1

begin T := nnroR; I~SERT (RENU ~ F U S I , C ~ M N A , ~ ~ ; "IS := POS t 1 ; if TIPO (RENGL [POSI) = IGUAL then begin

INSERT (RENGL CPOSI~CADENAI~) ; Pos := Pos t 1 ; T := HAY-TG; end

end ;

procedure R-MENOR: (t Reconocimiento de un siabolo de menor $1

begin T := HENOR; INSERT (RENGL CW3,CADENApl); ms := POS t 1; if TIPO ( R E M , EPOSI) = IGUAL then

Segin INSERT (RENGL [POSI,ChDENA,2); ms := m t 1; T := HEN-16;

end else

if TIPO (RENGL CPOSI) = tihYM1 then begin

INSERT (REEL C P O S I ~ C ~ D E N ~ , ~ ) ; ms := POS t I ; T := DIFER;

end t end ;

encontro := *alse; liminf := 1 ;

while (not encontro) and (liminf <= l i e u p do beqin . liesup := 171

mitad := ( l iainf t linsup) div 21 if tab-simp [nitadLsimb > S t h e n

else limsup := mitad - 1

if tab-sinesp Ceitadl+silrb < S t h e n

else liminf := nitad t 1

encontro := true; end

SIRB-ESP := encontro; i n d := Mitad;

end ;

procedure R-SIMRMO;

begin INSERT ( RENGL CPOSl ,CADEM, 1) ; SIRE := ' ' +

Pos := m t 1; if SIHB-ESP (SMB) then

T := TIP (IND t Al) else

b e g i n

sInB 211 := ~ENGL [MISI;

T := EAROR; R-ERROR (19) ;

end ; end;

procedure R-COMENTt

(t Reconocimiento de un simbolo t)

begin T := CORNThRIO;

while ((TIPO ! R E M CPOSI) <> LLhVE-I\ER) INSERT (RENU C W S I ~ C ~ D E N ~ , ~ ) ;

and (TIPO (REWGL CPLISI) 0 FIN"Cif)) M) begin

pos := POS t 1: i f TIPO (RMGL CPOSJ) = FIN-LINEA then

LEE-LINEA; end t

POS := POS t 1 if TIPO (RENGL CWSI) = LLAVE-DER then

else R-ERROR (20) ;

(t Reconociaiento de cowntarios 1)

procedure r-cadena; var long-cad: byte; begin

var-cadena := ' I ; long-cad := 1; while ((tipo (rengl [pal) <> COi!ILM)

and (tipo (re1191 tposl) O FIN"l+) and (long-cad < 61 1) do begin

if t i p o ( m g l Cposl) = FIN-LIKA t h e n

(I. Reconociniento de cadenas t)

LEE-LINEA

insert (rengl Cposl, var-cadena, long-cad) ; long-cad := long-cad t 1; end ;

else begin

pos := pos t 1; end t

r-error ( 3 9 ) i f ( t i p o Irengl Cposl) O CMILLN then

else pos := pos 4 1;

end I

procedure R-DOS-PUNTOS; I t Reconociaiento del silbolo ':' $1

begin T := DOS PUNTOS; INSERT ( " X CWSI ,CADENA, 11 ; Pos := ws t 1; if (TIPO (RENGL Cp(ls1) = IGUAL) then

begin INSERT (RENGL CPOSI,CADENh,2); Pos := POS t 1; T := A S I G W

end I end t

procedure R-DIGITO;

begin T := NW-ENTERot P := 1; uhile TIPO (RENGL CWS3) = DIGIT0 do begin

(Ir Reconociaiento de digitos t)

if end i

begin if

if p < 21 then begin INSERT IRENGL CPOSI,ZI1DENArP) t P := P t 1; end ;

POS := POS t 1; end ; p 3 20 then r-warning;

( $ Procedimiento LEX $3 not REGRESA-TOK then begin

tok := false; uhile n o t tok do

begin uhile (TIPO (RENGL CPOSII ESPACIO) do

i f TIPO (RENGL CPOSY) = FIN-LINEA then

else

POS := m t 1;

LEE-LINEA

tok := true; end 3

POS T := POS; CrrSE TIPO (RENGL CPOSI) OF cnmn := ";

FM-.hRCH : T := FIN nm; LETW : R-LETIM;-

Dos PUNTOS DIGTTO

+ + + + end 1

end else begin

if tot-ant = NUESTRA t h e n r-cadena; regresa-tok := false; end t

end C

Proyecto de Investigacion I1

Seccion IW: Analizador Sintactico

procedure PROGRAM; (t Control general del Analisis Sintactico t) U considerando las reqlas de produccion Y) M especificadas en la gnratica X)

var DX: ST20; dir-inicial: integer;

procedure inicirlliza'; Inicializacion de las variables utilizadas I- C en los analizadores lexica y sintactico >

begin sir& := ' ' e 9

p-as := o: . _ D l O C := 0; pos-t := o; godo := DEFINICION: tip-dat := PRoGt regresa-tok := false; doy-error := true; def-dib != false; uso-dib := false; id-def := false: d i b := fnlse; alias := o; id-alias := 0; cont-etiq := O ; p r-lec := false; pr-esc := false;

end;

function UTOPE (PILA: STACK; N: integer): integer;

begin if Y = 0 then It Regresa el valor del tope de la pila X)

else "TOPE := PILA, TOPEA+ INFO ( f de etiquetas o del t.ope menos uno $1

VTOPE := PILAtTOPEAtLIGAA~INFO; end ;

procedure GENERA-Till (N: integer); (t Crea N etiquetas Y las deposita X)

var I: integer; (X en la pila ' 1)

u: n P P n :

begin for I := 1 t o t( do beqin

procedure %M-ETIQ (N: integer); (8 Soca N etiquetas de la pila 8)

var I: integer;

begin M: A w n ;

for I := 1 t o N do begin if PILAtTOP€ 0 NIL then begin

W := PILh*TOPEt PILAtTOPE := PILAtTOPE**LIM; DISPOSE W t end

else writeln ('ERROR - Pila Uacio');

end I end t

procedure GENERA-CODIGO (ET: ST31 OP: ST41 bS,Olt02: ST'LO);

var IG: integer; Is: inteqer;

($ Grabacion del Codigo Generodo $1 begin ($ en el archivo CODINT $1

if NOT HAY-ERROR'then begin LINCOD := ' '; f o r IG := 1 t o 73 do

i f ET 0 '' t h e n beqin insert ( ' ',LINCOD,IG):

Is := length (et): i f 1s = 1 then insert (' 'pe t , l ) ; if 1s = 2 t h e n insert ( ' ' , e t t l ) ; insert (ETtLINCODtl)l . insert (':'tLItlcODt4)I end I

insert (OPtLINMIDt6); if DS O ' ' then begin

insert O('tLIEODtlO); insert (DStLINCOD,ll) I it" O! C . " %hen begin

insert (','tlIMCOD,31); insert tOltLINCODt32)l end ;

insert (','rLIKODt52); insert (02tLINC0D,53)1 end t

i f O? O " then begin

insert O)'tLINCODt73)t end i

writeln (CODINTtLINCOD); end;

end;

procedure LISTh-IDS; ( $ Andisis de uno l ista de identificodores t)

begin lex (1); i f T 0 IDENTIFICADOR then begin

r-error (2); if T = COMA then regresa-tok := true; end

else beqin cadena := " t str (id-oliostcodma) i if PR-LEC then

i f PR ESC then GENERA-CODIGO ("l'ESCR',CONCAT ( 'A ' ,~aden~)t ' ' r 'E) ;

end i lex (TI; if T 0 CMA

then regresa-tok := true else

tISTh-IDSt pr-lec := false; pr-esc := false;

end ;

procedure DECLS; It Analisis de la section de Declaraciones $1

begin lex I t ) ; i f T = ENTERO t h e n b e q i n

tip-dat := T; lista-ids; lex (T I ; if T 0 P-Y-COM then r-error (6); declsf end

regresa-td := true; else

end 1

procedure IDEN-O-NUH; (I: Identificacion de un Numero o un Identificador t)

var SIGN: STRING til; beain

error i d := false; :ex m ; SIGN != ''; if T = HENOS then

SIGN := '-'; if T IN tMS,I4EENOSl then lex (TI; i f NOT r T IN CIMNTIFICADOR,NUn_ENTER03) then beqin

r-error (16) I DOY-ERROR := false; error-id := true; while not (1 IN CP_Y_COHn,FIN,FIN-ARCHI) do

DOY-ERROR := true; -@grew-tok := true; end

lex (T?;

else i f T = IDENTIFICADOR then begin

codena := "; str iid-alias,cildena); id-exp := CONCAT ('A',SIGNlcadena) end

id-exp := CONCAT (SIGN,cadena); else

end ;

procedure EXF'R-SIWLEI (8 Analisis de una Expresion Simple $1

VQP x,tnp,oper: ST20; begin

IDEN-O-NUH 1 le:< (T); i f T in [MAS~HENOSIPOR~DIU13 then

?; cg i n cose T o f

HAS: oper := 'SUM'; YENOS: oper := 'REST'; FOR: oper := 'HULT'; DIUI: oper := 'DIVI';

end 1 t n p := id-exp; I DEN-O-WH t alias := d i a s t 1; ST4 lalias,x)t GENERA-CODIGO (",oper,M]NCAT ('A'rx)rtsp9id-exp); id-exp := CONCAT ( ' A ' ? x ) ;

end

regresa-toN := true; else

end I

ilt Reconocimiento de un operador relational $1

procedure OPER-RELAC ( v w oper: st.20);

begin lex (T11 if NOT (1 IN I ~ A Y O R l ~ A Y _ I G I ~ ~ R ~ ~ E N - I G ~ I ~ ~ L ~ ~ I F E R l ) then r-error ( 2 2 ) else

case T OF MAYOR: oper := 'MN 'i HE"): oper := 'HEn '; MAY-IG: oper := 'HAYI'; 14EN-16: oper := 'HENI'; IGUAL: oper := 'IGUA'I

, :!IFEX oper := 'DIFE'; end ;

end ;

procedure CoNDIC;

var x: st12; tmp,o er: st20; S1-d: Boolean;

SI-PAR := false; lex (TI; i f T = PAR-IZQ then

SI-PhR := true else

regreso-toN := true; EXPR SIHPLEI

begin

It Analisis de una Expresion Condicional $1

tu t = i d exp; OP~JELAE (oper) ; EXPR-SIHPLE; alias := alias t 1; STR (aliasrx)l GENERA-CODIGU (",operlCONCIM('A'rx)rtlplid-exp); lex (1); if NOT SI-PMI then

-

i f T = PAR-DER then r-error ( 2 5 )

else regresa-tok != true

i f T 0 PAR-PER then begin r-error t 12) ; regresa-toti := true; end ;

else

end ;

procedure LISTA-ID-L-E;

vor SI-PAR: BOOLEAN; begin

SI-PAR := false; lex (TI; i f T = PAR-IZO t h e n

SI-PAR := true else

regreso-tok := true; LISTA-IPS; lex (TI; if NOT SI-PAR then

if T = PARPER then r-error (25)

else regresa-tok := true

if T 0 PAR-DER then begin r-error (12) ; regreso-tok ! = true; end ;

else

end 1

procedure ver-cadena;

bpgin lex (1); i f T 0) COHILLA then

else begin r-error (37)

regresa tok := true; tok-ant-:= HUESTRAt lex (TI; tok-ant := T f end 1

end ;

procedure SERIE 1 FORWhRD; . ,

procedure INSTR; FíJRWARD;

(X Analisis de los pametros para $1 ($ las proposiciones Lee y Escribe $1

(X Analisis de una cadena empleada t) !X bajo la proposicion MSTRA $)

(8 Analisis de expresiones colpuestos X)

(S (uquellas que coaprenden las $1 begin (t clnusulas CONIENZA Y TERtlINA) ?)

lex (1); if T O CONIENZA then r-error (35); SERIE C lex ( T ) t i f T <> TERNINA

end ;

procedure DEFFIG

v w DD: ST2O; begin

lex (TI;

( $ Analisis de la Definition de procedimientos $1

i f T <>-DEFINE then

else begin regresa-tok := true

modo := DEFINXCIONt id-def := true; DEF-DIE := true! GENERh-ETIQ ( 1) ; DIR-DEF := WOK (mn,o) ; STR {DIR-XF ,DO); GEMERA-COBIGO ( D D , ” y ” , ” y ” I ;

SfCA-ETIQ (1 1 t lex (TI; i f T 0 IDENTIFICADOR then beqin

r-error (2) def-dib := false; end 1

lex (T); i f T (> P-Y-COM then begin.

r-error (6); regresa-tok := true; end I

id-def := false; DECLS; HOD0 := USO; INSCONP; lex (1); if T (S. P-Y-CMA then r-error : t i ;

def-dib := frllse; DEFFIG; end ;

GENERA-CODIGO ( ” p ’ R E G R ‘ , ” ~ ‘ ‘ ! ’ ’ ! ~

end ;

procedure PROWS; (8 lnalisis de una Proposicion en general $1 (X de acuerdo a las reglas de sintaxis t)

MIY-ERROR := true; regresa-tok := true;

end

begin else

if T 0 ASIGNA then r-error (8); EXPR-SINPLEi

LEE: bepin p r lec := true; LISTA-I@-L-E;

end;

ESCRIBE: begin pr-esc := true; LISTA-I@-L-E;

end ;

SI: beoin

lex (1); if 1 O EWTONCES then beqin

r-error ( 9 ) ; regreso-tok : = true; end i

str (vtope (pila, l ) ,w)i str (vtope (pilarol ,x);

if T = OTRO then begin INSTR; lex (TI; i f T O FINSI then begin

?-error (10) ; reqresa-t.ok := true; end ;

end

if T 0 FINSI then begin r-error (10) ; regresa-tok := true; end ;

else

genera-codigo fw?"p",","); saca-etiq (21;

end I

HIENTRAS: begin genera-etiq (2); str ivtope (pila,O) ,x); str (vtope (pila,l),u);

I . - ...... -. . . . . . . . . . . . . . . . . . . . - ......... . . . . . . . . . . .. .". . . . . . . . . ~ -~ ........... ..... - .-,

genera-codigo (“,‘SSIF’,x,concat 1WSTRf genera-codigo (”,’SALT’,u,”,”); genera-codigo (x,”,”,”,”!; saca-etiq (2);

- . - . . . ,

end j

REPITE: beqin; genera-etiq (1) 1 sir (vtope (pilato) ,x) ; genera-codiqo (X,”,”,”,“); INSTR lex (TI; if T 0 HASTAWIE then r-error (14); CONDIG; str (alias,y) 1 genera-codigo (“,‘SSIF’,x,concat (‘A’,y),/’); saca-etiq (1) ;

end ‘r

PARA : beqin genero-etiq ( 3 ;

- - . . . . , if T 0 IDENTIFICADOR then r-error i2) else

insert l’*‘,id-tenp,l); lex (TI; if T 0 ASIGNA then r-error (8) ; EXPR-SIHPLE; genera-codigo (“,’ASIG’,id_t~,id-exp,’’); lex ( T I ; if T 0 HASTA then r-error (29) ; EXPR-SIHPLE I alias := alias t 1; str (alias,z)f genera-codigo (u,”,”,”,”); genera-codiqo (“p’llENI‘,concat (‘*’,z),id-tenp,

2mera-codigo (“,’SSIF’,x,concat [‘A’?~),’‘!f

lex (TI; if T = P4SD then begin

str (id-tllias,id-teq);

i d p x p ) ;

iex ( T ) ; if T C. HUH-ENTERO then

else

end else begin

r-error (36)

t a p - w := cadena;

tmp-u := ’1’; regresa-tok := true; end

lex (t): if T <?.- HAZ then rprror (13) INSTR!

COIIIENZA: beqin lex (TI; while (T <> TERHIM) AND (T <> FIN-ARCH) do begin

reg resa-tok : = true SERIE; lex (TI; end t

i f T 0 TERHINA then r-error (15); end ;

EDRRA ?

LLAHh, REFLEJA: begin

USO-DIB := true; case T o f

B O W : oper := 'BORR'; LLNH: oper := 'LLAH'; REFLEJA: oper := 'REFL';

end ; lex (TI; if T 0 IDENTIFICADOR then r-error (2) else

begin str (dir-dib,x); if oper = 'REFL' then b e g i n

lex (t , ; if not (t in CEJEX? EJEY EJEI, EJEVI) then

begin r-error (38) f regresa-tok := true;

end else begin

cnse t o f EJEX: id-temp := ' X , ; EJEY: id-temp := 'Y'; EJEV: id-telp := 'U'; EJEI: id-temp := '1 ' ;

. end; genera-codigo ("?oper,x,id_temp,"): end ;

end

genero-codiqo ("rooerFx,","); else

end; USO-tlIB := false;

end 1

DIBUJA: beoin

i f T , C . IJJENTIFICADOR then begin regresa-tok := true; dir-dib := O ; end ;

str Idir-dibtx) t genera codigo ("I'DIBU',xt"y")~ uso-a1ti := false;

end;

VEA ?

?NEVE y

PONPUNTO: begin case T o f

VEA: opet.:= 'GOT '; NUEVE: oper := "NE'; WIIWHTO: oper := 'M';

end ; Iwn-a":

LLENA:

w

HUESTRA :

CIRCULO, CMDIOIDE, ASTROIDE, ESPIW , ROSM, mn3,

tr n o t error-id then beqin id-teap := id-exp; 1.x (TI; i f T O COHA then

r-error (30) else begin

I DEN-O-MUH ; genera-codigo (",operrid_telp,id_exp,")t end 1

end 1 end 1

b e q i n IllEN-O-Wi if not error-id then

id-temp := id-éxpl lex ( T I ; if T 0 COM then

begin

r-error (30)

IDEM-O-NU& if not, error-id then beqin

else begin

trp-x := id-exp; lex (t); if t <> COM then begin

regresa tok := true; dir-dib-:= 0 ; end

else begin uso-dib := true; lex (t); i f t 0 IDENTIFICAWR then

r-error (2); usa-dib := false; end ;

str fdir-dib,x); genera-codiqo (", 'LLEN',x,id-temp,tmp-x); end 1

end; end t

end

begin lex (T I ; regreso-tok : = true; i f T = COHILLA then beqin

ver-cadena; if not Hay-Error then

end else begin

IDEN-O-NUHl genera-codigo (",'DISP',id-exp,''r''); end;

writeln (Codint,' DISP(",var-cadena,")')I

end ;

m n 5 , ROSA8: begin

cnse t of CIRCULO: oper := 'CIRC'I ROSA3: oper := 'ROS3'; CARDIOIDR oper := 'CARD'; RQSh4: oper := 'ROS4'; ASTROIDE: oper := 'hSTR'; ROSh5: oper := ' R O S ' ; ESPIRAL: oper := 'ESPI'; REM: oper := 'ROSS'; end I

IMN-O-NUN I id-teap := id-exp; lex ( T I ; if T = COHA then begin

IDEN-O"!JH; tmp-x := id-exp; lex (t); if t O COHh then r-error ( 5 0 ) else begin iden-o-nu& genero-codigo (",'GOT ',trp-x,id-exp,");

end end

else regresa-tot := true;

genera-codigo (",oper,id_terp,","); end;

cnRnca, PtJLIGONO? ELIPSE: begin

case t o f ELIPSE: oper := 'ELIP'; CARACOL: oper := 'CMW; WLIGOWO: oper := 'WLI'; end; .

iden-o-numi id-temp := id-exp; lex ( T I ; if T <>. COM then r-error (30); iden-o-nun; tnp-x := id-exp; lex (t); i f t = C O M then begin

iden-o-nua; tap-y := id-exp; lex (t); if t O COM then r-error (30) else begin

iden-o-nul; genera-codigo O ' r ' G O T ',tq-y,id-exp,"); end

end

regresa tok := true; else

genera-coaigo (",oper,id-terp,tnp-x,"); end;

RECTANGULO: begin iden-o-nua; id-temp := id-exp; lex (TI; if 1 <> CORA then r-error (30); iden-o-nun; tip-x := id-exp; lex (t.);

i f t 0 COHA then r-error (30); iden-o-numi tg-y := id-exp ; lex (t); if t 0 CMA then r-error (30); iden-o-nua; genero-codiqo (",'RECT',id_te#p~tnp-x,''); genera-codiqo (",'RECT',tlp,y,"-exp,'');

end;

ARCO: beain

if T = W n A then begin iden-a-nun; tmp-z := id-expt lex (T?: if T <>'COHA then r-error (30) else begin

end end

regresa-tdt != true; else

genera-codigo (",'ARCO',id_teap,tnp-x,t#p_r); end t

SMIELIPSE: begin iden-o-nula;

if T O'CMh then r-error iden-o-nun;

if T <>-CORA then r-error

30) ;

30) ; iden-o-nun; tlp-y := id-expt lex (TI; if T <> C M then r-error ( 30 ) ; iden-o-nun; tap-z!= id-expt lex (t)l i f T = #M then begin

iden-o-nud id-temp := id-exp; lex (T I ; i f T 0 COhh then r-error (30) else

-

begin iden-o-numi !enera-codigo O'l'GOT ',id-teqt,id-exp,");

en end

else

~ " . . .

if T O'COM then r-error (30)i expr-sinple; str (dir-teap,x); genera-codi o t"Ioper,xrtnp-z,id-exp)l i f oper = 'PSCA' then begin

lex (t); i f t 0 PIVOTE then

else b e g i n regresa-tok := true

iden-o-nun; tmp-z := id-exp; lex (tli i f t 0 COM then r-error (30) else begin

iden-o-nuri .genera-codiqo ("r'PIUO',tlp_z,id-expt'')I end

end; end ;

end:

ROTA : begin

USO-DIE := true; lex (1); if T 0 IDENTIFIChDOR then r-error (2); USO-DIB := false; dir-temp := dir-dib; lex (1); if T 0 CON then r-error (33); EXPR-SIHPtEf str (dir_teap,x)i generQ-codigo (",'AOTA',x,id_exp,")f lex (t); i f t (> PIWTE then

else begin regresa-tok := true

iden-o-nur; tup-x := id-exp; lex (t); if t O COM t h e n r-error (30) else begin

iden-o-null genera-codigo ("r'PIVO',tlp_xtid-exp,''); end

end 4

end t

LINEA: begin lex ( T I ; if T = DESDE then

begin IKN-O-NM; id-temp := id-exp; lex ( T I ; i f T 0 CONA then r-error (30)) IDEN-O-NUtl; genera-codiqo I " , ' M T ',id_temp,id_exp,");

end else

i f NOT iT IN IHACIA,INCRl) then r-error (31) else

tok-ant := T; I tIEN-O"JH I tmp-y := id-exp; lex (TI; i f T 0 COYh then r-error (30)) I DEN-O-NU¡l) tmp-z := id-exp; . if tok-ant = !YCR then

dios := alios t 1; str alias,^); genera-codigo (",'SLlM',concat ( ' * ' ,y) , 'X ' ,

tmp-y := concat ('A',y); alias := alias t 1; str (elies,z); genera-codigo (",'SUHh',concat ('A',i),'Y',

tnp- ; := concat ('A',zj;

hegin

h p - Y 1 ;

tmp-z? ;

end ; lex ( T I ; i f T = TIP& then

begin :ex !l); i f NOT !T IN IIPUNTE~DA,LISn,PhYh~Al) then

P-vror (32)

m s e T o f zise

PUNTEADA: oper := 'PPUN': ?Ish: oper := 'LISA'; RAYAM: oper := 'RAYW;

end; end

begin else

regresa-tok := true) oper := usw;

end ; genera-codigo ("!'?INE',tep-y,t~lp-~,oper);

end ;

else b e g i n r-error (24); regresa-tok := true;

end 1

end end;

procedure SERIE; (1 Analisis de una Serie proposicional $1

begin if T <> FIN-ARCH then

begin PROPOS; lex (Tt; if T = P-Y-COM

then SERIE else regresa-tok := true; if T = FIN-ARCH t h e n r-error (23);

end; end ;

procedure INSTR;

begin lex (T); regresa-tok := true; if T = COMENZA then

else Il4sCOHP

PROWS; end f

procedure cross-ref (Y: apun3);

w r T: APUNl; z: AWN2; JfK: integer; c o n t - ~ n q l : byte;

begin uhile U 0 NIL do

begin if cont-iap >= 18 then

verif ica-impr ; T := U"4LIS; J := U"+INIC:

(t Analisis de una serie de instruccciones $1

M Ilrpresion de las referencias cruzadas t) it vaciando el contenido de las tablas $1 (t global y locales de identificadores $1

for K := 1 to YA.LONG do begin urite (TA,INFOR CJN; J := J + 1; if J > 20 then begin

T := TA,LIG1; J := 1; end;

end 1 for K := 1 t o (23 - WA,LON61 do

urite ( ' '1; case Y*,MFIN o f

m: u r i t e ('PROGRAM '1; ENTERO: write ('ENTERO ');

NO-IMNTIFICADO: write ('NO IDENT ');

DEF-FIGURA: write ('DEFINE '!' end i urite (uA+alias:3); write ( ' '3); cont-rengl := 0 ;

while Z 0 NIL do begin

i.

Z := WA+RENGL+PRItí;

if cont-renql = 9 then begin cont-rengl := O ; writelni cont-imp := cont-inp t 1; if cont-imp >= 18 then

verifica-impr; 3otoxy (46, UhereY); end;

urite (ZA,INFRG,' '1: cont-reng1 := cont-reng1 t 1; z := z".LIG2: end;

uriteln; cont-inp := cont-imp t 1; if uA+varloc+prim 3 Nil then begin

if cont-imp >= 18 then

writeln; cont-imp := cont-iap t 1; cross-ref (Y*+varloc+pris)i uriteln; cont-imp := cont-imp t 1; end ;

verifica-imprt

U := U"tlIG3; end I

end;

procedure IHP-REF-CR; (t Control de la inpresion de referencias cruzadas t)

var c: char; begin

NORHUIDEO; verifica-inprl zlrscr; cont-imp := 8 ;

write ('TABLA DE REFERENCIAS CRUZADAS');

uriteln ('IDEHTIFICABrJR','TIPO'~18~'ALIAS':l2r'RENGLONES'~12); writeln; cross-ref (Tab-Ident+Prin); verifica-impr;

gotoxy (23r4);

gotoxy ( 1 r7) ;

end ;

begin C Control general de la estructura 1 inicializa; x del prpqramo fuente > lex ( T ) ; if T O PROG t h e n r-error (1);

end i mLS; MODO := uso: TIP DAT := NO-II\ENTIFIChDO; DEFCIG: lex (T); if T 3 INICIO then begin

P er ror (27); regreso-tok := true;

{SR-) (Ran e checking off) C1Bt) (Boo 9 ean completa evaluation on1 IS+> {Stack cheching on3 <%It) . CI/O checking on) CSN-1 {No nuweric coprocessor)

Unit OPTIHIZ~

Proyecto de Investigacion I1

Seccion U: Optisizwion del Codiqo Generado

Interfrlce

Uses Crt, {Unit found in TURBO.TPL> GLOBHU f

procedure optimiza;

Inplementcltion

procedure optiriza;

var tab-etiq: pila-et: et-cod: et-nur: cont-cod: edo : oper-cod: dir-inic:

l i s ta ; stack; strinq E331 integer 1 inteqer; integer; string t41; integer;

?rocedure lee-codint: C Lectura del archivo de codiqo intermedio 1 begin

teadln (codint,lincod); cont-cod := cont-cod t 1;

end ;

procedure push-et; C hlnacenariento en la pila de las etiquetas 3 var u: appil; C sirbolicas de salto > begin

n e u (u); wA,li a := pila-et.tope; u A + i n P o := et-nun; pila-et.tope := u; cont-cod := cont-cod - 1 t

end;

procedure dm-etiqf C f~lnacenamiento en 1ct listo. de etiquetas > var p: apunt; { de aquellas contenidas en el archivo > begin C de codiqo, incluyendo su nueva 'direccion' >

new ( p ) ; pAtetiq := pila-et+top$+info; p A t d i r e c := cent-cod; pA+liga := tah-etiq.prirl tab-etiq,prim := p l

end ;

procedure pop-et; i Elinination de l o pila de las etiquetas silbolicos 3. var u: appil; begin

uhile pilaet*tope O nil do begin

ah-etiql u := pi la et*tope; pila-et*tipe := pila-et*topeA+liga; dispose (u) ',

end I end t

procedure caabia-dir; { Cambio de direccion de salto o referencia 3 vor u: opwt; C en aquellas pseudoinstrucciones que asi 3

et-salto: string C201; { lo requieran 3 dir-salto: inteqer;

temporal: inteqer;

pot := 10;

k,pot: byte;

begin et-salto != copy (lincodrllr20)i

dir-salto := O ; for k := 1 to.20 do

i f et-wlto Ckl in t'0'+,'9'3 then begin val (et-salto fkIrterporalfedo); dir-salto := dir-salto # pot t teapordi end ;

i f dir-salto O O then begin u := tab-etiq*prirC uhile (dir-salto 0 uA+etiq) and (u 0 nil) do

u := wA*li a', .delete (linco!,11r20)t . str (uAtdirec,et-salto); if uA+direc < 10 then

else insert ( ' ' ,et-saltof2) if wA+direc <: 100 then

eise insert ( ' ',et-salto~3)

insert ( ' ',et-saltor4); if oper-cod = ' Y H T ' then

dir-inic := uA,direc; insert (et-soltorlincod,ll); end ;

end ',

procedure grab-solcod', i Grabacion del aKhiuo de codigo 3 intermdio optimirado 3

begin codfin := copy (lincod, 6, 68); uriteln (salcod,codfin)',

end 1

procedure carnb-defdib; C Cambio de la direction de inicio de > C un procedimiento en la tabla global 1 c de identificadores 1

var q: apun3f u: apuntl

begin q := tab-ident.pri8; uhile q 0 nil do

begin if qA*ddin = DEF-FIGURh then begin

u := tab-etiq+prir; uhile (qA, indef O u%etiqI and (u <> nil) do w := U ~ , l i g u ;

qA,indef := uA+direc; p o d ;

q := qA,1iq33 end;

eqd;

procedure atragroc; C Huestra de 10s direcciones optiaizadas del inicio 1 C del progmno principal Y de los procedimientos 1

var u: apun3; t: apunl; c: char; i J , i k ! integer;

begin clrscrt

urite ('Dptirizacion de Codigo');

write ('Direcciones inicioles de definiciones y programa'); gotoxy (20r9)f urite ('Proceso: ','Direction:');

writeln ( 'Pro9 Principal ,dir-inicW; w := tnb-ident,primt uhile u <? nil do begin

t := uA+lis; ij := u*,inic; urite ( ' ' ! t 9 ) ; for i k := 1 to uA,lon9 do begin

gotoxy (26r4)

gotoxy (1397)

qotoxy [?0,11);

i f !$,defin = IrEF-FIGURII then b e q i n

grite (tA,infor Cijl); ij := i j t 1; if i j > 20 t h e n begin

t := t ~ , 1 i g 1 ; i j := 1; end ;

end ; write ( ' '1;

for ik := 1 t o (21 - u*,long) do

uriteln (uA,indef:b) 1 end ;

u := wA,1ig3; end f

HighUideo; urite ('<RETURN> para continuar'); c := ReadKeyl delline;

LouUideo;

gotoxy I50919);

gOtOXy 1,191 ;

end;

(X Procediaiento 'optiriza' $1 beqin close (codint); reset (todint); pila-eLtope := nil; tab eti!.prim iz nil; con€co 0: while not eof (codint) do C Primer Paro: Almacenamiento de las 3

{ etiquetas silbolicos utilizadas > begin ' C y w rediteccimamiento 1

lee-codint; et-cod := copy (lincwltlt3);

6

val (et-cod,et-nun,edo) i f et-nur O O then

else push-et

pop-et f

close (codint) 1 reset tcodint); cont-cod := O ; while not eof (codint) do { Sequndo Paso: Hodificacion en e l 3

{ archivo de codigo de la5 referecias 1 a etiquekas que 5e incluyen en . 1

{ las pseudoinstrucciones indicadas >

e n d ;

begin lee-codint; oper-cod := copy (lincodr6,4); if (oper-cod = 'SALT') or (oper-cod = 'SSIF') or

(ver-cod = 'ROTA') or (oper-cod = 'ESCA') or (oper-cod = 'BORR') or (oper-cod = 'DIBU') o r (oper-cod = 'TRAS') or (oper-cod = 'HKT ' ) or (oper-cod = 'FEFL') or (oper-cod = 'LLEN') o r toper-cod = 'LLAH' 1 then cambia-dir;

et-cod := copy !lincod,lt3); val (Pt-cod,et-numredo) 1 if et-nur O O then

else cont-cod := cont-cod - 1

grab-salcod 1 end;

camb-defdibl ntragrocl Release (HeapOrq 1 ; close (codintl I close (salcod 1:

end ;

CjR-) {Range checking of f} W t 3 (Boolean complete evaluation on3 {SS+} {Stack checking on} {SI+) C I / O checking on> {$N+) {Nureric coprocessor} {SE+) {Emulation 80873 {IF tl

Uni t INTERPRf

Proyecto de Invest igacion I1

Seccion VI: In terpretac ion de Codigo

In ter face

Uses Crt Graph, GLOMLU;

procedure interpreta;

Implementation

procedure interpreta;

Declarac ion de l t ipo de pseudoinstruccion que debe ejecutarse 1

t.ype t i p - i n s t r = ~ A C L ~ ~ ~ O , ~ S I G , A S T R ~ E ~ ~ ~ ~ ~ ~ C ~ ~ ~ C I R C f ~ I ~ f ~ I ~ f

LIWEtLLAH,LLEN,HAY tHllYIAEN ~HENI~~UEpHULT~PIUOf DISP,DIUI,ELIP,ESCApESCR,ESPIpGOT pHhLTpIGUhpLEE p

FULIpPUNT,RECT,REFLrREGR,RESTrROS3~ROS4,ROSS,ROSB, ROThpS~Li,SEFII,SSIFrSU~~plR~S,XXXX):

< Def in ic ion para l a t a b l a de var iab les 1

apunt-var = "nodo-var ; nodo-var = record

nus1 : byte; in for : ar ray C1,,323 o f real ; l i g a : apunt-vor;

end i l i s ta -var = record

prim: ilpunt-varl end t

< Definic ion para e l aapa de b i t s 1 apunt-vis = "nodo-vis; nodo-vis = record

info: o r v y C0,.39, 0.,1991 of byte; end ;

Def in ic ion para e l con t ro l de l o s ropas de } { los procedimientas y d e l programa pr inc ipr l l 3

apunt-def = "nodo-def; nodo-def = record

proced: integer; utiliz: boolean; visual: apunt-vis; liga: apunt-def 1

end; lista-def = record

p r i d apunt-def: end i

C Definition de la pila para alracenar el PC Y e l rapa actual 3

appil = nodpl =

stack =

var memoria: oper-cod: proctpcri: Inst:

"odpl ; record

infpc: integer; infpy: apunt-def; liga appil;

end i record

end t tope: appil ;

array C1, ,2563 of string t681; st4: integer; tip-instr;

:.:,y,tx,tY: integer; f in-ejecucion: boolean; 'm i a: string C731; dot: st20 1 nvar : byte; edo: integer; C: char I color: word ; pila: stack; tv : apunt-var ; apgroc! apunt-def $ tab-variab: 1 i s t a r 1 tab-defin: lista-deft GDIGN,CE: integer;

procedure carga; C Carga en memoria del program objeto 3 vor i: integer: tiegin

fo: i := 1 to $8 do

for i := 1 t o 256 do

i != 0; reset (salcod 1; uhile not eof Isalcod) do

insert { ' ' ,vaciorl)f

aeroriu Cil := vacia:

b e g i n i := i + 1; readln (salcod,Rerwria til);

end I PC := i ;

end 1

function ind-instr (cad: st41: integer; < Funcion que determina el 1 < t ipo de pseudoinstruccion >

var lininf ,liwsupfmitad: integer;

begin encontro: boolean;

ind-instr := 46; encontro := false;

liminf := O ; linsup != 46; while !not encontro) and (liminf <= lirsup) do

aitad := (liminf t limsup) div 2; if tab-cod hitad3 >. cad then

else

begin

l imp := nitad - 1

if tab-cod Caitadl < cad then

else liminf != mitad t 1

begin encontro := true; ind-instr := mitad;

end; end 1

end ;

procedure dame-nodo-vor (var q: apunt-var; n: byte);

vor i: byte; beqin

C Creacion de un nuevo nodo para d o j a r variables 3

neu (9 ) ; q"+numl := n i for i := 1 t o 32 do

qA+infor Ci2 := O ; qA,li9a := ni l ;

end;

function tabla-local (lis,nv: byte): real; C Obtiene el valor de una variable 1 C del nodo correspondiente 3

var p: apunt-var; n : byte;

beg in n := O ; p := tab-variab+prim; uhile pAlnuml lis do beqin

i f pA+liga = nil then beqin n != n t 1;

function determina-valor (dato: inteqer): r e d ;

I: Determinacion del valor de una variable o une constante 3 C ?a variable esta indicada por e l caracter 3 I: Se recibe el parametro 'dato' que indica cual de los 3 3

paraaetros del codigo intermedio debe ser determinado 3

,var signo-neg : boolean de-mem: boolean; num lis: byte; numIvar: byte; temporal: real ; lonq,k,pot: integer; valvar! real;

begin

signo-neq := fa lse; de-men := fa lse; dat po t := 10; case dato o f

S.. ' I * + - ?

1: dot := copy (aeaoric; Cpclr 6, 20); 2: dat := copy (aenoria Cpcl, 27, 20); 3: dat := copy (neaoria Cpclr 48, 20);

end ; if dat 111 = '*' then beqin

d e l e t e ( d a t r l r l ) ; de-mem := t rue; end 1

if ht 111 = I-' then signo-neg := t rue;

i f (da% 113 = '-'I or (dat Cil = 't') then de le te ( da t r l r l ) ;

i f dat t11 = 'X' then {'X' Y ' Y ' corresponden o l a s 1 temporal := x { coordenadas absolutas 1

e lse i f dat 111 = 'Y' then

e I s e teaporal := y

begin va lvor := O ; for k := 1 to 20 do

i f dat 1k3 i n C'0',,'9'3 then begin v a l ( d a t 1 k I ~ t e n p o r o l ~ e d o ~ ~ vo lvar := valvar t pot t temporal; end ;

if de-mea then begin num-lis := round ( va l va r ) d i v 32; nul-var := round (va lvar) Rod 32; teupora l := tab la - loca l (nu- l i s , nun-var) 1 end

temporal := v a l w r i

temporcd := O - temporcllt

e lse

i f signo-neg then

end ; determina-valor := t,enporal;

end ;

procedure det-wriabie; C Determina la posic ion que quarda una var iub le > I en e l correspondiente nodo donde se l o c d i z a >

vor krpot: byte; nua-iis: byte; temporal: integer;

begin dat := "I dat := copy (memoria C P C I F 7, 19);

war := O ; for k := 1 t o 20 do

pot := 10;

i f dot 1Rl i n C'0'++'9'1 then begin val (dot 1k3,terporalredo); nvar := nvar $ p o t + teaporal; end ;

num-lis := nvar d i v 32; n v w := nvar mod 32; temporal := round (tabla-local (nun-lis, nvar));

end

procedure push (px: integer; pp: apunt-def); { Almacena en la p i l a e l PC y 1

{ el apuntador al napa actual >

procedure pop ( va r py: integer; var ap_pr: apunt-def);

{ Saca de la pila los valores del PC Y del apuntador a l aapa a trabajar 3

var u: appil; begin

if pila,tope <? nil then begin u := pila+topet py := pila,tope"+infpc; ap-pr := pilo,tope*,infpp; pila+tope := pila,topeA,ligo; dispose (u) f end

else OutTextxv (159,99,'Error - Pila Uacia'):

begin n e w (ql; for i := O t o 39 do

for j := O to 199 do q*+ info Ci,jl := O ;

end ;

procedure dame-nodo-def (var p: apunt-def); C Creacion de un nuwo nodo para 1 i ~1 control de los nrlpas 3

begin new (p); p"proced := proc; pA,utiliz := false; daae nodo-vis (pAb,viáual); p4,1Tga := nil;

end ;

procedure selecciona-pantalla; { Seleccion del wpa que sera utilizado para 3 { e l almacenamiento de lo informacion qrafica >

var q: apunt-def; begin

q := tab-defin.pria; proc := round (determina-valor (1)); uhile qA,proced 0 proc do begin

if u*,liaa = nil then

end ; ap-proc != q;

end ;

procedure setpixel (Vis: apunt-vis; PxrPy: integer):

C Almacena en e l mapa correspondiente el pixel deseado, > C de acuerdo a las coordenadas especificados IP::,Py) 1

vor i n d - b y r i , j : byte; begin

if ((Px >.= O ? and (Px <= 319) 1 and ( (PY != O ) and (PY (I= 199)) then begin i := Px div 8; j := P y ; iod-by := 7 - (Px rod 8); Uis*.info [i,,jI := !Uis*.info ti,$) or (1 shl ind-by); end I

end

procedure dib-sector (sc,~c,rl~r2,anq-inic~ang-final: integer);

C Graficocion de un arco eliptico, 1 C ( X C , Y C ) son las coordenadas del centro, 1 C rl,r2 son las seriejes horizontal y vertical 1 C ong-inic es e l angulo de inicio del arco 1 I anq-final es el angulo final del orco 1

var xp3yp: integer;

begin xr,yr,delto,theta-ini,theta-fin: real;

if (ri 0 O ) and (r2 <> O ! then begin upgroc*,utiliz := true; theta-ini := (onginic * 2 X p i ) / 360; theto-fin := (ang-final 1: 2 t p i ) / 360; delta := 1 / ( ( r l t r2) t 2) ; uhile theta-fin >= theta-ini do begin

sr := xc t (r1 t cos (theta-fin)); yr := yc t (r2 t sin (theta-fin)); xp := rwnd (xr) ; yp := round (yr); setpicel (ap-procA,visual, xp3 yp) ; theto-fin := theta-fin - delta; end

x := xp; Y := v p ; end ;

end;

procedure dib-circ (xc3yc,r1,r2: inteqer);

i Graficacion de uno elipse con centro en (xc,yc) 1 C y seaiejes horizontol y verticcll r l y r? >

vnr pindlp,cpttrx,ry: reol; x1 ,VI: integer; ejex: boolean;

procedure puntos-circulo; begin

i f ejex then b e g i n setpixel (rlpgroc*+visua1, xc t xir yc t yl); setpixel (apgroc*,visual, xc t x l , YC - yl) ; setpixel (op-procA+visuolr xc - x l , yc t yl); setpixel (apgroc*+visuol, xc - x l , vc - yl l ; end

else begin setpixel (apgrocA+visualr xc + y l , yc + XI); setpixel (apgrocA.visual, xc t y l , YC - x l ) ; setpixel (apgrocAtvisual, xc - y13 yc t x l ) ; setpixel hpgrocA,visual, xc - y l , YE - XI);

end ; end 1

b e g i n i f ( r l 0 O ) and ( r2 c'? O ) then begin

~~pgroc"+lltil iz := true; rx := rl ; ry := r2; if r:.: >= ry then

eje>: := true else begin

e jex := false; t := rx; rx := ry; r y := t ; end 1

x1 := 0; y 1 := round (ry); c := (2 X ry ? ry) / (rx t rx) ; p := c - (2 t ry) i- 1; p i n d := sqrt !rs 4 Ir abs (int (PI)) t 1.4; while (y1 > O ) and (x1 (= rx) do begin

puntos-circulo; if p < O then

else begin 9 := p t I C Q ((2 X x l ) t 3))

p := p + !c x ((2 t xl) t 3)) - ( 4 x ( y 1 - 1)); y1 := y 1 - " 1 ,

end ; i f p C pind then

x1 := x1 t 1 else

p := p - pind; end 1

if x1 >. int (rx) then

else

while y 1 >. -1 do begin

;:I : = ;.:I - 1

if x1 < int ( rx ) then x1 := x1 t 1:

puntos-circulo; Y 1 := y1 - !; end ;

end 1 end ;

procedure dib-line ( x 1 7 y 1 , x 2 , y 2 : integer; tipl: 5 t 4 ) ;

C Graficacion de una linea recta de desde ( x 1 , y l ) hasta (x2,y

var c o l p n t : word: xflyinctti,t?: integer; i7dx,dy,constl,const2,p: integer; m: real;

begin apgroc*+utiliz := t,rue; if x1 = x2 then

m := 999 else

m := ( y 1 - y21 / (x! - x2); if I < O then

yinc != -1 else

t ipo 'tipl' 2

(X A es l a pendiente $1

(? Pendiente muy grande $1

uhile i: < x f do begin x := S t 1 ; if p C O then

else begin p := p t constl

p := p t const2; y := y + yinc;

if

end ;

end ; col-ant := color) i f ( t i p 1 = 'RAYA') and (ti mod 6) in C4,53) then color := O ; if ( t i p 1 = 'PWH') and ( ( i md 2) = 1) then color := O; if abs (m) > 1 then

else

i := i t 1 ; color := col-ant; end ; abs (m) > 1 then begin tl != x; >: := y; y := t1; end;

setpixel (apgrocA6visualr y ? x)

setpixel (ap-procA,visualt x, y);

procedure escalar (Sxt Sy: real; xpivt ypiv: integer);

C Graficucion de una imagen o escala > I Sx es el factor de esccrlcrcion en S 1 C Sr es e l factor de escalocion en y 1 C E l punto pivote es ( x p i v t y p i v ) >

vor vpz: opunt-vis; :.:S,YS: real; i t jtk ~ 8 : byte',

beqin xs := xpiv t (1 - Sx); YS := y p i v t (1 - Sy); dame-nodo-vis ( V I ;

...

Y - r",

. I -.- .. .- - . . . . . . . . - :* . . . . . . . .

........ ..... . . . . . . . . " . ,..."I _" ___"I___ I___.

. .

for i != O to 39 da for j := O to l?? do

for k := 7 downto O do begin m := apgroc*,visualA,info Eirj3 and !1 shl k); i f m C> O then

setpixel (v, round ( ( ( < i $ 8) t ( 7 - Io) X Sx) t xs)? round C C j I SY! t y s ) ) ;

end ; z := ap_proc*,visual; apgrocA+viwal := v ; dispose (2);

end ;

procedure ej-llarni forward;

procedure ejecut; foruard;

procedure e j - d a ; begin

delay (3000); CleerDevice; ;.; : = 0 ; y :=a;

end ;

C Linpieza total del monitor 1

procedure ej-arco; I Control de l a graficacion de un arco circular 1

vor rran-ini,m-fin: inteqer; begin

cm-ini := round (determina-valor (1)); an-fin := round (determina-valoi (2)); r := round (deteraina-valor (3)); dib-sector (x,y,rrr,an_iniran_fin)l

end;

p;ocedure ej-asig; C hplementacion de l a pseudoinstruccion de Asignacion > begin

det-variable; tv*+infor fnvarl != letersino-valor (21 1

?nd f

procedure e j - d r ; C Graficacion- de un astroide con paranetro radial ' rad' 1

vor x p ~ y p r ~ d : integer;

begin cttst,deltartheta: real;

red := round (determina-valor (1)); if rad .O O then begin

apgroc*,utiliz := true; delta := 1 / (rad $ ? I ; theta := O ; uhile theto < 6,283 do begin

c t := cos (theta); s t := sin (theta); xp := round ( x + Irrld f c t t c t t c t ) ) ; Yp := Found ( y t ( r ~ d Ir st I st t st)) ; setpixel (apgroc*.visual, xp? yp) ; theta := theta + delta;

ena end :

end ;

procedure ej-borr; C Borrado o iniciolizocion del mapa de un procediriento 3 var i,,j: byte ; begin

delay (1506) ; push ( P C , clp-proc); seleccionagantalla; for i := O to 39 do

for j := O t o 139 do

ap_procA,utiliz := false; ap-procA,visualA,info Ci,jI := O ;

POP ( P C , ap-proc); end ;

procedure ej-circ; C Control de la qraficacion de un circulo 1 var r: integer; begin

r := round (determina-vcrlor (1)); dib-circ ( x , y , r , r ) ;

esd i

{ hpresion en el manitor del 1 { mapa upuntado por V 1

procedure iaprise (U: apunt-vis);

v w i , , j , k , n : byte; begin

for i := O t o 39 do for j := O t o 199 do

for K := 7 dounto O do begin M := VA,info Ci,jl and ( 1 shl k); if R C> O then putpixel ( ( i # 8) + 17 - K), 199 - ,jr color); end t

end ; , .

procedure ej-dibu; C Impleaentacion de l a pseudoinstruccion DIBU 3 C Se controla lrl iapresion de los mpas 1 C llarando a 'imprime' con e l apl~ntador adecuado 3

vor pr: integer; ilp: apunt-lef ; ov: apunt-vis;

h9in pr := round cdetereina_valor (1)); i f p r = O then begin

ap := tob-defin+prirn; while ap O nil do begin

if apA,utiliz then beqin rlv := apA,visuol; inprime (av); end ;

up := apA,liga; end ;

end else begin

push ( P C , ap-prac 1 ; seleccionagantalla; if ap-procA,utiliz then

POP (pcr UPgrOc) ; impriue (apgrocA+visud);

end t end ;

beg ' n $et-variable; if (determina-valor (2)) (3 (determina-valor 13)) then

else tvA,infor Envorl := 1

tv*,infor Cnvarl := O ; end ;

i: byte; begin

var-cadena := "; if mentoria Epcl 161 = "' t.hen begin

i := I ; while aenoria Ipcl Ei + 63 0 '" do begin

insert (ReROT,iO [ P C ] Ci t 63, var-cadena, i); i' := i t 1; end ;

end

str trunc (deternina-valor ( 1)) y var-cadena) ; else

OutTextxy ( x f 199 - y f var-cadena); end;

C Iaplementacion de l a division 3 procedure ej-divi; var divisor: real; begin

divisor := determina-valor (31; if divisor 0 O then begin .

det-variable; tVA+infOr Cnvarl := determina-valor (2) .! divisor; end ;

end t

i Control dr !a graficccion de una elipse 3

procedure ej-esca; I Control del escalamiento de una inogen 1 var xpivfypiv : integer;

begin SxlS~~cs,ys: real ;

push ( P C , ap-proc); selecciona Dantalla: if not ap$ocA,utiiiz then begin

ej-1 1 amf repeat

until inst = REGR; end 1

Sx := deterlinn-valor (2); f Sx Y SY son los factores de > SY := deteruina-valor (3); C escalacion horiz Y vertical resp 3

ejecut

i f ap-proc A.utiliz then begin

inst := tip-instr (ind-instr (copy (memoria [PC .) l!! l1 4 ) ) ) ;

i f inst = PIVO then begin C Se comprueba la notificacion del 1 < punto pivote 1

PC, := PC t 1; xplv := round (determina-valor ( 1 ) ) ; ypiv := round (determina-valor (21); end

else begin xpiv := 159; ypiv := 99; end I

escalar (Sx, Sy, xpiv, ypiv) ; end f

pop (per ap-proc); end ;

procedure ej-figu (Forla: tip-irptrl; i Graficacion de: Caracol, Cardioide, > < Espiral Y F(osas de 3p4,S,8 petalos >.

var rpdeltartheta: real;

begin sp~~p,radiol,rrldio2: integer;

wdiol := round (determina-valor (1) 1; if Forma = CARA then C El caracol requiere de dos >

radio2 := round (determina-valor (2)); C pawnetros 1

if (:rldiol ,<> O ) and (((Forma = CAI#) and (radio2 0 Q ) ! or (Forma i> CARA)) then begin

apgr:*,utiliz := true; delta t= 1 / (radiol Y 3); t.heta != O ; while theta J< 6,283 do begin C Calculo de !os puntos para 1

C cada ccso >. case Forma of '

ChRN r := radio2 - (mdiol Y cos ( theta)) ; CARD: r := radiol t ( 1 - cos (theta)); ESPI: 7 := radiol # theta; R O S 3 r := radiol t sin (5 Y theta); FsOS3: r := radiol # sin (3 Ir theta); ROS4: r := radiol t sin !? $ theta); ROS% r := radiol t sin ( 4 % theta);

end ; xp := round ( x f ( r t cos (theta))); yp := round ( y t ( r # sin (theta))) ; setpixel lap-prrJcAlvisuafr xp? yp) ; theta := thetQ + deltrl; end ;

end 1 end ;

procedure ej-goto; C tiodificacion de las coordenadas X, Y debido a > C la ejecucion de la pseudoinstruccion 'GOT' 2-

begin x := round (deteruina-valor (1)) i Y := round (determina-valor (2));

end ;

procedure ej-halt; Inplementacion de HALT (Fin de interpretacion) > beg in

end ; fin-pjecucion := true;

procedure eJ-igua; Uerificacion de igualdad entre operandos 1 begin

det-variable; if (determina-valor (2)) = (deternina-valor (3)) then

else tv*,infor Cnvorl := 1

tvAtinfor Cnvarl := O ; end ;

procedure ej-lee; C Lectura de una variable desde el teclado > var c: char; begin

det-variable; c := HeadKey; tvA+infor hvarl := ord (c) - ord ( ' O ' ) ;

end;

procedure ej-line; C Control de la graficacion de una linea recta 1 var tipl: st41 begin

tipl := copy !aeworia Epcl, 48, 4); dib-line (x, Y ? round (deterwino-valor (1)) I

round (deternina-valor (2) 1 tipl 1; e n d 1

procedure ej-llam; C Llamado a un procediniento de definicion (DEFIHE) 3 begin

push Ipc, ap-proc); seleccionagantalla; pc := round (determina-valor (1)) - 1;

end ;

?rocedure llenar C.:.:l,yl: hteger) ; C Procedimiento recursivo para llenar un 1 C wea desde la coordenada xl,yl 1

var n , i n d - b y , i ! j : byte; be3in

if (x1 >= O ) and ixl (= 3191 tlnd (y1 >= O ) and (y1 <= 319) then b e g i n i := x1 d i v 8;

ind-by := 7 - :x1 nod 8); M := apgrocA,visualA,info [:i,jI and (1 shl ind-by); if RI = O then begin

j := y l f

setpixel Cp-nocA ,visual, x l , y1 1 ;

llenar (x1 + 1, yl) ; C Se prueban los puntos vecinos 3 llenar (x1 - 1, y l ) ; llenar { x l , y 1 t 1); llenar ( ~ 1 ~ r l - 1);

end ; end 1

end ;

~ ~-

procedure ej-llent Control del llenado de u n drea definida en u n Mapa 3 C local o global, dependiendo del valor del p?imer ;- X paraaetro de l a pseudoinstruccion LLEN 1

var x1 , y l : integer begin

push ( P C f apgroc); seleccionagantdla; if not ap-proc*+utiliz then begin

ej-llorn) repeat

ejecut until inst = REM; end;

x1 := round (determina-valor (2)); y1 := round (determina-valor (3)); llenar (x1,yl)J end;

pop (per apgroc);

if ap-procA,utiliz then'beqin

end ;

procedure ej-may; begin

det-variable; if (determina-valor

tvA,infor Cnvarl else

tvA,infor Cnvarl end ;

procedure e j a v i ; begin

det-variable; if (determina-valor

tvA,infor Envarl else

tv*,infor Cnvarl md;

procedure ej-men; begin

det-variuble; if (determina-vdor

tvA,infor Cnvarl else

tvA,infor Envarl Pnd

procedure ej-wni: begin

det variable; if TdeterIIin1l-vrllOr

tvA,infor Cnvarl else

tvA.infor Cnvarl end ;

procedure ej-rove;

beoin

C Iaplelentacion del condicional 'mayor' 1

( 2 ) ) > (determina-valor (3)) then := 1

:= o;

C Impleaentacion del condicional 'mayor o igual' 1

:= o;

C Implementacion del condicional 'mor ' 3

:= o;

C Iaplementacion del condicional 'menor o igual' >

C Increaento a los valores actuales de las 1 c coordenadas tlbsolutas X, Y >

I:

i

y := y t round (determina-valor (2)); "

end;

procedure ej-mult; begin

det-variable;

{ flultiplicacion de l o s operandos 2 Y 3 >

tvATinfor Cnvarl := deterBjna-valor (2) $ deternina-valor (3); end t

procedure ej-poli ; C Graficacion de u n poligono regular de 3 { 'num' hdos , siendo l a longitud de > C uno de el los igual a 'long' e iniciando 3 C el grafico en el punto Ix,y) 1

var ang,ang-inic: real ;

begin i ,xp,yp,xi ,yi ,xf ,yf,num,long: inteper;

;.;i := x; ;<p := x; vi := y; yp := y; nun := round (detemina-valor (I)) 1 long := round (deternino-valor (2)); if (nun > O ) and (long > O ) and ílong <: 200) then begin

ong := (2 t Pi) / n u n ; C Se determinan 113s etapas de rotation 3

Imq-inic := a n q ; ..:f := x i t long; y f := vi; dib-line ( x i , y i , xf, y f , 'LISA'); for i := 1 t o (nu# - 2) do begin

x i := xf; y i := y f ; sf := rwnd ( x f t (la9 t cos (ang))); rf := round ( y f t (long t sin (an9))); ang := rlnq t ang-inic; dib-l ine (xi , y i , uf, yf, 'LISA') end ;

e dib-line (xf, y f , xp, vpr 'LISA'); end 1

end ;

procedure ej-punt; var,xp,yp: integer; beg~n

a p g r o c A d i l i z := \rue; xp := round (determina-valor (1 ) ) ; yp := round (determina-valor ( ? I ? ; setpixel (rlpgrocA+visual, xp, yp) .:< ; = >:p ; Y := Y P t

end ;

procedure ej-recti vQr xi ,yi ,xf ,yf : integer; begin

xi := round (deteraim-valor (1)); vi := round (deternina-valor (2)); gc := P C t 1; xf := round (determina-valor (1) 1; vf := rwnd (determina-valor (2)); dib-line ( x i , y i , x f ,yi,'LISA'); dib-line (xf,yi,xf,yf,'LISn'); dib-line (xf,yf,xi,yf,'LISA');

< Graficacion de un punto 1

C Graficocion de un rectangulo 1

procedure ejrrefl; x Reflexion de una imagen con respecto a l e je indicado 3 v w eje: string Cll;

i , j , k , d byte; vIz: apunt-vis; C;:,Cyf integer; orig: boolean;

begin push ( P C , apgroc); seleccianagantalla; if not apgrocA,utiliz then begin

e,j-llam; repeat

ejecut until inst = RE% end

if ap_procA,utiliz then b e q i n e j e := copy ( m o r i a Cpcl, 27, 1);

t Deterninacion de los factores de 1 C reflexion dependiendo del e j e 3

if e je = 'X' then begin Cx := 1; Cy := -1; orlg := true; end

else if e je = ' Y ' then beqin

Cx := -1: Cy := 1; orig := true; end

else if e je 'U' then begin

Cx := -1; Cy := -1; orig := false; end

else begin Cx := 1; Cy := 1.; oriq := false; end ;

dame-nodo-vis (VI; for i := O t o 39 do

Por J := O t o 199 do far k := 7 dounto O do begin

m := apJrocA,visualA+info [:i, jl and ( 1 shl Kl; if m O O then

i f oriq then setpixel t v , round ( t ( ( i d 8) + ( 7 - 9 ) ) - 1591 f kc) + 159,

round C C j - 999) t Cv) + 99) else

.&pixel (v , round ! (J - 99) X C::) t 159, round ( ( ( ( i f 8 ) t !7 - I ! ) - 159) 3 C.*) t 991;

end; ?: := upgrocA,visual; apgroc*,visual := v; dispose (z); end f

pop (PC, ap-proc); end ;

procedure ej-regr; { Retorno desde un procedimiento que fue llamado 3 begin

pop ( pc , a p ~ roc ? ; end ;

$," ............ . ....... .... ... .... . . . . - .- ~. *. -... ." "_ ~ - .__I . . . . . . . . . . .

3 .......... .... .... .

>rocedure e,j-ro+,a; C Rotacion de t m imagen de acuerdo a1 w p l g i n d i d o 2 var i ? , j r k , f n t byte;

:.:pivrvpiv: integer; anq,xs,ys,ct,st,theta: real; V ~ Z : apunt-vis;

pilsh ( P C , ap-proc); seleccionagantallo; if not ap-pro~~~utiliz then begin

beqin

ej-1 1 aa ; repeat ejecut

until inst = REGR; end ;

unq := deteninavalor (2 ) ; theta := (ang $ 2 $ pi) / 360; inst := tip-instr (ind-instr (copy ( m o r i a tic t 13, 1, 4 ) ) ) ;

if inst = PIUO then beqin C Se verifica l a notificacion 1

if apgrocA,utiliz then begin

PC := PC t 1: c del punto pivote 3 xpiv := round (deternina-valor (1)): rpiv := round (deterlino-valor (2)); end

else begin xplv := 159; yp1v := 99; end ;

ct := cos (thek) I st := sin (theta); xs := (xpiu X (1 - ct)) t (ypiv t st); ys := Iypiv Ir (1 - ct)) - hpiv f st); dame-nodo-vis (v); for i := O t o 39 do ?or j != O to 1 9 9 do

for K := 7 dounto O do beqin m := ap-procA,visualA,info Ci,jI and (1 shl k); i f m 0 3 then setpixel ( v ? round ((((i $ 8) t ( 7 - K ) ) t ct) .- ! j t st) t XS),

round ( ( j X ct) t (((i $ 8) + ( 7 - k)) t st) t ,ysl); end;

Z := apgroc*,visual; apgrocA+visual := v; dispose (z!; end ;

POP (PC, apgroc); end ;

procedure ej-salt; C nodificacion del PC para Salto Incondicional 1 begin

end ; PC := round (determina-valor (1)) - 1;

procedure ej-semi; C Control de l a graficacion de un arco eliptico 1 var rl,r2,an-ini,an_fin: inteqer; beqin

an-ini := round (deterninQ-valor (1) 1;

S- .I :

. . . . . . . . . . . . . . . . . . . . . . . ...... . . . . . . . . . . . - . . ... . . . . . .

procedure ej-ssif 1 begin

if determina valor (2) = O then PC := round Tdeternina-vdor (1)) - 1;

end ;

{ Salto condicional 1

procedure ej-sura; { Suma de los operandos 2 Y 3 3 beqin

det-variable; tvA,infor [nvarl := determina-valor (2) t detenino-valor (31;

end

procedure ej-tras; { Traslacion de una imagen definida en un rapa 3 var Tx,Ty: integer; C Tx es la constante de traslacion horizontal >

i F j t K t d byte; C TY es la constante de traslacion vertical 1 VIZ: apunt-vis;

push (pc, apgroc); seleccionagantcllu; if not apgroc*,utiliz then b e g i n

begin

e j-1 1 aa repeat

until inst = REGRI end;

Tx := round (determina-valor (2)); Ty := round (determina-valbr (3)); dane-nodo-vis ( V I ; f o r i := O to 39 do

ejecut

if apgroc%utiliz then begin

for J := O t o 199 do for k := 7 dowrto O do begin

m := apgrocA,visualA,info ti7,jl and (1 shl K); if I <> O then

end ; setpixe! (v, íi J: S) t ( 7 - k ) t Tx, j t T y ) ;

z := apgrocA+visual; opgrocA+visual := 8::

dispose ( 2 ) ; end i

POP ( P C , q g r o c ) ; end ;

procedure ej-xxxx; var i: byte;

{ Pswdoinstruccion de 'error' 3

begin for i := 1 t o 6 do

write (#7); OutTextxy (159,99,'Error Fatal, Prograna Codigo Invalido') 1 fin-ejecucion := true;

end;

procedure ejecut; C Control de la ejecucion de las pseudoinstrucciones 3

{ Control general del interprete 1 begin window (l,lt80,2S)t clrscr; GD := Detect; InitGraph (GD,Gth" 1; M := GraphResultl if CE <> qrOK then begin

urite (17) 1 uriteln ('Error en Inicializacion a Hado Grafito'); Exit C end i

SetGraphHode (CGAC1) ; SetTextStyle (SnallFont,HorizDir,l)f SetTextJustify (CenterText,CenterText); SetUserCharSize (2,3,1,1); color := GettiaxColort ..; = o; y :=o: pila,tope := nil; fin-ejecucion := false: cargo: PC := rand (detemina-valor (1)) - 1; proc := O ; dale-nodo-var (tob-veriob+prim, O ) ; dame-nodo-def (tab-def in +prir); oprproc := tab-defin.prir; u h l e not fin-ejecucion do

Readln l CloseGmph;

ejecut;

end I

End +

.. . . .. - ...."I "

Compi 1 or S

Pr i n c i pl es, T e c h n i quos and TnoL S

Alfred V . Aho R a v i S e t h i Jeff rey D. Ullrnan Addi son-Wesl e y , 1386

METAVI S' A G e n e r a l P u r p o s e Graphic SOt,.em

P. B o d 1 i e r J . Gross -,' P. Jancene A. Lemai r e 1 F. P r rrsker E. Sal I n o t i t u t de R e c h e r c h e d ' I n f o r mat. i q u e et.. d' Automati que Rocquencour t - F r a n c e , 1976

P r i n c i p l e s sf 1 n t e r a c t . i v e Ccmp:lter Sraphi.cn Wi 1 1 i am M. Newman Robart F. Sproull Mc -Gr a w H i 11 , 1 !384

Gráficas por Camputadora Donal d Hear n M. P a u l i n e Baker P r e n t i ce-Hal 1 , 1 F3.F3z3

Turbo Pascal Refererce G1.1.i d e Ver si 5 n 5. O

Bor 1 and I n t e r n a t i onal , 19853

E n c i c l o p e d i a d e la. Informática Nusva L e n t e Ingelek , 1939.3