summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md13
-rw-r--r--box/box.tnsl2
-rw-r--r--box/list.tnsl16
-rw-r--r--box/map.tnsl185
-rw-r--r--box/optional.tnsl35
-rw-r--r--crypto/hash/siphash.tnsl21
-rw-r--r--internal/c_wrap_linux.tnsl23
-rw-r--r--io/file.tnsl6
-rw-r--r--io/path.tnsl63
-rw-r--r--libtnsl.tnsl3
-rw-r--r--text/cstr.tnsl173
11 files changed, 487 insertions, 53 deletions
diff --git a/README.md b/README.md
index 7441172..f02e219 100644
--- a/README.md
+++ b/README.md
@@ -2,20 +2,21 @@
The official implimentation of the TNSL Standard Library
-`libtnsl` is a "batteries included" standard library which aims to keep these batteries as small as possible.
+`libtnsl` is a "batteries included" standard library aimed at desktop or network operating systems which aims to keep each battery as small as possible.
To that end there are a variety of modules, some of which may be omitted for smaller but less capable distributions of the standard library.
---
## Module Groups
-Modules are grouped into `base`, `net`, or `full` groupings. Base is the core modules provided, net adds networking, and full is the complete set including graphics and audio.
+Modules are grouped into `base`, `net`, or `extended` groupings. Base is the core modules provided, net adds networking, and extended is the complete set including graphics and audio.
## Available Modules By Group
### Base Modules
* `algo`
* common algorithms which act on types implementing the common library interfaces
+ * non-cryptographic hash functions
* `box`
* common container types implementing the common library interfaces
* `crypto`
@@ -36,7 +37,7 @@ Modules are grouped into `base`, `net`, or `full` groupings. Base is the core m
* various protocols
* `lang`
* language constants
- * any language feature support which relies on the std library lives here
+ * any language feature support which relies on the standard library lives here
* common interfaces used throughout the rest of the library
* `math`
* common math functions
@@ -68,9 +69,9 @@ Modules are grouped into `base`, `net`, or `full` groupings. Base is the core m
* support for TLS using the base `crypto` module
* support for HTTP 1/2/3 protocol
* support for WebSockets
- * support for a variety of other internet protocols
+ * support for other internet protocols
-### Full Modules
+### Extended Modules
* `audio`
* support for audio playback through a graph based api with inputs, outputs, and transformers
* audio transformer and generator protocols
@@ -78,7 +79,7 @@ Modules are grouped into `base`, `net`, or `full` groupings. Base is the core m
* support for loading sound formats from files
* `graphics`
* basic windowing support
- * light wrappers over a variety of low level graphics libraries
+ * bindings over a variety of low level graphics libraries
* OpenGL
* OpenXR
* Vulkan
diff --git a/box/box.tnsl b/box/box.tnsl
index 189392f..ab934b8 100644
--- a/box/box.tnsl
+++ b/box/box.tnsl
@@ -1,5 +1,7 @@
/; module box
import "list.tnsl"
+ import "map.tnsl"
+ import "optional.tnsl"
;/
diff --git a/box/list.tnsl b/box/list.tnsl
index 58278e4..14ac1b8 100644
--- a/box/list.tnsl
+++ b/box/list.tnsl
@@ -9,14 +9,14 @@ struct List {
_elsz
}
-uint VECTOR_MIN_ELEMENTS = 4
-uint VECTOR_MAX_GROW = 256
+uint LIST_MIN_ELEMENTS = 4
+uint LIST_MAX_GROW = 256
-/; method Vector
+/; method List
/; init (uint elsz)
self._elsz = elsz
- self.size = VECTOR_MIN_ELEMENTS
+ self.size = LIST_MIN_ELEMENTS
self.count = 0
self.data = _alloc(self.size * self._elsz)
;/
@@ -41,10 +41,10 @@ uint VECTOR_MAX_GROW = 256
/; push (~void el)
/; if (self.size == self.count + 1)
- /; if (self.size < VECTOR_MAX_GROW)
+ /; if (self.size < LIST_MAX_GROW)
self._grow(self.size)
;; else
- self._grow(VECTOR_MAX_GROW)
+ self._grow(LIST_MAX_GROW)
;/
;/
@@ -127,8 +127,8 @@ uint VECTOR_MAX_GROW = 256
_delete(self.data)
;/
- /; copy [Vector]
- Vector out
+ /; copy [List]
+ List out
out.init(self._elsz)
/; loop (int i = 0; i < self.count) [i++]
diff --git a/box/map.tnsl b/box/map.tnsl
new file mode 100644
index 0000000..43d52a3
--- /dev/null
+++ b/box/map.tnsl
@@ -0,0 +1,185 @@
+
+# Currently just a hash map from cstring to an arbitrary pointer
+# but looking to expand this in the future with generics
+
+struct MapNode {
+ ~void
+ key,
+ value,
+
+ bool active
+}
+
+/; method MapNode
+ /; deleted [bool]
+ return self.active == false && self.key !== null
+ ;/
+;/
+
+struct Map {
+ uint
+ count,
+ size,
+
+ ~MapNode _under
+}
+
+uint MAP_MIN_SIZE = 8
+uint MAP_MAX_GROW = 4096
+
+/; method Map
+ /; _alloc
+ MapNode tmp
+ tmp.key = null
+ tmp.value = null
+ tmp.active = false
+
+ self._under = _alloc(self.size * len tmp)
+
+ /; loop (int i = 0; i < self.size) [i++]
+ self._under{i} = tmp
+ ;/
+ ;/
+
+ /; init
+ self.count = 0
+ self.size = MAP_MIN_SIZE
+
+ self._alloc()
+ ;/
+
+ /; _over [bool]
+ uint usage = (self.count * 10) / self.size
+ return usage > 7
+ ;/
+
+ /; _grow
+ ~MapNode old_list = self._under
+ uint old_size = self.size
+
+ self.count = 0
+ /; if (self.size < MAP_MAX_GROW / 2)
+ self.size = old_size * 2
+ ;; else
+ self.size = old_size + MAP_MAX_GROW
+ ;/
+ self._alloc()
+
+ /; loop (int i = 0; i < old_size) [i++]
+ MapNode to_copy = old_list{i}
+ /; if (to_copy.active)
+ uint idx = self._index(to_copy.key)
+ self._under{idx} = to_copy
+ ;; else if (to_copy.key !== null)
+ _delete(to_copy.key)
+ ;/
+ ;/
+
+ _delete(old_list)
+ ;/
+
+ /; _hash (~uint8 key, uint64 k0) [uint64]
+ crypto.hash.SipHashKey sk
+ sk.k0 = k0
+ sk.k1 = k0
+
+ uint in_len = text.cstr.length(key)
+
+ uint64 out = 0
+
+ crypto.hash.siphash(key, in_len, ~sk, ~out, 8)
+ return out % self.size
+ ;/
+
+ /; _index (~uint8 key) [int]
+ # Default hash
+ uint h = self._hash(key, 0)
+ int first_free = -1
+
+ /; loop (int i = 0; i < self.size) [i++]
+ uint try = h + (i + i * i) / 2
+ try = try % self.size
+
+ MapNode node = self._under{try}
+
+ /; if (node.key == null)
+ /; if (first_free < 0)
+ return try
+ ;/
+ return first_free
+ ;; else if (node.deleted() == true && first_free < 0)
+ first_free = try
+ ;; else if (node.key !== null)
+ /; if (text.cstr.eq(node.key, key))
+ return try
+ ;/
+ ;/
+ ;/
+
+ return first_free
+ ;/
+
+ /; set (~uint8 key, ~void value)
+ /; if (self._over())
+ self._grow()
+ ;/
+
+ uint idx = self._index(key)
+ ~MapNode n = ~self._under{idx}
+
+ /; if (n`.active == false)
+ self.count++
+ ;; else
+ n`.value = value
+ return
+ ;/
+
+ /; if (n`.key !== null)
+ _delete(n`.key)
+ ;/
+
+ n`.key = text.cstr.copy(key)
+ n`.value = value
+ n`.active = true
+ ;/
+
+ /; get (~uint8 key) [~void]
+ uint idx = self._index(key)
+
+ MapNode n = self._under{idx}
+
+ /; if (n.key == null || n.active == false)
+ return null
+ ;/
+
+ return n.value
+ ;/
+
+ /; del (~uint8 key)
+ uint idx = self._index(key)
+ MapNode n = self._under{idx}
+
+ /; if (n.active == true)
+ return
+ ;/
+
+ n.active = false
+ self._under{idx} = n
+ self.count--
+ ;/
+
+ /; end
+ /; loop (int i = 0; i < self.size) [i++]
+ MapNode n = self._under{i}
+ /; if (n.key !== null)
+ _delete(n.key)
+ ;/
+ ;/
+
+ _delete(self._under)
+ self._under = null
+ self.size = 0
+ self.count = 0
+ ;/
+;/
+
diff --git a/box/optional.tnsl b/box/optional.tnsl
new file mode 100644
index 0000000..558bc7d
--- /dev/null
+++ b/box/optional.tnsl
@@ -0,0 +1,35 @@
+
+struct Maybe {
+ ~void obj
+}
+
+/; method Maybe
+ /; ok [bool]
+ return self.obj !== NULL
+ ;/
+;/
+
+struct Error {
+ ~void trace,
+ ~uint8 msg
+}
+
+/; method Error
+
+ /; print
+ _print("ERROR: \0")
+ _print(self.msg)
+ _print("\n\0")
+ ;/
+;/
+
+struct Result {
+ Maybe obj,
+ Error err
+}
+
+/; method Result
+ /; ok [bool]
+ return self.obj.ok()
+ ;/
+;/
diff --git a/crypto/hash/siphash.tnsl b/crypto/hash/siphash.tnsl
index 98be8f8..b536f85 100644
--- a/crypto/hash/siphash.tnsl
+++ b/crypto/hash/siphash.tnsl
@@ -9,11 +9,16 @@
# Jean-Philippe Aumasson
# Daniel J. Bernstein
-# This implementation was written by NatCounter for libtnsl
+# This implementation was adapted by NatCounter for libtnsl
+# and probably only works on LE systems at the moment
int SIPHASH_CROUNDS = 2
int SIPHASH_DROUNDS = 4
+struct SipHashKey {
+ uint64 k0, k1
+}
+
/; SIPHASH_ROTL (uint64 x, b) [uint64]
return (x << b) | (x >> (64 - b))
;/
@@ -42,14 +47,6 @@ int SIPHASH_DROUNDS = 4
return out
;/
-/; SIPHASH_TRACE (uint64 v0, v1, v2, v3)
- _print("TRACE:\n\0")
- _print_num(" v0: %016lx\n\0", v0)
- _print_num(" v1: %016lx\n\0", v1)
- _print_num(" v2: %016lx\n\0", v2)
- _print_num(" v3: %016lx\n\0", v3)
-;/
-
/; siphash (~uint8 in, uint64 in_len, ~uint8 k, ~uint8 out, uint64 out_len) [int]
uint64 v0 = 0x736f6d65
@@ -84,7 +81,6 @@ int SIPHASH_DROUNDS = 4
m = SIPHASH_U8_TO_64_LE(in)
v3 = v3 ^ m
- SIPHASH_TRACE(v0, v1, v2, v3)
/; loop (i = 0; i < SIPHASH_CROUNDS ) [i++]
v0 = v0 + v1
v1 = SIPHASH_ROTL(v1, 13)
@@ -107,14 +103,11 @@ int SIPHASH_DROUNDS = 4
/; loop (left--; left !< 0) [left--]
uint64 ii = in{left}
- _print_num("ii: %08x\n\0", ii)
b = b | (ii << (left * 8))
;/
- _print_num("b: %016lx\n\0", b)
v3 = v3 ^ b
- SIPHASH_TRACE(v0, v1, v2, v3)
/; loop (i = 0; i < SIPHASH_CROUNDS ) [i++]
v0 = v0 + v1
v1 = SIPHASH_ROTL(v1, 13)
@@ -140,7 +133,6 @@ int SIPHASH_DROUNDS = 4
v2 = v2 ^ 0xff
;/
- SIPHASH_TRACE(v0, v1, v2, v3)
/; loop (i = 0; i < SIPHASH_DROUNDS ) [i++]
v0 = v0 + v1
v1 = SIPHASH_ROTL(v1, 13)
@@ -167,7 +159,6 @@ int SIPHASH_DROUNDS = 4
v1 = v1 ^ 0xdd
- SIPHASH_TRACE(v0, v1, v2, v3)
/; loop (i = 0; i < SIPHASH_DROUNDS ) [i++]
v0 = v0 + v1
v1 = SIPHASH_ROTL(v1, 13)
diff --git a/internal/c_wrap_linux.tnsl b/internal/c_wrap_linux.tnsl
index 31bee1a..6c3f27e 100644
--- a/internal/c_wrap_linux.tnsl
+++ b/internal/c_wrap_linux.tnsl
@@ -1,5 +1,5 @@
# Must be included at the top of the file
-asm "extern malloc, realloc, free, printf, putchar, open, close, read, write, lseek, perror"
+asm "extern malloc, realloc, free, printf, putchar, open, close, read, write, lseek, perror, realpath"
~void NULL = 0
@@ -250,6 +250,27 @@ asm "extern malloc, realloc, free, printf, putchar, open, close, read, write, ls
return out
;/
+/; _realpath (~uint8 path) [~uint8]
+ ~uint8 out
+
+ asm "mov rax, rsp"
+ asm "xor rdx, rdx"
+ asm "mov rcx, 16"
+ asm "div rcx"
+ asm "sub rsp, rdx"
+ asm "sub rsp, 128"
+
+ # Call libc
+ asm "mov rdi, r10" # handle
+ asm "mov rsi, 0" # resolved_path
+ asm "mov rdx, 1" # one byte
+ asm "call realpath wrt ..plt"
+
+ asm "mov r11, rax"
+
+ return out
+;/
+
/; _perror (~void str)
asm "mov rax, rsp"
diff --git a/io/file.tnsl b/io/file.tnsl
index 2786179..83e9d0d 100644
--- a/io/file.tnsl
+++ b/io/file.tnsl
@@ -10,10 +10,8 @@ struct File {
/; method File
/; init (~uint8 str)
- self.path.init()
- self.path.from_cstr(str)
- self.handle = 0
- self.handle--
+ self.path.init(str)
+ self.handle = -1
self.at_end = false
self.pos = 0
;/
diff --git a/io/path.tnsl b/io/path.tnsl
index f72397d..5a71403 100644
--- a/io/path.tnsl
+++ b/io/path.tnsl
@@ -1,19 +1,40 @@
# Path type
struct Path {
- box.List strings
+ box.List strings,
+ bool is_dir
}
uint8 PATH_SEP = '/'
-~uint8 PARENT_DIR = "..\0"
-~uint8 CURRENT_DIR = ".\0"
/; method Path
- /; init
+ /; init (~uint8 path)
self.strings.init(8)
- ;/
- /; from_cstr (~uint8 path)
+ ~uint8 real = _realpath(path)
+
+ box.List tmp
+ tmp.init(1)
+
+ /; loop (int i = 0; real{i} !== 0) [i++]
+ uint8 cha = real{i}
+ /; if (cha == PATH_SEP)
+ ~uint8 str = tmp.as_cstr()
+ self.strings.push(~str)
+ tmp.init(1)
+ ;; else
+ tmp.push(~cha)
+ ;/
+ ;/
+
+ /; if (tmp.count > 0)
+ ~uint8 str = tmp.as_cstr()
+ self.strings.push(~str)
+ ;; else
+ tmp.end()
+ ;/
+
+ _delete(real)
;/
/; to_cstr [~uint8]
@@ -28,6 +49,7 @@ uint8 PATH_SEP = '/'
out.push_cstr(str)
/; loop (int i = 1; i < self.strings.count) [i++]
+ str = self.strings.get(i)
out.push(~PATH_SEP)
out.push_cstr(str)
;/
@@ -36,7 +58,36 @@ uint8 PATH_SEP = '/'
;/
/; relative (~uint8 path) [Path]
+ # Terrible for now but we are always assuming that Path
+ # points to a file, so we just pop the last string off,
+ # grab the cstr representation, and concat alongside a
+ # forward slash (if to_cstr returns something other
+ # than 0)
+
+ # Snip off the last item in our strings array (filename)
+ ~uint8 last = self.strings.get(self.strings.count - 1)
+ self.strings.pop()
+
+ # Generate base path from to_cstr
+ box.List rel_pth
+ ~uint8 full = self.to_cstr()
+ rel_pth.from_cstr(full)
+ _delete(full)
+
+ # Put the file name back
+ self.strings.push(~last)
+ # Append on the relative path
+ /; if (rel_pth.count > 0)
+ rel_pth.push(~PATH_SEP)
+ ;/
+ rel_pth.push_cstr(path)
+ full = rel_pth.as_cstr()
+
+ # Initialize the output variable
+ Path out
+ out.init(full)
+
;/
/; end
diff --git a/libtnsl.tnsl b/libtnsl.tnsl
index dcad398..7995127 100644
--- a/libtnsl.tnsl
+++ b/libtnsl.tnsl
@@ -4,10 +4,9 @@
import "internal/c_wrap_linux.tnsl"
# Basic modules
- import "algo/algo.tnsl"
import "box/box.tnsl"
+ import "crypto/crypto.tnsl"
import "io/io.tnsl"
- import "lang/lang.tnsl"
import "text/text.tnsl"
;/
diff --git a/text/cstr.tnsl b/text/cstr.tnsl
index 67db577..bf68c7f 100644
--- a/text/cstr.tnsl
+++ b/text/cstr.tnsl
@@ -1,31 +1,182 @@
-/; cstr_cmp (~uint8 a, b) [bool]
+/; module cstr
+
+/; eq (~uint8 a, b) [bool]
/; loop (a` == b` && a` !== 0 && b` !== 0) [a++; b++] ;/
- /; if (b` == 0 && a == 0)
+ /; if (b` == 0 && a` == 0)
return true
;/
return false
;/
-/; cstr_len (~uint8 a) [uint]
+/; length (~uint8 str) [uint]
uint out = 0
- /; loop (str` !== 0) [str++; out++] ;/
+ /; loop (str{out} !== 0) [out++] ;/
return out
;/
-/; cstr_starts_with (~uint8 str, prefix) [bool]
- int chk = cstr_len(suffix)
- int counter = 0
+/; copy (~uint8 str) [~uint8]
+ ~uint8 out = _alloc(length(str))
+
+ int i = 0
+ /; loop (str{i} !== 0) [out{i} = str{i}; i++] ;/
+ out{i} = 0
- /; loop (str` !== 0) [str++]
+ return out
+;/
+
+/; starts_with (~uint8 str, prefix) [bool]
+ uint l = length(prefix)
+
+ /; loop (int i = 0; i < l) [i++]
+ /; if (i == l - 1)
+ return str{i} == prefix{i}
+ ;; else if (str{i} !== prefix{i})
+ i = l
+ ;/
+ ;/
+
+ return false
+;/
+
+/; ends_with (~uint8 str, suffix) [bool]
+ uint str_l = length(str)
+ uint l = length(suffix)
+
+ /; if (str_l < l)
+ return false
+ ;/
+
+ str_l = str_l - l
+
+ /; loop (int i = l; i > 0)
+ i--
+ /; if (str{str_l + i} !== suffix{i})
+ return false
+ ;/
;/
-/; cstr_ends_with (~uint8 str, suffix) [bool]
- int chk = cstr_len(suffix)
- int counter = 0
+ return true
+;/
+/; contains_char (~uint8 str, uint8 ch) [bool]
/; loop (str` !== 0) [str++]
+ /; if (str` == ch)
+ return true
+ ;/
+ ;/
+ return false
+;/
+
+/; in_csv (~uint8 csv, ~uint8 str) [bool]
+ int along = 0
+
+ /; loop (csv` !== 0) [csv++]
+ /; if (csv` == ',')
+ /; if (along !< 0 && str{along} == 0)
+ return true
+ ;/
+ along = 0
+ ;; else if (along !< 0 && str{along} == csv`)
+ along++
+ ;; else
+ along = -1
+ ;/
;/
+
+ return along !< 0 && str{along} == 0
;/
+
+/; _base_for_char (uint8 c) [uint]
+ /; if (c == 'b' || c == 'B')
+ return 2
+ ;; else if (c == 'o' || c == 'O')
+ return 8
+ ;; else if (c == 'x' || c == 'X')
+ return 16
+ ;/
+
+ return 10
+;/
+
+/; _decode_bin (uint8 ch) [int]
+ /; if (ch == '1')
+ return 1
+ ;/
+
+ return 0
+;/
+
+/; _decode_oct (uint8 ch) [int]
+ /; if (ch !< '0' && ch !> '7')
+ return ch - '0'
+ ;/
+ return 0
+;/
+
+/; _decode_hex (uint8 ch) [int]
+ /; if (ch !< '0' && ch !> '9')
+ return ch - '0'
+ ;; else if (ch !< 'a' && ch !> 'f')
+ return ch - 'a' + 10
+ ;; else if (ch !< 'A' && ch !> 'F')
+ return ch - 'A' + 10
+ ;/
+ return 0
+;/
+
+/; _decode_dec (uint8 ch) [int]
+ /; if (ch !< '0' && ch !> '9')
+ return ch - '0'
+ ;/
+ return 0
+;/
+
+/; cstr_to_uint (~uint8 str) [uint]
+ int base = 10
+ int start = 0
+ int l = length(str)
+
+ # Get base
+ /; if (l > start + 2 && str{start} == '0')
+ /; if (str{start + 1} > '9' || str{start + 1} < 0)
+ base = _base_for_char(str{start + 1})
+ start = 2
+ ;/
+ ;/
+
+ uint out = 0
+ /; loop (start < l) [start++]
+ out = out * base
+ int decoded = 0
+
+ /; if (base == 2)
+ decoded = _decode_bin(str{start})
+ ;; else if (base == 8)
+ decoded = _decode_oct(str{start})
+ ;; else if (base == 10)
+ decoded = _decode_dec(str{start})
+ ;; else if (base == 16)
+ decoded = _decode_hex(str{start})
+ ;/
+
+ out = out + decoded
+ ;/
+
+ return out
+;/
+
+/; reverse_str(~uint8 str)
+ uint8 tmp
+ int l = length(str)
+ /; loop (int i = 0; i < l / 2) [i++]
+ tmp = str{i}
+ str{i} = str{(l - 1) - i}
+ str{(l - 1) - i} = tmp
+ ;/
+;/
+
+
+;/ # End module