#✅ Cranberry Language (check second message)

1 messages · Page 1 of 1 (latest)

spring tangle
#

Cranberry is a dynamically-typed interpreted language inspired by Rust, C#, Python, Javascript and Lua.
The entire language runs on the cranberry executable file. Installation instructions will be provided after.
-# ---------------------------------------------------------------------------------------------------------------------

Language Syntax

Semicolons are not required and can be used to separate different statements and expressions.

Printing

Print stuff to standard output using print and println. The latter automatically adds a newline (\n) to the end.
You can also print multiple things using commas (it will add a space between each value). ```cpp
println("Hello World")
println("foo", 123)


# Comments

Define single line only comments using hashtags (`#`). ```py
# This doesn't do anything
print("yes")

Variables

Create variables using the let keyword. You can define multiple variables using commas. ```js
let x = 10
let width, height = 20


# Functions

Create functions using the `fn` keyword followed by the arguments in parentheses. Then a code block. ```rs
fn Hello() {
    println("Hello World")
}

Return values using the return keyword followed by a value.

Ranges

Like Rust, Cranberry supports Ranges which can be created using 2 numbers and the .. operator.

let range = 0..10
let range_inclusive = 0..=10
let range_stepped = 0..10..2

In the above example

  • range goes from 0 to 10 (exclusive, will stop before reaching 10)
  • range_inclusive goes from 0 to 10 (inclusive, will stop at 10)
  • range_stepped goes from 0 to 10 (exclusive, will skip by 2 each time)

Stepped is default to 1 and will become negative if the start and end positions are swapped.

Loops

Cranberry supports for, while, and loop.

For loop

Use the for keyword followed by the variable name you want and the Iterable object (string/list/range):

for i in 0..10 {
    print(i)
}
#

Cranberry Language (check second message)

#

While Loop

Use while followed by the condition. Will continue till the condition is false. ```rs
let x = 0
while x < 10 {
x++
}

#

Also cranberry supports x++, x--, and shorthand assignment operations like +=

#

Loop

This is just a while true but as a single keyword. Use break to stop a loop. Optionally output something using the break followed by a value. ```rs
let x = loop {
print("hi")
break 123
}

#

-# ~~ ~~

Scopes and blocks

A block is a collection of statements. Two types of blocks exist in Cranberry:

  • Single expression block
  • Multiple expression block

Single Expression Block

Use => followed by a value or expression and it will also out the value returned sometimes. ```rs
let x = if x > 10 => 123
else => 69


## Multiple expression block/scope
Use braces to define a block of multiple statements, like in a function, loop or if statement, etc. ```rs
fn hello() {
    let x = 10
    let y = 20
    return x + y
}

Expression Blocks

Use an @ symbol followed by braces to create a multi-line expression block. It's like a function but everything is ran instantly and something can be returned too. ```rs
let x = @ {
let hello = 120
out 120 * hello
}

#

-# ~~ ~~

The out keyword

The out keyword is a special evaluation statement that acts as a single-block return. It just outputs a value to the current scope and stops the scope from running. Kind of like a break value or return value. ```cs
print(if x < 10 {
println("hello world")
out 10
} else {
println("goodbye world")
out 55
})

imagine that as replacing the `if` statement with a value of `10`. Lots of statements act as expressions too.
#

-# ~~ ~~

Namespaces

A namespace is a collection of code. Anything you define in the namespace will be appended to the Namespace. So you have to write using followed by the namespace to use it. ```cs
namespace Hello;

let x = 10
To access that in the `main.cb` file we can writecpp
using Hello;

print(Hello.cb)
or optionally add an alias using `as`cpp
using Hello as wow;

print(wow.cb)

#

-# ~~ ~~

Includes

