From 7334c4b65d6722911c20af34f830ddc6c69b57e5 Mon Sep 17 00:00:00 2001 From: JJ Date: Thu, 16 May 2024 20:26:51 -0700 Subject: std: switch from chr to char --- docs/SYNTAX.md | 6 ++---- docs/book/SYNTAX.html | 6 ++---- std/ast.pk | 31 ++++++++++++++++++------------- std/prelude/io.pk | 2 +- std/prelude/iterators.pk | 2 +- std/prelude/lists.pk | 4 ++-- std/prelude/numbers.pk | 4 ++-- std/prelude/strings.pk | 18 +++++++++--------- 8 files changed, 37 insertions(+), 36 deletions(-) diff --git a/docs/SYNTAX.md b/docs/SYNTAX.md index 4e57b04..c6e96f6 100644 --- a/docs/SYNTAX.md +++ b/docs/SYNTAX.md @@ -248,7 +248,7 @@ The following identifiers are in use by the standard prelude: - logic: `not` `and` `or` `xor` `shl` `shr` `div` `mod` `rem` - logic: `+` `-` `*` `/` `<` `>` `<=` `>=` `==` `!=` `is` - async: `async` `await` -- types: `int` `uint` `float` `i\d+` `u\d+` +- types: `int` `uint` `float` `i[\d+]` `u[\d+]` - `f32` `f64` `f128` - `dec64` `dec128` - types: `bool` `byte` `char` `str` @@ -364,9 +364,7 @@ Signature ::= Ident Generics? ('(' Type (',' Type)* ')')? (':' Type)? ``` If ::= 'if' Expr 'then' Body ('elif' Expr 'then' Body)* ('else' Body)? When ::= 'when' Expr 'then' Body ('elif' Expr 'then' Body)* ('else' Body)? -Try ::= 'try' Body - ('except' Ident ('as' Ident)? (',' Ident ('as' Ident)?)*) 'then' Body)+ - ('finally' Body)? +Try ::= 'try' Body ('with' Pattern (',' Pattern)* 'then' Body)+ ('finally' Body)? Match ::= 'match' Expr ('of' Pattern (',' Pattern)* ('where' Expr)? 'then' Body)+ While ::= 'while' Expr 'do' Body For ::= 'for' Pattern 'in' Expr 'do' Body diff --git a/docs/book/SYNTAX.html b/docs/book/SYNTAX.html index a241b74..4860718 100644 --- a/docs/book/SYNTAX.html +++ b/docs/book/SYNTAX.html @@ -378,7 +378,7 @@ of that then ...
not
and
or
xor
shl
shr
div
mod
rem
+
-
*
/
<
>
<=
>=
==
!=
is
async
await
int
uint
float
i\d+
u\d+
+int
uint
float
i[\d]+
u[\d]+
f32
f64
f128
dec64
dec128
If ::= 'if' Expr 'then' Body ('elif' Expr 'then' Body)* ('else' Body)?
When ::= 'when' Expr 'then' Body ('elif' Expr 'then' Body)* ('else' Body)?
-Try ::= 'try' Body
- ('except' Ident ('as' Ident)? (',' Ident ('as' Ident)?)*) 'then' Body)+
- ('finally' Body)?
+Try ::= 'try' Body ('with' Pattern (',' Pattern)* 'then' Body)+ ('finally' Body)?
Match ::= 'match' Expr ('of' Pattern (',' Pattern)* ('where' Expr)? 'then' Body)+
While ::= 'while' Expr 'do' Body
For ::= 'for' Pattern 'in' Expr 'do' Body
diff --git a/std/ast.pk b/std/ast.pk
index 4b8a783..21a5569 100644
--- a/std/ast.pk
+++ b/std/ast.pk
@@ -1,13 +1,14 @@
## std.ast: Exposes the AST for building and operating on with macros.
## The `Expr` type represents the abstract syntax tree of Puck itself.
-## It notably lacks type information.
-## It is, however, syntactically correct-by-construction.
+## It notably lacks type information. It is also not necessarily syntactically
+## correct-by-construction: Cond, Try, and Match expressions must have at least
+## one branch in their branches (yet this is not expressible here).
pub type Expr = union
Ident(string)
Number(int)
Float(float)
- Char(chr)
+ Char(char)
String(str)
Struct(list[(field: str, value: Expr)])
Tuple(list[(field: str?, value: Expr)])
@@ -29,17 +30,25 @@ pub type Expr = union
params: list[(id: Pattern, kind: Type?)],
kind: Type?,
body: list[Expr])
- TypeDecl(public: bool, id: str, generics: list[str], alias: Type)
- Module(public: bool, id: str, body: list[Expr])
+ TypeDecl(
+ public: bool,
+ id: str,
+ generics: list[str],
+ body: Type)
+ Module(
+ public: bool,
+ id: str,
+ generics: list[str],
+ body: list[Expr])
Use(path: str)
Call(id: str, params: list[Expr])
Cond(
branches: list[(cond: Expr, body: list[Expr])],
- else_body: list[Expr]?)
+ else_body: list[Expr])
Try(
try_body: list[Expr],
catches: list[(exceptions: list[str], body: list[Expr])],
- finally_body: list[Expr]?)
+ finally_body: list[Expr])
Match(
item: ref Expr,
branches: list[(pattern: Pattern, guard: Expr?, body: list[Expr])])
@@ -54,15 +63,11 @@ pub type Type = ref union
Int(size: uint)
Dec(size: uint)
Float(size: uint)
- String
Func(from: list[Type], to: Type)
Struct(list[(id: str, kind: Type)])
Tuple(list[(id: str?, kind: Type)])
Union(list[(id: str, kind: Type)])
- Class( # todo: generics
- funcs: list[(id: str, from: list[Type], to: Type?)]
- for_type: Type?
- )
+ Class(list[(id: str, from: list[Type], to: Type?)])
Array(size: uint, kind: Type)
List(Type)
Slice(Type) # todo: plus ownership
@@ -76,7 +81,7 @@ pub type Type = ref union
pub type Pattern = union
Ident(str)
- Number(int), Float(float), Char(chr), String(str)
+ Number(int), Float(float), Char(char), String(str)
Struct(name: str, params: list[Pattern])
Tuple(list[Pattern])
List(list[Pattern])
diff --git a/std/prelude/io.pk b/std/prelude/io.pk
index 026d12a..d44f403 100644
--- a/std/prelude/io.pk
+++ b/std/prelude/io.pk
@@ -27,7 +27,7 @@ pub func read(file: File): str? =
pub func lines(file: File): Iter[str] =
...
-pub func chars(file: File): Iter[chr] =
+pub func chars(file: File): Iter[char] =
...
pub func exists(file: File): bool =
diff --git a/std/prelude/iterators.pk b/std/prelude/iterators.pk
index 1403b55..1e60379 100644
--- a/std/prelude/iterators.pk
+++ b/std/prelude/iterators.pk
@@ -159,7 +159,7 @@ pub func unzip[T, U](self: Iter[(T, U)]): (Iter[T], Iter[U])
# equivalent of collect. useful for type conversion
pub func to[T](self: LazyIter[T]): list[T]
-pub func to(self: LazyIter[chr]): str
+pub func to(self: LazyIter[char]): str
pub type BoundIter[T] = class
next(mut Self): T?
diff --git a/std/prelude/lists.pk b/std/prelude/lists.pk
index 1bce693..c4b8739 100644
--- a/std/prelude/lists.pk
+++ b/std/prelude/lists.pk
@@ -7,8 +7,8 @@
@[opaque] # opaque on a struct tells us raw field access breaks invariants.
pub type list[T] = struct
data: ptr T
- capacity: int
- length: int
+ capacity: uint
+ length: uint
## A transparent, common alias for a list of bytes.
pub type bytes = list[byte]
diff --git a/std/prelude/numbers.pk b/std/prelude/numbers.pk
index 801591a..aad747c 100644
--- a/std/prelude/numbers.pk
+++ b/std/prelude/numbers.pk
@@ -65,10 +65,10 @@ pub type dec128
pub type byte = u8
## A primitive char type. 4 bytes wide.
## These represent distinct Unicode characters, and are useful with `str`.
-pub type chr = u32
+pub type char = u32
## Get the underlying length of a char, in bytes. 1-4.
-pub func len(self: chr): uint =
+pub func len(self: char): uint =
...
## The IEEE floating point value of *Not a Number*.
diff --git a/std/prelude/strings.pk b/std/prelude/strings.pk
index a8c72fb..5c71047 100644
--- a/std/prelude/strings.pk
+++ b/std/prelude/strings.pk
@@ -22,7 +22,7 @@ pub func len(self: lent str): uint =
res
## Pushes a character to the end of a mutable string.
-pub func push(self: mut str, val: chr) =
+pub func push(self: mut str, val: char) =
self.data.push(val.byte) # todo: obsolete by from/to conversion??
## Pushes an owned string to the end of a mutable string.
@@ -34,29 +34,29 @@ pub func push(self: mut str, val: str) =
## SAFETY: We return early upon an empty string.
## And decrement by one char for a non-empty string.
@[safe]
-pub func pop(self: mut str): chr? =
+pub func pop(self: mut str): char? =
let char = self.chars.rev.next?
self.data.set_len(self.len - char.len) # this is normally unsafe.
Some(char)
## Returns the character at the provided index, if it exists.
-pub func get(self: str, i: uint): chr? =
+pub func get(self: str, i: uint): char? =
...
## Sets the character at the provided index, if it exists.
## As strings are packed, this may call str.grow and reallocate.
## oh fuck we have to insert + remove anyway
-pub func set(self: mut str, i: uint, val: chr) =
+pub func set(self: mut str, i: uint, val: char) =
...
## Inserts a character at an arbitrary position within a string.
## Panics on failure. (todo: can we do better?)
-pub func insert(self: mut str, i: uint, val: chr) =
+pub func insert(self: mut str, i: uint, val: char) =
...
## Removes and returns a character at an arbitrary position within a string.
## Panics on failure. (todo: can we do better?)
-pub func remove(self: mut str, i: uint): chr =
+pub func remove(self: mut str, i: uint): char? =
...
## Syntactic sugar for string appending.
@@ -70,9 +70,9 @@ pub func &(a: str, b: str): str =
## Conversion from a string to a list of bytes. Zero-cost.
pub func to(self: str): list[byte] = self.data
-## Conversion from a str to a list[chr]. Reallocates.
-pub func to(self: str): list[chr] =
- var res: list[chr]
+## Conversion from a str to a list[char]. Reallocates.
+pub func to(self: str): list[char] =
+ var res: list[char]
for char in self do res.push(char)
res
## Conversion from a char to an array of bytes. Zero-cost.
--
cgit v1.2.3-70-g09d2