Compare commits

...

3 Commits

Author SHA1 Message Date
4da3c5ac06 compat fix 2025-12-08 09:35:29 -08:00
31603d705a beef up math.random 2025-12-08 09:05:17 -08:00
b49619c110 add d20 dice roller example 2025-12-08 09:04:45 -08:00
3 changed files with 24 additions and 3 deletions

18
examples/d20.sh Normal file
View File

@ -0,0 +1,18 @@
#!/usr/bin/env shrimp
# usage: dice <sides>
import math only=random
import list only=first
import str only=[replace starts-with?]
sides = $.args | first
sides ??= 20
if sides | starts-with? d:
sides = replace sides //\D// ''
end
sides = number sides
echo 'Rolling d$sides...'
random 1 sides | echo

View File

@ -141,7 +141,7 @@ export class Tree {
// TODO: TEMPORARY SHIM // TODO: TEMPORARY SHIM
class SyntaxNodeType { class SyntaxNodeType {
constructor(public nodeType: NodeType) { } constructor(public nodeType: NodeType, public isError: boolean) { }
is(other: string) { is(other: string) {
return this.nodeType === other return this.nodeType === other
@ -333,7 +333,7 @@ export class SyntaxNode {
} }
get type(): SyntaxNodeType { get type(): SyntaxNodeType {
return new SyntaxNodeType(this.#type) return new SyntaxNodeType(this.#type, this.#isError)
} }
set type(name: NodeType) { set type(name: NodeType) {

View File

@ -16,7 +16,10 @@ export const math = {
if (n < 0) throw new Error(`sqrt: cannot take square root of negative number ${n}`) if (n < 0) throw new Error(`sqrt: cannot take square root of negative number ${n}`)
return Math.sqrt(n) return Math.sqrt(n)
}, },
random: () => Math.random(), random: (min = 0, max = 1) => {
if (min === 0 && max === 1) return Math.random()
return Math.floor(Math.random() * (max - min + 1)) + min
},
clamp: (n: number, min: number, max: number) => { clamp: (n: number, min: number, max: number) => {
if (min > max) throw new Error(`clamp: min (${min}) must be less than or equal to max (${max})`) if (min > max) throw new Error(`clamp: min (${min}) must be less than or equal to max (${max})`)
return Math.min(Math.max(n, min), max) return Math.min(Math.max(n, min), max)