From dcfa3c1c9938e0eb0bab13eabee04f0a3fd4753b Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 30 Apr 2023 11:56:09 -0400 Subject: [PATCH] step: add unary operations --- mumble | Bin 102428 -> 102428 bytes src/mumble.c | 43 +++++++++++++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/mumble b/mumble index c6e6f0ad016e8064c46a3115c5a6fd5a3da95a24..32556f0a0a380354b5f9c6f3840d0ce7eeec5a14 100755 GIT binary patch delta 1336 zcmZ8hYiv_x82;Yg*4@&vvJMBsGo-DUk&X8B1`Iav&@-I_Vd0NACT;0rFH2j{22pV$ zo|GnMl+LnDKRJ#?zqB{RWJ>t>EfL;>Vzp0Br_@8c`LR*wQT#KjY2Xop2!+ z)(Um?@kES%(Qs@ik_hC~jUGK{a92wQsB_^KUvq1UTi^w5o=8w! zOzCtE&)Qp|jQ_Da;A`B@nvLdVm3I`Cr?8J@4Zl3TV!*ZA@EFU$L--oI-xQNPbi7l; zl}bfPI*B_ST@c3+hXq17mm6&lutqn|x|(z$H=y!q{K1c=*VEd?9!alskdm|ykW)%t*)%97FPZ{Zq7iC^ za>!JXQuh>%4Kg)Lsj@;DWQyJZH`6Ojh-cC(j8iD~3X?p3!Ck#`QVP8)ZHKOnPSz*f zly`N@1*kx88wuQWikp^SH??VjPn0&k!DH_AY+hOsR}e*|N>?b1i?^KCM&w9YxAY#B zlynZ^d+xQSb=0IW+vOO!>RBf?c{V{9cYAov-K9zekN(h{``A-E1xuMMz5F|wYtRmb z%->*zcS$Qy2og?&ZW0z@4atZwPc8%Wz~`hQT5l7pio)|`uL@p*KgcB&T!9Yqni^h( zFUWm03_}kIX(-H)TN=0zH^@ybJOKjPsDlAuNm7K{d8X185>=YoV>_&%@Hfxns=#e*&}%>VnJp`C7QUr|J4SDJ0vu zBIHZ**M8Unr}LHraK}uTBqY5x=J>$ik*$n5j_Bj=Xe8l}B;t~d`lAuQ$o3x%zvz!K z(ExKeI27{5{1NIOlHx_1dBp6?7n5)Tj5XY`!O+pc#8Bi2lL-2mo&y8VE;= CO~$SO delta 1412 zcmZ8gZERCz6uzhJ?YfTPSYacCg*L1mB+T`5T_M#myzCCgSb&geBv@IuHE!+Fmd$}v zOr7zj_Sc)`-)ar1awXi9t(gH!sGffkK1mJm z(yH>oa3C1%;Udv6`KQ#C9v!HWcS8ecQUe>ht8EG1*=piMX*NS+^Vhts(Zz0nreM&rT zwn;abBV7p}k}1d@+NHVtC?R4A+Gz+qP)RT(8IgRkNjeIh8&!F>!eP|x5jAQuA2ji8 zb?Tg_Y!>d1UnU>S_v4?#Hn=eU2h_nyTmwXOVSx%O9-u;p$ElddlK?y6ZCs?<5Ux`Z z(Si4=;6=EDud3h%Y{IXo;bl0B3u+hy8}8Q-k;Ok~pa9c&j)y%kf@`&K5Nh!V6<_18 zv=D+L_=yszQ@Q)_pbnbx`4VV>6#k_Io`XZURY&qXjzc<#Y2WMNxL?oU`RAbyZz=^V z?8Tj>(5d>%kI$CEKGh{ZntIoOwR))4g#8?cemqf=ep(Oh06acb2BvjC9cAC8Fl3}R zC!aH^f|%fg^kNx2*u8FYBXhZIUEy*O|Gpn=Fr2m=fV<|kdc82@>p$j;27wg@RFkFdC6{t|Jum6SU*lzzhDcG1Ox`+85~W4*P?| Z9$@}dH*Pj}>(gJ1z$l~-pMdLY{{b_H=s^Gg diff --git a/src/mumble.c b/src/mumble.c index ca9b1eb..adedf4a 100644 --- a/src/mumble.c +++ b/src/mumble.c @@ -7,6 +7,15 @@ #include "mpc/mpc.h" #define VERBOSE 1 +long evaluate_unary_operation(char* op, long x) +{ + if (!strcmp(op, "+")) { + return x; + } else if (!strcmp(op, "-")) { + return -x; + } + return 0; +} long evaluate_operation(char* op, long x, long y) { if (!strcmp(op, "+")) { @@ -17,8 +26,6 @@ long evaluate_operation(char* op, long x, long y) return x * y; } else if (!strcmp(op, "/")) { return x / y; - } else if (!strcmp(op, "^")) { - return 42; // to take care of later } return 0; } @@ -31,8 +38,6 @@ int is_ignorable(mpc_ast_t* t){ long evaluate_ast(mpc_ast_t* t) { // Base case #1: It's a number - if (VERBOSE) - printf("\n\nEvaluating the ast"); if (strstr(t->tag, "number")) { if (VERBOSE) printf("\nCase #1, %s", t->contents); @@ -46,17 +51,28 @@ long evaluate_ast(mpc_ast_t* t) printf("\nCase #2, %s", t->children[0]->contents); return atoi(t->children[0]->contents); } + // Base case #3: Top level parenthesis if (t->children_num == 2 && strstr(t->children[0]->tag, "expr|>") && !strcmp(t->children[1]->tag, "regex")) { if (VERBOSE) printf("\nCase #3, top level parenthesis"); return evaluate_ast(t->children[0]); } - - // Case #4: Unary operations case - // Case #5: Binary (or more) operations case + + // "Real" cases long x; char* operation; + + // Case #4: Unary operations case + if (t->children_num == 3 && is_ignorable(t->children[0]) && strstr(t->children[1]->tag, "operator")) { + operation = t->children[1]->contents; + if (VERBOSE) + printf("\nCase #4, unary operation %s", operation); + x = evaluate_ast(t->children[2]); + x = evaluate_unary_operation(operation, x); + } + + // Case #5: Binary (or more) operations case if (t->children_num > 3 && is_ignorable(t->children[0]) && strstr(t->children[1]->tag, "operator")) { operation = t->children[1]->contents; if (VERBOSE) @@ -80,13 +96,13 @@ void print_ast(mpc_ast_t* ast, int num_tabs) for(int i=0; itag); - printf("%sContents: %s\n", tabs, strcmp(ast->contents, "") ? ast->contents : "None"); - printf("%sNumber of children: %i\n", tabs, ast->children_num); + printf("\n%sTag: %s", tabs, ast->tag); + printf("\n%sContents: %s", tabs, strcmp(ast->contents, "") ? ast->contents : "None"); + printf("\n%sNumber of children: %i", tabs, ast->children_num); /* Print the children */ for (int i = 0; i < ast->children_num; i++) { mpc_ast_t* child_i = ast->children[i]; - printf("%sChild #%d\n", tabs, i); + printf("\n%sChild #%d", tabs, i); print_ast(child_i, 1); } } @@ -128,7 +144,10 @@ int main(int argc, char** argv) mpc_ast_t* ast = result.output; // Print AST if VERBOSE - print_ast(ast, 0); + if(VERBOSE) print_ast(ast, 0); + + // Evaluate the AST + if(VERBOSE) printf("\n\nEvaluating the AST"); long result = evaluate_ast(ast); printf("\nResult: %li\n", result); } else {