# Differences

This shows you the differences between two versions of the page.

Both sides previous revision Previous revision | |||

about:core [2016/11/09 12:50] sbs |
about:core [2016/11/09 12:56] (current) sbs |
||
---|---|---|---|

Line 6: | Line 6: | ||

These requirements rule out global variables, pointers, and, hence, all kinds of data structures which rely on pointers. The control flow instructions break, continue, and goto must be dropped as well, but for the remaining language constructs found in C, transformation rules into an applied Lambda-calculus can actually be defined more or less straightforwardly. | These requirements rule out global variables, pointers, and, hence, all kinds of data structures which rely on pointers. The control flow instructions break, continue, and goto must be dropped as well, but for the remaining language constructs found in C, transformation rules into an applied Lambda-calculus can actually be defined more or less straightforwardly. | ||

- | The syntax of programs is outlined here: | + | SaC programs are sequences of top-level type and function definitions with a specific function main serving as the designated starting point for program execution. Function headers are almost identical to ANSI-C, the sole difference being that supports functions with multiple return values, similar to ID or SISAL . Hence, function definitions may start with sequences of return types, separated by commas, instead of a single return type only. The set of basic types is restricted to the four scalar data types known from C, which represent integer numbers, single and double precision floating point numbers and characters. In contrast to C, SaC explicitly distinguishes between integer and boolean values. Hence, a dedicated data type bool is added to the set of basic types. Furthermore, SAC supports function overloading , i.e., functions may share the same name as long as they differ with respect to the types of their formal parameters. The ordering of function definitions is irrelevant, i.e., functions may be applied before they are defined, thus making obsolete forward declarations of function prototypes. |

- | Program => [ TypeDef ]* [ FunDef ]* Main [ FunDef ]* | + | Function bodies consist of variable declarations, assignments, and a trailing return-statement. Local variable declarations are optional in SAC; they may be omitted. An assignment is either a simple let-statement or a compound assignment. In the presence of functions with multiple return values, let-statements may in one conceptual step bind multiple variables to values. A compound assignment is either a conditional or one of the three loop constructs known from C. Branches of conditionals as well as loop bodies may either be single assignments or sequences of multiple assignments delimited by curly brackets. However, such local blocks may not contain additional variable declarations, i.e., there are no local scopes in other than function bodies. The return-statement may contain a sequence of expressions, separated by commas, rather than a single one only, to support functions with multiple return values. |

- | TypeDef => typdef Type Id | + | |

- | FunDef => RetTypes Id ( [Parameters] ) FunBlock | + | |

- | RetTypes => Type [ , Type ]* | + | |

- | Parameters => Type Id [ , Type Id ]* | + | |

- | Main => int main ( ) FunBlock | + | |

- | Type => Id | + | |

- | | int | float | double | char | bool | + | |

- | | + | |

- | They are sequences of top-level type and function definitions with a specific function main serving as the designated starting point for program execution. Function headers are almost identical to ANSI-C, the sole difference being that supports functions with multiple return values, similar to ID or SISAL . Hence, function definitions may start with sequences of return types, separated by commas, instead of a single return type only. The set of basic types is restricted to the four scalar data types known from C, which represent integer numbers, single and double precision floating point numbers and characters. In contrast to C,SAC explicitly distinguishes between integer and boolean values. Hence, a dedicated data type bool is added to the set of basic types. Furthermore, SAC supports function overloading , i.e., functions may share the same name as long as they differ with respect to the types of their formal parameters. The ordering of function definitions is irrelevant, i.e., functions may be applied before they are defined, thus making obsolete forward declarations of function prototypes. | + | |

- | | + | |

- | FunBlock => { [Vardec]* [Assign]* Return } | + | |

- | Vardec => Type Id ; | + | |

- | Assign => Let ; | + | |

- | | if (Expr) AssignBlock [ else AssignBlock ] | + | |

- | | do AssignBlock while ( Expr ) ; | + | |

- | | while ( Expr ) AssignBlock | + | |

- | | for ( Let ; Expr ; Let ) AssignBlock | + | |

- | Let => Id [ , Id ]* AssignOp Expr | + | |

- | AssignOp => = | += | -= | *= | /= | %= | + | |

