Please enable JavaScript to view this site.

ASQL Reference

Navigation: » No topics above this level «

A-Shell BASIC

Scroll Prev Top Next More

A-Shell BASIC, normally referred to as "ASB," is the programming language used for developing applications in A-Shell. A super set of the original AlphaBASIC, which was introduced in 1976, it shares many features with other BASIC-like languages, while adding a number of features particularly useful for developing vertical market applications in Windows and Unix environments. These include a mechanism for precise memory layout of structures (MAP Statements), various integrated, cross-platform, file access methods, and the ability to call external routines written in C or ASB. It is partially compiled to p-code, which is then executed within the A-Shell runtime environment.

The following sections attempt to provide the necessary details so that both newcomers and long-time A-Shell developers can get the most out of ASB.

u Naming Programs

Like all file names, ASB programs consist of two parts: a base name or stem, and an extension.

The base name of your program can be up to ten characters.

The extension normally identifies the type of program file, with the two major type categories being source files and compiled programs.

Source Files

The extension of your source code files can be anything you want, such as .PPP for Perry's Perilous Programs; A-Shell doesn't care. When you compile a program using A-Shell's COMPIL command, it will work on any filename you specify, such as confused2.ppp, 222much.abc, 1234567890.bas, etc.

By convention, source files are named with extensions of either BAS or BP, with the latter being recommended.

BAS is the historical extension, and has been used as the extension for BASIC programs of all types and on all platforms for many years. In the case of A-Shell, BAS always has the benefit of being automatically recognized by COMPIL. To compile a program with the extension BAS, you do not need to specify the extension; the compiler knows to look for a BAS file with the stem name you have specified. So entering "COMPIL ABC" will result in the file abc.bas being compiled, whereas the same command results in the program source file ABC.XYZ being ignored.

The other common extension is BP, and there are a few good reasons to use it instead of BAS.

Many email programs will not allow the sending of BAS files, recognizing them as programs. So if you ever need to send program source files via email, you should not use BAS extensions.
BAS is also the default source file name for Microsoft's Visual Basic programs. Since it's easier for you to rename your programs than to get Microsoft to rename its programs, not using BAS is a good idea just to avoid confusion in the Windows world.
BP can serve as an indicator that this program (a) was written in and for the A-Shell environment, and (b) contains A-Shell extensions which require the /X:2 switch when compiling.
When compiling using the /X:2 switch (use A-Shell extensions), the compiler will find and act on BP files as well as BAS files without you having to specify the extension. If you have a file abc.bp, for example, the statement COMPIL ABC/X:2 will result in abc.bp being compiled.

Compiled Programs

Compiled programs, sometimes referred to as "RUN files" or "executables," even though the latter is somewhat misleading since they need to be processed by a run-time interpreter and are thus not truly executable, must have one of the following file extensions:

RUN: standard programs, to be executed with the RUN command.
SBX: external, dynamically loadable subroutines; see Calling External Routines.
LIT: commands, to be executed without the RUN command.

See Edit, Compile, Run for more details on the three forms of compiled programs.

 

u Program Structure

The structure of an ASB program is rather loose, consisting of one or more lines satisfying the following syntax:

