| 1. Audience |
|---|
|
This Slag language primer is written for the experienced Java programmer. Others may benefit, but Slag concepts are often defined using Java examples and terminology.
You will need some familiarity with using the system command prompt and a generic text editor of your choice.
|
| 2. Getting Started | |||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2.1. Setup2.1.1. WindowsUnzip the Slag installation to disk - for example, place the "Slag" root directory in "c:\Program Files\" or another location of your choice. Add "c:\Program Files\Slag\" (or similar) to your system path.
2.2. Hello World ProgramType in this program and save it as "hello.slag":
2.3. CompilingTo compile "hello.slag" into "hello.exe" (VM/bytecode bundle), type the following at the command prompt:
(or just "slagc hello") To compile "hello.slag" to a ETC (Execution-Tree Code) file, type this:
2.4. RunningYou can run your program by typing:
or
depending on how you compiled it.
2.5. Source File Organization2.5.1. Main Project FileThe single source file name you give you give to the compiler should directly or indirectly [include] all the other source files used in the project.The [mainClass] directive specifies the starting class of the project. An object of that type is created and its init method is called. 2.5.2. [include "filename.slag"] directive
2.5.3. [includeDir "directory_path"] directive
Example:
2.5.4. [mainClass ClassName] directive
Example:
2.6. Odds & Ends2.6.1. CommentsExamples:
2.6.2. Ellipsis Line Continuation
An ellipsis (...) at the end of a line means the next line should be considered a continuation of the previous line. This is only necessary when the syntax at the end of the previous line is incomplete.
2.6.3. Escape SequencesSlag supports the following control character escape sequences in character and string literals:
Slag supports arbitrary Unicode characters anywhere in the source code:
Unlike the control sequences above, Unicode sequences are processed before parsing. In general they are used in addition to but not instead of the control sequences. For example:
but:
|
| 3. Conventions | ||
|---|---|---|
3.1. Terminology
|
| 4. Primitives | |||||||
|---|---|---|---|---|---|---|---|
4.1 Primitive TypesThe following primitive data types are built-in.
4.2. Primitive Type Conversions
|
| 5. Literal Values |
|---|
|
Literals of various types may be expressed in the following ways.
See the section on Arrays & Lists for more details on list literals, readers, and readable things.
|
| 6. Console Input and Output | ||
|---|---|---|
6.1. Printing Output
6.2. Input
|
| 7. Simple Classes | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
7.1. Key Points
7.2. Example: Name ClassJava:
Slag:
7.3. Example: Stoplight ClassJava:
Slag:
7.4. Example: Die
7.5. Example: Temperature Class
|
| 8. Operators | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
8.1. Math Operators
Modulo operates differently than in Java and C++ when used with negative values. Any N % M yields 0..(M-1), preserving the pattern when N is negative. For example, -2 % 10 == 8. In addition, modulo behaves usefully with real numbers - (3.1 % 1.4 == 0.3). Increment and decrement must be used as stand-alone commands - they can't be part of a larger expression as with Java and C++. They simply add one or subtract one from the given variable. When used with lists of primitive values, arithmetic operators automatically operate on each element of the list. 8.2. Bitwise Operators
The bit rotate and shift commands are actual operators even though they use method call syntax. 8.3. Relational and Logical Operators
8.4. Range Operators
The range operators create a sequence (technically a readable Range object) that can be iterated over or turned into a list with the to_list method. upTo (..) counts from the first up to and including the last. A negative step size must be explicitly stated if you wish to count backwards. upToLessThan (..<) counts from the first up to but not including the last. downTo counts from the first down to and including the last. The step size is assumed to be (-1) if no other step size is given. downTo is the only range operator that doesn't have a shorthand equivalent.
downToGreaterThan (..>) counts from the first down to but not including the last. The step size is assumed to be (-1) if no other step size is given.
8.5. Type-checking and Cast Operators
The instanceOf operator returns true if the class or aspect on the right-hand side is the type or is a base type of the object on the left-hand side. A base Shape reference can be cast to an extended Circle reference with the syntax "shape.(Circle)". For example:
An extended reference can also be cast to a base type for purposes of selecting which overloaded method you wish to call (assuming there are several). Casting references like this never converts one type of object to another; it merely causes the compiler to treat an object as a more specialized or more generalized base class or sub class. Primitives may be cast in the same style: "intval = realval.(Int32)". They may also be cast using "constructor-style" notation: "intval = Int32(realval)".
8.6. 'duplicate' Operator
Any object or primitive may be cloned with the operator duplicate(existing). duplicate can be called on any value-type (such as a primitive or a compound) with no other preparation. To be able to duplicate an object, you must define the create_duplicate.ClassType method. The decision of whether to make a deep or shallow copy can be made on a case-by-case basis.
|
| 9. Control Structures | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
9.1. Single-line and Multi-line Commands
9.2. Selection9.2.1. 'if'
9.2.2. 'which'
9.2.3. 'contingent'
Slag introduces contingents as a new kind of conditional. They are similar in flow to a try/catch block but they deal with positive and negative logic tests rather than error generation.
9.3. Iteration9.3.1. 'while'
9.3.2. 'forEach'
The simple forEach "forEach (1..10)" just repeats its body the given number of times. The *forEach-in" creates a local variable with the specified name and sets it to each element in the expression. The *forEach-of" creates a local Int32 variable with the specified name and sets it to the index of each element in the expression. 9.3.3. 'loop'
The loop command defines an infinite loop. This is conceptually preferable over a "while (true)" loop. 9.3.4. 'repeat'
The body is repeated until the condition becomes true. 9.4. try-catch and throw
Slag's Error class is analogous to Java's Exception class. There are no checked exceptions in Slag - errors may be thrown at any desired point and enclosing try-catches are entirely optional. Error objects that are instanceOf the first catch type are handled by that catch, and so on. 9.5. Labels and Breaks9.5.1. Labels
Any structure can be labeled by following the initial keyword with a double-colon and then an arbitrary label name. 9.5.2. 'escapeX'
The 'escape' command is analogous to Java's "break". escapeWhile, for instance, breaks out of the innermost while command that encloses the escapeWhile. 9.5.3. 'nextIteration'
The 'nextIteration' command is analogous to Java's "continue". Used by itself it skips the remaining body of the current forEach, while, repeat, or loop but continues the loop. It can be used with a label to target an outer loop.
|
| 10. Extended Classes | |||
|---|---|---|---|
10.1. Key Points
10.2. Example: Shape class
|
| 11. Advanced Class Topics | |||||||
|---|---|---|---|---|---|---|---|
11.1. init_class method
Every class has a hidden method named init_class() that is called once at the beginning of a Slag program. Its purpose is to perform all the class property initializations. If you define a class method called init_class(), any statements you write will be performed after class property initializations. 11.2. init_object method
Every class has a hidden object method called init_object() that is called when an object is first created, before a regular init() method is called. Every init_object() method begins with a call to prior.init_object(), then performs property initialization. If you define an init_object() method, any statements you write will be performed after the automatic initialization. 11.3. Operator Methods
All of the basic arithmetic, relational, and bitwise operators can be defined on a per-class basis. When a is a reference, expressions of the form "a + b" are transformed into "a.op+(b)". The op+ must be defined in a's class, but the parameter and return type can be any desired value. Multiple overloaded methods are fine under the normal rules (signatures must vary). 11.3.1. Example: CaesarCypherMessage
11.4. 'promote' MethodIf "a + b" is encountered and b is an object but a is not, the expression is converted to:
For example, here's a fragment of some "BigInteger" class that would handle "5 + BigInteger(7)" (which would become "BigInteger.promote(5).op+(BigInteger(7))":
|
| 12. Compounds | |||
|---|---|---|---|
|
Syntax:
Compounds are composite by-value types that are composed of one or more primitives or other compounds.
Example:
|
| 13. 'enum' definitions | ||
|---|---|---|
|
Enums (rhymes with Tums) offer the same power and flexibility as they do in Java. By convention the type-name
is in ALL_CAPS to make enum parameters easy to spot; the categories (enum values) are in lowercase.
A simple enum:
A more complex enum that adds an additional property to each category:
13.1. BitFlags enum augmentAn enumeration augment called BitFlags is provided in the standard library to meet a common need for enumerated values: having a set of bit flags that can be manipulated with bitwise OR (setting/merging), AND (clearing), and NOT (toggling). See type BitFlags in the API for an example.
|
| 14. Templates | ||||||
|---|---|---|---|---|---|---|
14.1. Non-template / Template ComparisonNon-templated example:
Templated example:
14.2. Key Points
14.3. Example: Pair
14.4. Template ConditionalsTemplate conditionals allow template instances to be customized for particular substitution types by including or omitting certain members.Examples:
|
| 15. Aspects | ||
|---|---|---|
|
Slag aspects are a new approach to aspect-oriented programming and a powerful replacement for Java interfaces.
In simplest terms, an aspect is like a Java interface that can include method bodies and additional instance variables in its definition. Where Java implements interfaces, Slag incorporates aspects. During aspect incorporation, aspect method bodies are "cut & pasted" into existing methods of the same name. The syntax for controlling the order of this aspect layering is similar to that of making a prior (super-class) call. Example:
Note that after the above example is compiled, there is a single format method that is called once.
|
| 16. Augments | ||||
|---|---|---|---|---|
|
Augments are a way to add additional members and base types to a class separate from its primary definition.
Examples:
The following example is equivalent to the example above:
|
| 17. Primitive Methods | |||
|---|---|---|---|
|
Source code:
Compiler converts to:
To define the plus() class method in class Global:
|
| 18. Arrays and Lists | |||||
|---|---|---|---|---|---|
18.1. Arrays
18.2. Lists
18.2.1. Operators and ListsThe standard numerical and bitwise operators are defined for lists of the appropriate primitive types. They provide list functionality that's similar to Matlab's matrix functionality:
18.2.2. Technical Details
|
| 19. Standard Library & API Reference | |||||||
|---|---|---|---|---|---|---|---|
Refer to a Slag language reference (such as slag_reference.html) for an description
of all the classes and methods available to Slag programs. Here's a brief overview
of some of them:
19.1. GlobalMany of the "built-in" methods and methods that operate on primitives and compounds are actually Global class methods.19.2. RequiresCleanup
Objects of classes that incorporate RequiresCleanup have their cleanup method called when they have no more references to them and their memory is about to be freed. Object allocation and deletion involving objects that require cleanup is a little slower than for other objects. 19.3. File I/O
19.4. HashTable
19.5. PatternSlag patterns use a syntax that's closer to EBNF than to regular expressions.
Patterns are composed of the following symbols:
The following identifiers are also defined as the specified patterns:
|
| 20. SlagDoc |
|---|
|
Slag has an autodocumentation tool called "slagdoc", available in the root directory of the Slag distribution. Run it without arguments - "slagdoc" - to see how to use it.
SlagDoc recognizes comments immediately following the first line of a type definition, a member variable declaration, or a method definition and generates documentation containing those comments. No special tags are recognized. Any comment that begins more than one space after the initial "#" is considered indented and is displayed in a monospace font. See "standard.slag" for examples of writing SlagDoc comments.
|