POSTScript 2 - Operator Stack, Stack Operator and Mathematical Operators

xiaoxiao2021-03-06  20

introduction:

This is the second series of articles about PostScript. The main purpose of this article is to discuss the operation of the stack. Operating array may be the most important part of PostScript. Assignment, arithmetic or mathematical operations, cycles and logical operations are carried out in this special storage area. Yes it is! The stack is a special storage area and is used by postscript to perform almost all of us asking it to do. The stack will keep the first rule to keep information. You can simply form its image into a closed conduit. When you put some objects in it, it will push all things in the tube to the closed end to release the space. Therefore, the final entry element always leaves the end of the port. The elements in the stack can be a string, digital constant, key code, data block ...

Stack operator

Although elements in the stack have been arranged in an order, there are still stack operators that allow us to rearrange these elements. These operations apply one or more elements in the stack. The operator is pressed according to its definition of the elements within the stack. Do they require parameters (called operands in PostScript) depending on the operation performed. If parameters are required, they must first be pushed into the stack. Thereafter, the operator takes a corresponding action based on these parameters. Here we give a list of these operators and have a detailed description. We also give some examples later to clarify more details.

POP: This operator deletes an element of the operand stack (final entry).

Exch: This operator has two elements of the top of the operator.

DUP: This operator copies the elements that finally enter the operator stack and push it into the operator stack, that is, copy the top elements of the stack.

COPY: This operator requires an integer operand (ie, parameter) that is pushed before execution. When this integer parameter is n, the command n copy is given. After this operation is completed, the copy of the N elements located on the top of the stack is set as a set of operative count stacks as a group of final entered elements. In other words, COPY is a batch replication operator.

INDEX: This operator requires an integer operand that is pushed into the operator stack before execution. When this integer parameter is n, the command n index is given. After the operation is completed, a copy of the nth element of the stack is set as the final entry element being placed in an operating count. In other words, the Index operator selects an internal element of the operator stack to generate its copy and place it in the operator count. The index of the element begins with a stack top element.

ROLL: This operator requires two integer parameters that are pushed before execution. If they are m and n, the command M n index is given. Here M specifies the number of elements of the joining operation (ROLL), and n indicates the number of rolled operations. One rolling operation is defined as: Operating count stack top element becomes a m-one element in the stack, and the M-1 element below the original stack is moved in turn to the top of the stack. This process is only established when n is 1. When N is 2, a continuous two rollover operation will be performed. In other words, M 2 Roll is equivalent to M 1 Roll M 1 roll. The parameter N can be negative. The operation occurred at this time is opposite to N as a positive number. This means that the command M n roll m -n roll will not produce any effect, that is, there is no change of the operator stack. The index of the element begins with a stack top element.

CLEAR: This operator deletes all elements in the operator count.

Count: This operator calculates the number of elements in the operating count. The calculation result is pushed into the operating counting in the operator as a new element. If you don't want this new element, you can give a combination command count pstack pop. The result of the count operation here is displayed when the file operator PSTACK is displayed, and the POP operation is removed from the operating count stack.

Mark: This operator places a marker in the operator stack (-MarkType-). This element can divide the elements in the operator count into a subset. The other two operators Cleartomark and COUNTTOMARK are required to search for this element in the stack. If you can't find it, the system gives an error message.

CleartOMark: This operator deletes all elements from the top of the first tag -MarkType, including -marktype- itself. If there is no -marktype-element in the stack, the system gives an error message. COUNTTOMARK: This operator starts counting from the top elements in the operator count until the -marktype-element is encountered. The count result is a full value, which is pushed into the operating count stack as the last element. The -marktype-element encountered does not add a count value. If there is no -marktype-element in the stack, postscript generates an error message without any operation.

Let us now discuss the stack. If you want to see the execution of the above operator, you must first start the PostScript interpreter. As mentioned in the first article, a public interpreter software is used in the Linux world - GHOSTScript. Give the appropriate parameters in the command line, GostSripT can start in a different way. Usually, just type GS in the X window environment. Due to the problem of the X window, it is sometimes not started. At this time, an error message about the creating a simple graphics control window will appear. If this problem cannot be resolved, it is necessary to force GostScript to use the X11 drive device. To do this, you must type GS -SDEvice = X11. Whether it is a parameter or a simple command GS (if you can start), you will create a blank window with a white background during a session. Due to the description of the display, we don't need this display window, you can remove it. To this end, we do not have a display window after the GOSTScript interpreter is started by adding parameters after the command line GS or GS -SDevice = X11. If this is done, after displaying a copyright declaration, GostScript prompt GS> will appear in the new line. At this time, GOSTScript can handle the user typed. At this point the operator stack is empty.

To view the contents of the operator, you can use the file operator - Pstack. It is called a file operator because it sends the information of the operator's stack to the standard output file that is the provision of the screen. If you type this command after the prompt, but does not show anything, and the prompt GS> starts in the new line, this is how the operator stack is empty.

To the elements to the operator stack, you can type this element after the prompt. For example, if you want to send an element 1, you can type 1 after the prompt. After that, the next line will appear new prompts. At this time, the prompt is not GS>, but GS <1>. This new prompt indicates the number of elements in the operating counting stack. Therefore, if a prompt GS <123> occurs during your GostScript session, it means that 123 elements in the operator count.

You can enter multiple elements in an operational number stack in an operation. To do this, you must type all the elements continuously and separate it with a space. For example, if you type 1 2 3 4 5 6 after the prompt, the elements 1, 2, 3, 4, 5, 6 will be pushed into the operating count stack. If the PSTACK operation is executed after this, the element will be displayed in a vertical order, and the final entry is first displayed. The dialogue process of the above two command lines is shown below:

