Prelude of builtin functions #7

Merged
defunkt merged 45 commits from prelude into main 2025-10-29 20:15:37 +00:00
Owner

Changes in this branch:

Language Features

Conditionals: Added ConditionalOp statements at statement-level (eg do x: x > 2 end)

Operators:

  • Added % (modulo operator)
  • Switched to == for equality
  • Fixed or/and chaining behavior

Identifiers: Allowed ? character in identifier names (e.g., empty?, valid?)

Module System

load keyword: Import/use modules (evolved from use keyword)

Builtin Functions

Prelude system: Built-in global functions available in all Shrimp programs:

  • Parser now tracks builtin global functions for proper DotGettin'

Shrimp Prelude Functions

Output/Display

  • echo - Print values to console

Type Introspection

  • type - Get the type of a value
  • inspect - Format a value for display
  • length - Get the length of a string, array, or dict

Type Predicates

  • string? - Check if value is a string
  • number? - Check if value is a number
  • boolean? - Check if value is a boolean
  • array? - Check if value is an array
  • dict? - Check if value is a dict
  • function? - Check if value is a function or native function
  • null? - Check if value is null
  • some? - Check if value is not null

Boolean/Logic

  • not - Logical negation

Utilities

  • inc - Increment a number by 1
  • dec - Decrement a number by 1
  • identity - Return the input value unchanged

Collection Operations

  • at - Get element at index/key from array or dict
  • range - Create a number sequence
  • empty? - Check if string, array, or dict is empty

String Operations

Transformations

  • str.join - Join array elements into string
  • str.split - Split string by separator
  • str.to-upper - Convert to uppercase
  • str.to-lower - Convert to lowercase
  • str.trim - Remove leading/trailing whitespace
  • str.replace - Replace first occurrence
  • str.replace-all - Replace all occurrences
  • str.slice - Extract substring by indices
  • str.substring - Extract substring by indices (alt)
  • str.repeat - Repeat string n times
  • str.pad-start - Pad string at start
  • str.pad-end - Pad string at end
  • str.lines - Split string into lines
  • str.chars - Split string into characters

String Predicates

  • str.starts-with? - Check if starts with prefix
  • str.ends-with? - Check if ends with suffix
  • str.contains? - Check if contains substring
  • str.empty? - Check if string is empty
  • str.test? - Test string against regex
  • str.index-of - Find first index of substring
  • str.last-index-of - Find last index of substring
  • str.match - Match string against regex

List Operations

Core Operations

  • list.map - Transform each element
  • list.filter - Keep elements matching predicate
  • list.reduce - Reduce to single value
  • list.find - Find first matching element
  • list.slice - Extract subarray

List Predicates

  • list.empty? - Check if list is empty
  • list.contains? - Check if list contains item
  • list.any? - Check if any element matches
  • list.all? - Check if all elements match

Sequence Operations

  • list.reverse - Reverse list order
  • list.sort - Sort list
  • list.concat - Concatenate multiple lists
  • list.flatten - Flatten nested lists
  • list.unique - Remove duplicates
  • list.zip - Combine two lists into pairs

List Access

  • list.first - Get first element
  • list.last - Get last element
  • list.rest - Get all but first element
  • list.take - Take first n elements
  • list.drop - Drop first n elements
  • list.append - Add element to end
  • list.prepend - Add element to start
  • list.index-of - Find index of element

List Utilities

  • list.sum - Sum all numbers
  • list.count - Count elements matching predicate
  • list.partition - Split into matching/non-matching
  • list.compact - Remove null values
  • list.group-by - Group elements by key function

Dict Operations

Core Operations

  • dict.keys - Get all keys
  • dict.values - Get all values
  • dict.entries - Get key-value pairs as objects
  • dict.get - Get value by key with default
  • dict.merge - Merge multiple dicts
  • dict.map - Transform values
  • dict.filter - Keep entries matching predicate
  • dict.from-entries - Create dict from array of pairs

Dict Predicates

  • dict.has? - Check if key exists
  • dict.empty? - Check if dict is empty

Math Operations

Basic Math

  • math.abs - Absolute value
  • math.floor - Round down
  • math.ceil - Round up
  • math.round - Round to nearest
  • math.min - Minimum value
  • math.max - Maximum value
  • math.pow - Exponentiation
  • math.sqrt - Square root
  • math.random - Random number [0, 1)
  • math.clamp - Restrict value to range
  • math.sign - Sign of number (-1, 0, 1)
  • math.trunc - Truncate decimal

