Wednesday, April 21, 2010

Translator – Data Types and Internal Functions

The internal functions have a fixed number of arguments, though some functions have multiple forms with different number of arguments (MID$, INSTR and ASC). Each argument has a specific data type expected, however, integer and double data types are interchangeable as the necessary conversion will be perform like for operators.

Most of the math functions (e.g. INT, SQR, LOG, COS, etc.) have one argument, which is expected to be a double. These functions return a double. Only one code is required for these functions. If the argument is an integer, then a hidden CvtDbl code will be inserted after the integer operand. Here are some RPN examples of math functions:
A Sqr
B% CvtDbl Log
A few of the math functions (ABS and SGN), will return the same type as there operand. Two codes are required for these functions: Abs, AbsInt, Sgn, and SgnInt. The reason for having two codes is so that these functions can be used in integer expressions without any wasteful conversions to and from double precision. This is not necessary for the other math function because they will be calculated in double precision internally.

The conversion functions (CDBL and CINT) return the opposite date type as their operand. These functions work the same is the hidden CvtDbl and CvtInt codes. It would appear that these functions are unnecessary since the hidden conversion codes will be inserted as needed. However, they may be reasons where they are necessary like in function and subroutine calls.

The string functions deal with the string data type (implementation will be delayed along with the string data). Some string functions take an integer operand (CHR$ and SPACE$) or double operand (STR$) and produce a string, some take a string operand and produce an integer (ASC and LEN) or double (VAL), and some take both string and integer operands and produce a string (LEFT$, MID$, RIGHT$, and REPEAT$) or an integer (ASC and INSTR).

Next, the similarity between operators and internal functions...