Skip to content

Commit 17ec18d

Browse files
committed
Fully add first class types. This means types as Values.
1 parent 64aa7d1 commit 17ec18d

23 files changed

Lines changed: 598 additions & 40 deletions

README.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,25 @@ See [USAGE.md](./USAGE.md) and [examples](./examples/) for usage examples.
3939

4040
The syntax is s-expression like. There are only [builtin functions](#builtin-functions), identifiers and string literals.
4141

42-
| Syntax | Description |
43-
| ----------- | ------------------------------------------- |
44-
| `:a` | Reference to the variable `a` |
45-
| `?b` | Reference to the prompt `b` |
46-
| `!c` | Reference to the secret `c` |
47-
| `id` | Reference to the builtin `id` |
48-
| `@key` | Reference to the client context value `key` |
49-
| `(id :a)` | Call to builtin `id` with arguments: `:a` |
50-
| `` `foo` `` | String literal |
51-
| `true` | Literal boolean value `true` |
52-
| `false` | Literal boolean value `false` |
42+
| Syntax | Description |
43+
| ---------------------------------- | ---------------------------------------------------------- |
44+
| `:a` | Reference to the variable `a` |
45+
| `?b` | Reference to the prompt `b` |
46+
| `!c` | Reference to the secret `c` |
47+
| `id` | Reference to the builtin `id` |
48+
| `@key` | Reference to the client context value `key` |
49+
| `(id :a)` | Call to builtin `id` with arguments: `:a` |
50+
| `` `foo` `` | String literal |
51+
| `true` | Literal boolean value `true` |
52+
| `false` | Literal boolean value `false` |
53+
| `String` | Literal type `String` |
54+
| `Bool` | Literal type `Bool` |
55+
| `Type<String>` | Literal type `Type` of type `String` |
56+
| `Fn() -> Bool` | Literal type for builtin functions (no args) |
57+
| `Fn(Value) -> Bool` | Literal type for builtin functions (1 arg) |
58+
| `Fn(Value, Bool, String) -> Value` | Literal type for builtin functions (n args) |
59+
| `Fn(...String) -> String` | Literal type for builtin functions (variadic arg) |
60+
| `Fn(String, ...String) -> String` | Literal type for builtin functions (args and variadic arg) |
5361

5462
See [/spec](./spec/) for more syntax examples.
5563

USAGE.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ The lexer takes an input string and returns a stream of tokens.
2525
| ------------ | ----------------------------- | --------------------------------------------------------------------- |
2626
| `LParan` | `(` | Opening parantheses for a call expression |
2727
| `RParan` | `)` | Closing parantheses for a call expression |
28+
| `Comma` | `,` | Separator between arguments in `Fn` types |
29+
| `LAngle` | `<` | Generic type delimitor |
30+
| `RAngle` | `>` | Generic type delimitor |
31+
| `Arrow` | `->` | Separator between `Fn` arg parans and return type |
2832
| `Identifier` | `[!?:]?[a-zA-Z][a-zA-Z0-9_]*` | Identifer referencing a builtin function, variable, prompt, or secret |
2933
| `String` | `` `[^`]*` `` | A literal string of text delimited by backticks |
3034
| `True` | `true` | A literal boolean value of `true` |
@@ -103,9 +107,10 @@ pub struct ExprCall {
103107

104108
#### ExprIdentifier
105109

106-
An identifier referencing a builtin, variable, prompt, secret, or client value.
110+
An identifier referencing a builtin, type, variable, prompt, secret, or client value.
107111

108112
- `builtin_name`
113+
- `TypeName`, `TypeName<Type>`, `Fn(Type, ...Type) -> Type`
109114
- `:var_name`
110115
- `?prompt_name`
111116
- `!secret_name`
@@ -120,6 +125,7 @@ pub enum IdentifierKind {
120125
Prompt,
121126
Secret,
122127
Client,
128+
Type,
123129
}
124130

125131
let ident = ExprIdentifier::new(":foo");
@@ -226,6 +232,7 @@ Valid bytecode will always begin with 4 bytes representing the current language
226232
| `SECRET` | 3 | Secret (identifier prefixed with `!`) |
227233
| `USER_BUILTIN` | 4 | User provided builtin function |
228234
| `CLIENT_CTX` | 5 | Client provided value (identifier prefixed with `@`) |
235+
| `TYPE` | 6 | A type stored in `ExprByteCode` |
229236

230237
### Compile Time Environment
231238

@@ -267,6 +274,35 @@ pub struct FnArg {
267274

268275
See: [builtins.rs](./src/builtins.rs), [types.rs](./src/types.rs), [value.rs](./src/value.rs)
269276

277+
### ExprByteCode
278+
279+
The result of an expression compilation is `ExprByteCode`.
280+
281+
```rust
282+
pub struct ExprByteCode {
283+
version: [u8; 4],
284+
codes: Vec<u8>,
285+
strings: Vec<String>,
286+
types: Vec<Type>,
287+
}
288+
```
289+
290+
#### Version
291+
292+
The version of the compiler encoded as bytes.
293+
294+
#### Codes
295+
296+
The actual bytecode values.
297+
298+
#### Strings
299+
300+
An indexed list of strings encountered during compilation. These string indexes are referenced by the `opcode::CONSTANT` opcode.
301+
302+
#### Type
303+
304+
An indexed list of types encountered during compilation. These type indexes are referenced by the `opcode::GET` opcode and `lookup::TYPE` lookup.
305+
270306
### Usage
271307

272308
```rust

spec/invalid/not_type_id.expr.interpreted

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
RuntimeError(
44
TypeMismatch {
55
expected: Bool,
6-
actual: Type<Fn(Value) -> Value>,
6+
actual: Fn(Value) -> Value,
77
},
88
),
99
0..0,

spec/valid/type_literal_bool.expr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Bool
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
VERSION 0700
2+
----
3+
0000 GET TYPE 0 == 'Bool'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Type<Bool>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fn() -> Value
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Type<Fn() -> Value>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
String
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
VERSION 0700
2+
----
3+
0000 GET TYPE 0 == 'String'

0 commit comments

Comments
 (0)