Skip to content

Commit

Permalink
CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Astroner committed Apr 26, 2023
1 parent 68c16c3 commit 25df470
Show file tree
Hide file tree
Showing 18 changed files with 148 additions and 30 deletions.
98 changes: 68 additions & 30 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,64 +1,95 @@
CC=gcc-12
SOURCES=$(wildcard src/*.c src/*/*c)
OBJECTS=$(SOURCES:.c=.o)
HEADERS=headers

EXECUTABLE=./rede
# Core code
CORE_SOURCES=$(wildcard core/src/*.c core/src/*/*c)
CORE_OBJECTS=$(CORE_SOURCES:.c=.o)
CORE_HEADERS=core/headers
CORE_EXECUTABLE=./rede-core


# CLI code
CLI_SOURCES=$(wildcard cli/src/*.c cli/src/*/*.c)
CLI_OBJECTS=$(CLI_SOURCES:.c=.o)
CLI_HEADERS=cli/headers
CLI_EXECUTABLE=./rede


# Lib names
RUNTIME_LIB_NAME=RedeRuntime.h
COMPILER_LIB_NAME=RedeCompiler.h
STD_LIB_NAME=RedeSTD.h
GENERAL_LIB_NAME=Rede.h

all: build
$(EXECUTABLE)

build: $(OBJECTS)
$(CC) $(CFLAGS) -o $(EXECUTABLE) $^
.PHONY: core
cli: $(CLI_EXECUTABLE)
$(CLI_EXECUTABLE)

build-cli: $(CLI_EXECUTABLE)
echo "Done"

$(CLI_EXECUTABLE): $(CLI_OBJECTS)
$(CC) $(CFLAGS) -o $(CLI_EXECUTABLE) $^

$(CLI_OBJECTS): %.o: %.c
$(CC) $(CFLAGS) -c -o $@ -I$(CLI_HEADERS) -I./ -Wall -Wextra -std=c99 -pedantic $<


.PHONY: core
core: $(CORE_EXECUTABLE)
$(CORE_EXECUTABLE)

$(CORE_EXECUTABLE): $(CORE_OBJECTS)
$(CC) $(CFLAGS) -o $(CORE_EXECUTABLE) $^

$(CORE_OBJECTS): %.o: %.c
$(CC) $(CFLAGS) -c -o $@ -I$(CORE_HEADERS) -Wall -Wextra -std=c99 -pedantic $<

.c.o:
$(CC) $(CFLAGS) -c -o $@ -I$(HEADERS) -Wall -Wextra -std=c99 -pedantic $<

# Builds runtime STB-like lib
runtime: headers/RedeRuntime.h headers/RedeByteCodes.h src/RedeRuntime.c
cat headers/RedeRuntime.h > $(RUNTIME_LIB_NAME)
tail -n +2 headers/RedeRuntimeUtils.h >> $(RUNTIME_LIB_NAME)
$(RUNTIME_LIB_NAME): core/headers/RedeRuntime.h core/headers/RedeByteCodes.h core/src/RedeRuntime.c
cat core/headers/RedeRuntime.h > $(RUNTIME_LIB_NAME)
tail -n +2 core/headers/RedeRuntimeUtils.h >> $(RUNTIME_LIB_NAME)
echo "\n#if defined(REDE_RUNTIME_IMPLEMENTATION)" >> $(RUNTIME_LIB_NAME)
cat headers/RedeByteCodes.h >> $(RUNTIME_LIB_NAME)
tail -n +2 src/RedeRuntimeUtils.c >> $(RUNTIME_LIB_NAME)
tail -n +3 src/RedeRuntime.c >> $(RUNTIME_LIB_NAME)
cat core/headers/RedeByteCodes.h >> $(RUNTIME_LIB_NAME)
tail -n +2 core/src/RedeRuntimeUtils.c >> $(RUNTIME_LIB_NAME)
tail -n +3 core/src/RedeRuntime.c >> $(RUNTIME_LIB_NAME)
echo "\n#endif // REDE_RUNTIME_IMPLEMENTATION" >> $(RUNTIME_LIB_NAME)



