Skip to content

Commit 6a6769f

Browse files
Add custom function/keyword | Rv1.2.0
1 parent fd59fa6 commit 6a6769f

30 files changed

+985
-303
lines changed

.github/workflows/c-cpp.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
uses: actions/checkout@v4
3838

3939
- name: Install dependencies
40-
run: sudo apt-get install -y build-essential binutils
40+
run: sudo apt-get install -y build-essential binutils make
4141

4242
- name: Build project
4343
run: make

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ SOARE Changelog
66
- v1.0.0
77

88
First SOARE release
9+
10+
- v1.2.0
11+
12+
First SOARE release
13+

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#
1313

1414
APP = soare
15-
VERSION_MAJ = 1
15+
VERSION_MAJOR = 1
1616

1717
AR = ar
1818
CC = gcc
@@ -26,6 +26,7 @@ INCLUDE = include
2626
CFLAGS := -Wall
2727
CFLAGS += -Wextra
2828
CFLAGS += -Wno-unused-result
29+
CFLAGS += -Wno-unused-parameter
2930
CFLAGS += -Wno-implicit-fallthrough
3031

3132
ifeq ($(OS), Windows_NT)
@@ -41,7 +42,7 @@ SOARE_FLAGS += -D __SOARE_COLORED_OUTPUT
4142

4243
default: $(BIN)/$(APP)
4344

44-
$(LIB)/libsoare$(VERSION_MAJ).a: $(CORE_OBJS)
45+
$(LIB)/libsoare$(VERSION_MAJOR).a: $(CORE_OBJS)
4546

