Reorganize and add strings
This commit is contained in:
parent
cf63aa0ee6
commit
6a7392eff4
5 changed files with 79 additions and 31 deletions
|
|
@ -5,14 +5,15 @@
|
||||||
* [Variables](/variables/)
|
* [Variables](/variables/)
|
||||||
* [Result](/variables/result/)
|
* [Result](/variables/result/)
|
||||||
* [Type Casting and Inference](/variables/type_casting_inference/)
|
* [Type Casting and Inference](/variables/type_casting_inference/)
|
||||||
* [Types](/types/)
|
|
||||||
* [Objects](/types/objects/)
|
|
||||||
* [Enums](/types/enums/)
|
|
||||||
* [Distinct Types](/types/distinct/)
|
|
||||||
* [If, Else, While, Block](/if_else_while/)
|
* [If, Else, While, Block](/if_else_while/)
|
||||||
* [Case Statements](/case/)
|
* [Case Statements](/case/)
|
||||||
* [Numbers](/numbers/)
|
|
||||||
* [For Loops & Iterators](/for_iterators/)
|
* [For Loops & Iterators](/for_iterators/)
|
||||||
|
* [Types](/types/)
|
||||||
|
* [Object Types](/types/objects/)
|
||||||
|
* [Enum Types](/types/enums/)
|
||||||
|
* [Distinct Types](/types/distinct/)
|
||||||
|
* [Primitive Types](/types/primitives/)
|
||||||
|
* [Primitive Data Structures](/types/primitive_data_structures/)
|
||||||
* [Procedures](/procs/)
|
* [Procedures](/procs/)
|
||||||
* [Exceptions](/exceptions/)
|
* [Exceptions](/exceptions/)
|
||||||
* [Varargs](/varargs/)
|
* [Varargs](/varargs/)
|
||||||
|
|
|
||||||
|
|
@ -12,27 +12,3 @@ type
|
||||||
let a: int = 2
|
let a: int = 2
|
||||||
discard a + MyInteger(4)
|
discard a + MyInteger(4)
|
||||||
```
|
```
|
||||||
|
|
||||||
## 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
|
|
||||||
```
|
|
||||||
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
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
---
|
---
|
||||||
title: Numbers
|
title: Primitives
|
||||||
---
|
---
|
||||||
# Numbers
|
# Primitives
|
||||||
|
|
||||||
Nimrod has several primitive types:
|
Nimrod has several primitive types:
|
||||||
|
|
||||||
* signed integers: `int8`, `int16`, `int32`, `int64`, and `int`, where `int` is the same size as a pointer
|
* 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
|
* 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
|
* 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.
|
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.
|
||||||
|
|
||||||
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