GS> 1 2 3 4 5 6 GS <6> PSTACK654321GS <6> Improves elements in a single step operation and displaying stack content. All what you need to do is just add the pstack command behind the entered element, namely:

GS> 1 2 3 4 5 6 PSTACK654321GS <6> Until now, the elements we entered are numbers. The number is not the only type of elementally allowed, which can enter other types of elements such as variables or keycases, strings, data blocks, and the like. We will detailed in the back. However, now we must explain: If you want to enter as a single character A or a string ABC, an error message will appear. This is because PostScript can't understand these things. If you want to enter characters or strings, you must use () to surround it. Here we refer to a special type of element called MarkType. To this end, we give the following dialogue example: GS> 1 2 3 MarkType-321GS <7> Now let's see some operators for manipulating the operator. I will give a dialog example to explain how these operators act and end this section, not to prepare a further explanation.

GS> 1 2 3 mark 4 5 6 pstack654-marktype321GS <7> pop pstack54-marktype3 21GS <6> exch pstack45-marktype321GS <6> dup pstack445-marktype321GS <7> 2 copy pstack44445-marktype321GS <9> 5 index pstack-marktype-44445-marktype321GS <10> cleartomark cleartomark pstack321GS <3> 3 1 roll pstack213GS <3> count pstack3213GS <4> mark 7 8 pstack87-marktype-3213GS <7> counttomark pstack287-marktype-3213GS <8> clear Pstackgs>

Mathematical operator

In addition to manipulating the operator of the PostScript operator stack, there are some arithmetic and math operators. Here we give these operators, but there is no example. With the above dialogue example, the reader should be able to raise these examples.

Add: This command requires two numerical parameters involved in the additional operation. If these two values ​​are m and n, the command is indicated as m n add. When executed, M and N have entered the operating count stack, and the last step is to add two elements of the operand top. This operation generates a new element having a value of M and N. After the operation is completed, M and N are not retained in the operating count stack, and the result is the result of the opener of the operator stack.

Div: This command requires two numerical parameters involved in division of division. If these two values ​​are m and n, the command is indicated as M n DIV. Its execution mechanism is the same as addending. The division operation is performed on the arithmetic basis of floating point. After the operation is over, the result is left within the operating count stack as a new element, and M and N are not retained.

IDIV: This command requires two numerical parameters involved in integer division operations. If these two values ​​are m and n, the command is indicated as M n idiv. Other implementation mechanisms are the same as DIV in addition to the arithmetic operation of this division operation. This is an integer arithmetic division operation. If the parameter is not an integer, it will also perform it.

MOD: This command requires two integer parameters. It calculates the first parameter divided by the remainder obtained after the second parameter. If there is one of the parameters not an integer, the execution failed. After the execution, the resulting results were left in the operating count stack as a new element.

MUL: with add, div. It is a binary operator that requires two numeric parameters. The resulting results are the product of the parameters, and as a new element is kept in the operator stack.

SUB: with add, div, mul. The only difference is that the type of operation is. It subtracts the value of the second parameter from the first parameter. The parameters and results are numerical. After execution, the result is retained in the operator count. EXP: This is a binary mathematical operator that requires two parameters. The first is the base, the second is an index. It performs an index operation. The value of the parameter must be within the range allowed by the power function operation. The resulting result is a floating point number, and as a new element is retained within the operating counting stack.

Atan: This is another binary mathematical operator for seeking angular values. The resulting angle value is between 0 degrees and 360 degrees. It requires two parameters. The first parameter and the second parameter ratio represent the positive cut values ​​of the angle. Any parameter can be zero, but it cannot be zero. The symbol of the parameter determines the quadrant of the resulting result. The first parameter value is a positive y-axis status, accordingly, the second parameter is a positive x-axis pattern.

ABS: This is a one-dollar mathematical operator, only one parameter, the resulting result is its absolute value. Also, this result is kept as a new element in the operating count stack.

NEG: Change the symbol of its only parameters. This is a one-dollar arithmetic operator.

CEILING: This is a one-dimensional operator that is hiented to the parameters.

Floor: This is a one-dimensional operator that is fetched down the parameters.

Round: This is a one-dimensional operator that removes the most recent integers from the parameters.

Truncate: This is a one-dimensional operator that removes the fractional part of the parameters.

SQRT: This is a one-dimensional operator that calculates the square root of the parameters.

COS: This is a one-dimensional operator that calculates the cosine value of the parameters. The parameters are given in the form of angular values.

SIN: This is a one-dimensional operator that calculates the sine value of the parameters. The parameters are given in the form of angular values.

Ln: This is a one-dimensional operator, calculating the natural logar of parameters.

Log: This is a one-dimensional operator that calculates the value of the parameter at 10.

Before ending this article, there is a few need to point out. Although we have mentioned above, it may be indirectly, that is, the parameters of the command (called operand in PostScript term) may generate some troublesome issues. Command (in PostScript term is called an operator) seeks its parameters in the operating count stack. If found, they are used by commands and are removed from the operating count stack. Therefore, it is intentionally or unintentionally given a command that does not have the parameter value it needs in the operator stack. It is a mistake information about the type of stack top element and the required parameter type, which will delete the stack. Some elements of top. Users must notice this.

Before the end, we recommend that you want to use PostScript users to write some more complex and easy-to-understand programs. In this series, more details about PostScript language will be described. We are willing to accept any issues and suggestions about this article.

转载请注明原文地址:https://www.9cbs.com/read-44132.html

New Post(0)