From 51cd3df3fb6a4f01889b7b3ee5da2da2e4e7d173 Mon Sep 17 00:00:00 2001 From: Kenny <70860732+KennyOliver@users.noreply.github.com> Date: Thu, 9 Jan 2025 23:02:49 +0000 Subject: [PATCH 1/6] Create 21_sleep.flv #194 --- src/tests/21_sleep.flv | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/tests/21_sleep.flv diff --git a/src/tests/21_sleep.flv b/src/tests/21_sleep.flv new file mode 100644 index 0000000..f77bf75 --- /dev/null +++ b/src/tests/21_sleep.flv @@ -0,0 +1,4 @@ +for i in 1000..=1 by 5 { + serve(i); + sleep(i * 1); +} From bc317dbfbb20a8012de6034d77567a9c9a464ecc Mon Sep 17 00:00:00 2001 From: Kenny <70860732+KennyOliver@users.noreply.github.com> Date: Thu, 9 Jan 2025 23:03:03 +0000 Subject: [PATCH 2/6] Add: `sleep()` built-in function #194 --- src/interpreter/builtins.c | 50 +++++++++++++++++++++++++++++++++++ src/interpreter/builtins.h | 1 + src/interpreter/interpreter.c | 2 ++ src/interpreter/utils.c | 6 ++--- 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/interpreter/builtins.c b/src/interpreter/builtins.c index 34e76aa..e33828d 100644 --- a/src/interpreter/builtins.c +++ b/src/interpreter/builtins.c @@ -4,6 +4,11 @@ #include #include #include +#ifdef _WIN32 +#include +#else +#include +#endif // Helper function to check if a LiteralType matches an ArgType bool literal_type_matches_arg_type(LiteralType lit_type, ArgType arg_type) { @@ -814,3 +819,48 @@ InterpretResult builtin_length(ASTNode *node, Environment *env) { // Return the length as a LiteralValue return make_result(result, false, false); } + +/** + * @brief Built-in function to sleep (pause execution) for a given time (in + * milliseconds). + * + * @param node The AST node representing the function call. + * @param env The current environment. + * @return InterpretResult with a default value. + */ +InterpretResult builtin_sleep(ASTNode *node, Environment *env) { + // Define expected arguments: exactly one integer (> 0). + INT_SIZE ms; + + ArgumentSpec specs[1]; + specs[0].type = ARG_TYPE_INTEGER; + specs[0].out_ptr = &ms; + + // Interpret arguments + InterpretResult args_res = + interpret_arguments(node->function_call.arguments, env, 1, specs); + if (args_res.is_error) { + return args_res; + } + + // Validate the argument + if (ms <= 0) { + return raise_error("`sleep` requires a positive integer argument " + "representing milliseconds.\n"); + } + +// Perform the sleep based on the platform +#ifdef _WIN32 + Sleep((DWORD)ms); // sleep takes milliseconds on Windows +#else + if (ms > (INT_MAX / 1000)) { + return raise_error("`sleep` argument is too large.\n"); + } + + useconds_t us = (useconds_t)(ms * 1000); // usleep needs microseconds + usleep(us); +#endif + + LiteralValue result = create_default_value(); + return make_result(result, false, false); +} diff --git a/src/interpreter/builtins.h b/src/interpreter/builtins.h index d89d834..8a73027 100644 --- a/src/interpreter/builtins.h +++ b/src/interpreter/builtins.h @@ -35,6 +35,7 @@ InterpretResult builtin_file_read(ASTNode *node, Environment *env); InterpretResult builtin_file_write(ASTNode *node, Environment *env); InterpretResult builtin_file_append(ASTNode *node, Environment *env); InterpretResult builtin_length(ASTNode *node, Environment *env); +InterpretResult builtin_sleep(ASTNode *node, Environment *env); // Helpers bool literal_type_matches_arg_type(LiteralType lit_type, ArgType arg_type); diff --git a/src/interpreter/interpreter.c b/src/interpreter/interpreter.c index c1e7834..38eddd1 100644 --- a/src/interpreter/interpreter.c +++ b/src/interpreter/interpreter.c @@ -1596,6 +1596,8 @@ InterpretResult interpret_function_call(ASTNode *node, Environment *env) { return builtin_file_append(node, env); } else if (strcmp(func->name, "length") == 0) { return builtin_length(node, env); + } else if (strcmp(func->name, "sleep") == 0) { + return builtin_sleep(node, env); } else { return raise_error("Unknown built-in function `%s`\n", func->name); } diff --git a/src/interpreter/utils.c b/src/interpreter/utils.c index 3e05a90..b205b88 100644 --- a/src/interpreter/utils.c +++ b/src/interpreter/utils.c @@ -67,9 +67,9 @@ void initialize_builtin_function(Environment *env, const char *name) { void initialize_all_builtin_functions(Environment *env) { const char *builtin_functions[] = { - "string", "float", "int", "sample", - "serve", "burn", "random", "get_time", - "taste_file", "plate_file", "garnish_file", "length"}; + "string", "float", "int", "sample", "serve", + "burn", "random", "get_time", "taste_file", "plate_file", + "garnish_file", "length", "sleep"}; for (size_t i = 0; i < sizeof(builtin_functions) / sizeof(builtin_functions[0]); i++) { From 541341e3a8145c147c865a99fe5b36c219dde44b Mon Sep 17 00:00:00 2001 From: Kenny <70860732+KennyOliver@users.noreply.github.com> Date: Thu, 9 Jan 2025 23:03:44 +0000 Subject: [PATCH 3/6] Update 21_sleep.flv #194 --- src/tests/21_sleep.flv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/21_sleep.flv b/src/tests/21_sleep.flv index f77bf75..f1987a6 100644 --- a/src/tests/21_sleep.flv +++ b/src/tests/21_sleep.flv @@ -1,4 +1,4 @@ -for i in 1000..=1 by 5 { +for i in 1000..=1 by -5 { serve(i); sleep(i * 1); } From 31b5e680f56b23e5ad46ae9f2f4d0982e1aed1f8 Mon Sep 17 00:00:00 2001 From: Kenny <70860732+KennyOliver@users.noreply.github.com> Date: Thu, 9 Jan 2025 23:04:36 +0000 Subject: [PATCH 4/6] Update 21_sleep.flv #194 --- src/tests/21_sleep.flv | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tests/21_sleep.flv b/src/tests/21_sleep.flv index f1987a6..4c51eae 100644 --- a/src/tests/21_sleep.flv +++ b/src/tests/21_sleep.flv @@ -1,4 +1,4 @@ -for i in 1000..=1 by -5 { +for i in 100..=1 by -5 { serve(i); - sleep(i * 1); + sleep(i * 10); } From 9fb9f50acdbac26cb6ddf3b547c06cd1129488a9 Mon Sep 17 00:00:00 2001 From: Kenny <70860732+KennyOliver@users.noreply.github.com> Date: Thu, 9 Jan 2025 23:11:28 +0000 Subject: [PATCH 5/6] Add: New Standard Library section in `docs/` #194 --- README.md | 14 ++++++++------ docs/standard_library.md | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 docs/standard_library.md diff --git a/README.md b/README.md index 3530679..2251000 100644 --- a/README.md +++ b/README.md @@ -37,17 +37,19 @@ FlavorLang blends coding with culinary creativity! Write programs like recipes & ### `docs/` -5. [Syntax Examples](docs/syntax_examples.md) +5. [Standard Library](docs/standard_library.md) -6. [Debugging](docs/debugging.md) +6. [Syntax Examples](docs/syntax_examples.md) -7. [Language Design](docs/language_design.md) +7. [Debugging](docs/debugging.md) -8. [Lexer](docs/lexer.md) +8. [Language Design](docs/language_design.md) -9. [Parser](docs/parser.md) +9. [Lexer](docs/lexer.md) -10. [Interpreter](docs/interpreter.md) +10. [Parser](docs/parser.md) + +11. [Interpreter](docs/interpreter.md) --- diff --git a/docs/standard_library.md b/docs/standard_library.md new file mode 100644 index 0000000..307e133 --- /dev/null +++ b/docs/standard_library.md @@ -0,0 +1,20 @@ +# Standard Library Functions + +| **Function** | **Description** | +| -------------- | ------------------------------------------------------------------ | +| `string` | Casts a value to a string. | +| `float` | Casts a value to a float. | +| `int` | Casts a value to an integer. | +| `sample` | Reads input from the terminal. | +| `serve` | Outputs data to the terminal. | +| `burn` | Raises an error. If uncaught, it terminates the program. | +| `random` | Generates a random number: | +| | - 0 args: a random float between `0` and `1`. | +| | - 1 arg: a random integer between `0` and the given max value. | +| | - 2 args: a random integer between the given min and max values. | +| `get_time` | Returns the current UNIX timestamp. | +| `taste_file` | Reads the content of a file. | +| `plate_file` | Writes data to a file (overwrites the file if it already exists). | +| `garnish_file` | Appends data to a file. | +| `length` | Returns the length of a string or array. | +| `sleep` | Pauses program execution for the specified number of milliseconds. | From bcfd42d9bd3d3da1f9a61067139a619aa2e2c272 Mon Sep 17 00:00:00 2001 From: Kenny <70860732+KennyOliver@users.noreply.github.com> Date: Thu, 9 Jan 2025 23:12:13 +0000 Subject: [PATCH 6/6] Update syntax_examples.md #194 --- docs/syntax_examples.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/syntax_examples.md b/docs/syntax_examples.md index d9c7007..3f13a16 100644 --- a/docs/syntax_examples.md +++ b/docs/syntax_examples.md @@ -26,6 +26,7 @@ These are examples showcasing the unique (& fun) syntax of FlavorLang. They give 18. [Basic Arrays & Operations](#18) 19. [2D Arrays](#19) 20. [Call Functions by Reference (Even in Arrays)](#20) +21. [Sleep](#21) --- @@ -516,6 +517,15 @@ for i in 0..(length(c)) { } ``` +### 21. Sleep + +```js +for i in 100..=1 by -5 { + serve(i); + sleep(i * 10); +} +``` + --- ## License