# Builds compiler STB-like lib
compiler: headers/RedeCompiler.h headers/RedeSourceIterator.h headers/logs.h headers/RedeByteCodes.h src/RedeCompiler.c src/RedeSourceIterator.c
cat headers/RedeCompiler.h > $(COMPILER_LIB_NAME)
$(COMPILER_LIB_NAME): core/headers/RedeCompiler.h core/headers/RedeSourceIterator.h core/headers/logs.h core/headers/RedeByteCodes.h core/src/RedeCompiler.c core/src/RedeSourceIterator.c
cat core/headers/RedeCompiler.h > $(COMPILER_LIB_NAME)
echo "\n#if defined(REDE_COMPILER_IMPLEMENTATION)" >> $(COMPILER_LIB_NAME)
cat headers/RedeSourceIterator.h >> $(COMPILER_LIB_NAME)
cat core/headers/RedeSourceIterator.h >> $(COMPILER_LIB_NAME)
echo "\n" >> $(COMPILER_LIB_NAME)
tail -n +3 src/RedeSourceIterator.c >> $(COMPILER_LIB_NAME)
tail -n +3 core/src/RedeSourceIterator.c >> $(COMPILER_LIB_NAME)
echo "\n" >> $(COMPILER_LIB_NAME)
cat headers/logs.h >> $(COMPILER_LIB_NAME)
cat core/headers/logs.h >> $(COMPILER_LIB_NAME)
echo "\n" >> $(COMPILER_LIB_NAME)
cat headers/RedeByteCodes.h >> $(COMPILER_LIB_NAME)
cat core/headers/RedeByteCodes.h >> $(COMPILER_LIB_NAME)
echo "\n" >> $(COMPILER_LIB_NAME)
tail -n +5 src/RedeCompiler.c >> $(COMPILER_LIB_NAME)
tail -n +5 core/src/RedeCompiler.c >> $(COMPILER_LIB_NAME)
echo "\n#endif // REDE_COMPILER_IMPLEMENTATION" >> $(COMPILER_LIB_NAME)

# Build standard library
std: headers/RedeStd.h src/RedeStd.c


# Build STB-like standard library
$(STD_LIB_NAME): core/headers/RedeStd.h core/src/RedeStd.c
echo "#if !defined(REDE_RUNTIME_PATH)" > $(STD_LIB_NAME)
echo "#define REDE_RUNTIME_PATH \"RedeRuntime.h\"" >> $(STD_LIB_NAME)
echo "#endif // REDE_RUNTIME_PATH\n" >> $(STD_LIB_NAME)
echo "#include REDE_RUNTIME_PATH" >> $(STD_LIB_NAME)
tail -n +3 headers/RedeStd.h >> $(STD_LIB_NAME)
tail -n +3 core/headers/RedeStd.h >> $(STD_LIB_NAME)
echo "\n#if defined(REDE_STD_IMPLEMENTATION)" >> $(STD_LIB_NAME)
tail -n +2 src/RedeStd.c >> $(STD_LIB_NAME)
tail -n +2 core/src/RedeStd.c >> $(STD_LIB_NAME)
echo "\n#endif // REDE_STD_IMPLEMENTATION" >> $(STD_LIB_NAME)



# Builds general lib
general: runtime compiler std
$(GENERAL_LIB_NAME): $(RUNTIME_LIB_NAME) $(COMPILER_LIB_NAME) $(STD_LIB_NAME)
echo "#if defined(REDE_IMPLEMENTATION)" > $(GENERAL_LIB_NAME)
echo "#define REDE_COMPILER_IMPLEMENTATION" >> $(GENERAL_LIB_NAME)
echo "#define REDE_RUNTIME_IMPLEMENTATION" >> $(GENERAL_LIB_NAME)
Expand All @@ -69,11 +100,14 @@ general: runtime compiler std
tail -n +7 $(STD_LIB_NAME) >> $(GENERAL_LIB_NAME)



.PHONY: libs
# Builds libs
libs: runtime compiler std general
libs: $(RUNTIME_LIB_NAME) $(COMPILER_LIB_NAME) $(STD_LIB_NAME) $(GENERAL_LIB_NAME)
echo "\nDone"


