Skip to content

RichTeaLang/lib-std

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

RichTea standard library.

The core RichTea runtime is, by design, incredibly useless :)

It has almost no "out of the box" functionality - Limiting itself to providing the Import function. Import loads other RichTea modules and makes them available to the program being executed.

Therefore "out of the box", it has no notion of the most basic programming constructs, such as:

  • Variable declarations
  • Loops (For, While etc)
  • Branching (If, Switch etc)

The RichTea standard library module provides the missing functionality typically expected to be part of the core language.

Quick Example

Import("*" from:"modules/std")

Let(message:"I can create variables!")
Print("I can print messages and { message }")

If(true then:{
  Print("...and branch...")
})

Documentation Examples

All examples below assume:

Import("*" from:"modules/std")

Variables and assignment

Use Let to create attributes and Set to update them.

Import("*" from:"modules/std")

Let(name:"RichTea")
Print("Hello { name }")

Set("name" to:"RichTea Language")
Print("Updated name: { name }")

Conditionals (If)

If supports then and else branches.

Import("*" from:"modules/std")

Let(isMorning:true)

If(isMorning then:{
  Print("Good morning!")
} else:{
  Print("Good evening!")
})

Counted loops (For)

For repeats a block a fixed number of times. The loop variable defaults to i and can be renamed with as.

Import("*" from:"modules/std")

For(5 as:"index" do:{
  Print("Loop #{ index }")
})

Condition loops (While) with Increment

Use While for expression-based loops and Increment to mutate counters.

Import("*" from:"modules/std")

Let(counter:0)

While(counter < 3 do:{
  Print("counter = { counter }")
  Increment("counter")
})

Print("Done")

Iterating collections (ForEach)

Use ForEach to iterate over list-like input.

Import("*" from:"modules/std")

Let(items:["tea", "milk", "sugar"])

ForEach(items as:"item" do:{
  Print("Ingredient: { item }")
})

Mapping values (Map)

Map transforms each element in an input collection.

Import("*" from:"modules/std")

Let(numbers:[1, 2, 3, 4])

Let(doubled:Map(numbers mapFunction:{
  // value available as i by default
  i * 2
}))

Print("{ doubled }")

Branching by value (Switch)

Use Switch when selecting behavior by a single value.

Import("*" from:"modules/std")

Let(command:"status")

Switch(command :{
  Case("start" then:{ Print("Starting...") })
  Case("stop" then:{ Print("Stopping...") })
  Case("status" then:{ Print("Running") })
  Default({ Print("Unknown command") })
})

File I/O (WriteFile and ReadFile)

Write text to a file and read it back.

Import("*" from:"modules/std")

WriteFile("Hello from RichTea" path:"./output.txt")
Let(contents:ReadFile("./output.txt"))
Print("Read: { contents }")

Measuring runtime (Timer)

Wrap any block with Timer to measure execution time.

Import("*" from:"modules/std")

Timer(block:{
  For(1000 do:{
    // work
  })
})

Exiting programs (SystemExit)

Exit with an explicit status code.

Import("*" from:"modules/std")

Print("Fatal error: missing configuration")
SystemExit(1)

Discovering function usage (Man)

Use Man to inspect documentation for a standard function.

Import("*" from:"modules/std")

Man("Print")
Man("ForEach")

About

RichTea standard library

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors