From 4b68d60daf495cef5eb22a521ce809d798418aa5 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Wed, 15 Feb 2023 23:57:36 -0800 Subject: add license, some boolean functions, fixed bugs, improved visualizer and added flag --- LICENSE | 2 ++ doc/boolean.pcdt | 14 ++++++++++++++ eval.c | 24 +++++++++++++++++------- eval.h | 2 +- parse.c | 20 ++++++++++++++++++-- precedent.c | 37 ++++++++++++++++++++++++------------- 6 files changed, 76 insertions(+), 23 deletions(-) create mode 100644 LICENSE create mode 100644 doc/boolean.pcdt diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d2aaaae --- /dev/null +++ b/LICENSE @@ -0,0 +1,2 @@ +Copyright 2023 Kai Stevenson +All rights reserved diff --git a/doc/boolean.pcdt b/doc/boolean.pcdt new file mode 100644 index 0000000..81b1f62 --- /dev/null +++ b/doc/boolean.pcdt @@ -0,0 +1,14 @@ +store(1; xv;) +store(1; yv;) + +store( +eq(xv() yv()) +res;) + +if(res() +print(values equal;) +) + +if(not(res()) +print(values not;) +) diff --git a/eval.c b/eval.c index 3cbe987..05fd9b3 100644 --- a/eval.c +++ b/eval.c @@ -17,10 +17,10 @@ void init_eval() { } /* TODO: read base hashtable from file to allow modularity pair value value? */ -void exec_all(symset_t symset) { - for (int i = 0; i < symset.count; i++) { +void exec_all(sym_t** symbols, int count) { + for (int i = 0; i < count; i++) { /* dispose of result */ - eval(symset.symbols[i]); + eval(symbols[i]); } } sym_t* eval(sym_t* symbol) { @@ -58,10 +58,7 @@ sym_t* try_eval_internal(sym_t* symbol) { /* TODO: return last arg. that's the function return line */ if (hash == get_hash("root")) { /* execute all args as roots */ - symset_t symset; - symset.symbols = symbol->args; - symset.count = symbol->argCount; - exec_all(symset); + exec_all(symbol->args, symbol->argCount); return &trueSym; } else if (hash == get_hash("store")) { @@ -101,6 +98,19 @@ sym_t* try_eval_internal(sym_t* symbol) { } return &trueSym; } + else if (hash == get_hash("if")) { + sym_t* v = eval(symbol->args[0]); + if (symtoi(v) != 0) { + exec_all(symbol->args + 1, symbol->argCount - 1); + return &trueSym; + } + return &falseSym; + } + else if (hash == get_hash("not")) { + sym_t* v = eval(symbol->args[0]); + int b = symtoi(v); + return b ? &falseSym : &trueSym; + } else if (hash == get_hash("print")) { sym_t* v = eval(symbol->args[0]); switch (v->litType) { diff --git a/eval.h b/eval.h index 662774a..b91c621 100644 --- a/eval.h +++ b/eval.h @@ -3,7 +3,7 @@ #define EVAL void init_eval(void); void throw(char* error, int symbolLocale); -void exec_all(symset_t symset); +void exec_all(sym_t** symbols, int count); sym_t* eval(sym_t* symbol); sym_t* try_eval_internal(sym_t* symbol); #endif diff --git a/parse.c b/parse.c index c174c23..469b492 100644 --- a/parse.c +++ b/parse.c @@ -63,7 +63,7 @@ symset_t* parse_symbols(char** str) { /* TODO: float */ /* string */ if (isalpha(symbolName[0])) { - //symbolName = realloc(symbolName, i * sizeof(char)); + symbolName = realloc(symbolName, i * sizeof(char)); symbol->name = symbolName; symbol->litType = STRING; } @@ -120,7 +120,23 @@ void write_symbol_visual(sym_t* symbol, int offset) { for (int o = 0; o < offset; o++) { printf(" "); } - printf("%s (%s)\n", (char*)(symbol->name), symbol->province == KEY_REF ? "KEY" : "LITERAL"); + if (symbol->province == KEY_REF) { + printf("%s (%s)\n", (char*)(symbol->name), "KEY"); + } + else if (symbol->province == LITERAL) { + if (symbol->litType == INT) { + printf("%d (%s)\n", *(int*)(symbol->name), "LITERAL"); + } + else if (symbol->litType == FLOAT) { + printf("%f (%s)\n", *(float*)(symbol->name), "LITERAL"); + } + else if (symbol->litType == STRING) { + printf("%s (%s)\n", (char*)(symbol->name), "LITERAL"); + } + } + else { + printf("symbol parse error\n"); + } for (int i = 0; i < symbol->argCount; i++) { write_symbol_visual(symbol->args[i], offset + 4); } diff --git a/precedent.c b/precedent.c index 38fc617..b609fff 100644 --- a/precedent.c +++ b/precedent.c @@ -7,23 +7,34 @@ #include "eval.h" int main(int argc, char** argv) { + int visual = 0; if (argc > 1) { - if (strcmp("--version", argv[1]) == 0) { - printf("precedent, version 0.1\n"); - printf("Copyright 2023 Kai Stevenson\n"); - printf("This software was licensed under the MIT license;\n"); - printf("for more information, see the license file.\n"); - return 0; - } - else if (strcmp("--help", argv[1]) == 0) { - printf("usage: precedent [--version] [--help] \n"); - return 0; + for (int i = 1; i < argc; i++) { + if (strcmp("--version", argv[i]) == 0) { + printf("precedent, version 0.1\n"); + printf("Copyright 2023 Kai Stevenson\n"); + printf("All rights reserved. No part of the software may be sold\nor used in derivative works without explicit consent\n"); + return 0; + } + else if (strcmp("--help", argv[i]) == 0) { + printf("usage: precedent [--visualize] [--version] [--help]\n"); + return 0; + } + else if (strcmp("--visualize", argv[i]) == 0) { + visual = 1; + } } } + else { + printf("usage: precedent [--visualize] [--version] [--help]\n"); + return 0; + } sym_t* finalSymbol = parse_symbol_final(read_file(argv[1])); - printf("drawing program:\n"); - write_symbol_visual(finalSymbol, 0); - printf("\nexecuting program:\n"); + if (visual) { + printf("--BEGIN VISUALIZATION--\n"); + write_symbol_visual(finalSymbol, 0); + printf("--END VISUALIZATION--\n"); + } init_eval(); eval(finalSymbol); return 0; -- cgit v1.3.1