aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/collect.h8
-rw-r--r--src/err.h17
-rw-r--r--src/expression.h14
-rw-r--r--src/load.c (renamed from src/collect.c)52
-rw-r--r--src/load.h19
-rw-r--r--src/main.c51
-rw-r--r--src/parse.c2
-rw-r--r--src/parse.h4
-rw-r--r--src/run.c74
-rw-r--r--src/run.h8
-rw-r--r--src/util.h17
-rw-r--r--src/vm.c52
-rw-r--r--src/vm.h6
13 files changed, 130 insertions, 194 deletions
diff --git a/src/collect.h b/src/collect.h
deleted file mode 100644
index da118a5..0000000
--- a/src/collect.h
+++ /dev/null
@@ -1,8 +0,0 @@
-#ifndef _COLLECT_H_
-#define _COLLECT_H_
-
-#include "../api/vm.h"
-
-UwUVMProgram create_program(const char *progname, const char *modname);
-
-#endif
diff --git a/src/err.h b/src/err.h
deleted file mode 100644
index 1dccbf9..0000000
--- a/src/err.h
+++ /dev/null
@@ -1,17 +0,0 @@
-#ifndef _ERR_H_
-#define _ERR_H_
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-
-static inline void error(const char *format, ...)
-{
- va_list args;
- va_start(args, format);
- vfprintf(stderr, format, args);
- va_end(args);
- exit(1);
-}
-
-#endif
diff --git a/src/expression.h b/src/expression.h
deleted file mode 100644
index 55d10f7..0000000
--- a/src/expression.h
+++ /dev/null
@@ -1,14 +0,0 @@
-#ifndef _EXPRESSION_H_
-#define _EXPRESSION_H_
-
-typedef enum
-{
- EX_UNINIT,
- EX_INTLIT,
- EX_STRLIT,
- EX_ARGNUM,
- EX_FNNAME,
- EX_FNCALL,
-} ExpressionType;
-
-#endif
diff --git a/src/collect.c b/src/load.c
index ae15513..edeba97 100644
--- a/src/collect.c
+++ b/src/load.c
@@ -4,9 +4,9 @@
#include <stdarg.h>
#include <libgen.h>
#include <dlfcn.h>
-#include "err.h"
-#include "util.h"
-#include "collect.h"
+#include "common/err.h"
+#include "common/str.h"
+#include "load.h"
#include "parse.h"
#define DEBUG 0
@@ -79,8 +79,8 @@ typedef struct
char *std_path; // path to standard library
- UwUVMProgram program; // the result program
-} CollectorState;
+ Program program; // the result program
+} LoadState;
// functions
@@ -108,7 +108,7 @@ static inline char *get_filename(const char *module_path)
}
// module_path is a mallocated string
-static Module *require_module(CollectorState *state, char *module_path)
+static Module *require_module(LoadState *state, char *module_path)
{
for (size_t i = 0; i < state->num_modules; i++) {
Module *module = state->modules[i];
@@ -150,13 +150,13 @@ static Module *require_module(CollectorState *state, char *module_path)
char *err = dlerror();
if (err)
- error("%s\n", err);
+ error("library error: %s\n", err);
}
return module;
}
-static UwUVMFunction *require_function(CollectorState *state, Module *module, const char *name)
+static UwUVMFunction *require_function(LoadState *state, Module *module, const char *name)
{
for (size_t i = 0; i < module->num_functions; i++) {
FunctionLink *link = &module->functions[i];
@@ -180,7 +180,7 @@ static UwUVMFunction *require_function(CollectorState *state, Module *module, co
return ref;
}
-static UwUVMFunction *resolve_function(CollectorState *state, Module *caller_module, const char *full_name)
+static UwUVMFunction *resolve_function(LoadState *state, Module *caller_module, const char *full_name)
{
size_t len = strlen(full_name);
@@ -221,7 +221,7 @@ static UwUVMFunction *resolve_function(CollectorState *state, Module *caller_mod
return require_function(state, callee_module, fnname);
}
-static void translate_expression(CollectorState *state, Module *module, UwUVMExpression *vm_expr, ParseExpression *parse_expr)
+static void translate_expression(LoadState *state, Module *module, UwUVMExpression *vm_expr, ParseExpression *parse_expr)
{
UwUVMFunction *vm_function;
@@ -263,13 +263,10 @@ static void translate_expression(CollectorState *state, Module *module, UwUVMExp
free(parse_expr);
}
-static void load_functions(CollectorState *state, Module *module)
+static void load_functions(LoadState *state, Module *module)
{
for (; module->loaded_functions < module->num_functions; module->loaded_functions++) {
- FunctionLink *linkptr = &module->functions[module->loaded_functions];
- FunctionLink link = *linkptr;
-
- bool found = false;
+ FunctionLink *link = &module->functions[module->loaded_functions];
if (module->type == MODULE_PLAIN) {
ParseFunction **function = NULL;
@@ -277,34 +274,31 @@ static void load_functions(CollectorState *state, Module *module)
for (size_t i = 0; i < module->handle.ast.num_functions; i++) {
ParseFunction **fn = &module->handle.ast.functions[i];
- if (*fn && strcmp((*fn)->name, link.name) == 0) {
+ if (*fn && strcmp((*fn)->name, link->name) == 0) {
function = fn;
break;
}
}
if (function) {
- found = true;
- linkptr = NULL;
-
- translate_expression(state, module, link.ref->value.plain = malloc(sizeof(UwUVMExpression)), (*function)->expression);
+ translate_expression(state, module, link->ref->value.plain = malloc(sizeof(UwUVMExpression)), (*function)->expression);
free((*function)->name);
free(*function);
*function = NULL;
+ } else {
+ error("error: no function %s in module %s\n", link->name, module->filename);
}
} else {
- char *symbol = asprintf_wrapper("uwu_%s", link.name);
- linkptr->ref->value.native = dlsym(module->handle.lib, symbol);
+ char *symbol = asprintf_wrapper("uwu_%s", link->name);
+ link->ref->value.native = dlsym(module->handle.lib, symbol);
- if (! dlerror())
- found = true;
+ char *err = dlerror();
+ if (err)
+ error("library error: %s\n", err);
free(symbol);
}
-
- if (! found)
- error("error: no function %s in module %s\n", link.name, module->filename);
}
}
@@ -324,12 +318,12 @@ static void free_expression(ParseExpression *expr)
free(expr);
}
-UwUVMProgram create_program(const char *progname, const char *modname)
+Program load_program(const char *progname, const char *modname)
{
char *prog_dirname = dirname_wrapper(progname);
char *api_path = asprintf_wrapper("%s/api/api.so", prog_dirname);
- CollectorState state = {
+ LoadState state = {
.modules = NULL,
.num_modules = 0,
.std_path = asprintf_wrapper("%s/std", prog_dirname),
diff --git a/src/load.h b/src/load.h
new file mode 100644
index 0000000..5d6b00e
--- /dev/null
+++ b/src/load.h
@@ -0,0 +1,19 @@
+#ifndef _LOAD_H_
+#define _LOAD_H_
+
+#include <stddef.h>
+#include "api/vm.h"
+
+typedef struct
+{
+ void *api_library;
+ UwUVMFunction *main_function;
+ UwUVMFunction **functions;
+ size_t num_functions;
+ void **libraries;
+ size_t num_libraries;
+} Program;
+
+Program load_program(const char *progname, const char *modname);
+
+#endif
diff --git a/src/main.c b/src/main.c
index 60c22ba..2a29d80 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,56 +1,11 @@
-#include "err.h"
-#include "vm.h"
+#include "common/err.h"
+#include "run.h"
int main(int argc, char *argv[])
{
if (argc < 2)
error("usage: %s <module>\n", argv[0]);
- vm_run_file(argv[0], argv[1]);
+ run_module(argv[0], argv[1], argc > 2 ? (size_t) argc - 2 : 0, &argv[2]);
return 0;
}
-
-/*
-
-0123
-"asd"
-$arg
-&fnname
-func(asd)
-
-:int:add
-:str:cat
-:boo:and
-:arr:arr
-:set:set
-
-integer::add()
-integer::sub()
-integer::mul()
-integer::div()
-integer::mod()
-integer::pow()
-
-string::concat()
-string::split()
-string::find()
-
-array::array()
-array::select()
-array::insert()
-array::length()
-array::reduce()
-array::map()
-
-set::set()
-set::pair()
-set::select()
-set::insert()
-set::remove()
-set::contains()
-
-boolean::and()
-boolean::or()
-boolean::xor()
-
-*/
diff --git a/src/parse.c b/src/parse.c
index c8b430d..ed09de9 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
-#include "err.h"
+#include "common/err.h"
#include "parse.h"
#define DEBUG 0
diff --git a/src/parse.h b/src/parse.h
index 7aca6af..46f4542 100644
--- a/src/parse.h
+++ b/src/parse.h
@@ -3,11 +3,11 @@
#include <stddef.h>
#include <stdbool.h>
-#include "expression.h"
+#include "api/vm.h"
typedef struct ParseExpression
{
- ExpressionType type;
+ UwUVMExpressionType type;
union
{
int int_value;
diff --git a/src/run.c b/src/run.c
new file mode 100644
index 0000000..7733c8d
--- /dev/null
+++ b/src/run.c
@@ -0,0 +1,74 @@
+#include <dlfcn.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include "common/err.h"
+#include "load.h"
+#include "run.h"
+
+static void free_expression(UwUVMExpression *expr)
+{
+ if (expr->type == EX_FNCALL) {
+ for (size_t i = 0; i < expr->value.cll_value.num_args; i++)
+ free_expression(&expr->value.cll_value.args[i]);
+
+ free(expr->value.cll_value.args);
+ }
+
+ if (expr->type == EX_STRLIT)
+ free(expr->value.str_value);
+}
+
+void run_module(const char *progname, const char *modname, size_t num_args, char *args[])
+{
+ (void) num_args;
+ (void) args;
+
+ Program program = load_program(progname, modname);
+
+ UwUVMValue (*uwuvm_call_function)(UwUVMFunction *, size_t, UwUVMExpression *, UwUVMArgs *) = dlsym(program.api_library, "uwuvm_call_function");
+ char *(*uwuvm_print_value )(UwUVMValue ) = dlsym(program.api_library, "uwuvm_print_value" );
+ void (*uwuvm_delet_value )(UwUVMValue ) = dlsym(program.api_library, "uwuvm_delet_value" );
+
+ char *err = dlerror();
+ if (err)
+ error("library error: %s\n", err);
+
+ UwUVMExpression arg_expressions[num_args];
+
+ for (size_t i = 0; i < num_args; i++)
+ arg_expressions[i] = (UwUVMExpression) {
+ .type = EX_STRLIT,
+ .value = {
+ .str_value = args[i],
+ },
+ };
+
+ UwUVMValue result = uwuvm_call_function(program.main_function, num_args, arg_expressions, NULL);
+
+ char *str = uwuvm_print_value(result);
+ printf("%s\n", str);
+ free(str);
+
+ uwuvm_delet_value(result);
+
+ for (size_t i = 0; i < program.num_functions; i++) {
+ UwUVMFunction *function = program.functions[i];
+
+ if (function->type == MODULE_PLAIN) {
+ free_expression(function->value.plain);
+ free(function->value.plain);
+ }
+
+ free(function);
+ }
+
+ free(program.functions);
+
+ for (size_t i = 0; i < program.num_libraries; i++)
+ dlclose(program.libraries[i]);
+
+ if (program.libraries)
+ free(program.libraries);
+
+ dlclose(program.api_library);
+}
diff --git a/src/run.h b/src/run.h
new file mode 100644
index 0000000..010dec0
--- /dev/null
+++ b/src/run.h
@@ -0,0 +1,8 @@
+#ifndef _RUN_H_
+#define _RUN_H_
+
+#include <stddef.h>
+
+void run_module(const char *progname, const char *modname, size_t num_args, char *args[]);
+
+#endif
diff --git a/src/util.h b/src/util.h
deleted file mode 100644
index b0baa37..0000000
--- a/src/util.h
+++ /dev/null
@@ -1,17 +0,0 @@
-#ifndef _UTIL_H_
-#define _UTIL_H_
-
-#include <stdio.h>
-#include <stdarg.h>
-
-static inline char *asprintf_wrapper(const char *format, ...)
-{
- va_list args;
- va_start(args, format);
- char *ptr;
- vasprintf(&ptr, format, args);
- va_end(args);
- return ptr;
-}
-
-#endif
diff --git a/src/vm.c b/src/vm.c
deleted file mode 100644
index 8b1b13a..0000000
--- a/src/vm.c
+++ /dev/null
@@ -1,52 +0,0 @@
-#include <dlfcn.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include "collect.h"
-#include "vm.h"
-
-static void free_expression(UwUVMExpression *expr)
-{
- if (expr->type == EX_FNCALL) {
- for (size_t i = 0; i < expr->value.cll_value.num_args; i++)
- free_expression(&expr->value.cll_value.args[i]);
-
- free(expr->value.cll_value.args);
- }
-
- if (expr->type == EX_STRLIT)
- free(expr->value.str_value);
-}
-
-void vm_run_file(const char *progname, const char *modname)
-{
- UwUVMProgram program = create_program(progname, modname);
- UwUVMValue result = ((UwUVMValue (*)(UwUVMFunction *, size_t, UwUVMExpression *, UwUVMArgs *)) dlsym(program.api_library, "uwuvm_call_function"))(program.main_function, 0, NULL, NULL);
-
- char *str = ((char *(*)(UwUVMValue)) dlsym(program.api_library, "uwuvm_print_value"))(result);
-
- printf("%s\n", str);
- free(str);
-
- ((void (*)(UwUVMValue)) dlsym(program.api_library, "uwuvm_delet_value"))(result);
-
- for (size_t i = 0; i < program.num_functions; i++) {
- UwUVMFunction *function = program.functions[i];
-
- if (function->type == MODULE_PLAIN) {
- free_expression(function->value.plain);
- free(function->value.plain);
- }
-
- free(function);
- }
-
- free(program.functions);
-
- for (size_t i = 0; i < program.num_libraries; i++)
- dlclose(program.libraries[i]);
-
- if (program.libraries)
- free(program.libraries);
-
- dlclose(program.api_library);
-}
diff --git a/src/vm.h b/src/vm.h
deleted file mode 100644
index 5e23c8b..0000000
--- a/src/vm.h
+++ /dev/null
@@ -1,6 +0,0 @@
-#ifndef _VM_H_
-#define _VM_H_
-
-void vm_run_file(const char *progname, const char *modname);
-
-#endif