This commit is contained in:
Kyler Olsen 2025-10-26 16:57:31 -06:00
parent 366eec6546
commit 0bd7259fc3
1 changed files with 73 additions and 40 deletions

View File

@ -1,6 +1,6 @@
# Stack Language Specification
**Version**: 0.6.1
**Version**: 0.6.2
**Status**: Draft Specification
**Changes**:
- 0.5 (AI)
@ -42,9 +42,16 @@
3. **`inher` operator** - Decision: No, merge inheritance into trait operator (removed `inher`)
4. **Separate `inher` vs `impl`** - Decision: Keep them separate (impl is for implementations only)
- 0.6.2 (Human) - In progress
1. Created TODOs
2. Made various corrections (not everything was corrected)
3. Reviewed and corrected some code blocks
4. Made a few adjustments manually
5. Reversed removing `inher`
> **INSTRUCTIONS FOR AI:** AI agents are not allowed to change human reviewed md code blocks. If an AI reviewer thinks a change needs to be made to one of these blocks, the AI model can add a human todo explaining a suggested change or problem. It is totally possible for a md code block that was human reviewed and be correct for the current specification and new TODOs would change it, AIs are still not allowed to change it, only add a human todo. Reviewed md code blocks could also implement new TODOs already.
> **TODO: (FOR HUMAN)** These specifications may need to be reorganized. ie. do we need both "Stack Manipulation Trait" in 4.1 and "5. Stack Operations", etc. Should some of these be in the appendix?
> **TODO: (FOR HUMAN)** These specifications may need to be reorganized. ie. do we need both "Stack Manipulation Trait" in 4.1 and "5. Stack Operations", etc. Should some of these be in the appendix. What is the best structure for this document?
> **TODO:** Syntax like `(T -- T) { dup * } ::square<T:Multiplyable> fn` is not allowed, the correct syntax is `(Multiplyable -- Multiplyable) { dup * } ::square fn`.
> **TODO:** "Example - Actual Language Definitions" and variations of this are listed all over, but they are mostly not definitions of parts of the base language, just examples of language syntax. Other times they are definitions of parts of the base language used as examples and should just be examples. Basically this is improperly used and needs to be cleared out.
---
@ -170,11 +177,11 @@ Type tuples represent stack effects and are used in function signatures to speci
**Variable Arguments**: For operations that need variable numbers of arguments, use array syntax:
> **For AI Reviewers @ 0.6:** The following block has been human verified to be syntactically and logically correct. **NO AI IS ALLOWED TO CHANGE THIS.** If there is a discrepancy between this block and others, follow the syntax in this block.
> **TODO:** `(String [Stringifiable] --) printf:` should be `(String Iterable<Stringifiable> --) printf:`
```
(-- Size) depth: // Zero arguments, returns stack depth
(String Stringifiable[] --) printf: // String plus array of printable values
(String [Stringifiable] --) printf: // String plus array of printable values
([T] -- T) sum: // Array of values, returns sum
```
@ -342,7 +349,8 @@ The `Stackable` trait provides fundamental stack manipulation operations:
The `Size` trait represents types suitable for indexing and sizing operations:
```
[ ::Addable ::Comparable ::Convertible<i64> ] { } ::Size trait
[ ::Addable ::Comparable ::Convertible<i64> ] ::Size inher
{ } ::Size trait
```
Types implementing `Size` can be used as indices, loop bounds, and array sizes. Standard implementations include all integer types (`i8`, `i16`, `i32`, `i64`, `u8`, `u16`, `u32`, `u64`).
@ -496,7 +504,8 @@ Types implementing `Size` can be used as indices, loop bounds, and array sizes.
The `Number` trait represents the full suite of numeric operations by inheriting from multiple traits:
```
[ ::Addable ::Multiplyable ::Exponentiable ::Comparable ::Logarithmic ] { } ::Number trait
[ ::Addable ::Multiplyable ::Exponentiable ::Comparable ::Logarithmic ] ::Number inher
{ } ::Number trait
```
**Meta-Traits**
@ -517,12 +526,10 @@ Traits for defining and working with traits themselves:
### 4.2 Trait Definition
**Syntax**: `inheritance_array? { function_signatures } ::identifier<type_params>? trait`
**Syntax**: `{ function_signatures } ::identifier<type_params>? trait`
Traits can be defined with or without method signatures. Empty traits are valid and are typically used when inheriting from other traits to create composite traits (marker traits).
**Inheritance**: Traits can inherit from other traits by specifying an array of trait identifiers before the trait body. The inheritance array is optional.
**Example - Actual Language Definitions**:
```
// Trait with methods (no inheritance)
@ -547,10 +554,12 @@ Traits can be defined with or without method signatures. Empty traits are valid
{ } ::Serializable trait
// Trait with inheritance (no additional methods)
[ ::Orderable ::Equatable ] { } ::Comparable trait
[ ::Orderable ::Equatable ] ::Comparable inher
{ } ::Comparable trait
// Trait with inheritance and additional methods
[ ::Drawable ::Transformable ] {
[ ::Drawable ::Transformable ] ::GameObject inher
{
(Self -- ) update:
(Self -- ) destroy:
} ::GameObject trait
@ -568,6 +577,8 @@ Within the TokenString (the `{ }` block), identifiers like `Self`, `add:`, `draw
3. It is not required for all operators of a trait to be in the same implementation block - implementations can be split across multiple blocks
4. Later implementations can override earlier ones for the same type
> **TODO:** Yes the following is an example. No correction is needed.
**Example**:
> **For AI Reviewers @ 0.5:** The following block has been human verified to be syntactically and logically correct. **NO AI IS ALLOWED TO CHANGE THIS.** If there is a discrepancy between this block and others, follow the syntax in this block.
@ -594,7 +605,7 @@ Within the TokenString (the `{ }` block), identifiers like `Self`, `add:`, `draw
} ::Rectangle impl
```
> **TODO:** Yes the following are actual language definitions, but they are used here as an example.
> **TODO:** Yes the following are actual language definitions, but they are also list here as an example.
**Example - Actual Language Definitions (Human Verified)**:
@ -670,7 +681,7 @@ Within the TokenString (the `{ }` block), identifiers like `Self`, `add:`, `draw
### 4.4 Trait Inheritance
**Syntax**: `[ identifier_list ] { methods } ::identifier<type_params>? trait`
**Syntax**: `[ identifier_list ] ::identifier<type_params>? inher { methods } ::identifier<type_params>? trait`
Trait inheritance is specified by providing an array of trait identifiers before the trait body in the trait definition. The trait body may be empty if the trait only serves to combine inherited traits.
@ -681,22 +692,27 @@ Trait inheritance is specified by providing an array of trait identifiers before
**Example - Actual Language Definitions**:
```
// Combine multiple arithmetic traits
[ ::Addable ::Multiplyable ] { } ::BasicNumber trait
[ ::Addable ::Multiplyable ] ::BasicNumber inher
{ } ::BasicNumber trait
// Full Number inherits everything numeric
[ ::Addable ::Multiplyable ::Exponentiable ::Comparable ::Logarithmic ] { } ::Number trait
[ ::Addable ::Multiplyable ::Exponentiable ::Comparable ::Logarithmic ] ::Number inher
{ } ::Number trait
// Inheritance with additional methods
[ ::Drawable ::Transformable ::Collidable ] {
[ ::Drawable ::Transformable ::Collidable ] ::GameObject inher
{
(Self -- ) update:
(Self -- ) destroy:
} ::GameObject trait
// Size trait with composite behavior
[ ::Addable ::Comparable ::Convertible<i64> ] { } ::Size trait
[ ::Addable ::Comparable ::Convertible<i64> ] ::Size inher
{ } ::Size trait
// String trait inheriting and adding methods
[ ::Concatenable ] {
[ ::Concatenable ] ::String inher
{
(Self Size Size -- Self) substr:
(Self Self -- ArrayOf<Self>) split:
} ::String trait
@ -706,17 +722,20 @@ Trait inheritance is specified by providing an array of trait identifiers before
```
// Inheriting trait is also generic (generic parameter matches)
[ ::Container<T> ] {
[ ::Container<T> ] ::Stack<T> inher
{
(Self -- T) pop:
} ::Stack<T> trait
// Inheriting trait specifies concrete type
[ ::Container<i32> ] {
[ ::Container<i32> ] ::IntStack inher
{
(Self -- i32) pop:
} ::IntStack trait
// Multiple generic inheritance
[ ::Selectable<T> ::Sized ::Sliceable ] { } ::ArrayOf<T> trait
[ ::Selectable<T> ::Sized ::Sliceable ] ::ArrayOf<T> inher
{ } ::ArrayOf<T> trait
```
### 4.5 Using Traits in Functions
@ -767,9 +786,9 @@ roll // ( n times -- ) Rotate n items, times times
4 3 roll // Rotate top 4 items three times: a d e c b
```
## 6. Operators (Postfix)
## 6. Operators
**Every operator is backed by a trait and must be implemented for types that use it.**
> **TODO:** This should be renamed where it does not include all operators.
### 6.1 Arithmetic
@ -814,7 +833,7 @@ true not // ( a -- result ) Logical NOT - inverts truthiness
8 2 shr // ( a n -- result ) Right shift - shift a right by n bits
```
## 7. Functions (Postfix Definition)
## 7. Functions
Functions are defined in postfix notation. The signature and body come before the name.
@ -858,7 +877,7 @@ Functions are defined in postfix notation. The signature and body come before th
} ::abs fn
```
## 8. Control Flow (Postfix)
## 8. Control Flow
### 8.1 Conditionals
@ -969,7 +988,7 @@ status {
} match
```
## 9. Data Structures (Postfix)
## 9. Data Structures
### 9.1 Struct Definition
@ -999,7 +1018,9 @@ status {
### 9.2 Struct Field Access
**Syntax (postfix)**: `struct ::field get` or `struct value ::field set`
> **TODO:** get consumes the identifier and struct, set consumes value and identifier.
**Syntax**: `struct ::field get` or `struct value ::field set`
**Example - Using Actual Definitions**:
```
@ -1052,7 +1073,7 @@ Status::Pending // Creates Status::Pending
Status::Active // Creates Status::Active
```
## 10. Array Operations (Postfix)
## 10. Array Operations
### 10.1 Basic Array Operations
@ -1108,7 +1129,7 @@ Array combinators take TokenString arguments containing the function bodies to a
[1 2 3 4] 2 window // Sliding window: [[1 2] [2 3] [3 4]]
```
## 11. Eval Operator (Postfix)
## 11. Eval Operator
Execute code dynamically at runtime. The `eval` operator parses and executes its TokenString argument immediately.
@ -1125,7 +1146,7 @@ Execute code dynamically at runtime. The `eval` operator parses and executes its
operation_name " get" concat eval
```
## 12. Standard Library Concepts
## 12. Standard Library
All standard library functions and traits are automatically in scope (no imports needed in current version). See Appendix C for future module system design.
@ -1288,7 +1309,8 @@ Provides fundamental stack manipulation operations.
} ::Equatable trait
// Combined comparison (inherits both)
[ ::Orderable ::Equatable ] { } ::Comparable trait
[ ::Orderable ::Equatable ] ::Comparable inher
{ } ::Comparable trait
```
### A.4 Logical Operations
@ -1345,6 +1367,12 @@ Provides fundamental stack manipulation operations.
(Self -- i64) length:
} ::Sized trait
// Iterable
{
// Get next item
(Self -- Self Option<T>) next:
} ::Iterable<T> trait
// Element access
{
// Get element at index
@ -1364,7 +1392,8 @@ Provides fundamental stack manipulation operations.
} ::Sliceable trait
// Complete array operations (inherits all above)
[ ::Sized ::Selectable<T> ::Sliceable ] { } ::ArrayOf<T> trait
[ ::Sized ::Selectable<T> ::Sliceable ] ::ArrayOf<T> inher
{ } ::ArrayOf<T> trait
```
### A.7 Conversion Traits
@ -1401,13 +1430,16 @@ Provides fundamental stack manipulation operations.
```
// Size for indexing
[ ::Addable ::Comparable ::Convertible<i64> ] { } ::Size trait
[ ::Addable ::Comparable ::Convertible<i64> ] ::Size inher
{ } ::Size trait
// Full numeric operations
[ ::Addable ::Multiplyable ::Exponentiable ::Comparable ::Logarithmic ] { } ::Number trait
[ ::Addable ::Multiplyable ::Exponentiable ::Comparable ::Logarithmic ] ::Number inher
{ } ::Number trait
// String operations
[ ::Concatenable ] {
[ ::Concatenable ] ::String inher
{
// Extract substring from start to end
(Self Size Size -- Self) substr:
@ -1598,7 +1630,8 @@ This would provide stronger type safety but add complexity to the type checker.
{ (Self Self -- Self) logb: (Self -- Self) log: (Self -- Self) ln: } ::Logarithmic trait
// Number inherits from multiple traits
[ ::Addable ::Multiplyable ::Exponentiable ::Comparable ::Logarithmic ] { } ::Number trait
[ ::Addable ::Multiplyable ::Exponentiable ::Comparable ::Logarithmic ] ::Number inher
{ } ::Number trait
```
### D.3 Logarithm Usage
@ -1763,13 +1796,13 @@ union_def ::= type_tuple token_string identifier_literal generic_params? "union"
enum_def ::= token_string identifier_literal "enum"
// Trait definition
trait_def ::= inheritance? token_string identifier_literal generic_params? "trait"
trait_def ::= (inheritance)? token_string identifier_literal generic_params? "trait"
// Trait implementation
trait_impl ::= identifier_literal token_string identifier_literal generic_params? "impl"
// Trait inheritance (now part of trait definition)
inheritance ::= "[" identifier_list "]"
// Trait inheritance
inheritance ::= "[" identifier_list "]" identifier_literal generic_params? "inher"
```
### E.4 Control Flow
@ -1802,7 +1835,7 @@ match_expr ::= expression token_string "match"
**Trait Impl**: `::trait_id { (sig) { body } method: ... } ::type_id<type_params>? impl`
**Trait Inheritance**: `[ ::trait1 ::trait2 ... ] { methods } ::identifier<type_params>? trait`
**Trait Inheritance**: `[ identifier_list ] ::identifier<type_params> inher`
**If**: `condition { then } { else } if`