Skip to content

Commit

Permalink
Merge pull request #5 from KDesp73/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
KDesp73 authored May 30, 2024
2 parents d7c18ed + 686d503 commit 8876e9c
Show file tree
Hide file tree
Showing 29 changed files with 577 additions and 109 deletions.
36 changes: 31 additions & 5 deletions docs/Design.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
- echo "Hello World"
- exit `exit-code` (int)

- run "path/to/script" -- Run external kd script (maybe handled by the preprocessor)

- input() -- Should change
- val(expression) -- Should change
- str(expression) -- Should change
Expand All @@ -20,6 +18,14 @@ if condition {
} else {
}
if condition {
} else if condition {
} else {
}
```

Expand Down Expand Up @@ -65,11 +71,31 @@ call name <- x, 2, 3
a = call name <- 1.3
```

## Casting

```
a = 5 as float
```

## Preprocessor

@ indicates preprocessor tag

```
#import "path/to/file" -- Include methods from file
#assign A 2
#run "path/to/file" -- Run file as script
@import "path/to/file" -- Include methods from file
@alias A 2
@run "path/to/file" -- Run file as script
@export a
```

## Comments

```
# Single line comment
##
Multi-
line
Comment
#-
```
16 changes: 16 additions & 0 deletions examples/ansi.kd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
normal = "\033[0m"
red = "\033[91m"
green = "\033[92m"
blue = "\033[94m"

func print_red {
echo red, _1, normal, "\n"
}
func print_blue {
echo blue, _1, normal, "\n"
}
func print_green {
echo green, _1, normal, "\n"
}

call print_red <- "Hello World"
15 changes: 13 additions & 2 deletions examples/binary_search.kd
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ guess = 0
tries = 0

while guess != number {
echo "Guess: "
echo "Guess [0,100]: "
guess = val(input())
while guess < 0 | guess > 100 {
echo "Guess [0,100]: "
guess = val(input())
}

if guess < number {
echo "Your guess is less than the number\n"
}
Expand All @@ -16,4 +21,10 @@ while guess != number {
tries = tries + 1
}

echo "You found the number in ", tries, " tries!\n"
echo "You found the number in ", tries
if tries > 1 {
echo " tries"
} else {
echo " try"
}
echo "\n"
10 changes: 9 additions & 1 deletion examples/comment_in_string.kd
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# This should be removed

#
#
echo "hello # world\n" # this should be removed
#echo "hello # world\n" # this should be removed
# echo "hello # world\n" # this should be removed
# echo "hello # world\n" # this should be removed

if 1 { # Comment
echo "Hello # world\n"
}

# This should be removed
10 changes: 10 additions & 0 deletions examples/delay.kd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
func delay {
echo "Started...\n"
i = 100000
while i > 0 {
i = i - 1
}
echo "Finished\n"
}

call delay
11 changes: 11 additions & 0 deletions examples/else_if.kd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
a = 5

if a == 2 {
echo "a = 2\n"
} else if a == 3 {
echo "a = 3\n"
} else if a == 4 {
echo "a = 4\n"
}else {
echo "a is not 2 or 3 or 4\n"
}
10 changes: 10 additions & 0 deletions examples/escape_characters.kd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
echo "n: ===\n===\n"
echo "t: ===\t===\n"
echo "\': ===\'===\n"
echo "\": ===\"===\n"
echo "r: ===\r===\n"
echo "b: ===\b===\n"
echo "f: ===\f===\n"
echo "a: ===\a===\n"
echo "v: ===\v===\n"
echo "0: ===\0===\n"
37 changes: 37 additions & 0 deletions examples/int_diamond.kd
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
size = 5
num = 1
i = 1
while i <= size {
j = size
while j > i {
echo " "
j = j - 1
}
k = 0
while k < i*2-1 {
echo num
num = num + 1
k = k + 1
}
num = 1
echo "\n"
i = i + 1
}
i = 1
while i <= size -1 {
j = 0
while j < i {
echo " "
j = j + 1
}

k = (size - i)*2-1
while k > 0 {
echo num
num = num + 1
k = k - 1
}
num = 1
echo "\n"
i = i + 1
}
13 changes: 11 additions & 2 deletions examples/math.kd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ func divisionByZero {
while a >= 0 {
b = 12 / a
echo 12, "/", a, "=", b, "\n"

a = a - 1
}
}
Expand All @@ -16,5 +15,15 @@ func mod_example {
}
}

#call mod_example
func float_example {
b = 4.1 + 2.99
echo "b: ", b, "\n"
a = 2.91
echo "a: ", a, "\n"
a = a + 0.1
echo "a: ", a, "\n"
}

call divisionByZero
# call mod_example
# call float_example
25 changes: 14 additions & 11 deletions examples/menu.kd
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
# A Menu program that runs in an infinite recursive loop
# until Exit is selected

func home {
echo "Home selected\n"
echo "Home selected\n\n"
}

func settings {
echo "Settings selected\n"
echo "Settings selected\n\n"
}

func about {
echo "About selected\n"
echo "About selected\n\n"
}

func exit {
echo "Exiting...\n"
func exitProgram {
echo "Exiting...\n\n"
}

choice = 0
func menu {
echo "1. Home\n"
echo "2. Settings\n"
Expand All @@ -27,12 +31,6 @@ func menu {
choice = val(input())
}

if choice == 4 {
call exit
esc
}


if choice == 1 {
call home
}
Expand All @@ -44,6 +42,11 @@ func menu {
if choice == 3 {
call about
}

if choice == 4 {
call exitProgram
exit 0
}

call menu
}
Expand Down
16 changes: 16 additions & 0 deletions examples/method_arguments.kd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
func method {
echo _1, "\n"
echo _2, "\n"
echo _3, "\n"
echo _4, "\n"
echo _5, "\n"
echo _25, "\n"
echo _6, "\n"
echo _7, "\n"
echo _8, "\n"
echo _9, "\n"
}


call method <- 1, 2.2, "Hello world", 1, 2

22 changes: 14 additions & 8 deletions examples/pyramid.kd
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
# Method to print a star pyramid of user defined height
func print_pyramid {
echo "Enter height: "
height = val(input())
i = 1
while i <= height {
spaces = height - i
j = 1
while j <= _1 {
spaces = height - j
while spaces > 0 {
echo " "
spaces = spaces - 1
}

stars = 2 * i - 1
stars = 2 * j - 1
while stars > 0 {
echo "*"
stars = stars - 1
}

echo "\n"
i = i + 1
j = j + 1
}
}

call print_pyramid <- x, 2, 3
echo "Enter height: "
height = val(input())

i = 2
while i <= height{
echo "[", i, "]\n"
call print_pyramid <- i
i = i + 1
}
21 changes: 21 additions & 0 deletions examples/scope.kd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
a = 3
echo "global a: ", a, "\n"

func scope_test {
a = 1
echo "internal a: ", a, "\n"

b = 2.3

echo "scope_test b: ", b, "\n"
}

func another {
t = 32.19
s = "Hello"
echo "t: ", t, " s: ", s, "\n"
}

call scope_test
call another

15 changes: 15 additions & 0 deletions examples/siren.kd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
func delay {
i = _1
while i > 0 {
i = i - 1
}
}

func siren {
while 1 {
echo "\a."
call delay <- 100
}
}

call siren
15 changes: 15 additions & 0 deletions examples/std.kd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
func println {
echo _1, "\n"
}

func error {
call println <- _1
exit _2
}

####################################

call println <- "Hello" + " World"
call error <- "Error message", 1

####################################
Loading

0 comments on commit 8876e9c

Please sign in to comment.