Math Predicates

  • math.even? - Check if even
  • math.odd? - Check if odd
  • math.positive? - Check if positive
  • math.negative? - Check if negative
  • math.zero? - Check if zero

Enumerables

  • each - Iterate over list, return original list

Modules

  • load - Load and execute Shrimp module from file
# Changes in this branch: ## Language Features **Conditionals**: Added ConditionalOp statements at statement-level (eg `do x: x > 2 end`) **Operators**: - Added `%` (modulo operator) - Switched to `==` for equality - Fixed `or`/`and` chaining behavior **Identifiers**: Allowed `?` character in identifier names (e.g., `empty?`, `valid?`) ## Module System **`load` keyword**: Import/use modules (evolved from `use` keyword) ## Builtin Functions **Prelude system**: Built-in global functions available in all Shrimp programs: - Parser now tracks builtin global functions for proper DotGettin' --- # Shrimp Prelude Functions ## Output/Display - `echo` - Print values to console ## Type Introspection - `type` - Get the type of a value - `inspect` - Format a value for display - `length` - Get the length of a string, array, or dict ## Type Predicates - `string?` - Check if value is a string - `number?` - Check if value is a number - `boolean?` - Check if value is a boolean - `array?` - Check if value is an array - `dict?` - Check if value is a dict - `function?` - Check if value is a function or native function - `null?` - Check if value is null - `some?` - Check if value is not null ## Boolean/Logic - `not` - Logical negation ## Utilities - `inc` - Increment a number by 1 - `dec` - Decrement a number by 1 - `identity` - Return the input value unchanged ## Collection Operations - `at` - Get element at index/key from array or dict - `range` - Create a number sequence - `empty?` - Check if string, array, or dict is empty ## String Operations ### Transformations - `str.join` - Join array elements into string - `str.split` - Split string by separator - `str.to-upper` - Convert to uppercase - `str.to-lower` - Convert to lowercase - `str.trim` - Remove leading/trailing whitespace - `str.replace` - Replace first occurrence - `str.replace-all` - Replace all occurrences - `str.slice` - Extract substring by indices - `str.substring` - Extract substring by indices (alt) - `str.repeat` - Repeat string n times - `str.pad-start` - Pad string at start - `str.pad-end` - Pad string at end - `str.lines` - Split string into lines - `str.chars` - Split string into characters ### String Predicates - `str.starts-with?` - Check if starts with prefix - `str.ends-with?` - Check if ends with suffix - `str.contains?` - Check if contains substring - `str.empty?` - Check if string is empty - `str.test?` - Test string against regex ### String Search - `str.index-of` - Find first index of substring - `str.last-index-of` - Find last index of substring - `str.match` - Match string against regex ## List Operations ### Core Operations - `list.map` - Transform each element - `list.filter` - Keep elements matching predicate - `list.reduce` - Reduce to single value - `list.find` - Find first matching element - `list.slice` - Extract subarray ### List Predicates - `list.empty?` - Check if list is empty - `list.contains?` - Check if list contains item - `list.any?` - Check if any element matches - `list.all?` - Check if all elements match ### Sequence Operations - `list.reverse` - Reverse list order - `list.sort` - Sort list - `list.concat` - Concatenate multiple lists - `list.flatten` - Flatten nested lists - `list.unique` - Remove duplicates - `list.zip` - Combine two lists into pairs ### List Access - `list.first` - Get first element - `list.last` - Get last element - `list.rest` - Get all but first element - `list.take` - Take first n elements - `list.drop` - Drop first n elements - `list.append` - Add element to end - `list.prepend` - Add element to start - `list.index-of` - Find index of element ### List Utilities - `list.sum` - Sum all numbers - `list.count` - Count elements matching predicate - `list.partition` - Split into matching/non-matching - `list.compact` - Remove null values - `list.group-by` - Group elements by key function ## Dict Operations ### Core Operations - `dict.keys` - Get all keys - `dict.values` - Get all values - `dict.entries` - Get key-value pairs as objects - `dict.get` - Get value by key with default - `dict.merge` - Merge multiple dicts - `dict.map` - Transform values - `dict.filter` - Keep entries matching predicate - `dict.from-entries` - Create dict from array of pairs ### Dict Predicates - `dict.has?` - Check if key exists - `dict.empty?` - Check if dict is empty ## Math Operations ### Basic Math - `math.abs` - Absolute value - `math.floor` - Round down - `math.ceil` - Round up - `math.round` - Round to nearest - `math.min` - Minimum value - `math.max` - Maximum value - `math.pow` - Exponentiation - `math.sqrt` - Square root - `math.random` - Random number [0, 1) - `math.clamp` - Restrict value to range - `math.sign` - Sign of number (-1, 0, 1) - `math.trunc` - Truncate decimal ### Math Predicates - `math.even?` - Check if even - `math.odd?` - Check if odd - `math.positive?` - Check if positive - `math.negative?` - Check if negative - `math.zero?` - Check if zero ## Enumerables - `each` - Iterate over list, return original list ## Modules - `load` - Load and execute Shrimp module from file
defunkt added 1 commit 2025-10-26 03:43:29 +00:00
defunkt added 6 commits 2025-10-26 22:58:40 +00:00
defunkt added 1 commit 2025-10-26 22:59:31 +00:00
defunkt added 1 commit 2025-10-28 19:47:49 +00:00
defunkt force-pushed prelude from 05cf93ab36 to 4f092fca3f 2025-10-28 19:51:54 +00:00 Compare
defunkt force-pushed prelude from 4f092fca3f to de30d85304 2025-10-28 20:11:33 +00:00 Compare
defunkt force-pushed prelude from de30d85304 to 0eca3685f5 2025-10-29 06:02:16 +00:00 Compare
defunkt added 10 commits 2025-10-29 18:34:15 +00:00
defunkt added 1 commit 2025-10-29 19:07:15 +00:00
defunkt added 1 commit 2025-10-29 19:14:00 +00:00
defunkt changed title from WIP: start on a prelude of builtin functions to Prelude of builtin functions 2025-10-29 19:14:14 +00:00
defunkt added 2 commits 2025-10-29 19:21:15 +00:00
defunkt added 1 commit 2025-10-29 19:28:55 +00:00
probablycorey approved these changes 2025-10-29 19:39:47 +00:00
probablycorey left a comment
Owner

