-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Welcome to SL Wiki page!
SL = Simple Language/Scripting Language/Simple scripting Language.
Call it whatever you want.
SL uses dynamic variables. For example:
# Correct way to define variable
var number = 10 # integer
var string = "Hello, World" # string
var char = 'A' # char
var double = 10.1 # floating point
var bool = true # booleans
var long = 0xBFFF # or binary or octal, also used as pointer, but base language doesnt support pointers as well.
var number2 = 10 var number3 = 20 # number2 is 10 and number3 is 20
# Wrong way
var number: int = 10
int number = 20
var number int = 20
var number = 10, number2 = 40
I dont support hidden assignments in SL, code should be clear enough on its own, and you should explicitly state what you re doing, which is why assignments aren t included in expressions.
For example:
# Correct way
var number = 10
$number = 23
# Correct way
var number2 = 20
$number2 = 10 $number = 50 # now number2 is 10 and number is 50
# Wrong way
10 + ($number = 32) - 20
# Wrong way
if ($number = 32) equ 32 then # ...
# Wrong Way
function($number = 32, "Hello", false)
In SL the only supported condition is the if block.
For example:
# Condition expressions:
# equ = equal
# neq = not equal
# eqg = equal or greater
# eql = equal or lesser
# > = greater
# < = lesser
# Correct way to use if
if (10 + 10) equ (20 + 10) - 10 then
<CODE>
end
# Correct way to use if with else
if (10 + 10) equ (20 + 10) - 10 then
<CODE>
else
<CODE>
end
if $number1 equ 20 then # ...
if function(10, 10) equ 20 then # ...
if 20 > function(10, 10) then # ...
# Correct way to use if with elif
if (10 + 10) equ (20 + 10) - 10 then
<CODE>
elif (10 + 10) equ (20 - 10) + 10 then
<CODE>
else
<CODE>
end
# Wrong way
if (10 + 10) equ (20 + 10) - 10 then
<CODE>
else if
<CODE>
else
<CODE>
end
SL only supports while loop. It works similarly to if. (Both continue and break are supported)
For Example:
var i = 0
while $i < 10 then
<CODE>
end
# Wrong way
while $i < 10 { #...
Dont forget that while loops are very costly in SL, so heavy use of them can significantly reduce performance. Make sure to optimize your while loops as much as possible. Since SL doesnt use bytecode, while loops can be slower than in languages like Python, Lua, or Ruby, but with reasonable usage and proper optimization, their performance can sometimes be comparable.
SL uses a simple function syntax.
For example:
# Function without arguments
def func then
<CODE>
end
func()
# Function with arguments
def func -> number1, number2, string then
return $number1 + $number2
end
func(10 + 10, 10 / 2)
func($number1 + 100 / (40 + 24), 10, "Hello, World")
# Function with VA ARGS
def func -> number1, number2, ... then
# then arguments named like: <function_name>_VA_ARGUMENT_<index>
return $number1 + $number2 + $func_VA_ARGUMENT_0 # which is first one
end
func(10, 10, 10)
# Wrong way
def func() then #...
def func(number1, number2) then #...
def func -> number1 = 10, number2 = 20 then # ...
SL supports all mathematical expressions and solves them at the speed of light.
import file_name # without STRING LITERAL!!
Here I'll provide information about the SL API so you can implement your own.
The project does not bundle stdlib.h together with the standard sl.c and sl.h single-header libraries included in the project directory.
If you want access to the standard library when using the scripting language, you will need to include your own implementation of stdlib.h in your project.
The required steps are described below:
// INCLUDE IT
#include "stdlib.h" // or your path
// Initialize it to load the standard library.
// This must be done after init_sl_process() and before calling
// open_sl_process() or sl_dostr_sl_process(), since the functions
// need to be loaded before the runtime starts.
init_sl_stdlib(&sl_code, argc, argv);
// When you are finished, shut it down to clean up the standard
// library and the memory allocations associated with it.
close_sl_stdlib();In SL if you want to automatically allocate memory and continue on your way without having to deal with it yourself you first need to init a process.
// MAIN:
struct SL_Code sl_code = sl_init_sl_process();In the later steps I'll continue with the examples using the variable we allocated (sl_code).
enum TokenTypes {
T_UNKNOWN = 0,
T_IF,
T_DEF,
T_WHILE,
T_THEN,
T_END,
T_VAR,
T_ELSE,
T_ELIF,
T_IMPORT,
T_BREAK,
T_CONTINUE,
T_RETURN,
T_EQU,
T_NEQ,
T_EQG,
T_EQL,
T_AND,
T_OR,
T_SHLEFT,
T_SHRIGHT,
};
struct SL_Code
{
char **code;
enum TokenTypes *types;
int token_count;
struct SL_Variable *vars;
int total_size_v;
int total_vars;
struct SL_Function *funcs;
int total_size_f;
int total_funcs;
};Linking functions in SL is very simple as shown below.
// YOUR FUNCTION
struct SL_Variable myfunc(struct SL_Code *code, struct SL_L_Function func) {
// GETTING FUNCTION ARGUMENTS
struct SL_Variable first_arg = sl_get_argument(*code, func, 0);
// sl_get_argument(code from myfunc's arguments, func from myfunc's arguments, which argument? 0 is first, 1 is second, ...)
}
// MAIN:
sl_add_func(&sl_code, "myfunc", myfunc);If you want to get information about a function in SL you can use this function and store the function information in a variable of type SL_Function.
// MAIN:
struct SL_Function getfu = sl_get_func(sl_code, "print");
printf("Linked function? : %d\n", getfu.linked_function);struct SL_L_Function {
int total_arguments;
int *argument_indexes;
int starting_index;
};
struct SL_Function
{
char *name;
struct SL_Variable *arguments;
int total_arguments;
char** code_tokens;
int code_len;
int vaargs;
struct SL_Variable (*funcr)(struct SL_Code*, struct SL_L_Function);
int linked_function;
};Since adding a variable requires operations such as manually allocating memory, use the sl_dostr_sl_process function instead of the sl_add_var function (which hasn't even been tested).
// MAIN:
sl_dostr_sl_process(&sl_code, "var a = 10");
// FOR STRINGS
sl_dostr_sl_process(&sl_code, "var a = \"Hello, World\"");If you want to copy information about a variable in SL, you can use this function and store the result in a variable of type SL_Variable.
// MAIN:
struct SL_Variable copied = sl_copy_variable(var);If you want to get information about a variable in SL, you can use this function and store the result in a variable of type SL_Variable.
// MAIN:
struct SL_Variable getva = sl_get_var(sl_code, "enable_notify");
printf("Value of [%s]: %d\n", getva.name, getva.valb);Although pointer types aren't used in the base language, they are compatible with valh when used in functions linked from C.
enum SL_Types
{
INIT = 0,
INTEGER = 1,
DOUBLE = 2,
CHAR = 3,
STRING = 4,
BOOLEAN = 5,
RETURN = 6,
LONG = 7,
ERROR = 8,
POINTER = 9,
};
struct SL_Variable
{
char *name;
enum SL_Types type;
union
{
int vali;
double valf;
int valb;
char valc;
char *vals;
long valh;
};
};As mentioned earlier you can use the sl_dostr_sl_process function to run strings that don't contain \n and aren't considered files.
// MAIN:
sl_dostr_sl_process(&sl_code, "print($a, \"\n\")");You can automatically run files by opening an SL process.
// MAIN:
if (sl_open_sl_process(&sl_code, "file.sl") != 0) {
exit(-1);
}
// sl_open_sl_process(SL_Code as pointer, filename)You can automatically close a process that has been opened or initialized.
// MAIN:
if (sl_close_sl_process(&sl_code) == -1) {
fprintf(stderr, "close_sl_process failed.");
return -1;
}import lib.sl
use("io")
var enable_notify = true
io.print("Hello world", "\n", 10, "\n", true, "\n", false, "\n", 10.1, "\n", 'A', "\n")
$enable_notify = false # setting false again
io.print("10 + 200 = ", add(10, 200), "\n")
Yes that's it! You've finished the entire wiki.