Skip to content

Commit

Permalink
V1.13.5: Added Loops and Functions to docs and updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
Sas2k committed Nov 4, 2022
1 parent 389dd82 commit 1ddcf8a
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 6 deletions.
1 change: 1 addition & 0 deletions NumberScript/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def __init__(self):

def interpret(self, code: str, variables_dict: dict = None, function_dict: dict = None, debug_mode: bool = False,library: bool = False) -> str:
"""Interprets the code"""
code = code.replace("\n", " ").replace("\t", "").replace("\r\n", " ").replace("\r", " ")
code = code.split(" ")
variables_dict = {} if variables_dict is None else variables_dict
function_dict = {} if function_dict is None else function_dict
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<img src="docs\images\Logo-256.png" alt="Logo of NumberScript"/>
<img src="https://raw.githubusercontent.com/Sas2k/NumberScript/main/docs/images/Logo-256.png" alt="Logo of NumberScript"/>

# NumberScript

[![Downloads-Month](https://pepy.tech/badge/numberscript/month)](https://pepy.tech/project/numberscript)
[![Downloads-Total](https://pepy.tech/badge/numberscript)](https://pepy.tech/project/numberscript)
![Discord](https://img.shields.io/discord/1005410273609400402?label=Discord&style=social)
[![Discord](https://img.shields.io/discord/1005410273609400402?label=Discord&style=for-the-badge&color=purple)]((https://discord.gg/wRXR72zJ6W))

[NumberScript Discord-Invite](https://discord.gg/wRXR72zJ6W)

Expand Down
83 changes: 83 additions & 0 deletions docs/Tutorial/Functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Functions

## What are functions?

Functions are like a mini-program, that you can call in your main program.

so like in some text editors, let's take word for example. there is a button in the top corner that makes the text bold. this is a function. like this there a lot of functions in Word.

## Defining Functions in NumberScript

now to define functions we need the `7` command.

so the basic syntax is this

```
7function-name$function-arguments$function-body
```

now the function-name is the name of the function. the function-arguments are the arguments that the function takes. and the function-body is the code that the function runs.

so here is a function that adds two numbers and prints them.

```
7add$number1,number2$2^number1+number2
```

now how do I call this function well just do this

```
add$5,5
```

and it will print 10.

so how can I have more then one command in the function body?
well simply you can add the function code split in between the commands.
the function split is the `$`.

so here is our function that prints the sum of two numbers. but this function is different.
I changed it to display the double of the sum.

```
7add$number1,number2$3sum:^number1+number2$2^sum*2
```

so if we call this with `add$5,5` it will print 20.

and that's about it for functions.

## Exercises

Now that you got the basics of the functions. try these exercises.

### 1) fisherman's problem

now A fisherman has a brand new programmable fishing rod. now the rod is programmed with NumberScript.
so now the fisherman asks you to program the rod. since he is a fisherman he doesn't know how to program.

so he asks you to make a program to take input. the options are `1` for `small fish` and `2` for `big fish`.

now when the fisherman sees a small fish he input the option (1 for small fish, 2 for big fish) and the rod will ask him to input the weight of the fish. then the rod will display how hard to pull the line.

now the calculation on how to pull the small fish is `weight/2 + 1`.

for big fish it's `weight * 2 / 4 + 1`.

so here is an example input and output.

```
Enter-Option>1
Enter-Weight>4
Pull-the-fish-with,
3
grams
```

```
Enter-Option>2
Enter-Weight>10
Pull-the-fish-with,
6
grams
```
2 changes: 1 addition & 1 deletion docs/Tutorial/If-Else.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Here is an example of a program using If-Else,

Now that you got the basics of the If-Else Sentences. try these exercises.

### Even or Odd
### 1) Even or Odd

In this exercise you must create a program where it will take an input and checks whether it is Even or Odd.

Expand Down
78 changes: 78 additions & 0 deletions docs/Tutorial/loops.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Loops

## What are loops?

now what loops do is repeat an instruction a number of time.

now imagine you have an instruction you have to do a 100 times.
you can write it 100 times. but that's not efficient. and it's not fun.
so instead you can use loops.

now in NumberScript there is only 1 type of loop. which is the for loop.

## Loops in NumberScript

now the syntax for loops is this

```
6loop-variable\repeat-count\loop-code
```

now the loop-variable is the variable that will be used to count the number of times the loop has been repeated.
the repeat count is the times it will repeat. the loop code is the instruction that will be repeated.

now let's see an example

```
6i\10\2i
```

here this will print the numbers from 0 to 9.

> Loops start from 0 so the first number will be 0. and the end number will be one less then the placed amount.
now let's see another example

```
6i\10\?i=5:2i|2i-is-5
```

here this will print the numbers from 0 to 9. but if the number is 5 it will print `i-is-5` instead.

## Exercises

### 1) Odd or Even

now, we made a program to see if the number is even or odd in the [If-Else](NumberScript/Tutorial/if-else/#even-or-odd) section. but now your task is to make it so the program loops through the numbers from 0 to 99 and prints if the number is even or odd.

here is the output.

```
0
is even
1
is odd
2
is even
3
is odd
...
```

### 2) FizzBuzz

Now your task is to create a FizzBuzz program where it loops over the numbers from 1 to 100 and prints `Fizz` if the number is divisible by 3, `Buzz` if the number is divisible by 5, `FizzBuzz` if the number is divisible by 3 and 5, and the number if it's not divisible by 3 or 5.

here is the output.

```
1
2
Fizz
4
Buzz
Fizz
7
8
...
```
5 changes: 4 additions & 1 deletion docs/about.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# About

Created by [Sas2k](https://github.com/Sas2k)
Created by [Sas2k](https://github.com/Sas2k) on a rainy day.

> Also some of you might have noticed that this language works with only string parsing.
> No Lexers, Parsers or anything of that sort. :smile:
2 changes: 1 addition & 1 deletion docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Which you can easily download from `pypi` (aka. `pip`)
Now the interpreter is downloaded.
we need to set it up Now run the command below.

`nspm setup`
`nspm setup` _also you only need to run this command once_

> nspm: NumberScript Package Manager
Expand Down
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ nav:
- Basics: Tutorial/basics.md
- Variables: Tutorial/Variables.md
- If-Else: Tutorial/If-Else.md
- Loops: Tutorial/loops.md
- Functions: Tutorial/Functions.md
- About: about.md
theme:
name: material
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="NumberScript",
version="1.13.2",
version="1.13.5",
description="possibly the world's most simplest and restricted language.",
author="Sasen Perera",
long_description=longest_description,
Expand Down

0 comments on commit 1ddcf8a

Please sign in to comment.