Skip to content

Commit 08de091

Browse files
committed
Project init.
1 parent e3a76a2 commit 08de091

File tree

7 files changed

+308
-0
lines changed

7 files changed

+308
-0
lines changed

.vscode/launch.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "gcc.exe - Build and debug active file",
9+
"type": "cppdbg",
10+
"request": "launch",
11+
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
12+
"args": [],
13+
"stopAtEntry": false,
14+
"cwd": "C:\\MinGW\\bin",
15+
"environment": [],
16+
"externalConsole": false,
17+
"MIMode": "gdb",
18+
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
19+
"setupCommands": [
20+
{
21+
"description": "Enable pretty-printing for gdb",
22+
"text": "-enable-pretty-printing",
23+
"ignoreFailures": true
24+
}
25+
],
26+
"preLaunchTask": "C/C++: gcc.exe build active file"
27+
}
28+
]
29+
}

.vscode/tasks.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"tasks": [
3+
{
4+
"type": "cppbuild",
5+
"label": "C/C++: gcc.exe build active file",
6+
"command": "C:\\MinGW\\bin\\gcc.exe",
7+
"args": [
8+
"-g",
9+
"${file}",
10+
"-o",
11+
"${fileDirname}\\${fileBasenameNoExtension}.exe"
12+
],
13+
"options": {
14+
"cwd": "C:\\MinGW\\bin"
15+
},
16+
"problemMatcher": [
17+
"$gcc"
18+
],
19+
"group": {
20+
"kind": "build",
21+
"isDefault": true
22+
},
23+
"detail": "Task generated by Debugger."
24+
}
25+
],
26+
"version": "2.0.0"
27+
}

