Фигуры МЕТА

Фигуры МЕТА

sergey shishkin

https://telegra.ph/META-II-05-11

...

Figure 3: A Program Compiled for the VALGOL-I Machine

...

Figure 4: Output from the VALGOL-I Program Given in Figure 3

...

Figure 5: The META-II Compiler Written in its Own Language

...

MACHINE CODES

TST  string  TEST After deleting initial blanks in the input string, compare it to the string given as argument. If the comparison is met, save and delete the matched portion from the input and set switch. If not, reset switch.

ID IDENTIFIER After deleting initial blanks in the input string, test if it begins with anidentifier, i.e. a letter followed by a sequence of letters and/or digits. If so, save and delete the identifier and set switch. If not, reset switch.

NUM NUMBER After deleting initial blanks in the input string, test if it begins with a number. A number is defined as a string of digits, optionally preceded by "-". If a number is found, save and delete it and set switch. If not, reset switch. [This is a departure from the original version which allowed real numbers as well as integers.]

SR STRING After deleting initial blanks in the input string, test if it begins with a string, i.e. a single quote followed by a sequence of any characters other than single quote followed by another single quote. If a string is found, save and delete it and set switch. If not, reset switch.

CLL  aaa  CALL Enter the subroutine beginning at location aaa. In addition to the return address, save the state of the label generation variables and, optionally, the most recently saved input element on the stack. [Saving the input element is an addition to standard META-II]

R RETURN Return from a subroutine, restoring the state of the label generation variables and, optionally, the saved input from the stack.

SET SET Set branch switch on.

B aaa BRANCH Branch unconditionally to address aaa.

BT aaa BRANCH TRUE Branch to location aaa if switch is set, otherwise continue in sequence.

BF aaa BRANCH FALSE Branch to location aaa if switch is reset, otherwise continue in sequence. BE BRANCH ERROR IF FALSE Halt if switch is reset, otherwise continue in sequence. [This implementation displays as much error information as possible, including an indi- cation of the location of the error in the source line, and the name of the META-II equation in which the BE was executed.]

CL string COPY LITERAL Output the variable length string given as the argument. A blank character will be inserted in the output following the string [unless the argument was quoted].

CI COPY INPUT Output the last sequence of characters deleted from the input string. This command may not function properly if the last command which could cause deletion failed to do so.

GN1 GENERATE 1 If the first label variable is blank, generate a new label in sequence. Whether the label is new or not output it [formatted according to the options set and whether it is a true label or an operand].

GN2 GENERATE 2 As above, except that it concerns the second label variable.

LB LABEL Prepare the output routines to output a true label, as distinct from an operand.

OUT OUTPUT Output a constucted line, the precise format of which is dictated by the options set.

ADR ident ADDRESS This is effectively a CLL instruction to the main equation in the META-II pattern.

END END End of the pattern.

Figure 6: Order List of the META-II Machine

.SYNTAX PROGRAM

ARRAYPART = ’[’ EXP ’]’ .OUT(’AIA’);

CALLPART = ’(’ .OUT(’LDF’) (EXP $(’,’ EXP) | .EMPTY) ’)’ .OUT(’CLL’); VARIABLE = .ID .OUT(’LD ’ *) (ARRAYPART | .EMPTY);

PRIMARY = ’WHOLE’ ’(’ EXP ’)’ .OUT(’WHL’) | .ID .OUT(’LD ’ *) (ARRAYPART | CALLPART |

.EMPTY) | ’.TRUE’ .OUT(’SET’) | ’.FALSE’ .OUT(’RST’) |

’0 ’ .OUT(’RST’) | ’1 ’ .OUT(’SET’) | .NUMBER .OUT(’LDL’ *) | ’(’ EXP ’)’;

TERM = PRIMARY $(’*’ PRIMARY .OUT(’MLT’) | ’/’ PRIMARY .OUT(’DIV’) | ’./.’ PRIMARY .OUT(’DIV’) .OUT(’WHL’));

EXP2 = ’-’ TERM .OUT(’NEG’) | ’+’ TERM | TERM;

EXP1 = EXP2 $(’+’ TERM .OUT(’ADD’) | ’-’ TERM .OUT(’SUB’));

RELATION = EXP1 (’.L=’ EXP1 .OUT(’LEQ’) | ’.L’ EXP1 .OUT(’LES’) |

’.=’ EXP1 .OUT(’EQU’) | ’.-=’ EXP1 .OUT(’EQU’) .OUT(’NOT’) | ’.G=’ EXP1 .OUT(’LES’) .OUT(’NOT’) |

’.G’ EXP1 .OUT(’LEQ’) .OUT(’NOT’) | .EMPTY); BPRIMARY= ’.-’ RELATION .OUT(’NOT’) | RELATION;