4647
CORE_OBJS := $(patsubst $(CORE)/%.c, $(LIB)/%.o, $(wildcard $(CORE)/*.c))
4748

@@ -54,8 +55,8 @@ $(LIB)/%.o: $(CORE)/%.c
5455
$(BIN)/$(APP): $(LIB) $(CORE_OBJS) $(SRC)/Main.c
5556
mkdir -p $(BIN)
5657
$(WINDRES) windows/resources/app.rc -coff $(RES)
57-
$(AR) rcs $(LIB)/libsoare$(VERSION_MAJ).a $(CORE_OBJS)
58-
$(CC) $(RES) $(SRC)/Main.c -o $(BIN)/$(APP) -I $(INCLUDE) -L$(LIB) -lsoare$(VERSION_MAJ) $(CFLAGS) $(SOARE_FLAGS)
58+
$(AR) rcs $(LIB)/libsoare$(VERSION_MAJOR).a $(CORE_OBJS)
59+
$(CC) $(RES) $(SRC)/*.c -o $(BIN)/$(APP) -I $(INCLUDE) -L$(LIB) -lsoare$(VERSION_MAJOR) $(CFLAGS) $(SOARE_FLAGS)
5960
rm $(CORE_OBJS) $(RES)
6061

6162
run:

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ SOARE is distributed under the [MIT License](LICENSE).
77

88
## 📖 Documentation
99

10-
> [!IMPORTANT]
10+
> [!IMPORTANT]
1111
> See [SOARE Documentation](doc/documentation.md) and [SOARE Changelog](CHANGELOG)
1212
1313
## 🧑‍💻 INTERPRETER
1414

1515
```txt
16+
1617
? This is your first SOARE code !
17-
write "Hello World!";
18+
write("Hello World!");
19+
20+
? SOARE information
21+
soareinfo();
22+
1823
```
1924

2025
## 🛠️ Recommended tools

core/Custom.c

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
/**
6+
* _____ _____ ___ ______ _____
7+
* / ___|| _ |/ _ \ | ___ \ ___|
8+
* \ `--. | | | / /_\ \| |_/ / |__
9+
* `--. \| | | | _ || /| __|
10+
* /\__/ /\ \_/ / | | || |\ \| |___
11+
* \____/ \___/\_| |_/\_| \_\____/
12+
*
13+
* Antoine LANDRIEUX (MIT License) <Custom.c>
14+
* <https://github.com/AntoineLandrieux/SOARE/>
15+
*
16+
*/
17+
18+
#include <SOARE/SOARE.h>
19+
20+
/*
21+
22+
==============================================================
23+
Example: Custom Function - Add Numbers with Variable Arguments
24+
==============================================================
25+
26+
This example demonstrates how to implement a custom function
27+
that adds together an unknown number of integer arguments.
28+
29+
----------------------------------------------------------
30+
Function Name:
31+
char *int_add(soare_arguments_list args)
32+
33+
Arguments:
34+
- args: A linked list of arguments passed to the function.
35+
Use soare_getarg(args, i) to retrieve the i-th argument
36+
as a string.
37+
38+
Return Value:
39+
- Returns NULL (no value returned to SOARE).
40+
- If you want to return a value, allocate memory for the
41+
result string before returning.
42+
43+
----------------------------------------------------------
44+
Implementation Steps:
45+
1. Initialize result accumulator.
46+
2. Loop through arguments using soare_getarg.
47+
3. Convert each argument from string to integer.
48+
4. Add to result.
49+
5. Print the result.
50+
6. Return NULL (or a string if needed).
51+
52+
----------------------------------------------------------
53+
Memory Management:
54+
- If returning a string, always allocate memory for it.
55+
- Example:
56+
char *ret = malloc(6 * sizeof(char));
57+
if (!ret)
58+
return NULL;
59+
strcpy(ret, "value");
60+
return ret;
61+
62+
----------------------------------------------------------
63+
Code:
64+
----------------------------------------------------------
65+
66+
char *int_add(soare_arguments_list args)
67+
{
68+
// Accumulator for the sum
69+
int result = 0;
70+
// Pointer to current argument string
71+
char *x = NULL;
72+
73+
// Loop through all arguments
74+
for (int i = 0; 1; i++)
75+
{
76+
// Retrieve the i-th argument
77+
x = soare_getarg(args, i);
78+
if (!x)
79+
// Exit loop if no more arguments
80+
break;
81+
82+
// Convert argument to integer and add to result
83+
result += atoi(x);
84+
}
85+
86+
// Output the result to the console
87+
printf("%d", result);
88+
89+
// Return NULL (no value returned to SOARE)
90+
// If you need to return a value, allocate memory as shown above
91+
return NULL;
92+
}
93+
94+
Implement this function: soare_addfunction(<function name>, <function>)
95+
96+
soare_addfunction("int_add", int_add);
97+
98+
----------------------------------------------------------
99+
100+
*/
101+
102+
/* Functions */
103+
static struct soare_functions functions_list[100];
104+
/* Count */
105+
static size_t functions_count = 0;
106+
107+
/**
108+
* @brief Add defined function
109+
*
110+
* @param name
111+
* @param function
112+
* @return unsigned int
113+
*/
114+
unsigned int soare_addfunction(char *name, char *(*function)(soare_arguments_list))
115+
{
116+
if (functions_count >= 99 || !name || !function)
117+
return 0;
118+
119+
struct soare_functions fn = {name, function};
120+
121+
functions_list[functions_count] = fn;
122+
functions_list[functions_count + 1].name = NULL;
123+
124+
functions_count = functions_count + 1;
125+
return functions_count;
126+
}
127+
128+
/**
129+
* @brief Get defined function
130+
*
131+
* @param name
132+
* @return soare_function
133+
*/
134+
soare_function soare_getfunction(char *name)
135+
{
136+
static soare_function none = {NULL, NULL};
137+
138+
if (!name)
139+
return none;
140+
141+
for (size_t i = 0; i < functions_count; i++)
142+
if (!strcmp(functions_list[i].name, name))
143+
return functions_list[i];
144+
145+
return none;
146+
}
147+
148+
/**
149+
* @brief Get argument from a function call
150+
*
151+
* @param args
152+
* @param position
153+
* @return char*
154+
*/
155+
char *soare_getarg(soare_arguments_list args, unsigned int position)
156+
{
157+
for (; position && args; position--)
158+
args = args->sibling;
159+
return Eval(args);
160+
}
161+
162+
/*
163+
164+
==============================================================
165+
Example: Custom Keyword - Clear screen
166+
==============================================================
167+
168+
This example demonstrates how to implement a custom keyword.
169+
170+
----------------------------------------------------------
171+
Function Name:
172+
void clear(void)
173+
174+
----------------------------------------------------------
175+
Code:
176+
----------------------------------------------------------
177+
178+
void clear(void)
179+
{
180+
// ANSI escape code to clear the screen
181+
printf("\033c\033[3J");
182+
}
183+
184+
Implement this keyword: soare_addkeyword(<keyword name>, <function>)
185+
186+
soare_addkeyword("clear", clear);
187+
188+
----------------------------------------------------------
189+
190+
*/
191+
192+
/* Keyword */
193+
static struct soare_keywords keywords_list[100];
194+
/* Count */
195+
static size_t keywords_count = 0;
196+
197+
/**
198+
* @brief Add defined keyword
199+
*
200+
* @param name
201+
* @param keyword
202+
* @return unsigned int
203+
*/
204+
unsigned int soare_addkeyword(char *name, void (*keyword)(void))
205+
{
206+
if (keywords_count >= 99 || !name || !keyword)
207+
return 0;
208+
209+
struct soare_keywords fn = {name, keyword};
210+
211+
keywords_list[keywords_count] = fn;
212+
keywords_list[keywords_count + 1].name = NULL;
213+
214+
keywords_count = keywords_count + 1;
215+
return keywords_count;
216+
}
217+
218+
/**
219+
* @brief Get defined keyword
220+
*
221+
* @param name
222+
* @return soare_keyword
223+
*/
224+
soare_keyword soare_getkeyword(char *name)
225+
{
226+
static soare_keyword none = {NULL, NULL};
227+
228+
if (!name)
229+
return none;
230+
231+
for (size_t i = 0; i < keywords_count; i++)
232+
if (!strcmp(keywords_list[i].name, name))
233+
return keywords_list[i];
234+
235+
return none;
236+
}
237+
238+
/**
239+
* @brief Check if a keyword exists
240+
*
241+
* @param name
242+
* @return unsigned char
243+
*/
244+
unsigned char soare_iskeyword(char *name)
245+
{
246+
if (!name)
247+
return 0;
248+
249+
for (size_t i = 0; i < keywords_count; i++)
250+
if (!strcmp(keywords_list[i].name, name))
251+
return 1;
252+
253+
return 0;
254+
}

core/Error.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void *LeaveException(SoareExceptions error, char *string, Document file)
103103
soare_write(
104104
//
105105
__soare_stderr,
106-
"Except: %s\n\t\"%.13s\"\n\t ^~~~\n\tAt file %s:%lld:%lld\n",
106+
"\aExcept: %s\n\t\"%.13s\"\n\t ^~~~\n\tAt file %s:%lld:%lld\n",
107107
Exceptions[error],
108108
string,
109109
file.file,
@@ -120,9 +120,5 @@ void *LeaveException(SoareExceptions error, char *string, Document file)
120120

121121
// Set error at level EXIT_FAILURE (1)
122122
errorlevel = EXIT_FAILURE;
123-
124-
// Store the error in the `__ERROR__` variable
125-
MemSet(MemGet(MEMORY, "__ERROR__"), strdup(Exceptions[error]));
126-
127123
return NULL;
128124
}

core/Math.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static void zeros(char *string)
107107
*/
108108
static char *__int(int number)
109109
{
110-
// Convert long double to string
110+
// Convert int to string
111111
char string[100] = {0};
112112
sprintf(string, "%d", number);
113113

core/Memory.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ MEM MEMORY = NULL;
2727
*/
2828
MEM Mem(void)
2929
{
30-
MEM memory = (mem*)malloc(sizeof(struct mem));
30+
MEM memory = (mem *)malloc(sizeof(struct mem));
3131

3232
if (!memory)
3333
return __SOARE_OUT_OF_MEMORY();
@@ -142,6 +142,8 @@ MEM MemSet(MEM memory, char *value)
142142
return memory;
143143
}
144144

145+
#ifdef __SOARE_DEBUG
146+
145147
/**
146148
* @brief Display all variables
147149
*
@@ -159,6 +161,8 @@ void MemLog(MEM memory)
159161
MemLog(memory->next);
160162
}
161163

164+
#endif /* __SOARE_DEBUG */
165+
162166
/**
163167
* @brief Join 2 memories
164168
*

0 commit comments

Comments
 (0)