Skip to content
Wesley Yan Soares Brehmer edited this page Apr 10, 2024 · 25 revisions
 _       __           __  
| |     / /_  _______/ /_ 
| | /| / / / / / ___/ __ \
| |/ |/ / /_/ (__  ) /_/ /
|__/|__/\__, /____/_.___/ 
       /____/

Official documentation

Creating variables:

Wysb is a strongly and statically typed language, which means that when defining variables and arguments, you need to define the type of the value to be stored. To understand this better, let's look at an example: Here are 4 types of (random) data:

  1. ball;
  2. 4;
  3. 8.45;
  4. True;
  • Look at this data. The first is a string, the second is an integer, the third is a float and the last is a bool. In summary, strings are letters of the alphabet, integers are numbers that don't have a comma, floats are numbers that carry some value after the comma, and booleans can only be two values: True or False. Now, in practice, in the Wysb language, here's an example:
$[string] name = "John">;
$[int32] age= 21>;
$[float32] weight = 64.72>;
$[bool] isProgrammer = True>;

Creating functions

In a nutshell, functions are encapsulated, reusable blocks of code that can be called at any time (with arguments or not). In Wysb, functions are created with the keyword "!fun", and the arguments are enclosed in parentheses, with the types in square brackets ([]). Here's an example:

$[string] name = "John">;
!fun sayHello(name[string]) {
  return "Good moring, hello ${name}"
}
@::sayHello()?

Calling functions

In Wysb, the function call works as follows: first, you use the identifier "@::", then the name of the function, then the arguments (if any) in parentheses, and finally the closing "?". Here's an example:

@::sayHello()?

Arithmetic operations with the math library

You can use the built-in "math" library to perform arithmetic operations. math is imported automatically, and does not need to be imported explicitly. Spacing between the first number, the symbol and the second number is mandatory. With the math library, only operations with 2 numbers are supported, i.e. to operate with more numbers, you need to use several variables

$[int32] num1 = <math.sum 5 + 5!>>;
$[float64] num2 = <math.div 12 / 6!>>;
$[int32] num3 = <math.sub ${num1} - 3!>>;
$[float64] num4 = <math.mult 5 * <math.pi!>!>>;
$[int32] num5 = <math.pow 4 :: 2!>>;
$[int32] num6 = <math.sqrt 81!>>;

Working with input

There are several ways of working with data inputs, one of which is the terminal itself. You can read what the user types and process the information you collect as you wish. This can be done with the standard "io" library, which is imported automatically. Here's a simple example:

[string] name = <io.input!>>;
println("Your name is ${name}?")

Using variables anywhere in the code

It's not enough to create variables, you'll need to use their values later. For this, you can use an easy solution, which is to use the standard referrer, which consists of: using the identifier "${", putting the name of the variable you want to get the value of, and closing with "}". Here's an example:

println("Your name is ${name}?")

Working with conditional structures

Often, you'll need to know what's going on in parts of your code. Think about it, you'll need to process the data, and do different things with it depending on the situation, and that is, conditions. In Wysb, there is no else or else if, only "if", so you'll need to build your own else and else if. See examples:

$if[==] name :: John ! hiJohn();
$if[!=] name :: John ! isNotJohn();
$if[<] age :: 12 ! kid();
$if[>] age :: 18 ! adult();

Working with files

Of course, at some point you will need files. Maybe you need to read them, maybe you need to rename them, maybe you need to delete them, maybe you need to move them. Here are some examples:

<os.WriteFile example.txt :: "Hello World"!>
$[string] filecontent = <os.ReadFile example.txt!>>;
<os.RenameFile example.txt :: newexample.txt!>

Working with cryptography

Encryption is great security. It's basically a piece of information (large or small) that you want to protect and hide, with several random characters that are defined with a key, and with that same key, you can decrypt and visualize the hidden content, as if it were a chest, which had a single key, and with that single key it would be possible to open or close such a chest, which hides mysteries. Here's an example (xxxxxxxxxx is a example key):

$[string] name = "John Doe">;
$[string] encryptedName = <cryptography.Encrypt xxxxxxxxxx :: ${name}!>>;
$[string] decryptedEncryptedName = <cryptography.Decrypt xxxxxxxxxx :: ${name}!>>;
println(${decryptedEncryptedName})

Working with logs

The logger is an evolution of println. It provides more information, useful for more robust systems, such as the exact date, time, second, and millisecond when the information was issued, with different colors for different information, such as warnings, errors or normal information.

