Some things are broken.
EVERYTHING IS BROKEN AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.
This commit is contained in:
parent
40b91b96bd
commit
eb3fbca030
3
TODO.md
3
TODO.md
@ -1,2 +1,5 @@
|
|||||||
|
FIX EXCEPTION PRINTING (probably broken because of string literals being dereferenced)
|
||||||
|
|
||||||
|
|
||||||
1. Change editor to GNU Readline.
|
1. Change editor to GNU Readline.
|
||||||
2. Make variables persist through lines in the editor.
|
2. Make variables persist through lines in the editor.
|
||||||
|
@ -11,7 +11,7 @@ TEST_DIR = test
|
|||||||
TEST_BUILD_DIR = $(BUILD_DIR)/test
|
TEST_BUILD_DIR = $(BUILD_DIR)/test
|
||||||
TEST_OBJ_DIR = $(TEST_BUILD_DIR)/obj
|
TEST_OBJ_DIR = $(TEST_BUILD_DIR)/obj
|
||||||
|
|
||||||
CC = clang
|
CC = clang -std=c2x
|
||||||
LINK = clang
|
LINK = clang
|
||||||
CFLAGS = -Wall -DDBG -ggdb -fsanitize=leak
|
CFLAGS = -Wall -DDBG -ggdb -fsanitize=leak
|
||||||
LDFLAGS = -lm
|
LDFLAGS = -lm
|
||||||
|
17
src/ast.c
17
src/ast.c
@ -9,7 +9,7 @@ extern AST* root;
|
|||||||
static char* asttype_names[] = {
|
static char* asttype_names[] = {
|
||||||
[AST_TYPE_CALL] = "FUNC CALL", [AST_TYPE_NUM] = "NUMBER",
|
[AST_TYPE_CALL] = "FUNC CALL", [AST_TYPE_NUM] = "NUMBER",
|
||||||
[AST_TYPE_VREF] = "VAR REFERENCE", [AST_TYPE_VDEF] = "VAR DEFINITION",
|
[AST_TYPE_VREF] = "VAR REFERENCE", [AST_TYPE_VDEF] = "VAR DEFINITION",
|
||||||
[AST_TYPE_BLOCK] = "BLOCK",
|
[AST_TYPE_BLOCK] = "BLOCK", [AST_TYPE_EXC] = "EXCEPTION"
|
||||||
};
|
};
|
||||||
|
|
||||||
AST* ast_init(ASTType type, void* data) {
|
AST* ast_init(ASTType type, void* data) {
|
||||||
@ -50,6 +50,7 @@ void ast_print_i(AST* ast, int i) {
|
|||||||
printf("%s %lf\n", INDENT_spacing->buf, *(ASTNumData*)ast->data);
|
printf("%s %lf\n", INDENT_spacing->buf, *(ASTNumData*)ast->data);
|
||||||
break;
|
break;
|
||||||
case AST_TYPE_CALL: ast_call_print(ast->data, i + 2); break;
|
case AST_TYPE_CALL: ast_call_print(ast->data, i + 2); break;
|
||||||
|
case AST_TYPE_EXC: ast_exc_print(ast->data, i + 2); break;
|
||||||
case AST_TYPE_VREF: ast_vref_print(ast->data, i + 2); break;
|
case AST_TYPE_VREF: ast_vref_print(ast->data, i + 2); break;
|
||||||
case AST_TYPE_VDEF: ast_vdef_print(ast->data, i + 2); break;
|
case AST_TYPE_VDEF: ast_vdef_print(ast->data, i + 2); break;
|
||||||
case AST_TYPE_BLOCK: ast_block_print(ast->data, i + 2); break;
|
case AST_TYPE_BLOCK: ast_block_print(ast->data, i + 2); break;
|
||||||
@ -77,8 +78,18 @@ void ast_num_print(ASTNumData* data, int i) {
|
|||||||
INDENT_END;
|
INDENT_END;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTExcData* ast_exc_data_init(char* msg) {
|
ASTExcData* ast_exc_data_init(char* msg) { return (ASTExcData*)msg; }
|
||||||
return (ASTExcData*) msg;
|
|
||||||
|
void ast_exc_print(ASTExcData* data, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
|
||||||
|
INDENT_TITLE("ASTExcData", data);
|
||||||
|
INDENT_FIELD("msg", "\"%s\"", *data);
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**)) {
|
||||||
|
return (ASTBIFData*)fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) {
|
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) {
|
||||||
|
23
src/builtin.c
Normal file
23
src/builtin.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include "include/builtin.h"
|
||||||
|
#include "include/ast.h"
|
||||||
|
#include "include/util.h"
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
AST* builtin_sum(size_t argc, AST** argv) {
|
||||||
|
log_dbg("Got here");
|
||||||
|
ASTNumData total = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < argc; i++) {
|
||||||
|
AST* arg = argv[i];
|
||||||
|
if (arg->type != AST_TYPE_NUM)
|
||||||
|
return ast_init(
|
||||||
|
AST_TYPE_EXC,
|
||||||
|
ast_exc_data_init("Sum can't sum some non-num arguments.")
|
||||||
|
);
|
||||||
|
|
||||||
|
total += *(ASTNumData*)arg->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ast_init(AST_TYPE_NUM, ast_num_data_init(total));
|
||||||
|
}
|
60
src/exec.c
60
src/exec.c
@ -3,6 +3,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "include/ast.h"
|
#include "include/ast.h"
|
||||||
|
#include "include/builtin.h"
|
||||||
#include "include/exec.h"
|
#include "include/exec.h"
|
||||||
#include "include/htab.h"
|
#include "include/htab.h"
|
||||||
#include "include/stack.h"
|
#include "include/stack.h"
|
||||||
@ -10,11 +11,17 @@
|
|||||||
|
|
||||||
extern AST* root;
|
extern AST* root;
|
||||||
|
|
||||||
|
AST* exec_find(char* name);
|
||||||
|
|
||||||
AST* exec_start(AST* ast) {
|
AST* exec_start(AST* ast) {
|
||||||
scope = stack_init();
|
scope = stack_init();
|
||||||
|
|
||||||
HTab* global = htab_init();
|
HTab* global = htab_init();
|
||||||
|
|
||||||
|
htab_ins(
|
||||||
|
global, "sum", ast_init(AST_TYPE_BIF, ast_bif_data_init(builtin_sum))
|
||||||
|
);
|
||||||
|
|
||||||
// Push global namespace to `scope`.
|
// Push global namespace to `scope`.
|
||||||
stack_push(scope, global);
|
stack_push(scope, global);
|
||||||
|
|
||||||
@ -51,48 +58,20 @@ AST* exec_block(AST* ast) {
|
|||||||
|
|
||||||
AST* exec_call(AST* ast) {
|
AST* exec_call(AST* ast) {
|
||||||
log_dbg("Started call execution.");
|
log_dbg("Started call execution.");
|
||||||
fflush(stdout);
|
ASTCallData* data = (ASTCallData*)ast->data;
|
||||||
ASTCallData* calldata = (ASTCallData*)ast->data;
|
size_t argc = data->argc;
|
||||||
if (calldata->argc >= 1) {
|
AST** argv = data->argv;
|
||||||
if (!strcmp(calldata->to, "sum")) {
|
char* fname = data->to;
|
||||||
AST* total = ast_init(AST_TYPE_NUM, ast_num_data_init(0));
|
|
||||||
|
|
||||||
for (size_t i = 0; i < calldata->argc; i++) {
|
AST* fdef = exec_find(fname);
|
||||||
AST* arg = exec_exp(calldata->argv[i]);
|
|
||||||
|
|
||||||
if (arg->type != AST_TYPE_NUM) {
|
switch (fdef->type) {
|
||||||
return ast_init(
|
case AST_TYPE_BIF:
|
||||||
AST_TYPE_EXC, ast_exc_data_init("Wrong type, fool.")
|
ASTBIFData* bifdata = fdef->data;
|
||||||
);
|
return (*bifdata)(argc, argv);
|
||||||
}
|
default: return ast_init(AST_TYPE_EXC, ast_exc_data_init("Good job"));
|
||||||
|
|
||||||
*((ASTNumData*) total->data) += *((ASTNumData*)arg->data);
|
|
||||||
}
|
|
||||||
|
|
||||||
return total;
|
|
||||||
} /*else if (!strcmp(calldata->to, "sub")) {
|
|
||||||
double total = exec_exp(calldata->argv[0]);
|
|
||||||
|
|
||||||
for (size_t i = 1; i < calldata->argc;
|
|
||||||
total -= exec_exp(calldata->argv[i++]));
|
|
||||||
|
|
||||||
return total;
|
|
||||||
} else if (!strcmp(calldata->to, "mul")) {
|
|
||||||
double total = exec_exp(calldata->argv[0]);
|
|
||||||
|
|
||||||
for (size_t i = 1; i < calldata->argc;
|
|
||||||
total *= exec_exp(calldata->argv[i++]));
|
|
||||||
|
|
||||||
return total;
|
|
||||||
} else if (!strcmp(calldata->to, "div")) {
|
|
||||||
double total = exec_exp(calldata->argv[0]);
|
|
||||||
|
|
||||||
for (size_t i = 1; i < calldata->argc;
|
|
||||||
total /= exec_exp(calldata->argv[i++]));
|
|
||||||
|
|
||||||
return total;
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ast_init(AST_TYPE_EXC, ast_exc_data_init("No such function found."));
|
return ast_init(AST_TYPE_EXC, ast_exc_data_init("No such function found."));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,8 +84,7 @@ AST* exec_find(char* name) {
|
|||||||
if (val != NULL) return val;
|
if (val != NULL) return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
log_dbgf("Could not find var %s", name);
|
return ast_init(AST_TYPE_EXC, ast_exc_data_init("Couln't find term."));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_vdef(AST* ast) {
|
AST* exec_vdef(AST* ast) {
|
||||||
|
@ -16,6 +16,7 @@ typedef enum {
|
|||||||
AST_TYPE_LIST, // A list (variable size, variable type).
|
AST_TYPE_LIST, // A list (variable size, variable type).
|
||||||
|
|
||||||
// Misc. types.
|
// Misc. types.
|
||||||
|
AST_TYPE_BIF, // Built-in function.
|
||||||
AST_TYPE_CALL, // A function call.
|
AST_TYPE_CALL, // A function call.
|
||||||
AST_TYPE_VDEF, // A variable definition.
|
AST_TYPE_VDEF, // A variable definition.
|
||||||
AST_TYPE_VREF, // A variable reference.
|
AST_TYPE_VREF, // A variable reference.
|
||||||
@ -43,6 +44,15 @@ void ast_num_print(ASTNumData*, int i);
|
|||||||
typedef char* ASTExcData;
|
typedef char* ASTExcData;
|
||||||
ASTExcData* ast_exc_data_init(char* msg);
|
ASTExcData* ast_exc_data_init(char* msg);
|
||||||
void ast_exc_data_destroy(ASTExcData* exc);
|
void ast_exc_data_destroy(ASTExcData* exc);
|
||||||
|
void ast_exc_print(ASTExcData*, int i);
|
||||||
|
|
||||||
|
// A built-in function.
|
||||||
|
typedef AST* (*ASTBIFData)(size_t argc, AST** argv);
|
||||||
|
|
||||||
|
// Create a built-in function.
|
||||||
|
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**));
|
||||||
|
|
||||||
|
// There is no `ASTBIFData` destroy function, as function pointers are immortal.
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* to; // What the call's to.
|
char* to; // What the call's to.
|
||||||
|
12
src/include/builtin.h
Normal file
12
src/include/builtin.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#ifndef BUILTIN_H
|
||||||
|
#define BUILTIN_H
|
||||||
|
|
||||||
|
#include "ast.h"
|
||||||
|
|
||||||
|
// Sum some nums.
|
||||||
|
AST* builtin_sum(size_t argc, AST** argv);
|
||||||
|
|
||||||
|
// The list of built-in functions.
|
||||||
|
static AST* (*builtin_fns[])(size_t argc, AST** argv) = {builtin_sum};
|
||||||
|
|
||||||
|
#endif
|
Loading…
x
Reference in New Issue
Block a user