Love it

Love it
ha.sh Outdated
@ -0,0 +5,4 @@
]
echo bob
echo (mike | at name)

Is this an example or just a tmp file.

Is this an example or just a tmp file.
Author
Owner

Whoopsie. Just a tmp file.

Whoopsie. Just a tmp file.
defunkt marked this conversation as resolved
@ -17,6 +18,7 @@ const operators: Array<Operator> = [
{ str: '-', tokenName: 'Minus' },
{ str: '>', tokenName: 'Gt' },
{ str: '<', tokenName: 'Lt' },
{ str: '%', tokenName: 'Modulo' },

LOL, I was thinking about this on the drive home from school yesterday.

LOL, I was thinking about this on the drive home from school yesterday.
@ -0,0 +3,4 @@
import { Compiler } from '#compiler/compiler'
import { type Value, VM, Scope } from 'reefvm'
export const load = async function (this: VM, path: string): Promise<Record<string, Value>> {

Why use this here instead of vm?

Why use `this` here instead of `vm`?
src/testSetup.ts Outdated
@ -110,3 +109,3 @@
if (value instanceof RegExp) value = String(value)
if (value === expected) {
if (isEqual(value, expected)) {

I rewrote this in the branch I'm on too!

If you replace this line with

    expect(value).toEqual(expected)
      return {
        message: () => `Expected evaluation to be ${expected}, but got ${value}`,
        pass: true,
      }

You get to lean on bun's type checking code and you get all the nice diff support.

I rewrote this in the branch I'm on too! If you replace this line with ```ts expect(value).toEqual(expected) return { message: () => `Expected evaluation to be ${expected}, but got ${value}`, pass: true, } ``` You get to lean on bun's type checking code and you get all the nice diff support.
Author
Owner

Awesome. Done.

Awesome. Done.
defunkt marked this conversation as resolved
@ -59,2 +38,2 @@
assertNever(result)
}
if (result.type === 'function') return Function
else return fromValue(result)

🧠

🧠
defunkt added 4 commits 2025-10-29 20:12:46 +00:00
defunkt added 1 commit 2025-10-29 20:14:41 +00:00
defunkt added 1 commit 2025-10-29 20:15:04 +00:00
defunkt merged commit 00eb1cf6f1 into main 2025-10-29 20:15:37 +00:00
Sign in to join this conversation.
No reviewers
No Label
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: probablycorey/shrimp#7
No description provided.