<logger.Send Info :: "My name is Yan"!>
<logger.Send Warn :: "Maybe your name isn't Yan"!>
<logger.Send Error :: "Surely your name isn't Yan"!>

Randomly random

You may need to generate integers or floats, or even strings, randomly, i.e. procedurally. To do this, you need 2 arguments, first the type of the sequence to be generated, and then the maximum size.

$[float32] randomFloat = <rand.Num float :: 20!>>;
$[int32] randomInt = <rand.Num int:: 12!>>;
$[string] randomString = <rand.Num string :: 30!>>;

How to import functions and libraries

Often, you can choose to work on more than one file, or you can use external libraries in your code (for example, the ones you can download with Cardwmy). To do this, you can use the "extends" built-in function. See examples:

<extends lib.wys!>

Using comments

In short, comments are used to explain parts of the code, either to yourself or to a team that will work on it later. It's important to make it readable and facilitate maintenance. In Wysb, comments are defined between "!:!", and they can be single-line or multi-line, with the same identifier ("!:!"). Here's an example:

$[string] name = "John" !:! Define name !:!
!fun sayHello(name[string]) { 
  return "Good moring, hello ${name}" !:! Return good moring and the string name !:!
}
@::sayHello()? !:! Call the function !:!

Allocating and freeing memory

Sometimes (ok, I admit it, I rarely do this), we can choose to allocate memory, to use later, and after using it, free it. This is advantageous in terms of performance, but in Wysb, there aren't so many loopholes for doing this, since by fate, the language has been formed in such a way that this isn't so necessary, but even so, it can sometimes be a good option. Here are some examples:

$[string] name = <wysb.Allocate string :: name!>
$[string] name = <io.input>
<wysb.Free name!>

Replacing strings

Believe it or not, but in the Wysb compiler, almost everything is about string substitutions and their mapping. In Wysb, with great affection and as a way of thanking string substitution, I created a library called strings (built-in), and a function called Replace, which does just that. Check it out:

$[string] city = "London is amazing"
<strings.Replace city :: London ~ New York!>

web library (BETA)

The "web" library is a built-in (automatically imported) solution for back-end and web development. It provides functions for creating routes and manipulating data. See examples of each function:

$[string] examplehtml = <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Example</title>
</head>
<body>
    <p>Hello world</p>
    <button>Say hello</button>
</body>
</html>>;

<web.get / :: examplehtml!>
<web.post /sayhello/:name :: "Hello"!>
<web.listen :3030!>

gglare library (BETA)

GalaktaGlare (gglare for short) is a built-in (automatically imported) library for machine/deep learning. It offers features for decision trees, image classification, text classification, anomaly detection and data analysis.

<gglare.imagescan ./imgdb/all :: ./imageToClassify.png!>
<gglare.textclassifier "Hello World" :: classifydb.toml!>

Obtaining Date, Time, and Performance Metrics in wYSB

1. Obtaining Date and Time

Wysb provides the Date object for working with dates and times. You can create a new Date object to get the current date and time:

!fun getDate() {
let currentDate = new Date();
return currentDate
}
println(@::getDate()?)

To get specific components of the date and time such as year, month, day, hour, minute, second, and millisecond, you can use various methods of the Date object:

let year = currentDate.getFullYear();
let month = currentDate.getMonth(); !:! Month is zero-based (0 - 11) !:!
let day = currentDate.getDate();
let hours = currentDate.getHours();
let minutes = currentDate.getMinutes();
let seconds = currentDate.getSeconds();
let milliseconds = currentDate.getMilliseconds();

2. Measuring Function Execution Time

You can measure the execution time of functions in Wysb using the performance object, which provides high-resolution timestamps.

!fun fibonacci(n) {
    if (n <= 1) return n;
    return fibonacci(n - 1) + fibonacci(n - 2);
}

!fun useFib() {
let startTime = performance.now();
fibonacci(40); !:! Example function call !:!
let endTime = performance.now();

return endTime - startTime;
}
println(@::useFib()?)

Why are some things different within functions?

At Wysb, some things change in and out of office. Inside functions, you can't create variables using strong typing, but you can get values from strongly typed variables that have been defined outside the function. Another difference is that you can't use the "math" library inside functions, in fact you can, but it's not recommended, instead use the conventional arithmetic symbols only (+, *, /, %, -). Within functions, the keyword used to define variables is "let".

Clone this wiki locally