.PHONY: tests
tests: libs
$(CC) -o tests/runtime.test tests/runtime.c
$(CC) -o tests/compiler.test tests/compiler.c
Expand All @@ -88,8 +122,12 @@ tests: libs
rm -f tests/runtime.test tests/compiler.test tests/std.test tests/general.test



.PHONY: clean
clean:
rm -f $(OBJECTS) $(EXECUTABLE)
rm -f $(CORE_OBJECTS) $(CORE_EXECUTABLE) $(CLI_OBJECTS) $(CLI_EXECUTABLE)


.PHONY: clean-all
clean-all: clean
rm -f $(RUNTIME_LIB_NAME) $(COMPILER_LIB_NAME)
3 changes: 3 additions & 0 deletions Rede.h
Original file line number Diff line number Diff line change
Expand Up @@ -1307,8 +1307,11 @@ typedef struct RedeFunction {
RedeFunction functions[7] = {
[0] = { "log", Rede_std_log },
[1] = { "sum", Rede_std_sum },
[2] = { "" },
[3] = { "mult", Rede_std_mult },
[4] = { "" },
[5] = { "length", Rede_std_length },
[6] = { "" },
};

int Rede_std(
Expand Down
3 changes: 3 additions & 0 deletions RedeSTD.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ typedef struct RedeFunction {
RedeFunction functions[7] = {
[0] = { "log", Rede_std_log },
[1] = { "sum", Rede_std_sum },
[2] = { "" },
[3] = { "mult", Rede_std_mult },
[4] = { "" },
[5] = { "length", Rede_std_length },
[6] = { "" },
};

int Rede_std(
Expand Down
2 changes: 2 additions & 0 deletions cli/src/Rede.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define REDE_IMPLEMENTATION
#include "Rede.h"
69 changes: 69 additions & 0 deletions cli/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <stdio.h>
#include <string.h>

#include "Rede.h"

#define MAX_LINE_WIDTH 100

typedef struct CLIData {
RedeRuntimeMemory* memory;
int quit;
} CLIData;

int fCall(const char *name, size_t nameLength, const RedeFunctionArgs *args, RedeVariable *result, void *sharedData) {
int status = Rede_std(name, nameLength, args, result, NULL);
if(status != -2) return status;

CLIData* data = sharedData;

if(strcmp(name, "memory") == 0) {
Rede_printMemory(data->memory);
return 0;
} else if(strcmp(name, "quit") == 0 || strcmp(name, "q") == 0) {
data->quit = 1;
return 0;
}

return 0;
}

int main(void) {
printf("ReDe Scripting CLI\n");

char buffer[MAX_LINE_WIDTH];
memset(buffer, 0, MAX_LINE_WIDTH);

Rede_createStringSource(code, buffer);
Rede_createCompilationMemory(compilation, 200, 256);

Rede_createRuntimeMemory(runtime, 100, 256, 1024);
Rede_createByteCodeFromBuffer(bytes, compilation->buffer);

CLIData data = {
.quit = 0,
.memory = runtime,
};

while(!data.quit) {
fgets(buffer, MAX_LINE_WIDTH, stdin);

int status = Rede_compile(code, compilation);

if(status < 0) {
printf("Failed to compile\n");
goto loop_end;
}

status = Rede_execute(bytes, runtime, fCall, &data);

if(status < 0) {
printf("Failed to execute\n");
}

loop_end:
memset(buffer, 0, MAX_LINE_WIDTH);
compilation->bufferActualLength = 0;
}

return 0;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions src/RedeStd.c → core/src/RedeStd.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ typedef struct RedeFunction {
RedeFunction functions[7] = {
[0] = { "log", Rede_std_log },
[1] = { "sum", Rede_std_sum },
[2] = { "" },
[3] = { "mult", Rede_std_mult },
[4] = { "" },
[5] = { "length", Rede_std_length },
[6] = { "" },
};

int Rede_std(
Expand Down
File renamed without changes.

0 comments on commit 25df470

Please sign in to comment.