Simple Programming Language for fun.

Related tags

Command-line Chap
Overview

Chap

Chap is an Easy to learn, dynamic, interpretive, isolated, and keywordless scripting language written in Rust. It is useful when you want your user to control some stuff dynamically and perform some custom calculations in a safe sandbox.


Table of content

  1. Why it named 'Chap'
  2. Features
  3. Keywords
  4. Syntax
  5. Operators
  6. ControlFlow
  7. Samples
  8. Data Types
  9. Installation
  10. How to use
  11. TODOs for version 2.0.0
  12. Builtin function

Name

Rust or راست in persian means right and Chap or چپ means left.

If you code in rust(right) too much you gradually became capitalist after a while. so you need to write some chap(left) to escape from matrix.


Features

  1. Its so easy to learn, (your grandma can learn it in 15 minutes max).
  2. Cross platform (chap runs on Linux, MacOS, Web(WASM) and even Windows!)
  3. OOP or FP? Chap does not gives a sh*t about other programming languages and do the thing in its own way (aka Gigachap paradaim) .

Keywords

What makes a programming language hard to learn?

"Keywords"
Language Keywords Difficulty level
C# 102 5/5
Java 48 4/5
Python 35 3/5
Lua 22 2/5
Chap 0 0/5

Syntax

A normal line of code in chap has 3 chunks separated with -> operator:

chunk1 -> chunk2 -> chunk3
Chunk 1 Chunk 2 Chunk 3
input params function name output variable
param1, param2 -> function_name -> $output_variable

For example:

1, 2 -> add -> $sum
  1. 1 and 2 separated by "," are input params.
  2. These input params are moving to "add" function
  3. Finally $sum is a variable that holds the add result in it.

Note: "add" is not a keyword its a builtin function.

Ok but why?

English language is a "left to right" (aka LTR) language. and programming languages should follows the same rule right?

wrong:

// c base languages:
result = send(sqrt(1 + 2).toString());
                          
   5       4    2     1       3
// chap
1,2 -> add -> sqrt -> to_string -> send -> $result
        ↑       ↑         ↑          ↑        ↑
        1       2         3          4        5

This is acctually left to right like normal english.

TODO: "Chaining" is not supported in version 1.0.0. and every chap line can have 3 chunk max.


Syntax Rules

Make a comment with // and anything you write in right side will be ignored.

1, 2 -> add -> $sum // this is a comment

You can write many line of code in one line by using ;

1 -> $a; $a, 2-> sum -> $b; $b -> print -> $_

Input params are separated by comma character ",".

Input params can be:

  1. Variable
  2. String with " character around like: "hello world"
  3. Int just a number: 5
  4. Float just a normal form of floating point number 3.14
  5. Bool is a boolean value which is a true or false
  6. Tag starts with @. more on control flow
$a, "hello", 5, 3.14, false -> function_name -> $output_var

Function names are not case sensitive.

Function names are not sensitive about anything else:

// to_string = ToString = TOSTRING = to string = t o s t r_i_n_g

variables should starts with $ which known as most loved feature of PHP.
variable name rules:

$12 // Ok
$sp a ce // OK
$#^3 //Ok
$a,b // comma not allowed
$really? // question mark at end not allowed
$rea?lly // OK
$some->thing // -> is not allowed 

Short syntax features

If a function have no output variable you can remove chunk3:

"hello world" -> print
    ↑              ↑         ↑
   input         function   removed chunk3

If a function have no input param you can remove chunk1:

              input -> $user_input
 ↑              ↑         ↑
nothing      function    output

removing chunk2 (function name) means assign:

1 -> $variable
// its actually short form of:
1 -> assign -> $variable

If a function have no input param and output_var you just write function name:

exit

If function has output var but you removed chunk3 the result of function will get printed:

1, 2 -> add
// its short for of:
1, 2 -> add -> $temp
$temp -> print

If you just write some params. chap will print them:

1, 2
// result: 1, 2

// or
$a
// prints whatever $a is

We have worlds smallest hello world:

"Hello World"

I wish I could remove double quotes too :)

Operators

There is one operator -> which moves data from left to right and it is language logo.

Why operators are bad?
becuase they behave different with different types. look at this python example:

number = input("Enter a number:")
result = number * 5 # multiply number to 5
print(number, "* 5 =", result)

Following code has bug and result will be:

Enter a number: 3
3 * 5 = 33333
# no runtime error

Why? because python use same operator for math.multiply and strings.repeat.

So * operator is not a type safe operator and it will do unexpected things when your forget to pass right type to it and it will happen without throwing runtime errors (which is bad).

Same code in chap:

input -> $number
$number, 5 -> multiply -> $result 
$result
// error in line 2: multiply function works only with numbers int and float

Runtime errors are much better than logical error. and in chap we have repeat function:

"foo ", 3 -> repeat
// foo foo foo 

In many languges "+" operator has same problem:

# python
def add(a, b):
    a + b # concat or add? both?

add("1", "2") # 12
add(1, 2) # 3
// Chap:
"1", "2" -> concat // 12
1, 2 -> concat // 12 // you can concat integers safly
1, 2 -> add // 3
"1", "2" -> add // runtime error

