Difference between revisions of "S2 Guide: Language Tutorial"

From Dreamwidth Notes
Jump to: navigation, search
m (Literals)
Line 1: Line 1:
S2 is a programming language designed .
+
S2 is a programming language designed to be a style system. It has properties in common with Perl, Python, and Java.  As a programming language, it is:
  
== Literals ==
+
* Object oriented: You can define classes that bundle up functions and variables into an object.
 +
* Statically typed: Variables must be assigned a type, and that type does not change.
  
Literals are basically ways of defining values inside of code -- usually when we are assigning them to variables!
+
= Variables =
 +
 
 +
Variables are like containers that hold values.  You can put values into them, use their values in your programming, or change their value to something else.
 +
 
 +
== Declaring a variable ==
 +
 
 +
In S2, variables must be declared ahead of time in order to be used.  The basic format goes:
 +
 
 +
var TYPE variablename;
 +
 
 +
* <tt>var</tt> indicates you're creating a new variable
 +
* <tt>TYPE</tt> describes what type of variable you're making. The type can be one of the basic types, described below, or a previously defined class (to be explained later).  This type doesn't change.
 +
* <tt>variablename</tt> is the name you give the variable in your code. This name must start with a letter and can contain letters, digits, and the underscore character ("_").
 +
 
 +
You can assign a variable a value when declaring it, or wait until later--but make sure to assign a variable a value before you use it! An example of declaring a variable while assigning it is:
 +
 
 +
var TYPE variablename = VALUE;
 +
 
 +
Note: you can assign the value of a variable to another variable, as well, if they are the same type.  Here's an example that gives <tt>variable1</tt> the value of <tt>variable2</tt>:
 +
 
 +
var TYPE variable1 = variable2;
 +
 
 +
== Basic Types ==
  
 
=== Strings ===
 
=== Strings ===
  
There are two different types of strings in S2.   
+
String variables, which are lists of characters, are declared as <tt>string</tt> in S2.  You can define strings in one of two ways.
  
The first kind comes in between double quotes:
+
The first way comes in between double quotes:
  
 
<syntaxhighlight lang="s2">
 
<syntaxhighlight lang="s2">
Line 15: Line 38:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The second kind comes in between sets of three double quotes:
+
The second way comes in between sets of three double quotes--this is very useful when writing HTML, which can have a lot of single double quotes in it.  Here's an example:
  
 
<syntaxhighlight lang="s2">
 
<syntaxhighlight lang="s2">
"""I am also a string, but I can have "quotes" inside me."""
+
"""I am also a string, but I can have "quotes" inside me.""";
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
The above example demonstrates another property of string values, in that if you don't assign them to a variable name, they'll print to output.
  
 
Some characters need to be escaped to show up properly in strings, with a backslash (\):
 
Some characters need to be escaped to show up properly in strings, with a backslash (\):
Line 30: Line 55:
 
=== Integers ===
 
=== Integers ===
  
Integers (whole numbers) are another value like strings that the S2 language understands. This is an example of a variable being assigned a literal integer value:
+
Integers (whole numbers) are another value like strings that the S2 language understands, and are declared with <tt>int</tt>. This is an example of a variable being assigned a literal integer value:
  
 
<syntaxhighlight lang="s2">
 
<syntaxhighlight lang="s2">
Line 36: Line 61:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== True/False ===
+
=== Booleans ===
  
You can also use <tt>true</tt> and <tt>false</tt> as values in code:
+
A boolean is a variable that is either <tt>true</tt> or <tt>false</tt>, and is declared with <tt>bool</tt>.  Here is an example that expresses having candy but no cake as boolean variables:
  
 
<syntaxhighlight lang="s2">
 
<syntaxhighlight lang="s2">
var bool isTrue = true;
+
var bool has_candy = true;
var bool isFalse = false;
+
var bool has_cake = false;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Arrays ===
+
== Arrays ==
  
 
An array is a list of items.  They must all be the same type!  Here is an example with strings:
 
