diff options
| author | Peter Mikkelsen <petermikkelsen10@gmail.com> | 2026-05-16 10:59:21 +0200 |
|---|---|---|
| committer | Peter Mikkelsen <petermikkelsen10@gmail.com> | 2026-05-16 10:59:21 +0200 |
| commit | d86f31325ca1343355f81918f0ad449f8788baa1 (patch) | |
| tree | 277f2a324bd0f6b3d85ba4df693c05722934badf | |
| parent | 3b0ed8d3296efd16e422e66168dab66399f60776 (diff) | |
Commit work in progress changes
| -rw-r--r-- | lib/Makefile.am | 3 | ||||
| -rw-r--r-- | lib/aplwc_internal.h | 25 | ||||
| -rw-r--r-- | lib/compile.c | 73 | ||||
| -rw-r--r-- | lib/decode_instr.c | 37 | ||||
| -rw-r--r-- | lib/encode_instr.c | 42 | ||||
| -rw-r--r-- | lib/eval.c | 13 | ||||
| -rw-r--r-- | lib/run_line.c | 1 |
7 files changed, 192 insertions, 2 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am index 9644940..664f9be 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -4,6 +4,9 @@ noinst_HEADERS = aplwc_internal.h libaplwc_la_SOURCES = \ autocomplete.c \ + compile.c \ + decode_instr.c \ + encode_instr.c \ eval.c \ exit.c \ free_ast.c \ diff --git a/lib/aplwc_internal.h b/lib/aplwc_internal.h index dd676fb..44785ed 100644 --- a/lib/aplwc_internal.h +++ b/lib/aplwc_internal.h @@ -22,6 +22,7 @@ #define APLWC_INTERNAL_H #include <stdarg.h> +#include <stdint.h> #include <stdbool.h> enum aplwc_token_tag { @@ -37,6 +38,12 @@ enum aplwc_ast_tag { APLWC_AST_ERROR, }; +enum aplwc_instr_tag { + APLWC_INSTR_SYSCMD_CALL, + APLWC_INSTR_ERROR, + APLWC_INSTR_END, +}; + struct aplwc { void *(*alloc)(size_t); void (*free)(void *); @@ -58,6 +65,8 @@ struct aplwc_eval_context { size_t token_offset; struct aplwc_ast *ast; + + struct aplwc_instrs *instrs; }; struct aplwc_token { @@ -80,15 +89,29 @@ struct aplwc_ast { } data; }; +struct aplwc_instrs { + size_t n_instrs; + uint64_t *instrs; +}; + +struct aplwc_instr { + int type; + enum aplwc_instr_tag op; + + uint64_t encoded; +}; struct aplwc_syscmd *aplwc_lookup_syscmd(struct aplwc *, const char *); struct aplwc_eval_context *aplwc_new_eval_context(struct aplwc *); void aplwc_scan_line(struct aplwc_eval_context *, const char *); void aplwc_parse(struct aplwc_eval_context *); +void aplwc_compile(struct aplwc_eval_context *); void aplwc_eval(struct aplwc_eval_context *); void aplwc_free_eval_context(struct aplwc_eval_context *); void aplwc_free_ast(struct aplwc *, struct aplwc_ast *); char *aplwc_strdup(struct aplwc *, const char *); void aplwc_output(struct aplwc *, const char *, ...); void aplwc_voutput(struct aplwc *, const char *, va_list); - +void aplwc_encode_instr(struct aplwc_instr *); +void aplwc_decode_instr(struct aplwc_instr *); + #endif /* APLWC_INTERNAL_H */ diff --git a/lib/compile.c b/lib/compile.c new file mode 100644 index 0000000..ce7b5db --- /dev/null +++ b/lib/compile.c @@ -0,0 +1,73 @@ +/* Aplwc - A Programming Language With Constraints + * + * Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com> + * + * This file is part of aplwc. + * + * Aplwc is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Aplwc is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with aplwc. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <string.h> +#include <stdio.h> +#include <aplwc.h> +#include "aplwc_internal.h" + +static void compile(struct aplwc_eval_context *, struct aplwc_ast *); +static void emit(struct aplwc_eval_context *, uint64_t instr); +static void emit_t0(struct aplwc_eval_context *, enum aplwc_instr_tag); +static uint64_t encode_t0(enum aplwc_instr_tag); + +void +aplwc_compile(struct aplwc_eval_context *context) +{ + compile(context, context->ast); +} + +static void +compile(struct aplwc_eval_context *context, struct aplwc_ast *node) +{ + context->instrs = context->aplwc->alloc(sizeof(*context->instrs)); + memset(context->instrs, 0, sizeof(*context->instrs)); + + switch(node->tag){ + case APLWC_AST_SYSCMD: + emit_t0(context, APLWC_INSTR_SYSCMD_CALL); + break; + case APLWC_AST_ERROR: + emit_t0(context, APLWC_INSTR_ERROR); + break; + } + + emit_t0(context, APLWC_INSTR_END); +} + +static void +emit(struct aplwc_eval_context *context, uint64_t instr) +{ + context->instrs->n_instrs++; + context->instrs->instrs = context->aplwc->realloc(context->instrs->instrs, sizeof(*context->instrs->instrs) * context->instrs->n_instrs); + + context->instrs->instrs[context->instrs->n_instrs-1] = instr; +} + +static void +emit_t0(struct aplwc_eval_context *context, enum aplwc_instr_tag op) +{ + struct aplwc_instr instr = { + .type = 0, + .op = op, + }; + aplwc_encode_instr(&instr); + emit(context, instr.encoded); +} diff --git a/lib/decode_instr.c b/lib/decode_instr.c new file mode 100644 index 0000000..e16133f --- /dev/null +++ b/lib/decode_instr.c @@ -0,0 +1,37 @@ +/* Aplwc - A Programming Language With Constraints + * + * Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com> + * + * This file is part of aplwc. + * + * Aplwc is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Aplwc is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with aplwc. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <aplwc.h> +#include "aplwc_internal.h" + +static void decode_t0(struct aplwc_instr *); + +void +aplwc_decode_instr(struct aplwc_instr *instr) +{ + if((instr->encoded & 1) == 0) + decode_t0(instr); +} + +static void +decode_t0(struct aplwc_instr *instr) +{ + instr->op = instr->encoded >> 1; +} diff --git a/lib/encode_instr.c b/lib/encode_instr.c new file mode 100644 index 0000000..d7e590e --- /dev/null +++ b/lib/encode_instr.c @@ -0,0 +1,42 @@ +/* Aplwc - A Programming Language With Constraints + * + * Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com> + * + * This file is part of aplwc. + * + * Aplwc is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Aplwc is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with aplwc. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <aplwc.h> +#include "aplwc_internal.h" + +static void encode_t0(struct aplwc_instr *); + +void +aplwc_encode_instr(struct aplwc_instr *instr) +{ + instr->encoded = 0; + + switch(instr->type){ + case 0: + encode_t0(instr); + break; + } +} + +static void +encode_t0(struct aplwc_instr *instr) +{ + instr->encoded |= (instr->op << 1); +} @@ -19,13 +19,23 @@ */ #include <stdio.h> +#include <inttypes.h> #include <aplwc.h> #include "aplwc_internal.h" void aplwc_eval(struct aplwc_eval_context *context) { - struct aplwc_syscmd *syscmd; + struct aplwc_instr instr; + for(size_t i = 0; i < context->instrs->n_instrs; i++){ + instr.encoded = context->instrs->instrs[i]; + aplwc_decode_instr(&instr); + + printf("INSTR: 0x%016" PRIX64 " type=%02x op=%04x\n", instr.encoded, instr.type, instr.op); + } + + +/* struct aplwc_syscmd *syscmd; switch(context->ast->tag){ case APLWC_AST_SYSCMD: @@ -35,4 +45,5 @@ aplwc_eval(struct aplwc_eval_context *context) case APLWC_AST_ERROR: printf("cannot evaluate since parsing failed\n"); } +*/ } diff --git a/lib/run_line.c b/lib/run_line.c index 22f6055..354ab07 100644 --- a/lib/run_line.c +++ b/lib/run_line.c @@ -29,6 +29,7 @@ aplwc_run_line(struct aplwc *aplwc, const char *line) aplwc_scan_line(context, line); aplwc_parse(context); + aplwc_compile(context); aplwc_eval(context); aplwc_free_eval_context(context); } |