BTERM= BPRIMARY $(’.-’ .OUT(’BF ’ *1) .OUT(’POP’) BPRIMARY) .LABEL *1; BEXP1 = BTERM $(’.V’ .OUT(’BT ’ *1) .OUT(’POP’) BTERM) .LABEL *1;

IMPLICATION1 = ’.IMP’ .OUT(’NOT’) .OUT(’BT ’ *1) .OUT(’POP’) BEXP1 .LABEL *1; IMPLICATION = BEXP1 $ IMPLICATION1;

EQUIV = IMPLICATION $(’.EQ’ .OUT(’EQU’));

EXP = ’.IF’ EXP ’.THEN’ .OUT(’BFP’ *1) EXP .OUT(’B ’ *2) .LABEL *1 ’.ELSE’ EXP .LABEL *2 | EQUIV;

ASSIGNPART = ’=’ EXP (ASSIGNPART .OUT(’ST ’) | .EMPTY .OUT(’SST’));

ASSIGNCALLST = .ID .OUT(’LD ’ *) (ARRAYPART ASSIGNPART | ASSIGNPART | (CALLPART | .EMPTY .OUT(’LDF’) .OUT(’CLL’)) .OUT(’POP’));

UNTILST = ’.UNTIL’ .LABEL *1 EXP ’.DO’ .OUT(’BTP’ *2) ST .OUT(’B ’ *1) .LABEL *2; WHILECLAUSE = ’.WHILE’ .OUT(’BF ’ *1) .OUT(’POP’) EXP .LABEL *1 | .EMPTY; FORCLAUSE = VARIABLE ’=’ .OUT(’FLP’) .OUT(’BFP’ *1) EXP ’.STEP’ .OUT(’SST’)

.OUT(’B ’ *2) .LABEL *1 EXP ’.UNTIL’ .OUT(’ADS’) .LABEL *2

.OUT(’RSR’) EXP .OUT(’LEQ’) WHILECLAUSE ’.DO’;

FORST = ’.FOR’ .OUT(’SET’) .LABEL *1 FORCLAUSE .OUT(’BFP’ *2) ST .OUT(’RST’)

.OUT(’B ’ *1) .LABEL *2;

IOCALL = ’READ’ ’(’ VARIABLE ’,’ EXP ’)’ .OUT(’RED’) |

’WRITE’ ’(’ VARIABLE ’,’ EXP ’)’ .OUT(’WRT’) |

’EDIT’ ’(’ EXP ’,’ .STRING .OUT(’EDT’ *) ’)’ |

’PRINT’ .OUT(’PNT’) | ’EJECT’ .OUT(’EJT’); IDSEQ1 = .ID .LABEL * .OUT(’BLK 1’);

IDSEQ = IDSEQ1 $(’,’ IDSEQ1); TYPEDEC = ’.REAL’ IDSEQ;

ARRAY1 = .ID .LABEL * ’[’ ’0’ ’..’ .NUMBER .OUT(’BLK 1’) .OUT(’BLK’ *) ’]’; ARRAYDEC = ’.ARRAY’ ARRAY1 $(’,’ ARRAY1);

PROCEDURE = ’.PROCEDURE’ .ID .LABEL * .LABEL *1 .OUT(’BLK 1’)

’(’ (IDSEQ | .EMPTY) ’)’ .OUT(’SP 1’)’;’ ST .OUT(’R ’ *1); DEC = TYPEDEC | ARRAYDEC | PROCEDURE;

BLOCK = ’.BEGIN’ .OUT(’B ’ *1) $(DEC ’;’) .LABEL *1 ST $(’;’ ST) ’.END’ (.ID | .EMPTY); UNCONDITIONALST = IOCALL | ASSIGNCALLST | BLOCK;

CONDST = ’.IF’ EXP ’.THEN’ .OUT(’BFP’ *1) (UNCONDITIONALST (’.ELSE’ .OUT(’B ’ *2)

.LABEL *1 ST .LABEL *2 | .EMPTY .LABEL *1) | (FORST | UNTILST) .LABEL *1);

ST = CONDST | UNCONDITIONALST | FORST | UNTILST | .EMPTY; PROGRAM = BLOCK .OUT(’HLT’) .OUT(’SP 1’) .OUT(’END’);

.END T(VALGOL2,V2,META2) 4/1/87

Figure 7: VALGOL-II Compiler Written in META-II

MACHINE CODES

LD aaa LOAD   Put the address aaa on top of the stack.

LDL number   LOAD LITERAL Put the given number on top of the stack

SET SET    Put the integer 1 on top of the stack.

RST RESET   Put the integer 0 on top of the stack.

ST STORE Store the contents of the register "stack1" in the address which is on top of the stack, then pop the stack.

ADS (Note 1)   ADD TO STORE  Add the number on top of the stack to the number whose address is next to the top, and place the sum in the register "stack1"; then store the contents of the register in that address, and pop the top two items.