Makefile

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
########################################################################
2+
####################### Makefile Template ##############################
3+
########################################################################
4+
5+
# Compiler settings - Can be customized.
6+
CC = gcc
7+
CXXFLAGS = -std=c11 -Wall
8+
LDFLAGS =
9+
10+
# Makefile settings - Can be customized.
11+
APPNAME = myapp
12+
EXT = .c
13+
SRCDIR = src
14+
OBJDIR = obj
15+
16+
############## Do not change anything from here downwards! #############
17+
SRC = $(wildcard $(SRCDIR)/*$(EXT))
18+
OBJ = $(SRC:$(SRCDIR)/%$(EXT)=$(OBJDIR)/%.o)
19+
DEP = $(OBJ:$(OBJDIR)/%.o=%.d)
20+
# UNIX-based OS variables & settings
21+
RM = rm
22+
DELOBJ = $(OBJ)
23+
# Windows OS variables & settings
24+
DEL = del
25+
EXE = .exe
26+
WDELOBJ = $(SRC:$(SRCDIR)/%$(EXT)=$(OBJDIR)\\%.o)
27+
28+
########################################################################
29+
####################### Targets beginning here #########################
30+
########################################################################
31+
32+
all: $(APPNAME)
33+
34+
# Builds the app
35+
$(APPNAME): $(OBJ)
36+
$(CC) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)
37+
38+
# Creates the dependecy rules
39+
%.d: $(SRCDIR)/%$(EXT)
40+
@$(CPP) $(CFLAGS) $< -MM -MT $(@:%.d=$(OBJDIR)/%.o) >$@
41+
42+
# Includes all .h files
43+
-include $(DEP)
44+
45+
# Building rule for .o files and its .c/.cpp in combination with all .h
46+
$(OBJDIR)/%.o: $(SRCDIR)/%$(EXT)
47+
$(CC) $(CXXFLAGS) -o $@ -c $<
48+
49+
################### Cleaning rules for Unix-based OS ###################
50+
# Cleans complete project
51+
.PHONY: clean
52+
clean:
53+
$(RM) $(DELOBJ) $(DEP) $(APPNAME)
54+
55+
# Cleans only all files with the extension .d
56+
.PHONY: cleandep
57+
cleandep:
58+
$(RM) $(DEP)
59+
60+
#################### Cleaning rules for Windows OS #####################
61+
# Cleans complete project
62+
.PHONY: cleanw
63+
cleanw:
64+
$(DEL) $(WDELOBJ) $(DEP) $(APPNAME)$(EXE)
65+
66+
# Cleans only all files with the extension .d
67+
.PHONY: cleandepw
68+
cleandepw:
69+
$(DEL) $(DEP)

WS-code.code-workspace

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "."
5+
}
6+
],
7+
"settings": {}
8+
}

inc/interpreter.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Include Guard
2+
#ifndef INTERPRETER_H_
3+
#define INTERPRETER_H_
4+
5+
6+
#include <stdio.h>
7+
#include <stdint.h>
8+
#include <string.h>
9+
10+
#define NUM_OF_API_FUNCS 7
11+
12+
// structure for instruction data
13+
typedef struct API_t{
14+
15+
struct API_t *left; // Left element on a binatry tree branch
16+
struct API_t *right; // Right element on a binary tree branch
17+
const char **name; // Name of the instruction
18+
const char **desc; // Description of the command
19+
void(*func)(char*,char*); // Function pointer to the command function
20+
21+
}API_t;
22+
23+
24+
void add_interpreter_instruction(const char **name, const char **desc, void(*func)(char*,char*));
25+
26+
void init_interpreter(void);
27+
28+
void print_apis_in_order(API_t *head);
29+
30+
31+
void teszt(void);
32+
33+
#endif

src/interpreter.c

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include "../inc/interpreter.h"
2+
3+
API_t API_tree[NUM_OF_API_FUNCS];
4+
uint32_t API_cntr;
5+
6+
//macro to add new instruction to the instructions binary tree
7+
#define create_instruction_data(name, desc) \
8+
const char *name##_API_NAME = (const char *)#name; \
9+
const char *name##_API_DESC = (const char *)desc;
10+
11+
// ***** Create instruction data for the API *****
12+
create_instruction_data(cica, "cica fuggveny leirasa, jeeee :)");
13+
create_instruction_data(kutya, "kutya fuggveny leirasa, joooo :)");
14+
create_instruction_data(tehen, "tehen fuggveny leirasa, jii :)");
15+
create_instruction_data(majom, "majom fuggveny leirasa, jaaaa :)");
16+
create_instruction_data(eger, "eger fuggveny leirasa, gyoooo :)");
17+
create_instruction_data(okor, "eger fuggveny leirasa, gyaaa :)");
18+
create_instruction_data(csiga, "eger fuggveny leirasa, dikk :)");
19+
20+
#define add_instruction(name, func) add_interpreter_instruction(&name##_API_NAME, &name##_API_DESC, func);
21+
22+
void cica_func(char *args, char *response)
23+
{
24+
printf("Cica!\r\n");
25+
printf("Args: %s\r\n", args);
26+
}
27+
28+
void kutya_func(char *args, char *response)
29+
{
30+
printf("Kutya!\r\n");
31+
printf("Args: %s\r\n", args);
32+
}
33+
34+
void init_interpreter(void)
35+
{
36+
// Initialize the 'API_cntr' variable as zero before adding new items
37+
API_cntr = 0;
38+
39+
// ***** Create the binary tree of the instructions *****
40+
add_instruction(majom, kutya_func);
41+
add_instruction(tehen, kutya_func);
42+
add_instruction(cica, cica_func);
43+
add_instruction(kutya, kutya_func);
44+
add_instruction(eger, kutya_func);
45+
add_instruction(okor, kutya_func);
46+
add_instruction(csiga, kutya_func);
47+
48+
print_apis_in_order( &API_tree[0] );
49+
50+
if( API_cntr != NUM_OF_API_FUNCS ){
51+
printf("**ERROR**\tAPI function number mismatch!!!\r\n");
52+
}
53+
}
54+
55+
void add_interpreter_instruction(const char **name, const char **desc, void (*func)(char *, char *))
56+
{
57+
API_t *next;
58+
API_t *prev;
59+
int32_t comp_res;
60+
61+
// if the API_cntr value is greater or equal than the defined maximum
62+
// API command number, there is a problem in the code!
63+
if (API_cntr >= NUM_OF_API_FUNCS)
64+
{
65+
66+
printf("**ERROR**\tToo many instruction, memory is full!\r\n");
67+
return;
68+
}
69+
70+
// print out the new command name
71+
//printf("%s\r\n", (char *)*name);
72+
73+
// if it is the first command we hacve to create the root of the
74+
// binary tree
75+
if (API_cntr == 0)
76+
{
77+
API_tree[0].name = (const char**)name; // address of the name string( char** type )
78+
API_tree[0].desc = (const char**)desc; // address of the description string( char** type )
79+
API_tree[0].func = func; // function pointer to the actual function
80+
81+
API_tree[0].left = NULL; // because it is the first element of the tree,
82+
API_tree[0].right = NULL; // left and right branches has to be NULL
83+
}
84+
85+
// if it is ot the first command we have to find it's place in the tree
86+
else
87+
{
88+
prev = &API_tree[0]; // get the address of the root element
89+
comp_res = strcmp( (char*)*(prev->name), (char*)*name ); // compare the names and save the result to 'comp_res'
90+
91+
// compare( ABC order ) the root element name and the new element name
92+
(comp_res > 0) ? (next = (prev->left)) : ( next = (prev->right));
93+
94+
// find the place in the tree
95+
while (next != 0)
96+
{
97+
prev = next;
98+
comp_res = strcmp( *(prev->name), *name);
99+
(comp_res > 0) ? (next = (prev->left)) : (next = (prev->right));
100+
}
101+
102+
// link the new item on the previous branch
103+
( comp_res > 0 ) ? ( ( prev->left ) = &API_tree[API_cntr] ) : ( ( prev->right ) = &API_tree[API_cntr] );
104+
105+
API_tree[API_cntr].name = (const char**)name; // address of the name string( char** type )
106+
API_tree[API_cntr].desc = (const char**)desc; // address of the description string( char** type )
107+
API_tree[API_cntr].func = func; // function pointer to the actual function
108+
109+
API_tree[API_cntr].left = NULL; // close the branch
110+
API_tree[API_cntr].right = NULL;
111+
}
112+
113+
API_cntr++;
114+
115+
}
116+
117+
void print_apis_in_order(API_t *head){
118+
119+
if( head==0 ){
120+
return;
121+
}
122+
123+
print_apis_in_order( head -> left );
124+
printf( "%s\r\n", *(head->name) );
125+
print_apis_in_order( head -> right );
126+
}

src/main.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdio.h>
2+
3+
#include "../inc/interpreter.h"
4+
5+
6+
int main(){
7+
8+
printf("Program START...\r\n");
9+
10+
11+
init_interpreter();
12+
13+
14+
15+
return 0;
16+
}

0 commit comments

Comments
 (0)