Reorganize and add strings
This commit is contained in:
parent
cf63aa0ee6
commit
6a7392eff4
5 changed files with 79 additions and 31 deletions
44
content/types/primitive_data_structures.md
Normal file
44
content/types/primitive_data_structures.md
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
---
|
||||
title: Primitive Data Structures
|
||||
---
|
||||
# Primitive Data Structures
|
||||
|
||||
Nimrod contains several basic builtin data structures: sequences, strings, bitsets, and arrays.
|
||||
|
||||
## Strings
|
||||
|
||||
``` nimrod
|
||||
echo "words words words ⚑"
|
||||
echo """
|
||||
<html>
|
||||
<head>
|
||||
<head/>\n\n
|
||||
|
||||
<body>
|
||||
<body/>
|
||||
<html/> """
|
||||
|
||||
proc re(s: string): string = s
|
||||
|
||||
echo r" "" "
|
||||
echo re"\b[a-z]++\b"
|
||||
```
|
||||
``` console
|
||||
$ nimrod c -r strings.nim
|
||||
words words words ⚑
|
||||
<html>
|
||||
<head>
|
||||
<head/>\n\n
|
||||
|
||||
<body>
|
||||
<body/>
|
||||
<html/>
|
||||
"
|
||||
\b[a-z]++\b
|
||||
```
|
||||
|
||||
There are several things to note about strings:
|
||||
|
||||
- Quoted Strings: Created by wrapping the body in triple quotes, they never interpret escape codes
|
||||
- Raw Strings: created by prefixing the string with an `r`. Escape sequences don't work, except for `"`, which can be escaped as `""`
|
||||
- Proc Strings: raw strings, but the method name that prefixes the string is called
|
||||
37
content/types/primitives.md
Normal file
37
content/types/primitives.md
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
---
|
||||
title: Primitives
|
||||
---
|
||||
# Primitives
|
||||
|
||||
Nimrod has several primitive types:
|
||||
|
||||
* signed integers: `int8`, `int16`, `int32`, `int64`, and `int`, where `int` is the same size as a pointer
|
||||
* unsigned integers are similar with `u` prepended to the type
|
||||
* floating points numbers: `float32`, `float64`, and `float`, where `float` is the processor's fastest type
|
||||
* characters: `char`, and basically an alias for uint8
|
||||
|
||||
To indicate the size of an integer literal, append `u` or `i` and the size you'd like to the end. However, this is not typically necessary.
|
||||
|
||||
Integers can also have `0[xX]`, `0o`, `0[Bb]` prepended to indicate a hex, octal, or binary literal, respectively. Underscores are also valid in literals, and can help with readability.
|
||||
|
||||
``` nimrod
|
||||
let
|
||||
a: int8 = 0x7F # Works
|
||||
b: uint8 = 0x1111_1111 # Works
|
||||
d = 0xFF # type is int
|
||||
c: uint8 = 256 # Compile time error
|
||||
```
|
||||
|
||||
Precedence rules are the same as most other languages, but instead of `^`, `&`, `|`, `>>`, `<<`, the `xor`, `and`, `or`, `shr`, `shl` operators are used, respectively.
|
||||
|
||||
``` nimrod
|
||||
let
|
||||
a: int = 2
|
||||
b: int = 4
|
||||
echo 4/2
|
||||
```
|
||||
``` console
|
||||
$ nimrod c -r numbers2.nim
|
||||
2.0
|
||||
```
|
||||
Another difference that may be surprising is that the `/` operator returns a floating point result, even when the operands are integers. If integer division is needed, the `div` operator should be used.
|
||||
26
content/types/range.md
Normal file
26
content/types/range.md
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
---
|
||||
title: Range Types
|
||||
---
|
||||
# Range Types
|
||||
|
||||
Range types that guarantee a value will always be within a given range also exist. Whenever possible, out-of-bounds values will fail at compile-time, otherwise they will fail at run-time.
|
||||
|
||||
``` nimrod
|
||||
type
|
||||
TwoDigits* = range[10..99]
|
||||
|
||||
let a = 2
|
||||
var b: TwoDigits = 99 + a # Fails at run-time
|
||||
let c: TwoDigits = 100 # Fails at compile-time
|
||||
```
|
||||
|
||||
Also note that range types also work on any other ordinal type, so a range over an enum is also valid:
|
||||
|
||||
``` nimrod
|
||||
type
|
||||
MyEnum = enum
|
||||
A, B, C, D, E
|
||||
BToD = range[B..D]
|
||||
|
||||
let value: BToD = C
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue