Skip to content

Commit

Permalink
indent capibability, for loop, bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jooapa committed Jan 10, 2024
1 parent 01590a7 commit 4e415a8
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 65 deletions.
45 changes: 33 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ nilf $num1 == $num2 : slingshot possu3
```

### Creating a pig (function)
You can create a line receiver by using the `pig` keyword.
You can create a line receiver by using the `pig` keyword. Use `gip` to end the function.
```bash
pig possu
oink "Hello World!"
oink "Hello World!"
gip
```

Expand All @@ -130,12 +130,21 @@ slingshot possu


### For loops
For loops can be created by using the `nör` keyword. and the second parameter is the times the loop will run. Use `når` to end the loop.
For loops can be created by using the `nör` keyword. Second parameter is for the loop name. and the third parameter is the times the loop will run. Use `når` to end the loop.

Be careful, you cannot use the same loop name twice.

```bash
nör 5
oink "oinking 5 times"
når // to end the loop
nör oink 5
oink "oinking 5 times"
når

```
```bash
nöf times = 5
nör oink $times
oink "oinking $times times"
når

```

Expand Down Expand Up @@ -205,10 +214,10 @@ slingshot cubic_root_num1
// function to cubic root the num1
pig cubic_root_num1

multiply $num1 $num2
power $num1 0.333
multiply $num1 $num2
power $num1 0.333

oink "$num1"
oink "$num1"

gip

Expand All @@ -225,15 +234,27 @@ nilf $num1 < $num2 : slingshot possu2
nilf $num1 == $num2 : slingshot possu3

pig possu1
oink "num1 is bigger than num2"
oink "$num1 is bigger than $num2"
gip

pig possu2
oink "num1 is smaller than num2"
oink "$num1 is smaller than $num2"
gip

pig possu3
oink "num1 is equal to num2"
oink "$num1 is equal to $num2"
gip

```
## Simple for loop Example
```bash
nöff my_program

nöf num1 = 16
nöf num2 = 2

nör oink 5
oink "oinking 5 times"
når

```
20 changes: 10 additions & 10 deletions example.nöff
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
nöff my_program
nöff complex_program

nöf num1 = 16
nöf num2 = d
nöf count = 0
nöf limit = 5

slingshot cubic_root_num1
nör main_loop $limit
oink "Current count: $count"
add $count 1
når

// function to cubic root the num1
pig cubic_root_num1

divide num1 1

oink "$num1"
niff $count == $limit : slingshot end_program

pig end_program
oink "Program finished with count: $count"
gip
40 changes: 40 additions & 0 deletions interpeter/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,44 @@ def last_line_not_empty():
def not_number(value):
print("Error: \"" + value + "\" is not a number")
line_error()
exit()

def invalid_for_loop():
print("Error: Invalid for loop")
line_error()
exit()

def loop_not_found(loop):
print("Error: Loop \"" + loop + "\" not found")
line_error()
exit()

def no_variable_value():
print("Error: No variable value")
line_error()
exit()

def invalid_if_statement():
print("Error: Invalid if statement")
line_error()
exit()

def no_variable_name():
print("Error: No variable name")
line_error()
exit()

def invalid_slingshot():
print("Error: Invalid slingshot")
line_error()
exit()

def invalid_pig():
print("Error: Invalid pig")
line_error()
exit()

def nothing_to_gip():
print("Error: Nothing to gip")
line_error()
exit()
56 changes: 56 additions & 0 deletions interpeter/for_loop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import start, error, variable


def start_loop():
loop_name = get_loop_name_from_start_for_loops(start.line_number)
try:
only_loop_name = loop_name.split(" ", 2)[0]
except:
error.invalid_for_loop()

loop_number = loop_name.split(" ", 2)[1]

if variable.is_variable(loop_number):
loop_number = variable.get_variable_value(loop_number)
loop_number = int(loop_number)

loop_number -= 1
change_loop_name(start.for_loops, start.line_number,
only_loop_name + " " + str(loop_number))

def end_loop():
# get the start line number by the giving the end line number
try:
start_line, loop_name = get_start_line(start.for_loops,
start.line_number)
loop_number = loop_name.split(" ", 2)[1]
except:
error.invalid_for_loop()

if variable.is_variable(loop_number):
loop_number = variable.get_variable_value(loop_number)
loop_number = int(loop_number)

