Charm (programming language)
Encyclopedia
Charm is a computer programming language
devised in the early 1990s with similarities to the RTL/2
, Pascal
and C
languages in addition to containing some unique features of its own. The Charm language is defined by a context-free grammar
amenable to being processed by recursive descent parser
as described in seminal books on compiler
design.
A set of Charm tools including a compiler, assembler and linker released for the Acorn market has been reviewed in Acorn User
magazine under the category of programming software.
along with descriptive examples of Charm constructs is defined on the Charm language page.
The language is block structured, with each block being introduced by a language keyword that is descriptive of the operation being performed in the block e.g. for, while, repeat (iteration), case (selection). Each block is ended by the same language keyword prefixed by end_ e.g. a while block is ended by end_while. This feature of the language is intended to ease comprehension. Additionally language lines within a block are normally indented for the same reason, though this not required as white space is ignored.
Each grammatically conforming text (with module being the start symbol of the grammar) represents a collection of executable code and associated data which can be used by a Charm tool set as a component when assembling a program that can be run under an operating system utilising the services it provides to do useful work such as data processing or interacting with users through a Graphical user interface
.
Data aggregates of the same type may be declared and statically initialised using the array keyword, and these may be multidimensional. Aggregates of different types may be declared using the record keyword, and it is allowable for such a declaration to define a union of record fields that overlay each other in terms of storage allocation.
References to data constructs and procedures may be made using the ref keyword. These can be dereferenced using the val keyword. When using reference variables, comparison operators are available to check whether two reference variables refer to the same item of data ( :=: ) or whether the data they point to is the same ( = ).
written in Charm is:
ext proc write_string (ref array char);
module hello;
ent proc start ;
write_string ("Hello world");
end_proc;
end_module;
At the time of writing only one Charm tool set installation is available (free of charge) for download. The tools are themselves written in the Charm language, and the source code is available under the terms of the GNU General Public License
. They run on RISC OS
PCs with ARM CPUs and on emulators for RISC OS which are hosted on Windows or Linux platforms (such as RPCEmu).
assembly language, and the other generating ARM Architecture
.
The quadruple output from the hello world example is:
param l1$
call write_string[proc (ref array char) void]
and the assembler output is:
string "hello"
xdef _start
align
_start
xref _write_string
stmfd sp!,{rp}
adr r0,_l1$
bl _write_string
ldmfd sp!,{pc}
address
align
_l1$
string "Hello world"
direct
end
Note that in more recent releases of Charm, the I/O procedures have been organised along with other standard library procedures into a set of globally available singleton record trees with record references as branches and procedures as leaves. As part of this reorganisation, the write_string method is now invoked through the root run time library record rtl via record reference .out as procedure str i.e. in the hello world example above write_string ("Hello world") becomes rtl.out.str ("Hello world").
Assembler listing of @.arm.hello
1:0000:6D795F6D string "hello"
2: xdef _start
3: align
4: _start
5: xref _write_string
6:0000:E92D4000 stmfd sp!,{rp}
7:0004: adr r0,_l1$
8:000C:EBFFFFFE bl _write_string
9:0010:E8BD8000 ldmfd sp!,{pc}
10: address
11: align
12: _l1$
13:0000:48656C6C string "Hello world"
14: direct
15: end
ext proc start (int |argc|, ref array ref array char |argv|);
This is analogous to the main function in the C
and Java
languages. Here |argc| contains the number of parameters passed on the command line and |argv| contains a reference to an array of |argc| + 1 strings (one string per positional parameter in order and a terminating nil).
The linker adds any necessary header information required by the operating system in order to execute the program, and ensures the run time library assembler support code is run which sets up the run time environment (data and stack pointers) and passes control to the start procedure of the application.
A map file showing the names of all modules linked to form the program along with global data and code references is optionally produced which can be used by debuggers and profilers.
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
devised in the early 1990s with similarities to the RTL/2
RTL/2
RTL/2 was a high-level programming language developed at Imperial Chemical Industries Ltd by J.G.P. Barnes. It was originally used internally within ICI but was distributed by SPL International in 1974...
, Pascal
Pascal (programming language)
Pascal is an influential imperative and procedural programming language, designed in 1968/9 and published in 1970 by Niklaus Wirth as a small and efficient language intended to encourage good programming practices using structured programming and data structuring.A derivative known as Object Pascal...
and C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
languages in addition to containing some unique features of its own. The Charm language is defined by a context-free grammar
Context-free grammar
In formal language theory, a context-free grammar is a formal grammar in which every production rule is of the formwhere V is a single nonterminal symbol, and w is a string of terminals and/or nonterminals ....
amenable to being processed by recursive descent parser
Recursive descent parser
A recursive descent parser is a top-down parser built from a set of mutually-recursive procedures where each such procedure usually implements one of the production rules of the grammar...
as described in seminal books on compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...
design.
A set of Charm tools including a compiler, assembler and linker released for the Acorn market has been reviewed in Acorn User
Acorn User
Acorn User magazine was founded by Acorn Computers in 1982, contract-published by Addison-Wesley, to coincide with the launch of the BBC Micro. It covered the range of Acorn home computers, the BBC Micro and Atom at first and later the Electron, Archimedes and Risc PC.The first issue was dated...
magazine under the category of programming software.
Grammar
The definition of the Charm grammar in Backus–Naur FormBackus–Naur form
In computer science, BNF is a notation technique for context-free grammars, often used to describe the syntax of languages used in computing, such as computer programming languages, document formats, instruction sets and communication protocols.It is applied wherever exact descriptions of...
along with descriptive examples of Charm constructs is defined on the Charm language page.
The language is block structured, with each block being introduced by a language keyword that is descriptive of the operation being performed in the block e.g. for, while, repeat (iteration), case (selection). Each block is ended by the same language keyword prefixed by end_ e.g. a while block is ended by end_while. This feature of the language is intended to ease comprehension. Additionally language lines within a block are normally indented for the same reason, though this not required as white space is ignored.
Each grammatically conforming text (with module being the start symbol of the grammar) represents a collection of executable code and associated data which can be used by a Charm tool set as a component when assembling a program that can be run under an operating system utilising the services it provides to do useful work such as data processing or interacting with users through a Graphical user interface
Graphical user interface
In computing, a graphical user interface is a type of user interface that allows users to interact with electronic devices with images rather than text commands. GUIs can be used in computers, hand-held devices such as MP3 players, portable media players or gaming devices, household appliances and...
.
Data types
Charm is a strongly typed language, but does allow some implicit conversions between numeric and floating point types. The following basic variable types are supported:- int - integers
- char - characters
- boolean - boolean values (true or false)
- real - floating point numbers
Data aggregates of the same type may be declared and statically initialised using the array keyword, and these may be multidimensional. Aggregates of different types may be declared using the record keyword, and it is allowable for such a declaration to define a union of record fields that overlay each other in terms of storage allocation.
Referencing
Data or procedures within the scope of a module may be made global to the final application by using the ent keyword. If a module wishes to reference a procedure or data from another Charm module, it does so using the ext keyword.References to data constructs and procedures may be made using the ref keyword. These can be dereferenced using the val keyword. When using reference variables, comparison operators are available to check whether two reference variables refer to the same item of data ( :=: ) or whether the data they point to is the same ( = ).
Example
The classic Hello world programHello world program
A "Hello world" program is a computer program that outputs "Hello world" on a display device. Because it is typically one of the simplest programs possible in most programming languages, it is by tradition often used to illustrate to beginners the most basic syntax of a programming language, or to...
written in Charm is:
ext proc write_string (ref array char);
module hello;
ent proc start ;
write_string ("Hello world");
end_proc;
end_module;
Tool Set
Tool set implementations are expected to provide a compiler and an assembler to generate object files from Charm source code and assembler source code, which can then be linked together along with library and run time support files to generate an executable program.At the time of writing only one Charm tool set installation is available (free of charge) for download. The tools are themselves written in the Charm language, and the source code is available under the terms of the GNU General Public License
GNU General Public License
The GNU General Public License is the most widely used free software license, originally written by Richard Stallman for the GNU Project....
. They run on RISC OS
RISC OS
RISC OS is a computer operating system originally developed by Acorn Computers Ltd in Cambridge, England for their range of desktop computers, based on their own ARM architecture. First released in 1987, under the name Arthur, the subsequent iteration was renamed as in 1988...
PCs with ARM CPUs and on emulators for RISC OS which are hosted on Windows or Linux platforms (such as RPCEmu).
Compiler
The Charm compiler is a recursive descent single pass compiler which parses Charm source code to generate quadruples of the form result := lhs op rhs in an intermediate language that supports arithmetic, logical and flow of control operations. Data is stored in temporaries which are assigned to registers and memory locations in the back end of the compiler. Two back ends are currently in existence, one generating Motorola 68000Motorola 68000
The Motorola 68000 is a 16/32-bit CISC microprocessor core designed and marketed by Freescale Semiconductor...
assembly language, and the other generating ARM Architecture
ARM architecture
ARM is a 32-bit reduced instruction set computer instruction set architecture developed by ARM Holdings. It was named the Advanced RISC Machine, and before that, the Acorn RISC Machine. The ARM architecture is the most widely used 32-bit ISA in numbers produced...
.
The quadruple output from the hello world example is:
param l1$
call write_string[proc (ref array char) void]
and the assembler output is:
string "hello"
xdef _start
align
_start
xref _write_string
stmfd sp!,{rp}
adr r0,_l1$
bl _write_string
ldmfd sp!,{pc}
address
align
_l1$
string "Hello world"
direct
end
Note that in more recent releases of Charm, the I/O procedures have been organised along with other standard library procedures into a set of globally available singleton record trees with record references as branches and procedures as leaves. As part of this reorganisation, the write_string method is now invoked through the root run time library record rtl via record reference .out as procedure str i.e. in the hello world example above write_string ("Hello world") becomes rtl.out.str ("Hello world").
Assembler
The assembler accepts instruction mnemonics, data declarations and directives and constructs an object file containing information readily understandable by the CPU of the target processor, in particular code instructions coded in binary.Assembler listing of @.arm.hello
1:0000:6D795F6D string "hello"
2: xdef _start
3: align
4: _start
5: xref _write_string
6:0000:E92D4000 stmfd sp!,{rp}
7:0004: adr r0,_l1$
8:000C:EBFFFFFE bl _write_string
9:0010:E8BD8000 ldmfd sp!,{pc}
10: address
11: align
12: _l1$
13:0000:48656C6C string "Hello world"
14: direct
15: end
Linker
One and only one of the Charm modules linked to form an executable program must contain a procedure matching the external signature:ext proc start (int |argc|, ref array ref array char |argv|);
This is analogous to the main function in the C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
and Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
languages. Here |argc| contains the number of parameters passed on the command line and |argv| contains a reference to an array of |argc| + 1 strings (one string per positional parameter in order and a terminating nil).
The linker adds any necessary header information required by the operating system in order to execute the program, and ensures the run time library assembler support code is run which sets up the run time environment (data and stack pointers) and passes control to the start procedure of the application.
A map file showing the names of all modules linked to form the program along with global data and code references is optionally produced which can be used by debuggers and profilers.