Skip to content

Commit

Permalink
Add string/integer/float/boolean literals.
Browse files Browse the repository at this point in the history
  • Loading branch information
watcol committed Nov 14, 2021
1 parent b67714d commit 9934e89
Showing 1 changed file with 160 additions and 10 deletions.
170 changes: 160 additions & 10 deletions doc/language.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Statement is a base unit of Walnut, categorized into these types:
- [Key/Expression Pair](#keyexpression-pair)
- [Table Header](#table-header)
- [Function Definition](#function-definition)
- [Import Declaration](#import-declaration)

Statements basically consist of one line (separated with [newline](#the-terms)
characters), except these cases:
Expand Down Expand Up @@ -154,36 +153,187 @@ baz = "baz"
```

### Function Definition
*Comming soon...*

## Expression
Expression is a way to express a value by evaluating
[literal values](#literal-value) or values refered through [keys](#key), using
[operators](#operator).

### Literal Value
Literal value is a value expressed directly. Literal values are classified to
these types:
- [String](#string)
- [Integer](#integer)
- [Float](#float)
- [Boolean](#boolean)
- [Array](#array)
- [Inline Table](#inline-table)
- [Inline Function](#inline-function)

#### String
String is an array of characters. There are 4 ways to express strings.

##### Double Quoted String
Double quoted string starts with a quatation mark (`U+0022`), ends with a not
escaped quatation mark. Any Unicode characters can be used except quatation
mark and backslash (`U+005C`), and escape sequences starts with backslashes
can be used:

- `\n` ... linefeed (`U+000A`)
- `\r` ... cariage return (`U+000D`)
- `\t` ... horizontal tab (`U+0009`)
- `\"` ... quatation mark (`U+0022`)
- `\\` ... backslash (`U+005C`)
- `\xXX` ... 8 bit character (`U+00XX`)
- `\u{XXXX}` ... unicode character (`U+XXXX`)

Note that 8 bit characters are 2-digit hex values, and unicode characters are
hex values with any digits.

```toml
string = "The \"Double quated\"\r
\tString"
```

##### Single Quoted String
Single quoted string is a string surrounded by two apostrophes (`U+0027`).
No escape sequences are allowed, so apostrophes cannot be included in single
quoted strings.

```toml
string = '\\ServerX\admin$\system32\'
```

##### Double Quoted Unidented String
Double quoted unindented string is similar to double quoted string, but
surrounded with triple quotation marks (`"""`), and will be
[unindented](https://github.com/dtolnay/indoc/tree/master/unindent).
Quotation marks can be used in the strings.

```toml
# python = "def hello():\n print('Hello, World!')\n\nhello()"
python = """
def hello():
print("Hello, world!")
hello()
"""
```

##### Single Quoted Unindented String
Single quoted unindented string is similar to single quoted string, but
surrounded with triple apostrophes (`'''`), and will be
[unindented](https://github.com/dtolnay/indoc/tree/master/unindent).
Apostrophes can be used in the strings.

```toml
# python = "def hello():\n print('Hello, World!')\n\nhello()"
python = '''
def hello():
print('Hello, world!')
hello()
'''
```

#### Integer
Integer is a whole number. Decimals, hexadecimals (with prefix `0x`), octals
(with prefix `0o`), and binaries (with prefix `0b`) are supported. In
hexadecimals, numbers from ten to fifty are expressed by `A-F` or `a-f`.
Leading zeros are not allowed.

```toml
decimal = 42
hex1 = 0xDEADBEEF
hex2 = 0xcafebabe
oct = 0o644
bin = 0b11010110
```

An integer prefixed with `+` will be treated as positive number, one prefixed
with `-` will be treated as negative number. If an integer does not have
neither `+` or `-`, it will be a positive number. `+0`, and `-0` are identical
to `0`.

### String
```toml
pos1 = 42
pos1 = +1
zero1 = 0
zero2 = -0
neg1 = -5
neg2 = -0xcafebabe
```

Underscores between digits are allowed for readability.

```toml
int1 = 5_349_221
int1 = 1_2_3_4_5
int2 = 0b1101_0110
```

### Integer
Accepted range is from `-2^63` to `2^63-1` (64bit signed integer).

### Float
#### Float
Float is a IEEE 754 binary64 value.

### Boolean
A float consists of integer part, fractional part and exponent part. An
integer part is required, and follows same rule as decimal [integer](#integer)
. A fractional part is prefixed with a full stop (`U+002E`), and consists of
one or more decimal digits. An exponent part is prefixed with `e` or `E`, and
consists of an integer, which follows same rule as decimal [integer](#integer)
but leading zeros are allowed. Either a fractional part or an exponent part
are required.

### Array
A float will be expressed by `(i + f) * (10 ** e)` where `i` is an integer
part, `f` is a fractional part prefixed with `0.` (if eliminated, `f` is 0),
and `e` is an exponent part (if eliminated, `e` is 1).

### Inline Table
```toml
float1 = 3.14
float2 = +1.23 # same as 1.23
float3 = -0.001
float4 = 3e2 # same as 300.0
float5 = 1e-02 # same as 0.01
float6 = -2E+4 # same as -20000.0
float7 = 5_000.000_003 # same as 5000.000003
```

#### Boolean
Boolean is a value, either `true` or `false`.

```toml
bool1 = true
bool2 = false
```

### Inline Function
#### Array

#### Inline Table

#### Inline Function
*Comming soon...*

### Key

#### Function Key
*Comming soon...*

### Operators
*Comming soon...*

#### Arithmetic Operators
*Comming soon...*

#### Logical Operators
*Comming soon...*

#### Comparison Operators

### Function
*Comming soon...*

#### Function Call
*Comming soon...*

## The terms
- "Whitespace" means tab (`U+0009`) or space (`U+0020`).
Expand Down

0 comments on commit 9934e89

Please sign in to comment.