if int(loop_number) <= 0:
pass
else:
start.goto_number = start_line

def get_start_line(loops, end_line):
for loop_name, (start_line, loop_end_line) in loops.items():
if loop_end_line == end_line:
return start_line, loop_name
return None

def change_loop_name(loops, start_line, new_name):
for loop_name, (loop_start_line, end_line) in list(loops.items()):
if loop_start_line == start_line:
loops[new_name] = loops.pop(loop_name)
return
error.loop_not_found(start_line)

def get_loop_name_from_start_for_loops(start_line):
for loop_name, (loop_start_line, end_line) in start.for_loops.items():
if loop_start_line == start_line:
return loop_name
return None
30 changes: 13 additions & 17 deletions interpeter/if_statement.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import start, error, slingshot, pig
import start, error, slingshot, pig, variable

if_statement_was_True = False

def if_statement():
global if_statement_was_True

value1 = start.full_line.split(" ", 2)[1]
try:
value1 = start.full_line.split(" ", 2)[1]
except:
error.invalid_if_statement()

operator = start.full_line.split(" ", 3)[2]
value2 = start.full_line.split(" ", 4)[3]
goto_function = start.full_line.split(" ", 5)[5]

if value1.startswith("$"):
value1 = value1.replace("$", "")
if value1 in start.variables:
value1 = start.variables[value1]
else:
error.variable_not_found(value1)

if value2.startswith("$"):
value2 = value2.replace("$", "")
if value2 in start.variables:
value2 = start.variables[value2]
else:
error.variable_not_found(value2)

if variable.is_variable(value1):
value1 = variable.get_variable_value(value1)
if variable.is_variable(value2):
value2 = variable.get_variable_value(value2)
value1 = variable.convert_to_number(value1)
value2 = variable.convert_to_number(value2)

if operator == ">":
if value1 > value2:
if_statement_was_True = True
Expand Down
12 changes: 10 additions & 2 deletions interpeter/interpret.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sys, os, start
import sys, os, start, textwrap
from error import invalid_extension


Expand Down Expand Up @@ -28,12 +28,20 @@ def interpret(args):
klinoff_file = get_absolute_path(args[0])
klinoff_content = get_contents(klinoff_file)

# remove indentations
no_indent_klinoff_content = ""
for line in klinoff_content.split("\n"):
no_indent_klinoff_content += textwrap.dedent(line) + "\n"

# remove last \n
no_indent_klinoff_content = no_indent_klinoff_content[:-1]

for arg in args:
if arg == "--debug" or arg == "-d":
start.Debug_mode = True

os.system("cls")
start.start(klinoff_content)
start.start(no_indent_klinoff_content)

if __name__ == "__main__":
interpret(sys.argv[1::])
4 changes: 1 addition & 3 deletions interpeter/oink.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import start, error

def say():
has_squigel = False

if start.full_line == "oink":
print("")
return
Expand All @@ -27,9 +25,9 @@ def say():
for word in words:
if word.startswith("$"):
if "§" in word:
has_squigel = True
word = word.split("§")[0]
word = word.replace("§", " ")

word = word.replace("$", "")
if word in start.variables:
variable = start.variables[word]
Expand Down
11 changes: 8 additions & 3 deletions interpeter/pig.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@


def pig():
function_variable = start.full_line.split(" ", 1)[1]
try:
function_variable = start.full_line.split(" ", 1)[1]
except:
error.invalid_pig()
start_line, end_line = start.functions[function_variable]
start.goto_number = end_line + 1

Expand All @@ -11,8 +14,10 @@ def pig():
def gip():

lines = start.full_code.split("\n")

start.goto_number = start.last_came[-1]
try:
start.goto_number = start.last_came[-1]
except:
error.nothing_to_gip()
# print(if_statement.if_statement_was_True)
if if_statement.start.coming_from_if and if_statement.if_statement_was_True:
if_statement.start.coming_from_if = False
Expand Down
7 changes: 5 additions & 2 deletions interpeter/slingshot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import start
import start, error


def slingshot(line):
slingshot_variable = line.split(" ", 1)[1]
try:
slingshot_variable = line.split(" ", 1)[1]
except:
error.invalid_slingshot()
start.last_came.append(start.line_number + 1)
start.goto_number = start.functions[slingshot_variable][0] + 1

Loading

0 comments on commit 4e415a8

Please sign in to comment.