Skip to content

Instantly share code, notes, and snippets.

@lovely-error
Last active August 4, 2020 06:38
Show Gist options
  • Save lovely-error/ec6a9eff536d81d6c916c7443bccc4bd to your computer and use it in GitHub Desktop.
Save lovely-error/ec6a9eff536d81d6c916c7443bccc4bd to your computer and use it in GitHub Desktop.
A few sketches of klim code
```
//when ever you create an object and do not specify mutability specification
//S-CLISP treats that object with whole range (iso..tag) mutability
object Box {} //is similar to
object iso..tag Box {}
//this means that you can instantiate object with the mutability you need.
//there is 6 mutability sybtypes for every object
//iso - in local scope: one writer, one reader
//trn - in local scope: one writer, many readers
//ref - in local scope: many writers, many readers
//val - in global scope: no writer, many readers
//box - in local scope: no writers, many readers
//tag - in global scope: no writers, no readers
//demostration of object declaration, type cases, and extension
object iso..tag Collection with [Element: this.. `Any Object`] {
var `memory buffer`: this `Memory Buffer` with [
Stride = Bytes{amount=`stride of (Element)`},
Alignment = `alignment of (Element)`]
= .`make new buffer with capacity for (32) elements`
}
category `Can be traversed` {
applicant object;
$.name.`designates meaning that ("collection can be iterated")`;
$.members.`filter all elements which are ({$0 == Function.`with name ("traverse")`})` == .`candidate found`;
}
extend Collection {
fn `make new fix-sized collection
with maximum size (size: Unsigned Integer with [`Bit Width`: Unsigned Integer that `Equal or less than (Environment.`available memory`.`in bits`)`])
for element of type (type: meta 'i T)`
with [T: `Any Object`]
-returns: { meta 'i Fix-Sized Collection with [`Maximum Size` = size, Element = 'i.. T] }
-body: {
return `Fix-Sized` Collection .`make new with capacity (size) for element of type (type)`
// when type information can be inferred, you may not write it explicitly
};
}
case 'Fix-Sized' with [`Maximum Size`: let Unsigned Integer with [`Bit Width`: Unsigned Integer]]
of some iso..tag Collection {
override self var `number of elements`: this Unsigned Integer with [`Bit Width`: Unigned Integer]
=> var `number of elements`: this `Maximum Size` that `Cannot be higher than (`Maximum Size`)`;
}
//type can have multiple cases. some of them might be incompatible with each other and thus require to explicetly state
//forbiden configuration. also the compiler checks if your code has overriden any properties or functions in incopmatible manner
object Water {}
case Vaporized of not Liquid Water {}
case Liquid of not Vaporized Water {}
case Compressed with [Density: `Pressure Unit`] //UInt is a name alias
of Water {}
var `vaporized compressed water`: iso = Compressed Vaporized Water with [Density = `Pressure Unit`.Pascal] {}
var `vaporized compressed water`: iso = Compressed Liquid Water with [Density = `Pressure Unit`.Pascal] {}
// functions, currying
fn `print message (massage: box Fix-Sized String with [`Maximum Size` = 15, `Glyph Type` = 8bit `UTF Scalar`])
(`number of times`: Unsigned Integer that `Higher than (0)`) number of times`
-body: {
'match (`number of times`) {
'when (case.`Bit Width`. 'is equal or more than (16)') do {...}'
...
}'
...
}
var `message printer`: ref = `print message ("He there! Doing well?") (_) number of times`
`message printer`.`number of times` = 8
'execute (`message printer`)'
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment