{"id":48,"date":"2016-06-26T15:58:20","date_gmt":"2016-06-26T14:58:20","guid":{"rendered":"http:\/\/reldb.org\/wordpress\/?page_id=48"},"modified":"2016-06-26T15:58:20","modified_gmt":"2016-06-26T14:58:20","slug":"language-enhancements","status":"publish","type":"page","link":"https:\/\/reldb.org\/c\/index.php\/read\/language-enhancements\/","title":{"rendered":"Language Enhancements"},"content":{"rendered":"<h1>Language Enhancements<\/h1>\n<p>Note: The following enhancements to or deviations from <b>Tutorial D<\/b> should be considered experimental. Depending on their value (or lack thereof) they may or may not be included in future versions of <i>Rel<\/i>.<\/p>\n<p><b>Catalog:<\/b><i>Rel<\/i> provides a simple system catalog, via a predefined relvar called &#8220;sys.Catalog&#8221;. The contents of the Catalog, i.e., the relvars currently defined in a database, may be displayed by retrieving the contents of the sys.Catalog relvar. For example:<\/p>\n<blockquote>\n<pre><code>sys.Catalog\r\n<\/code><\/pre>\n<\/blockquote>\n<p>A relation of all defined relvar names can be obtained using:<\/p>\n<blockquote>\n<pre><code>sys.Catalog {Name}\r\n<\/code><\/pre>\n<\/blockquote>\n<p><b>Case sensitivity:<\/b><i>Rel<\/i> language keywords are not case sensitive. By convention, keywords are shown here in upper case to visually distinguish them, but the parser recognises them in lower case as well. <i>Rel<\/i> identifiers and string comparisons are case sensitive.<\/p>\n<p><b>OUTPUT, WRITE, and WRITELN statements:<\/b> Statements have been introduced to send raw text to the client. For example, the following:<\/p>\n<blockquote>\n<pre><code>VAR i INTEGER;\r\nDO i := 1 TO 3;\r\n\tOUTPUT i;\r\nEND;\r\n<\/code><\/pre>\n<\/blockquote>\n<p>Will send the following to the client:<\/p>\n<blockquote>\n<pre><code>1\r\n2\r\n3\r\n<\/code><\/pre>\n<\/blockquote>\n<p>OUTPUT, WRITE, and WRITELN each have slightly different behaviour. OUTPUT sends text in a form that can be parsed by <i>Rel<\/i>, followed by a new line. WRITELN sends text in a natural human-readable form, followed by a new line. WRITE is the same as WRITELN, but does not send a new line. In the current implementation, the only significant difference between OUTPUT and WRITELN is that OUTPUT will present a CHAR surrounded by double quotes, whereas WRITELN will not include the double quotes.<\/p>\n<p>WRITELN and OUTPUT may be specified with no expression, in order to send a new line. Eg.:<\/p>\n<blockquote>\n<pre><code>WRITELN;\r\n<\/code><\/pre>\n<\/blockquote>\n<p><b>DROP OPERATOR:<\/b> Since operators may be overloaded, an alteration to the specified grammar is required to disambiguate the desired operator. For example, the following operator definitions are allowable:<\/p>\n<blockquote>\n<pre><code>OPERATOR foo(x INTEGER, y INTEGER) RETURNS INTEGER;\r\n\tRETURN x + y;\r\nEND OPERATOR;\r\n\r\nOPERATOR foo(x INTEGER) RETURNS INTEGER;\r\n\tRETURN x + 2;\r\nEND OPERATOR;\r\n<\/code><\/pre>\n<\/blockquote>\n<p>Dropping the first operator is performed with the following:<\/p>\n<blockquote>\n<pre><code>DROP OPERATOR foo(INTEGER, INTEGER);\r\n<\/code><\/pre>\n<\/blockquote>\n<p>Dropping the second is performed with the following:<\/p>\n<blockquote>\n<pre><code>DROP OPERATOR foo(INTEGER);\r\n<\/code><\/pre>\n<\/blockquote>\n<p><b>Compile-time announcements:<\/b> A statement has been provided to output strings during compilation. This may be used to provide compilation progress indicators in lengthy scripts. Eg.:<\/p>\n<blockquote>\n<pre><code>ANNOUNCE 'Operator fibonacci (INTEGER) INTEGER being created...';\r\nOPERATOR fibonacci (r INTEGER) RETURNS INTEGER;\r\n   IF r &lt;= 1 THEN\r\n       RETURN r;\r\n   ELSE\r\n       RETURN fibonacci(r - 1) + fibonacci(r - 2);\r\n   END IF;\r\nEND OPERATOR;\r\n<\/code><\/pre>\n<\/blockquote>\n<p><b>Comments:<\/b> Comments are specified using conventional C++ and Java syntax. Eg:<\/p>\n<blockquote>\n<pre><code>\/\/ This is a comment line\r\n\r\n\/* This is a multi-line\r\n   comment block *\/\r\n<\/code><\/pre>\n<\/blockquote>\n<p><b>ORDER:<\/b> An ORDER operator has been implemented to accept a relation and return a row-ordered ARRAY. This is useful for generating human-readable output. For example:<\/p>\n<blockquote>\n<pre><code>WRITELN P ORDER (DESC WEIGHT, ASC P#);\r\n<\/code><\/pre>\n<\/blockquote>\n<p><b>ARRAY element dereference:<\/b> As defined in the <b>Tutorial D<\/b> grammar, ARRAY elements are dereferenced with &#8220;(&#8230;)&#8221;, e.g.:<\/p>\n<blockquote>\n<pre><code>VAR catalog ARRAY SAME_TYPE_AS(TUPLE FROM sys.Catalog {Name});\r\nLOAD catalog FROM sys.Catalog {Name} ORDER (ASC Name);\r\nOUTPUT catalog(3);\r\n<\/code><\/pre>\n<\/blockquote>\n<p>However, <i>Rel<\/i> defines ARRAY element dereference as &#8220;[&#8230;]&#8221;. The above will not work. Instead, use:<\/p>\n<blockquote>\n<pre><code>VAR catalog ARRAY SAME_TYPE_AS(TUPLE FROM sys.Catalog {Name});\r\nLOAD catalog FROM sys.Catalog {Name} ORDER (ASC Name);\r\nOUTPUT catalog[3];\r\n<\/code><\/pre>\n<\/blockquote>\n<p>This avoids ambiguity with operator invocation, and follows a familiar convention that users of C, C++, C#, Java, and a number of other popular general-purpose programming languages are likely to recognise.<\/p>\n<p><b>FOR:<\/b> The &#8216;FOR&#8217; statement permits iterating over the tuples in an ARRAY and executing arbitrary code. For example:<\/p>\n<blockquote>\n<pre><code>FOR sys.Catalog {Name} ORDER(ASC Name); \r\n    WRITELN \"Name = \" || Name;\r\nEND FOR;\r\n<\/code><\/pre>\n<\/blockquote>\n<p>The above example is shorthand for:<\/p>\n<blockquote>\n<pre><code>VAR catalog ARRAY SAME_TYPE_AS(TUPLE FROM sys.Catalog {Name});\r\nLOAD catalog FROM sys.Catalog {Name} ORDER (ASC Name);\r\nVAR i INTEGER;\r\nDO i := 0 TO COUNT(catalog) - 1;\r\n   BEGIN;\r\n       VAR Name INIT(Name FROM catalog[i]);\r\n       WRITELN \"Name = \" || Name;\r\n   END;\r\nEND DO;\r\n<\/code><\/pre>\n<\/blockquote>\n<p>FOR is typically used conjunction with ORDER (<i>see above<\/i>) to permit iterating tuples from a relational expression in a specified sequence, but if no particular sequence is required, ORDER may be specified as shown in the following example:<\/p>\n<blockquote>\n<pre><code>FOR sys.Catalog {Name} ORDER(); \r\n    WRITELN \"Name = \" || Name;\r\nEND FOR;\r\n<\/code><\/pre>\n<\/blockquote>\n<p><b>EXECUTE:<\/b> The EXECUTE statement permits run-time execution of dynamically-generated <b>Tutorial D<\/b> code. For example, the following uses EXECUTE to emit <b>Tutorial D<\/b>definitions of user-defined real relvars, by dynamically generating and executing an &#8216;OUTPUT&#8217; statement for each relvar using the relvar name obtained at run-time:<\/p>\n<blockquote>\n<pre><code>FOR sys.Catalog WHERE Owner &lt;&gt; \"Rel\" AND NOT isVirtual ORDER();\r\n   BEGIN;\r\n\tWRITELN \"VAR \" || Name || \" REAL \" || Definition || \";\";\r\n\tWRITE Name || \" := \";\r\n\tEXECUTE \"OUTPUT \" || Name || \";\";\r\n\tWRITELN \";\";\r\n   END;\r\nEND FOR;\r\n<\/code><\/pre>\n<\/blockquote>\n<p>Note that EXECUTE runs in the global scope, rather than the local scope in which it is used.<\/p>\n<p><b>User-defined OPERATORs written in Java:<\/b> OPERATORs may be defined in Java. See the Scripts directory of a <i>Rel<\/i> installation for examples.<\/p>\n<p><b>User-defined TYPEs written in Java:<\/b> New built-in TYPEs may be defined using Java within <i>Rel<\/i>. See the Scripts directory of a <i>Rel<\/i> installation for examples.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Language Enhancements Note: The following enhancements to or deviations from Tutorial D should be considered experimental. Depending on their value (or lack thereof) they may or may not be included in future versions of Rel. Catalog:Rel provides a simple system catalog, via a predefined relvar called &#8220;sys.Catalog&#8221;. The contents of the Catalog, i.e., the relvars <a href=\"https:\/\/reldb.org\/c\/index.php\/read\/language-enhancements\/\" rel=\"nofollow\"><span class=\"sr-only\">Read more about Language Enhancements<\/span>[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":17,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"class_list":["post-48","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/pages\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":1,"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/pages\/48\/revisions"}],"predecessor-version":[{"id":49,"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/pages\/48\/revisions\/49"}],"up":[{"embeddable":true,"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/pages\/17"}],"wp:attachment":[{"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/media?parent=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}