An array is a list of items.  They must all be the same type!  Here is an example with strings:
Line 53: Line 78:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Hashes ===
+
=== Associative arrays ===
  
A hash is like an array, except instead of just a list of items, all of those
+
Instead of an ordered list of items, an associative array gives each item in it a "key" that serves as an identifying label.  All keys must be strings, and all the times must be the same type.  Here's an example with strings:
items get to have a "key" labeling it.  All keys must be strings, and all the times must be the same type.  Here's an example with strings:
+
  
 
<syntaxhighlight lang="s2">
 
<syntaxhighlight lang="s2">
 
var string{} fruits = ["apple" => "red", "lemon" => "yellow", "grape" => "purple"]
 
var string{} fruits = ["apple" => "red", "lemon" => "yellow", "grape" => "purple"]
 
</syntaxhighlight>
 
</syntaxhighlight>
 
== Variables ==
 
  
 
== Properties ==
 
== Properties ==
  
== Operators ==
+
Properties are special global variables that are available between layers.  EXPAND
 +
 
 +
= Statements =
  
== Statements ==
+
= Operators =
  
== Functions ==
+
= Functions =
  
 
== Classes ==
 
== Classes ==
  
 
[[Category: S2 Guide]]
 
[[Category: S2 Guide]]

Revision as of 23:07, 13 June 2010

S2 is a programming language designed to be a style system. It has properties in common with Perl, Python, and Java. As a programming language, it is:

  • Object oriented: You can define classes that bundle up functions and variables into an object.
  • Statically typed: Variables must be assigned a type, and that type does not change.

Variables

Variables are like containers that hold values. You can put values into them, use their values in your programming, or change their value to something else.

Declaring a variable

In S2, variables must be declared ahead of time in order to be used. The basic format goes:

var TYPE variablename;
  • var indicates you're creating a new variable
  • TYPE describes what type of variable you're making. The type can be one of the basic types, described below, or a previously defined class (to be explained later). This type doesn't change.
  • variablename is the name you give the variable in your code. This name must start with a letter and can contain letters, digits, and the underscore character ("_").

You can assign a variable a value when declaring it, or wait until later--but make sure to assign a variable a value before you use it! An example of declaring a variable while assigning it is:

var TYPE variablename = VALUE;

Note: you can assign the value of a variable to another variable, as well, if they are the same type. Here's an example that gives variable1 the value of variable2:

var TYPE variable1 = variable2;

Basic Types

Strings

String variables, which are lists of characters, are declared as string in S2. You can define strings in one of two ways.

The first way comes in between double quotes:

var string test = "Testing";

The second way comes in between sets of three double quotes--this is very useful when writing HTML, which can have a lot of single double quotes in it. Here's an example:

"""I am also a string, but I can have "quotes" inside me.""";

The above example demonstrates another property of string values, in that if you don't assign them to a variable name, they'll print to output.

Some characters need to be escaped to show up properly in strings, with a backslash (\):

  • If you need a newline, use: \n
  • If you need a double quote inside single double quotes (or need to make three or more double quotes inside a triple double quotes), use: \"
  • If you need to make a backslash, use: \\
  • If you want to make a dollar sign (you'll learn why later in variables), use: \$

Integers

Integers (whole numbers) are another value like strings that the S2 language understands, and are declared with int. This is an example of a variable being assigned a literal integer value:

var int width = 500;

Booleans

A boolean is a variable that is either true or false, and is declared with bool. Here is an example that expresses having candy but no cake as boolean variables:

var bool has_candy = true;
var bool has_cake = false;

Arrays

An array is a list of items. They must all be the same type! Here is an example with strings:

var string[] counting = ["one", "two", "three"]

Associative arrays

Instead of an ordered list of items, an associative array gives each item in it a "key" that serves as an identifying label. All keys must be strings, and all the times must be the same type. Here's an example with strings:

var string{} fruits = ["apple" => "red", "lemon" => "yellow", "grape" => "purple"]

Properties

Properties are special global variables that are available between layers. EXPAND

Statements

Operators

Functions

Classes