SST (Note 2) SAVE & STORE Put the number on top of the stack into the register"stack1"; then store the contents of that register in the address which is next to the top and pop the top two items.

RSR RESTORE Put the contents of the register "stack1" on top of the stack.

ADD (Note 2) ADD Replace the two numbers which are on top of the stack by their sum.

SUB (Note 2) SUBTRACT Subtract the number which is on top of the stack from the number next to the top, then replace them both by the difference.

MLT (Note 2) MULTIPLY Replace the numbers which are on top of the stack by their product.

DIV (Note 2) DIVIDE Divide the number which is next to the top of the stack by the number on top of the stack, then replace them by the quotient.

NEG (Note 1) NEGATE Change the sign of the number oMLT (Note 2) MULTIPLY Replace the numbers which are on top of the stack by their product.n top of the stack. WHL WHOLE Truncate the number which is on top of the stack.

NOT NOT If the number which is on top of the stack is the integer 0, then replace it by the integer 1, otherwise replace it by the integer 0.

LEQ (Note 2) LESS OR EQUAL If the number which is next to the top of the stack is less than or equal to the number on top of the stack then replace them by the integer 1, otherwise replace them by the integer 0.

LES (Note 2) LESS THAN If the number which is next to the top of the stack is less than the number on top of the stack then replace them by the integer 1, otherwise replace them by the integer 0.

EQU (Note 2) EQUAL Compare the two numbers on top of the stack. Replace them by the integer 1 if they are equal, or by the integer 0 if they are unequal.

B aaa BRANCH Branch to the address aaa.

BT aaa BRANCH TRUE Branch to the address aaa if the top term in the stack is not the integer 0, otherwise continue in sequence. Do not pop the stack.

BF aaa BRANCH FALSE Branch to the address aaa if the top term in the stack is the integer 0, otherwise continue in sequence. Do not pop the stack.

BTP aaa BT and POP As BT, but pop the stack.

BFP aaa BF and POP As BF, but pop the stack.

CLL CALL Enter a procedure at the address which is below the flag.

LDF LOAD FLAG Put t1h6e address which is in the flag register on top of the stack, and put the address of the top of the stack into the flag register.

R aaa RETURN Return from procedure

Note 1: If the top item in the stack is an address, it is replaced [at execution time] by its contents before beginning this operation. Note 2: Same as Note 1 (above) except it applies to either or both of the top two stack items.

Figure 8: Order List of the VALGOL-II Machine.

.BEGIN

.PROCEDURE DETERMINANT(A,N);

.BEGIN

.PROCEDURE DUMP();

.BEGIN

.REAL D;

.FOR D = 0 .STEP 1 .UNTIL N-1 .DO WRITE(MATRIX[N*D],N);

PRINT

.END DUMP;

.PROCEDURE ABS(X);

ABS = .IF 0 .L= X .THEN X .ELSE -X;

.REAL PRODUCT, FACTOR, TEMP, R, I, J; PRODUCT = 1;

.FOR R = 0 .STEP 1 .UNTIL N-2

.WHILE PRODUCT .-= 0 .DO

.BEGIN

I = R;

.FOR J = R+1 .STEP 1 .UNTIL N-1 .DO

.IF ABS(A[N*I+R]) .L ABS(A[N*J+R])

.THEN

I = J;

.IF A[N*I+R] .= 0

.THEN

PRODUCT = 0

.ELSE

.IF I .-= R

.THEN

.BEGIN

PRODUCT = -PRODUCT;

.FOR J = R .STEP 1 .UNTIL N-1 .DO

.BEGIN

TEMP = A[N*R+J]; A[N*R+J] = A[N*I+J]; A[N*I+J] = TEMP

.END

.END; TEMP = A[N*R+R];

.FOR I = R+1 .STEP 1 .UNTIL N-1 .DO

.BEGIN

FACTOR = A[N*I+R] / TEMP;

.FOR J = R .STEP 1 .UNTIL N-1 .DO

A[N*I+J] = A[N*I+J] - FACTOR * A[N*R+J]; DUMP

.END

.END;

.FOR I = 0 .STEP 1 .UNTIL N-1 .DO PRODUCT = PRODUCT * A[N*I+I]; DETERMINANT = PRODUCT

.END DETERMINANT;

.REAL M, W, T;

.ARRAY MATRIX[0..24];

.UNTIL .FALSE .DO

.BEGIN

EDIT(1,’FIND DETERMINANT OF’); PRINT;

PRINT;

READ(M,1);

.FOR W = 0 .STEP 1 .UNTIL N-1 .DO

.BEGIN

READ(MATRIX[M*W],M);

WRITE(MATRIX[M*W],M)

.END; PRINT;

T = DETERMINANT(MATRIX,M); WRITE(T,1);

PRINT;

PRINT

.END

.END PROGRAM

Figure 9: Example Program in VALGOL-II


Report Page