From 20c3430ad2601a0b5d64677039faf3512c31080b Mon Sep 17 00:00:00 2001 From: Centrix14 Date: Wed, 1 Jan 2020 17:47:33 +0300 Subject: [PATCH] fixed a couple of bugs, added code examples --- README.md | 22 ++++++++++++++++------ arr_lib.c | 6 ++---- examples/01_hello_world.spf | 6 ++++++ examples/02_navigation.spf | 6 ++++++ examples/03_output.spf | 7 +++++++ examples/04_math.spf | 6 ++++++ examples/05_intervals.spf | 5 +++++ examples/06_fill.spf | 5 +++++ examples/07_assigment.spf | 6 ++++++ examples/08_logic.spf | 6 ++++++ examples/09_if_else.spf | 8 ++++++++ examples/10_iterator.spf | 6 ++++++ examples/11_swap.spf | 4 ++++ ssp.c | 1 + 14 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 examples/01_hello_world.spf create mode 100644 examples/02_navigation.spf create mode 100644 examples/03_output.spf create mode 100644 examples/04_math.spf create mode 100644 examples/05_intervals.spf create mode 100644 examples/06_fill.spf create mode 100644 examples/07_assigment.spf create mode 100644 examples/08_logic.spf create mode 100644 examples/09_if_else.spf create mode 100644 examples/10_iterator.spf create mode 100644 examples/11_swap.spf diff --git a/README.md b/README.md index b5379cf..ba51462 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ + `^` -- go to the item with the number (`^123` - now 123 is the current work item) + `>` / `<` -- move right / left in the array (`>>` - move the current work item pointer 2 items forward) + `_!` -- display the value of the current item (`:123(_!)` - displays the value of element 123 on the screen) - + `~` -- a comment that continues until the next language command (`~ comment`) + + `~ (text)` -- a comment whose text is placed in brackets (`~ (comment)`) + `|` -- fills the array from the current position with elements (`|6 8 2` - the numbers 6, 8, 2 will be added to the array) + `-!` -- displays the values of all array elements (`^0|6 8 12(-!)` - displays 6, 8, 12 if the array was initially empty) + `;` -- reports that the data entered is a number (`;:12` - writes 12 to the currently selected element, and reports that it is a number) @@ -30,13 +30,13 @@ + `!-` -- copies the inverted value from the current work item to the selected one (`:123(!-)2` - now element 2 stores 0) + `&` -- returns the conjunction of all array elements in element 2047 (`|1 2 3&` - in element 2047, 1 will be returned) + `\` -- returns the disjunction of all array elements in element 2047 (`|1 2 0\` - in element 2047, 1 will be returned) - + `?~` -- predicative execution tool, if the result of the previous operation is true, then all subsequent commands will be executed, if it is not, then all subsequent operations will be skipped, analogous to "if" in other languages (`|1 2 3+(?~)^0|(true)@(-!)` - will display "true", since 1+2+3 not equal to 0) - + `?!` -- inverts the result of the previous operation, similar to else in other languages (`|0 0 0+(?~)^0|(true)@(-!)(?!)|(false)@(-!)` - will output "false" as 0+0+0 early 0) + + `?~` -- predicative execution tool, if the result of the previous operation is true, then all subsequent commands will be executed, if it is not, then all subsequent operations will be skipped, analogous to `if` in other languages (`|1 2 3+(?~)^0|(true)@(-!)` - will display "true", because 1+2+3 not equal to 0) + + `?!` -- inverts the result of the previous operation, similar to `else` in other languages (`|0 0 0+(?~)^0|(true)@(-!)(?!)|(false)@(-!)` - will output "false" as 0+0+0 early 0) + `|~` -- iterator, performs some action on all elements of the array (`(|~)(_! )` - displays the values of all elements of the array, in fact analog ( -!), it is important that operations that must be performed iteratively are enclosed in brackets and end with a space) + `<->` -- exchanges values between the current work item and the selected one (`^2047(<->)2` - the values of elements 2047 and 2 will be reversed) ## Some remarks - + some commands must be surrounded by parentheses, as follows: `(_!)`, `(-!)`, `(:-)`, `(!-)`, `(?~)`, `(?!)`, `(|~)`, `(<->) + + some commands must be surrounded by parentheses, as follows: `(_!)`, `(-!)`, `(:-)`, `(!-)`, `(?~)`, `(?!)`, `(|~)`, `(<->)` + specifically for saol, I wrote a preprocessor called ssp (standard SAOL preprocessor), it runs separately from the main interpreter, and has the following features: + `.link ... ` -- connecting third-party files + `.macro ` -- creates a macro named name, the next time it is encountered, expr will be substituted for it @@ -53,7 +53,17 @@ ### Important notes + Macro names in the program must be surrounded by spaces + ``` + .macro foo bar + .prog + ^ foo : bar + ``` + If the macro value contains spaces, use `{} `to combine expressions. Thanks to this, you can write a macro that will generate another macro. + ``` + .macro gen {.macro foo bar} + .prog + gen + ``` ## Installation and use ### Installation @@ -73,7 +83,7 @@ to install, you will need: ### use ``` - ./saol my_programm.saol // for simple interpretation - ./ssp my_programm.saol out.saol // for preprocessing my_programm.saol in out.saol + ./saol my_programm.spf // for simple interpretation + ./ssp my_programm.spf out.saol // for preprocessing my_programm.saol in out.saol ./isi // to call an interactive interpreter ``` diff --git a/arr_lib.c b/arr_lib.c index 4373f17..59826e7 100644 --- a/arr_lib.c +++ b/arr_lib.c @@ -1,4 +1,4 @@ -/* Saol interpreter library v1.0 31/12/2019 by Centrix */ +/* Saol interpreter library v1.2 31/12/2019 by Centrix */ #include #include @@ -97,10 +97,8 @@ void mult(char *arg) { void idiv(char *arg) { for (int i = start; i < end; i++) { - if (arr[i]) { + if (arr[i]) arr[MAX-1] /= arr[i]; - puts("div"); - } } } diff --git a/examples/01_hello_world.spf b/examples/01_hello_world.spf new file mode 100644 index 0000000..7fc6626 --- /dev/null +++ b/examples/01_hello_world.spf @@ -0,0 +1,6 @@ +~ (this is a comment) +~ (programm 'hello_world') + +@|(Hello, World!)(-!) ~ (the `@` character indicates that the data being operated on is a character) +~ (the `|` character sequentially fills the array with characters in parentheses) +~ (command `-!` displays the contents of the array on the screen) diff --git a/examples/02_navigation.spf b/examples/02_navigation.spf new file mode 100644 index 0000000..ee6752e --- /dev/null +++ b/examples/02_navigation.spf @@ -0,0 +1,6 @@ +~ (saol is focused on array processing, so it has tools for navigating arrays) + +:123 ~ (initially, the array element pointer is set to 0 element, so this string initializes the 0 cell with the value 123) +>:456 ~ (command `>` moves the pointer 1 forward) +< ~ (similarly, the command ` > ' moves the pointer back 1) +^45 ~ (the ` ^ ' command sets the pointer to a specific position, here in cell 45) diff --git a/examples/03_output.spf b/examples/03_output.spf new file mode 100644 index 0000000..10f1fa1 --- /dev/null +++ b/examples/03_output.spf @@ -0,0 +1,7 @@ +~ (here we will look at displaying values on the screen) +~ (only values from the array are displayed) +~ (for example let's display the following text: "The answer is: 42") + +|(The answer is: 42) ~ (first, enter the text in the array using `|`) +@ ~ (now we will specify that it is necessary to output the text, but not the number) +(-!) ~ (actually output the values from the array) diff --git a/examples/04_math.spf b/examples/04_math.spf new file mode 100644 index 0000000..fc808c5 --- /dev/null +++ b/examples/04_math.spf @@ -0,0 +1,6 @@ +~ (let's study the application of mathematics in saol, for this we calculate the value of the expression (2+5*6-4)/7) +~ (in saol, all mathematical operations have equal execution priority, so we will calculate (5*6+2-4)/7) + +;:5^2047:6[0]1* ~ (in the zero element we write 5, and in the 2047 element we write 6, because all the numbers are added / subtracted / multiplied and divided by the value of the 2047 element) +^0:2+:4' ~ (write 2 in the zero cell produce addition, write 4 in the zero cell, subtract) +:7/^2047(_!) ~ (the result is divided by 7 and printed on the screen) diff --git a/examples/05_intervals.spf b/examples/05_intervals.spf new file mode 100644 index 0000000..efdac4d --- /dev/null +++ b/examples/05_intervals.spf @@ -0,0 +1,5 @@ +~ (in past lessons, you may have noticed constructions like `[0]2`, so let's talk about them) +~ (initially, the commands are applied to all 2048 elements of the array, but to limit the range of action, a construction of the form is used: []) +~ (for example) + +@|abcdefg [0]3 (-!) ~ (displays characters from 0 to 3) diff --git a/examples/06_fill.spf b/examples/06_fill.spf new file mode 100644 index 0000000..775e0e8 --- /dev/null +++ b/examples/06_fill.spf @@ -0,0 +1,5 @@ +~ (sometimes you need to fill an array, or part of it, with the same characters / numbers, using the `#`command) +~ (for example) + +[0]3 @ #Q (-!) ~ (filling elements from 0 to 3 with `Q ' characters, output to the screen) +; #6 (-!) ~ (filling the same gap with numbers 6) diff --git a/examples/07_assigment.spf b/examples/07_assigment.spf new file mode 100644 index 0000000..f5a88f9 --- /dev/null +++ b/examples/07_assigment.spf @@ -0,0 +1,6 @@ +~ (if you want to copy a value from one cell to another, use the commands `:-` & `!-`) +~ (for example) + +:789 (:-) 1 > (_!) ~ (copy the value 789 from the 0th element to the 1st element) +^3:10@(_!)^1; ~ (jump to a new line) +< (!-) 2 >> (_!) ~ (copying the inverted value 789 from element 0 to element 2) diff --git a/examples/08_logic.spf b/examples/08_logic.spf new file mode 100644 index 0000000..a033842 --- /dev/null +++ b/examples/08_logic.spf @@ -0,0 +1,6 @@ +~ (in saol, logical operations are also available, namely, & - AND, \ - OR) +~ (for example) + +|1 0 [0]2 & ^2047 (_!) ~ (calculating the conjunction 1 and 0) +^3:10@(_!);^2047 ~ (jump to a new line) +\ (_!) ~ (calculating disjunction 1 and 0) diff --git a/examples/09_if_else.spf b/examples/09_if_else.spf new file mode 100644 index 0000000..b00571e --- /dev/null +++ b/examples/09_if_else.spf @@ -0,0 +1,8 @@ +~ (branches based on predicative execution are available in saol) +~ (it works like this: if the result of the previous operation (that is, the condition) is not zero, then the subsequent operations are executed, but they will not be executed if the result is 0) +~ (for example) + +:6 ^2047 :12 [0]1 ' ~ (condition 12 - 6) +(?~) ^0 [0]2048 |(true) @ (-!) ~ (after the command `?~` is the code that must be executed if the condition is true) +(?!) ^0 [0]2048 |(false) @ (-!) ~ (after the command `?!'this is the code that must be executed if the condition is false) + ~ (in this case, the condition is true and `true` is output, but if you replace 6 with 12, the condition becomes false and `false`is output) diff --git a/examples/10_iterator.spf b/examples/10_iterator.spf new file mode 100644 index 0000000..e9459fd --- /dev/null +++ b/examples/10_iterator.spf @@ -0,0 +1,6 @@ +~ (the iterator allows you to run the same code for each element of an array or span) +~ (the iterator command looks like this: ( | ~ ) after it, in brackets is the code that will have to be executed for each element sequentially, but most importantly at the end, before closing the bracket, put a space!) +~ (for example) + +@|(i am text!) ~ (write the string `i am text`to the array) +(|~)(_! ) ~ (iteratively display the characters on the screen, by the way this is the implementation of the command `-!`) diff --git a/examples/11_swap.spf b/examples/11_swap.spf new file mode 100644 index 0000000..ceca978 --- /dev/null +++ b/examples/11_swap.spf @@ -0,0 +1,4 @@ +~ (if you need to swap the values of 2 elements, use the command `<->`) +~ (for example) + +:678 >> : 123 (<->) 0 (_!) << (_!) ~ (write 678 in the 0-th element, 123 in the 2-th and change their places) diff --git a/ssp.c b/ssp.c index dce6de4..efd4c00 100644 --- a/ssp.c +++ b/ssp.c @@ -45,6 +45,7 @@ int main(int argc, char *argv[]) { fclose(src); fclose(dst); + return 0; }