debugger

You can put a ? end of line to debug that line:

1 -> $a
2 -> $b
$a, $b -> add -> $c?
// result 1, 2 -> add -> 3

Chap also has a function called "dump" which prints every variable you have.

Control Flow

You can create tag like this:

@tag_name

And you can jump to it:

@tag_name -> jump
// or
@tag_name, true -> jump_if
// or
@tag_name, 1, 1 -> jump_if_equal
// or
@tag_name, 1, 0 -> jump_if_not_equal

loop

jumping backward makes loops

@l
    "Hello util your battery dies"
@l -> jump

if

@i, 1, 1 -> jump_if_equal
    "this will not print"
@i

Note: Indenation is not nessessery


Samples

hello_world.chp

"Hello world"
Hello world

couter.chp

0 -> $counter
@l
    $counter -> increase
@l, $counter, 100 -> jump_if_not_equal
$counter
100

number_guess_game.chp

1,10 -> random_number -> $answer
@loop
    input -> $guess
    $guess -> to_int -> $guess
    @win, $answer, $guess -> jump_if_equal
    "wrong"
@loop -> jump

@win
"you win"
1
wrong
2
wrong
3
you win

christmas_tree.chp

0 -> $counter
@loop
    $counter -> increase

    "*", $counter -> repeat -> $stars
    10, $counter -> minus -> $space_size
    " ", $space_size -> repeat -> $spaces

    $spaces, $stars -> cat
@loop, $counter, 10 -> jump if not equal
         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********

DataTypes

// REPL log
-> 1 -> type_of
int
-> 3.14 -> type of
float
-> "ali" -> TypeOf
string
-> true -> type      
boolean

// TODO arrays

Installation

Download release

link

Build

git clone https://github.com/ali77gh/Chap
cargo build --release
sudo cp ./target/release/chap /usr/bin

How To Use

REPL

./repl/mod.rs

❯ chap
-> "hello world"
hello world
-> 

File_executor

./file_executor/mod.rs

❯ chap number_guess_game.chp 
1
wrong
2
wrong
3
you win answer was: 3

Library

./lib.rs

As lib

cargo build --release --lib

As Wasm module

TODO

TODOs

Version 2.0.0:

  • Arrays
  • Chaining syntax (1,2->add->toString->print)
  • tests for builtin functions
  • static analyzer
  • fix: 'random' module will not work on WASM
  • eval function
  • function name autocompelete (maybe a simple LSP)
  • WASM lib
  • ChapApp

Builtin Functions

runtime/builtin_function
Chap has 46 builtin function(version 1.0.1) (less than javas keywords)