To include files outside of your current source folder, just use the include keyword followed by a string of the path to the file.
You can also use a list of them using []. ```php
include "hello.cb"
include [
"C:/Users/.../Desktop/Sigma.cb",
"C:/Users/.../Desktop/Game.cb"
]

#

-# ~~ ~~

The standard library

Standard library (or Std) is a collection of builtin modules/namespaces which have some methods and members that could prove useful. The current namespaces that exist are

  • Math (collection of math functions and constants)
  • Task (collection of threading, Wait(), and Time stuff)
  • IO (Console commands, such as reading input and writing to the console)
  • FS (file system functions including Files and Directory management)
#
using Std::IO;

# Cast the strings to a number
let a = number(IO.ReadLine("Enter `a`: "))
let b = number(IO.ReadLine("Enter `b`: "))

# Format strings using `$`
println($"The sum of `a` and `b` is {a + b}")
#

✅ Cranberry Language (check second message)

#

Theres a lot more stuff like switch statements but im too lazy to write it

leaden wolf
spring tangle
mild carbon
#

looks cool

#

do you have a file icon

spring tangle
near sluice
#

open source? 🤑

spring tangle
near sluice
#

thank you 🙏

spring tangle
leaden wolf
#

is that the shadow

#

(the black circle/oval)

spring tangle
#

terrible imitation

leaden wolf
#

i like the more realistic one

#

leaves can be improved imo

spring tangle
#

it'll start looking like the uncanny windows 7 icons

spring tangle
#

HOLY SHIT

#

WOOOH

#

AAAAAAAAAAAAAAAAA

leaden wolf
spring tangle
spring tangle
spring tangle
#

Woah Http namespace omg omg

spring tangle
weary gazelle
#

Hows the performance?

#

Does it beat Python 👀

spring tangle
#

when are you going to do that

weary gazelle
#

Yea that's very bad

#

But it's still cool I guess

#

Because it works

#

:D

spring tangle
#

I hope you're not doing hard calculations 10000000 times in a single frame

#

cranberry also supports threads so you can use those too

#
using Std::Task

Task.Spawn(fn() {

})
weary gazelle
spring tangle
#

which is why I switched from python to rust

spring tangle
#

Generators and let const thingies

#

this is an equivalent

spring tangle
spring tangle
#

@summer star check this out

#

using = inside a dictionary will let you use identifiers

#

but using : will let you use expressions as keys

summer star
#

python is very optimized

#

cranberry is interpreted too

#

if you want speed you don't use an interpreted language

#

so speed is not an important benchmark in comparison

spring tangle
#

dang thats a good point

summer star
#

you can technically do

#
{
    function_name(...) : "test"
}```
#

if function_name returns 5 then it sets 5 to test

spring tangle
spring tangle
spring tangle
#

i added pcalls

spring tangle
#

also errors look good now

burnt mantle
#

My favorite cock and ball torture language

summer star
spring tangle
#

you wouldn't believe how many times I had to copy paste start_token

#

god damn

summer star
spring tangle
summer star
spring tangle
#

and now it uses columns and stuff in error underlines

#

this also helps since I can use that for the LSP if I ever make one

spring tangle
#

small update 😛

#

might be slightly bigger than 0.7.0

leaden wolf
rough bobcat
#

I should still have relatively good performance from an interpreted language

#

it's not black and white

#

you can't justify 20x perform diff just because you decided to use an interpreted language

#

(and we are comparing with other interpreted languages)

#

cranberry is cool

#

but not usable in real world unless heavily optimized

summer star
weary gazelle
#

:D

rough bobcat
summer star
#

soup is transpiled iirc

spring tangle
#

yes

spring tangle
#

i doubt it will ever be

#

but its easy to write

summer star
#

python is very very fast

spring tangle
#

yes

rough bobcat
spring tangle
#

not better

#

faster

summer star
#

better is subjective

#

faster yes

#

i do agree tho

weary gazelle
#

Fast for a slow language

summer star
#

slightly ignorant

spring tangle
#

is java faster than python

weary gazelle
#

They’re improving it a lot it seems though soon

#

I heard it will be like 3x faster 🤯

#

Not sure if it’s true though

weary gazelle
#

It’s an interpreted (slow) language

#

Interpreted languages simply are slow compared to compiled ones.

summer star
#

they dont share the same usecase to its illogical to compare them

spring tangle
#

python isnt jit compiled

#

and gets ran line by line

#

k

spring tangle
spring tangle
#

Added Signals

weary gazelle
spring tangle
#

added ternary operator

leaden wolf
#

tbh i dont like ternary operator syntax

#

not in cranberry just generally

leaden wolf
spring tangle
#

sometimes useful

#

when im just too lazy to write ```rs
if x > 10 => "hello"
else => "world"

#

i can write

#
x > 10 ? "hello" : "world"
summer star
#

i like it

#

slightly unreadable

near sluice
#

why is () not required around the condition

#

i feel like it helps with readability

rough bobcat
#

icl

#

unless it's a compiled language it's stupid

summer star
#

defeats the point

near sluice
#

i mean its all about preference for putting them there or not but that is some weird criteria

spring tangle
#

I just don't make it mandatory

#

in practice it is best to setup parenthesis if you use ternary operators in expressions

#

since order of operations matters, you should put parenthesis around anything important

rough bobcat
#

like inlining assembly

#

or doing some crazy compiler optimization

#

so being a little more strict can help with syntax

near sluice
#

oh alr 🤷

spring tangle
rough bobcat
#

you need to account for a lot more things

#

like localization, registers, cache, stack, heap, allocation and different architectures, different machines, different instruction set

#

it's hell of a mess

#

LuaJIT is as close as you get

median glade
#

where's JIT for my LuaJIT

spring tangle
#

Just Lua in Jit Time

#

🥶

#

Jit innit

spring tangle
#

I don't understand how ParseTerm() and ParseAddSubtract() actually works lmao

#

ChatGPT came in clutch for cranberry. I did learn a lot though.

spring tangle
#

Zed extension for cranberry

#

fml 🥀