From 8763fa35dde6393e281dfcecfbdaa2646da7dff2 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 14 Dec 2024 20:35:36 -0500 Subject: [PATCH] Updated README.md --- .clang-format | 3 ++- README.md | 17 ++++++++++++----- src/ast.c | 17 +++++++++-------- src/dstr.c | 12 ++++++++---- src/exec.c | 10 +++++----- src/include/lexer.h | 14 +++++++------- src/lexer.c | 13 +++++++------ src/main.c | 19 ++++++++----------- src/stack.c | 2 +- 9 files changed, 59 insertions(+), 48 deletions(-) diff --git a/.clang-format b/.clang-format index fbd9e79..07403e6 100644 --- a/.clang-format +++ b/.clang-format @@ -1,12 +1,13 @@ --- -BasedOnStyle: LLVM AlignConsecutiveShortCaseStatements: Enabled: true AcrossEmptyLines: true AcrossComments: true +IndentCaseLabels: true AllowShortBlocksOnASingleLine: Always AllowShortCaseLabelsOnASingleLine: true AllowShortIfStatementsOnASingleLine: AllIfsAndElse AllowShortLoopsOnASingleLine: true IndentWidth: 4 PointerAlignment: Left +AlignAfterOpenBracket: BlockIndent diff --git a/README.md b/README.md index 17517e4..17c7016 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# SCL: Simple Calculator Language +# SCL: Simple CAS Language ## Syntax @@ -12,16 +12,24 @@ As one would expect, you can evaluate simple infix expressions: You can also define your own functions: ```scl -> f(x) 2x +> f(x) = 2x > f(2) = 4 ``` +Symbolic algebra is done in the following manner: + +```scl +> f(x) = e^x +> diff(f, x:sym, 2) += e^x +``` + SCL will dynamically decide on types, but you can state them explicitly as well: ```scl -> f(x:int) 2x +> f(x:int) = 2x > f(2.2) ! f(x:int): x must be of type int. ``` @@ -32,6 +40,5 @@ Variables can be defined, with several attributes: > a = 1 // Interpret type automatically. > b:int = 1 // Must be int. > c:const:int = 1 // Constant: value can never change. -> d:lazy = (1 + 1) // Interpreter will wait as long as possible before - // evaluating. +> x:sym // Treated symbolicaly. ``` diff --git a/src/ast.c b/src/ast.c index 0bc0918..5cb85c0 100644 --- a/src/ast.c +++ b/src/ast.c @@ -24,9 +24,10 @@ void ast_destroy(AST* ast) { if (!ast) return; switch (ast->type) { - case AST_TYPE_NUM: ast_num_data_destroy(ast->data); break; - case AST_TYPE_CALL: ast_call_data_destroy(ast->data); break; - default: log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX); + case AST_TYPE_NUM: ast_num_data_destroy(ast->data); break; + case AST_TYPE_CALL: ast_call_data_destroy(ast->data); break; + default: + log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX); } } @@ -39,11 +40,11 @@ void ast_print_i(AST* ast, int i) { INDENT_FIELD("type", "%s", asttype_names[ast->type]); INDENT_FIELD_EXT_NONL_START("data"); switch (ast->type) { - case AST_TYPE_NUM: - printf("%s %lf\n", INDENT_spacing->buf, *(ASTNumData*)ast->data); - break; - case AST_TYPE_CALL: ast_call_print(ast->data, i + 2); break; - default: exit(1); + case AST_TYPE_NUM: + printf("%s %lf\n", INDENT_spacing->buf, *(ASTNumData*)ast->data); + break; + case AST_TYPE_CALL: ast_call_print(ast->data, i + 2); break; + default: exit(1); } INDENT_FIELD_NONL_END; INDENT_END; diff --git a/src/dstr.c b/src/dstr.c index 0f64570..29dafd9 100644 --- a/src/dstr.c +++ b/src/dstr.c @@ -25,8 +25,10 @@ void dstr_append(Dstr* dest, char* src, size_t ln) { // Double the buffer size when overflown. dest->bufsz *= 2; dest->buf = realloc(dest->buf, dest->bufsz); - log_dbgf("dstr @ %p doubled from %ld to %ld", dest, dest->bufsz / 2, - dest->bufsz); + log_dbgf( + "dstr @ %p doubled from %ld to %ld", dest, dest->bufsz / 2, + dest->bufsz + ); } // Overwrites the \0 at the end of the string, keeps the null from the given @@ -40,8 +42,10 @@ void dstr_appendch(Dstr* dest, char ch) { // Double the buffer size when overflown. dest->bufsz *= 2; dest->buf = realloc(dest->buf, dest->bufsz); - log_dbgf("dstr @ %p doubled from %ld to %ld", dest, dest->bufsz / 2, - dest->bufsz); + log_dbgf( + "dstr @ %p doubled from %ld to %ld", dest, dest->bufsz / 2, + dest->bufsz + ); } // Overwrites the preexisting null terminator, and adds one of its own. diff --git a/src/exec.c b/src/exec.c index fc34b6f..57928ca 100644 --- a/src/exec.c +++ b/src/exec.c @@ -11,11 +11,11 @@ ASTNumData exec_expr(AST* ast) { ast_print(ast); log_dbg("Started execution."); switch (ast->type) { - case AST_TYPE_CALL: return exec_call(ast); - case AST_TYPE_NUM: - exec_print(*(ASTNumData*)ast->data); - return *(ASTNumData*)ast->data; - default: printf("what\n"); + case AST_TYPE_CALL: return exec_call(ast); + case AST_TYPE_NUM: + exec_print(*(ASTNumData*)ast->data); + return *(ASTNumData*)ast->data; + default: printf("what\n"); } } diff --git a/src/include/lexer.h b/src/include/lexer.h index e040ea2..d3adfb9 100644 --- a/src/include/lexer.h +++ b/src/include/lexer.h @@ -4,14 +4,14 @@ #include #ifdef __has_include - #if __has_include("../../build/grammars/grammar.tab.h") - #include "../../build/grammars/grammar.tab.h" - #else - #warn "Build resources not present!" - #endif +#if __has_include("../../build/grammars/grammar.tab.h") +#include "../../build/grammars/grammar.tab.h" #else - #warn "Not sure whether build-time resources are present." - #include "../../build/grammars/grammar.tab.h" +#warn "Build resources not present!" +#endif +#else +#warn "Not sure whether build-time resources are present." +#include "../../build/grammars/grammar.tab.h" #endif extern YYSTYPE yylval; diff --git a/src/lexer.c b/src/lexer.c index 8b579d1..73ead29 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -1,7 +1,7 @@ #include #include -#include #include +#include #include "include/lexer.h" @@ -24,13 +24,14 @@ double acc_float(int c) { inp++; } - if (*inp == '.') { + if (*inp == '.') { inp++; while (isdigit(*inp)) { // TODO: // Accumulate as int, divide once at end. - // value = value + (((double)(*inp - '0'))/pow(10.0l, (double)(inp-oinp))); // Accumulate value. + // value = value + (((double)(*inp - '0'))/pow(10.0l, + // (double)(inp-oinp))); // Accumulate value. value = value * 10 + (*inp - '0'); // Accumulate value. dplaces++; inp++; @@ -62,9 +63,9 @@ int yylex() { } switch (c) { - case '+': return PLUS; - case '\n': return NL; - default: return CALL; + case '+': return PLUS; + case '\n': return NL; + default: return CALL; } fprintf(stderr, "Unexpected character: %c\n", c); diff --git a/src/main.c b/src/main.c index ecb0327..9e9fa97 100644 --- a/src/main.c +++ b/src/main.c @@ -2,9 +2,9 @@ #include "include/ast.h" #include "include/dstr.h" +#include "include/exec.h" #include "include/lexer.h" #include "include/util.h" -#include "include/exec.h" #include "../build/grammars/grammar.tab.h" @@ -29,10 +29,9 @@ int main(int argc, char** argv) { do { c = getc(stdin); switch (c) { - case EOF: dstr_destroy(ln); goto lnskip; - case '\n': goto lnend; - - default: dstr_appendch(ln, c); log_dbgf("cchar: %c", c); + case EOF: dstr_destroy(ln); goto lnskip; + case '\n': goto lnend; + default: dstr_appendch(ln, c); log_dbgf("cchar: %c", c); } } while (1); @@ -41,14 +40,12 @@ int main(int argc, char** argv) { log_dbgf("cline: %s", ln->buf); if (ln->ln > 0) { - // I hope it's null-terminated. + // I hope to god it's null-terminated. inp = ln->buf; - if (yyparse() == 0) - printf("Parsed successfully!\n"); - else - printf("Parse error.\n"); + if (yyparse() == 0) printf("Parsed successfully!\n"); + else printf("Parse error.\n"); - //exec_expr(root); + // exec_expr(root); ast_print(root); } diff --git a/src/stack.c b/src/stack.c index b84f6de..66090a4 100644 --- a/src/stack.c +++ b/src/stack.c @@ -2,8 +2,8 @@ #include #include -#include "include/util.h" #include "include/stack.h" +#include "include/util.h" Stack* stack_init() { talloc(Stack, stack);