- | AssignBlock => Assign | + | |

- | | {[Assign]*} | + | |

- | Return | return ( Expr [ , Expr ]*) ; | + | |

- | As shown in the table, a function body consists of variable declarations, assignments, and a trailing return-statement. Local variable declarations are optional in SAC; they may be omitted. An assignment is either a simple let-statement or a compound assignment. In the presence of functions with multiple return values, let-statements may in one conceptual step bind multiple variables to values. A compound assignment is either a conditional or one of the three loop constructs known from C. Branches of conditionals as well as loop bodies may either be single assignments or sequences of multiple assignments delimited by curly brackets. However, such local blocks may not contain additional variable declarations, i.e., there are no local scopes in other than function bodies. The return-statement may contain a sequence of expressions, separated by commas, rather than a single one only, to support functions with multiple return values. | + | |

The mapping of SAC function definitions to an applied Lambda-calculus is rather straightforward. A sequence of let-statements is mapped to nestings of let-blocks with the trailing return-statement defining the goal expression of the entire term. An if-clause is interpreted as a functional conditional with the continuation code being copied into both branches. Last but not least, loops are considered syntactic sugar for equivalent tail-end recursive functions. | The mapping of SAC function definitions to an applied Lambda-calculus is rather straightforward. A sequence of let-statements is mapped to nestings of let-blocks with the trailing return-statement defining the goal expression of the entire term. An if-clause is interpreted as a functional conditional with the continuation code being copied into both branches. Last but not least, loops are considered syntactic sugar for equivalent tail-end recursive functions. | ||

- | The following table gives an overview of SAC expressions, which may be constants, variable identifiers, or applications of built-in operators, primitive functions, or defined functions. SAC supports the usual arithmetic, relational, and boolean operators just as C. The usual precedence rules apply, but expressions may be grouped by round brackets. | + | To a large extent SAC expressions are almost identical to those available in C. Expressions can be constants, variable identifiers, or applications of built-in operators, primitive functions, or defined functions. SAC supports the usual arithmetic, relational, and boolean operators just as C does. The usual precedence rules apply, but expressions may be grouped by round brackets. |

- | Expr => Const | Id | ||

- | | (Expr) | ||

- | | MonOp Expr | ||

- | | Expr BinOp Expr | ||

- | | Prf ( [Expr [ , Expr ]* ]) | ||

- | | Id ( [Expr [ , Expr ]* ]) | ||

- | MonOp => ! | ||

- | BinOp => + | - | * | / | % | ||

- | | == | != | < | <= | > | >= | ||

- | | && | || | ||

- | Prf => toi | tof | tod | min | max | ||

The primitive functions toi, tof, and tod allow for explicit conversion between the three numerical data types int, float, and double; there is no implicit conversion as in C. Additionally, SAC provides built-in minimum and maximum functions. | The primitive functions toi, tof, and tod allow for explicit conversion between the three numerical data types int, float, and double; there is no implicit conversion as in C. Additionally, SAC provides built-in minimum and maximum functions. | ||

- | This relatively small and simple language kernel allows for the specification of clear and concise purely functional programs. Yet, their functional semantics, as defined by mapping them into an applied Lambda-calculus, exactly coincides with their usual imperative interpretation. This is illustrated in the following example program, which computes the greatest common denominator of two non-negative numbers, following the Euclidian algorithm. Rather than using the built-in modulo operator %, the function diffrest computes both the quotient and the remainder of its two arguments to illustrate the use of functions featuring multiple return values. | + | This relatively small and simple language kernel allows for the specification of clear and concise purely functional programs. Yet, their functional semantics, as defined by mapping them into an applied Lambda-calculus, coincides exactly with their usual imperative interpretation. This is illustrated in the following example program, which computes the greatest common denominator of two non-negative numbers, following the Euclidian algorithm. Rather than using the built-in modulo operator %, the function diffrest computes both the quotient and the remainder of its two arguments to illustrate the use of functions featuring multiple return values. |

+ | <code c> | ||

int gcd( int high, int low) | int gcd( int high, int low) | ||

{ | { | ||

Line 80: | Line 47: | ||

return( gcd( 22, 27)); | return( gcd( 22, 27)); | ||

} | } | ||

+ | </code> |