Names Input params output description
assign any any put a value or variable in other variable 1 -> $a
std_out, print, show any, any, any,... any prints params to console
std_in, input nothing string read user input from console
exit, quit, kill, end nothing nothing ends execution
jump @tag nothing moves executor curser to closest tag with specfied name
jump_if @tag, bool nothing jumps to tag if 1st param is true
jump_if_not @tag, bool nothing jumps to tag if 1st param is false
jump_if_equal, jeq @tag, any, any nothing jumps to tag if 2th and 3th params are equal
jump_if_not_equal, jneq @tag, any, any nothing jumps to tag if 2th and 3th params are not equal
new_tag @tag nothing creates tag (you can call this just by writing tag name
add num, num num adds two numbers 1 + 2 = 3 or 1.5 + 1 = 2.5
add_many, add_all num, num, num,... num adds many numbers 1 + 2 + 3 = 6
minus num, num num minus two numbers 3 - 2 = 1
multiply num, num num minus two numbers 3 * 2 = 6
divide num, num num divede two numbers 3 / 2 = 1.5
modulus, mod num, num num divide remaining 3 / 2 = 1
power, pow num, num num power 3 ** 2 = 9
square_root, sqrt num num square root 9 -> sqrt -> 3
increase, inc $num nothing adds one to variable short form of: $a,1 -> add -> $a
decrease, dec $num nothing minus one from variable short form of: $a,1 -> minus -> $a
equal, eq any, any bool true if 1st and 2nd are equal and false if they are not
not_equal, neq any, any bool true if 1st and 2nd are not equal and false if they are
and bool, bool bool and logical gate
or bool, bool bool or logical gate
not bool bool not logical gate
greater_than, gt num, num bool true if 1st param is bigger than 2nd param 3,2 -> true
less_than, lt num, num bool true if 1st param is less than 2nd param 3,2 -> false
concat, cat any, any string convert inputs to string and concat them "al","i" -> "ali"
repeat any, int string convert inputs to string and repeat "a",3 -> "aaa"
length, len any int convert input to string and returns length 456 -> 3
contains, has any bool convert inputs to string and returns 1st contains 2nd 11,1->true
slice, sub_string any, int, int string "hello", 1, 3 -> "el"
to_string any string convert input to string 1 -> "1"
to_float string float convert input to float "1.5" -> 1.5 ; "a"->error
to_int string int convert input to int "1" -> 1 ; "a"->error
dump, dump_memory nothing nothing prints all variables with values
type_of, type any str prints type of param 1 -> int; "s" -> string
now_sec, now, unixtime nothing float unix time standard in seconds
wait_mil, wait_millis int nothing delay code execution for 1st milliseconds
wait_sec, wait_sec int nothing delay code execution for 1st seconds
wait_min, wait_minute int nothing delay code execution for 1st minutes
wait_hour,wait_hour int nothing delay code execution for 1st hours
You might also like...
Rust port of https://github.com/hunar4321/life_code with some fun features.
Rust port of https://github.com/hunar4321/life_code with some fun features.

Smarticles A Rust port of Brainxyz's Artificial Life simulator with some fun features. A simple program to simulate primitive Artificial Life using si

collaboration project focusing on rust. Made for fun

Collaboration space for on rust project(s) setup Pull down the repo first then do the following steps cd into lil-devils cargo fetch cargo build cargo

An operating system written in Rust (for fun and educational purposes)

Prestige Prestige is an operating system written for fun and educational purposes in Rust. It targets the x86-64 architecture and can run on common em

🚀 TaskFly - The Fun Cron Alternative

🚀 TaskFly - The Fun Job Scheduler Daemon Experience a whole new way of scheduling tasks with TaskFly. Representing tasks' urgency with emojis, TaskFl

A just-for-fun™ JVM implementation in Rust

Caoimhe's Rust JVM (CaoRVM) This is a just-for-fun™ JVM implementation in Rust. This project was inspired by one of my previous C++ projects, CaoVM. S

My own image file format created for fun! Install the "hif_opener.exe" to open hif files. clone the repo and compile to make your own hif file

Why am i creating this? I wanted to create my own image format since I was 12 years old using Windows 7, tryna modify GTA San Andreas. That day, when

A fun app for all purposes!

Setup Install cargo-mobile2 cargo install --git https://github.com/tauri-apps/cargo-mobile2 To update the libray just run: cargo mobile update Now in

A Text User Interface library for the Rust programming language
A Text User Interface library for the Rust programming language

Cursive Cursive is a TUI (Text User Interface) library for rust. It uses ncurses by default, but other backends are available. It allows you to build

🎄My Advent of Code 2021 solutions in the Rust programming language

Advent of Code 2021 in Rust My Advent of Code 2021 solutions in the Rust programming language. This repository holds a separate Rust project for each

Comments
  • Support History on REPL

    Support History on REPL

    support hitory on REPL, so the user does not have to retype again and again the same things, but can use down/up arrows in order to search and re-edit already inserted lines of text.

    opened by amiremohamadi 6
Owner
Ali Ghahremani
Software Engineer. Founder of UniTools and Hrypton
Ali Ghahremani
A C-like programming language that is similar to Rust's syntax. Toy programming language.

CRUST This is a hobby project to learn about compilers and language design. I've designed the language to be similar to C and Rust. Heavily inspired b

Mahmoud Almontasser 50 Jul 13, 2024
Programming language made by me to learn other people how to make programming languages :3

Spectra programming language Programming language made for my tutorial videos (my youtube channel): Syntax Declaring a variable: var a = 3; Function

Adi Salimgereyev 3 Jul 25, 2023
The Amp programming language: a language designed for building high performance systems.

A language designed for building high performance systems. Platform Support x86_64-pc-windows ✅ x86_64-unknown-linux ⚠️ untested x86_64-unknown-darwin

The Amp Programming Language 5 Mar 17, 2023
Nexa programming language. A language for game developers by a game developer

NexaLang Nexa programming language. A language for game developers by a game developer. Features High-Level: Nexa is an easy high level language Two M

Sabe 3 Aug 21, 2023
A simple lexer which creates over 75 various tokens based on the rust programming language.

Documentation. This complete Lexer/Lexical Scanner produces tokens for a string or a file path entry. The output is a Vector for the user to handle ac

null 1 Nov 27, 2022
A structure editor for a simple functional programming language, with Vim-like shortcuts and commands.

dilim A structure editor for a simple functional programming language, with Vim-like shortcuts and commands. Written in Rust, using the Yew framework,

Joomy Korkut 6 Nov 18, 2022
2048 in `tui`, just for fun

TUI 2048 - Have a relax at anytime - ?? ^_^ How to run repo clone this repo, git clone https://github.com/WanderHuang/game-2048-tui.git cd game-2048-t

wander 43 Dec 16, 2022
A rust binary that will flip one or more bits of a file (mostly for messing with images for fun).

file-bitflipper A rust binary that will flip one or more bits of a file (mostly for messing with images for fun). Example (bitflipped bentley) Usage $

null 1 Aug 31, 2022
A fun rust terminal program so you can make Christmas trees!

Xmastree 2021 A fun christmas tree where you can add ornaments! Then, you can export the file into either: a rust file a txt file About Taking a break

Junhao 2 Dec 21, 2021
82 fun and easy to use, lightweight, spinners for Rust, with minimal overhead.

Spinners for Rust 82 fun and easy to use, lightweight, spinners for Rust, with minimal overhead, all the way from simple dots, to fun emoji based "spi

Juliette Cordor 2 May 17, 2022