Include a file: Difference between revisions

Content added Content deleted
(Applesoft BASIC)
(→‎{{header|ALGOL 68}}: add example Algol68 code)
Line 16: Line 16:
-- The use-clause allows the program author to write a subprogram call shortly as
-- The use-clause allows the program author to write a subprogram call shortly as
-- Put_Line("some text");</lang>
-- Put_Line("some text");</lang>
=={{header|ALGOL 68}}==
The formal definition of Algol68 make numerous references to the standard '''prelude''' and '''postlude'''.

At the time the language was formally defined it was typical for code to be stored on decks of [[wp:Punched card|punched cards]] (or [[wp:Punched tape|paper tape]]). Possibly because storing code on [[wp:Hard disk drive|disk]] (or [[wp:Drum memory|drum]]) was expensive. Similarly it was better that card could be read sequentially from ''just'' one [[wp:Punched card input/output|card reader]]. It appears the Algol68 "standard" assumed all cards could be simply stacked before and after the actual ''source code'', hence the references "prelude" and "postlude" in the formal standard.
==={{header|ALGOL 68G}}===
In the simplest case a file can be included as follows:
<lang algol68>PR read "file.a68" PR</lang>

But in Algol68 formal report - it appears - the intention was to have a more structure approach.
{{works with|ALGOL 68|Revision 1 - one extension to language used - PRAGMA READ - a non standard feature similar to C's #include directive.}}
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.7 algol68g-2.7].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
'''File: prelude/test.a68'''<lang algol68># -*- coding: utf-8 -*- #
BEGIN
# Exception setup code: #
on value error(stand out, (REF FILE f)BOOL: GOTO value error not mended);
# Block setup code: #
printf(($"Prelude test:"l$))</lang>'''File: postlude/test.a68'''<lang algol68># -*- coding: utf-8 -*- #
# Block teardown code: #
printf(($"Postlude test."l$))
EXIT
# Exception code: #
value error not mended: SKIP
END</lang>'''File: test/include.a68'''<lang algol68>#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #

PR read "prelude/test.a68" PR;
printf($4x"Hello, world!"l$);
PR read "postlude/test.a68" PR</lang>'''Output:'''
<pre>
Prelude test:
Hello, world!
Postlude test.
</pre>

'''Other implementations: e.g. [[ALGOL 68RS]] and [[ALGOL 68G]]'''<br>
Note that actual ''source code'' inclusion with parsing can avoided because of a more generalised separate compilation method storing declaration specifications in a ''[[wp:data dictionary|data dictionary]]'. Different to '''#include''' found in the [[C]] where the include file needs to be parsed for each source file that includes it.

==={{header|ALGOL 68RS}}===

This British implementation of the language has various ways to include it's own source code and and integrate with code compiled from other languages.<br>
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d].}}
* [[wp:ALGOL 68RS#Separate compilation|Separate compilation]]
* [[wp:ALGOL 68RS#Declaration modules|Declaration modules]]
* [[wp:ALGOL 68RS#Nested modules|Nested modules]]
* [[wp:ALGOL 68RS#Code and Alien access|Code and Alien access]]

In order to support a top-down programming style ALGOL 68RS provided the '''here''' and '''context''' facilities.

A program could be written with parts to be filled in later marked by a '''here''' tag followed by a ''keeplist'' of declarations to be made available.

'''program''' (pass1, pass2) compiler
'''begin'''
'''string''' source := ...;
'''tree''' parsetree;
...
'''here''' pass1 (source, parsetree);
...
'''instructions''' insts;
'''here''' pass2 (parsetree, insts);
...
'''end'''
'''finish'''

The code to be executed in the context of the '''here''' tags would be written as:

'''program''' pass1 implementation
'''context''' pass1 '''in''' compiler
'''begin'''
... { code using "source" and "parsetree" }
'''end'''
'''finish'''

'''here''' is similar to the ALGOL 68C '''environ''' and '''context''' is equivalent to the ALGOL 68C '''using'''.

==={{header|ALGOL 68C}}===

Separate compilation in ALGOL 68C is done using the ENVIRON and USING clauses. The ENVIRON saves the complete environment at the point it appears. A separate module written starting with a USING clause is effectively inserted into the first module at the point the ENVIRON clause appears.
* [[wp:ALGOL 68C#The ENVIRON and USING clauses|The ENVIRON and USING clauses]]

''' Example of <code>ENVIRON</code> clause '''<br>
A file called ''mylib.a68'':
<lang algol68>BEGIN
INT dim = 3; # a constant #
INT a number := 120; # a variable #
ENVIRON EXAMPLE1;
MODE MATRIX = [dim, dim]REAL; # a type definition #
MATRIX m1;
a number := ENVIRON EXAMPLE2;
print((a number))
END</lang>

''' Example of <code>USING</code> clause '''<br>
A file called ''usemylib.a68'':
<lang algol68>USING EXAMPLE2 FROM "mylib"
BEGIN
MATRIX m2; # example only #
print((a number)); # declared in mylib.a68 #
print((2 UPB m1)); # also declared in mylib.a68 #
ENVIRON EXAMPLE3; # ENVIRONs can be nested #
666
END</lang>


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==