Jargon Language Description
The Jargon Programming Language v0.3 20030120 <> 20030915by Joe Wingbermuehle
- Background
- Basic Syntax
- Literals
- Identifiers
- Special Characters
- Primary Types
- Operators
- Conditional Constructs
- Looping Constructs
- Arrays
- Enumerations
- Modules
- Handlers
- Objects
- Attributes
- Concurrency
- Exceptions
- Miscellaneous Language Features
- Features To Be Realized
Background
Jargon is a message based programming language. Execution of a Jargon program begins in the "Main" module in the "main" handler, which takes an array of strings as input. Variables and modules must be declared before use. However, handlers may be declared at any point within the module in which they reside. Jargon supports single inheritance of modules and overloaded message handlers.
Basic Syntax
Jargon is case sensitive (all keywords are lowercase). The character sequence "--" denotes a comment. The comment remains in effect until the end of the line. The ';' character denotes the end of a statement. Statements may span multiple lines. Note that Jargon defines a statement to be a smaller entity than most languages. In Jargon, a statement cannot contain another statement. This makes parsing the language more straight-forward and allows better error detection and recovery.
Literals
There are five types of literals: boolean, character, floating point, integral, and string. A boolean literal is either "true" or "false". A character literal is surrounded by single quotes '''. A floating point literal contains numbers and a decimal point '.'. An integral literal contains numbers. Finally, a string is surrounded by double quotes '"'. Within both character and string literals, special characters may be inserted by means of an escape sequence. Strings are null-terminated. All numbers are assumed to be in base 10 unless a suffix is applied. Possible suffixes are 'o' for octal, 'h' for hexidecimal, and 'b' for binary ('d' may be used for decimal for clarity, but is not necessary nor recommended in most cases).
Identifiers
Identifiers are case sensitive and may be of any length >= 1. The first character must be alphabetic [a-zA-Z_], but the following characters may also include digits [0-9].
Special Characters
Special characters can be represented as literals by using the following escape sequences.
| Sequence | Description |
|---|---|
| \' | Character quote ''' |
| \" | Double quote '"' |
| \\ | Backslash '\' |
| \0 | Null |
| \a | Alert (bell) |
| \b | Backspace |
| \d??? | Decimal value |
| \n | New line |
| \o??? | Octal value |
| \r | Carriage return |
| \t | Tab |
| \x?? | Hex value |
Primary Types
Jargon supports six primary types.
| Type | Description |
|---|---|
| bool | Boolean value: "true" or "false". All conditional expressions must reduce to type bool. |
| char | Signed 8-bit ASCII character. |
| int | Signed integer. |
| float | Signed floating point value. |
| string | Unbounded character string. |
| object | Pointer to an instance of a module. |
Operators
Expressions are parsed from left to right. Operators are parsed based on the order given below, which is very similar to the standard order of operations.
| Op | Order | Description | bool | char | int | float | string | object |
|---|---|---|---|---|---|---|---|---|
| and | 6 (infix) | Logical AND. | X | |||||
| or | 6 (infix) | Logical OR. | X | |||||
| == | 5 (infix) | Test for equality. Returns type bool. | X | X | X | X | X | |
| /= | 5 (infix) | Test for inequality. Returns type bool. | X | X | X | X | X | |
| >= | 5 (infix) | Test for greater than or equal to. Returns type bool. | X | X | X | X | ||
| <= | 5 (infix) | Test for less than or equal to. Returns type bool. | X | X | X | X | ||
| > | 5 (infix) | Test for greater than. Returns type bool. | X | X | X | X | ||
| < | 5 (infix) | Test for less than. Returns type bool. | X | X | X | X | ||
| + | 4 (infix) | Addition or string concatenation. | X | X | X | X | ||
| - | 4 (infix) | Subtraction. | X | X | X | |||
| * | 3 (infix) | Multiplication. | X | X | X | |||
| / | 3 (infix) | Division. | X | X | X | |||
| % | 3 (infix) | Modulus. | X | X | ||||
| & | 3 (infix) | Bitwise AND. | X | X | ||||
| | | 3 (infix) | Bitwise OR. | X | X | ||||
| ^ | 2 (infix) | Power. | X | X | X | |||
| not | 1 (prefix) | Logical NOT. | X | |||||
| - | 1 (prefix) | Negation. | X | X | X | |||
| ~ | 1 (prefix) | Bitwise complement. | X | X |
Conditional Constructs
-- If exp1 is true, perform statements1, else if -- exp2 is true perform statements2, else perform -- statements3. As many elsif sections may be -- added as needed. Both the elseif and else sections -- are optional. if exp1; statements1; elsif exp2; statements2; else; statements3; end; -- Go to the first when clause that matches exp1. -- If no when clause matches, go to the default case. -- The default and when clauses are all optional. switch exp1; when exp2; statements1; when exp3, exp4; statements2; ... default; statements3; end;
Looping Constructs
-- Perform statements while exp is true. while exp; statements; end; -- Set ident to exp1 and loop until it reaches -- (or exceeds) exp2. ident is incremented if "upto" -- is used. If "downto" is specified, ident is -- decremented. The direction of the increment -- determines if ident has "exceeded" exp2. for ident = exp1 ( upto | downto ) exp2; statements; end; -- Break out of the innermost loop break; -- Start next iteration of the innermost loop continue;
Arrays
Arrays are a special kind of object. Like objects, they must be explicitly allocated. Arrays (like strings) are zero based. Arrays of multiple dimensions can be simulated using arrays of arrays. Arrays have certain Attributes that may be accessed using the single quote character.
-- Create an array of eight bool's bool[] flags = new bool[8]; -- Initialize the values to "false" int x; for x = 0 upto flags'length - 1; flags[x] = false; end; -- Delete the array of bool's delete flags; -- Create a 2d matrix of integers int[][] matrix = new object[5]; for x = 0 upto matrix'length - 1; matrix[x] = new int[4]; end; [Jargon:print matrix'length]; -- Output 5 [Jargon:print matrix[0]'length]; -- Output 4 -- Delete the matrix for x = 0 upto matrix'length - 1; delete matrix[x]; end; delete matrix;
Enumerations
Enumerations may be used to create a list of related constants. Enumerations may be defined within the global scope, modules, or handlers. Enumerations are used as follows:
enum name; first, second, ... ; end; -- Note that enumerations work exactly like -- constant integers. i = name.value;
Modules
Jargon programs consist of hierarchies of modules and instances of these modules called objects. Modules may contain data, handlers, and modules. Each module has the form:
module name; local data declarations; handlers; -- Statements to be executed when an instance of -- this module is created with the new keyword. new; local data declarations; statements; end; -- Statements to be executed when an instance of -- this module is destroyed with the delete keyword. delete; local data declarations; statements; end; end;
Note that the new and delete sections are optional. The new and delete sections are only run when the new or delete keyword is used respectfully. Also, local data declarations within a module cannot have default values (this may be changed in the future such that the initial values only apply to the static instance of the module or possibly allowed completely).
Handlers
Handlers are a parallel to (member) functions in most programming languages. Arguments are always passed by value. Handlers may be overloaded by argument type. Handlers must always exist within a module. A value may or may not be returned by the handler. It is assumed that the caller will use a return value (if any) appropriately.
-- Declare a handler named "Foo" with a comma -- separated list of arguments -- (in the format: type name). handler [Foo arguments]; local data declarations; statements; end;
Objects
An object is an instance of a module. Each object has its own data distinct from the module of which it is an instance and from other instances of that module. Examples:
-- Create a new instance of "Foo" object o = new Foo; -- Delete the object "o" delete o;
Attributes
Attributes in Jargon look and act much like attributes in Ada. Note that
all array types have the same set of attributes and that strings are
considered arrays of characters in this context.
Attributes are not keywords and may be used as variable names. The
single quote ''' is used to denote an attribute.
Attributes may be used on the name of a type or module
or a variable of a type or an instance of an module (an object).
| Attribute | Description | Primary | Object |
|---|---|---|---|
| handlers | Returns an array of handler names for an object. Return type string[]. | X | |
| length | Number of elements in an array. Returns type int. | X | |
| max | Largest value that can be stored in this type. This is the value "true" for booleans. | X | |
| min | Smallest value that can be stored in this type. This is the value "false" for booleans. | X | |
| name | The name of this object. Return type string. | X | |
| size | Size (in bytes) of an object or type. Note that the compiler may pack boolean types making them 0 bytes. Return type int. | X | X |
The following attributes are used for type conversion. Note that all conversions are "smart." For example, a conversion from a string to an int will try to parse the string as an integer.
Conversion Attributes| Attribute | Description |
|---|---|
| bool | Convert to a bool. |
| char | Convert to a char. |
| float | Convert to a float. |
| int | Convert to an int. |
| string | Convert to a string. |
Concurrency
Concurrency in Jargon is accomplished using a special kind of module, called a thread, and critical sections. A thread module acts very much like a regular module which does not require the new handler to return. Handlers in threads work the same way as handlers in modules.
thread TheThread; Local variables and handlers -- Thread entry point new; end; -- Optional destructor delete; end; end;
A thread can be stopped by either invoking delete on the object from outside the thread or by returning from the new handler from within the thread, in which case the thread must still be deleted to reclaim memory. If the thread is in a critical section when delete is invoked, the delete will stall until the thread exits the critical section. Critical sections may be used within thread handlers to lock the thread. Only one thread of execution for an object can be in a critical section at a time.
critical; Code that may modify thread variables end;
Exceptions
This is not currently implemented. I am still considering whether exceptions would be useful in Jargon. With inheritance and dynamic handler dispatch, exceptions seem superfluous.
Any type of object (primative or class instance) can be used as an exception.
-- Raise an exception string exception = "an error occurred"; raise exception; -- Raise an exception that is an instance of a class. Foo exception = new Foo; raise exception; raise new Foo; -- Also works -- Catching an exception try; -- Do something that might raise an exception catch string s; -- A string (s) was thrown as an exception catch Foo e; -- A Foo object (e) was thrown as an exception end;
Miscellaneous Language Features
-- Import the contents of "file" in directory "dir" -- "file" should not contain a file extension import dir/file; -- Compiler/platform specific pragma pragma blah(blah); -- Insert an assembly language block -- This is used to implement the standard library and no -- standard interface to the language is specified (yet). asm; assembly code end;
Features To Be Realized
- Static array data.