{line #}{label:}{statement(s)} {! comment}

Throughout this document, squiggly brackets {  } indicate optional elements, so each of the four parts of the typical program line above is optional.

Line numbers served two purposes in early versions of BASIC: as a target for GOTO or GOSUB / CALL statements, and as a way of identifying locations of runtime errors. Although they remain supported, their use is generally deprecated now in favor of labels, which are themselves typically limited to places where they are needed to identify the target of a GOTO or GOSUB. Some programmers insert labels as a kind of road map (e.g. BEGIN: or MAIN'BODY: ) but a label without any references to it should probably be replaced by a comment, just to eliminate any doubt as to whether the location could be the target of a GOTO statement.

Statements are terminated implicitly by the end of the source text line (LF or CRLF), or explicitly by the comment character "!".  To extend a logical statement over multiple source text lines, use the "&" character to indication continuation, e.g.

statement'syntax$ = lineno$      &

                    + label$     &  ! optional comment may follow "&"

                    + statement$    ! another comment

 

As shown in the example above, comments may follow the & continuation character without interfering with the continuation. However, be careful about ending a comment with the & continuation character, since it effectively extends the comment to the next line. For example, in the following, the "print b" statement will become part of the comment at the end of the previous line, due to it ending in & ...

print a    ! don't end a comment with &

print b    ! else following line (this one) will be part of comment

 

Multiple statements may also be combined on a single source text line by separating them with a colon, e.g.

if SWAP then &

    V1 = V1+V2 : V2 = V1-V2 : V1 = V1-V2    ! 3 statements on a line

 

Note that while the above form is typical in older code, the style—if without endif, continuation, multiple statements per line—like the archaic verb "LET", is generally deprecated in favor of the more readable:

if SWAP then

    V1 = V1 + V2

    V2 = V1 - V2

    V1 = V1 - V2

endif

 

Statements fall into the following categories:

Declaration of Variables

See Also

 

u Flow of Control

ASB program execution starts at the first executable statement in the program as defined by the source code. This is unlike some languages, such as C, which start executing at a specially named routine. The flow proceeds forward, or as directed by explicit control statements, until it hits an explicit END or CHAIN statement or simply comes to the end of the program.

User defined functions and procedures, however, are ignored except when explicitly called. In other words, the program will never "fall into" a function or procedure merely by virtue of its position. This is not true of GOSUB or error trap routines, which are not automatically protected from being fallen into.

Note that MAP Statements are considered executable, so the program needs to flow through them in order for them to get properly initialized.

See Also

 

u Sample Program I

Here is the canonical Hello World program in ASB:

PRINT "Hello World"

END

 

The program must exist first as a text file, typically with a BAS or BP extension, created in any text editor such as VUE or APN. Assuming the file is assigned the name hello.bas, it can be compiled from the A-Shell dot prompt as follows:

.COMPIL HELLO.BAS

 

The compiler responds:

Phase 1 - Parse source and generate object code

Phase 2 - Adjust object file and process errors

Memory usage:

Total work space - 18600 bytes

Label symbol tree - 16 bytes

Variable symbol tree - 20 bytes

Declaration tree - 16 bytes

Structure symbol tree - 16 bytes

Data statement pool - 0 bytes

Variable indexing area - 0 bytes

Compiler work stack - 16 bytes

Excess available memory - 1911620 bytes

End of compilation

 

The compiled program will typically have a .RUN extension, can be seen and manipulated with file commands such as DIR, SIZE, COPY, etc. (see LIT Commands), and can be executed with the RUN command. For example:

.DIR HELLO

HELLO      BAS  1

HELLO      RUN  1

Total of 2 files in 2 blocks.

 

.RUN HELLO

Hello World

.

 

u Sample Program II

Here is a slightly larger sample program that provides a better sense of the typical ASB program structure and illustrates a few features of the language, including:

PROGRAM assigns a version to the RUN module (visible with DIR/V)
! (exclamanation point) starts a comment up thru the end of the line
? (question mark) is an alias for PRINT, outputting text to the screen or a stream file
; (semi-colon) at end of print statement stifles CRLF output.
++INCLUDE reads in source code from external file, like #include in C
MAP Statements define variables in a fixed layout
Dynamic arrays (DIMX) allocate a dynamic array
DEFINE statements (symbol definitions)
Error trapping (ON ERROR GOTO)
IF … ENDIF conditional statements

 

program factor,1.0(100)  ! factorization example

!-------------------------------------------------------

!Edit History

![100] April 05, 2014 12:51 PM          Edited by jack

!   created as example

!-------------------------------------------------------

 

on error goto TRAP

 

++include ashinc:types.def          ! extended data type defs (e.g. BOOLEAN)

 

define TRUE = -1

define FALSE = 0

 

map1 params

    map2 nf,i,4                     ! the number to factor

    map2 nx,i,4                     ! working value

    map2 i,i,4                      ! loop counter

    map2 count,i,2                  ! # of factors found

 

dimx factors(1),i,4, auto_extend    ! array of factors

 

    input "Enter number to factor: ",nf

 

    nx = nf

    i = 2                           ! first factor to consider

 

    do while i <= nx

        if fn'is'factor(i,nx) then

            nx /= i

            count += 1

            factors(count) = i

            ? "found ";i;" target now ";nx

            repeat

        endif

        i += 1

    loop

    

    ? "Factors of ";nf;": ";

 

    if count then   

        for i = 1 to count

            if i > 1 ? ",";

            ? factors(i);

        next i

    elseif nf = 1 then

        ? 1                         ! only factor of 1 is 1

    else

        ? "none found" 

    endif

    ?

    end

 

TRAP:

    ? "Error #";err(0)

    end

 

!---------------------------------------------------------

! return TRUE if candidate is a factor of target

!---------------------------------------------------------

function fn'is'factor(candidate as i4, target as i4) as BOOLEAN

    if (target mod candidate) = 0 then

        fn'is'factor = TRUE

    endif

endfunction