From 416119944d8e04708ca7cb19e48d6cd73aa854ee Mon Sep 17 00:00:00 2001 From: koder77 Date: Thu, 1 Feb 2024 21:47:18 +0100 Subject: [PATCH] Compiler infix math rewrite --- ChangeLog | 12 +++++++++ comp/main.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++- comp/main.h | 3 +++ include/global.h | 2 +- 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index cf7d1b2d..3d68c03a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +L1VM (3.0.0) + NEW: now math expressions can be written like this in Brackets: + + {i = xd + yd} + + The () brackets are not longer needed for this, to work. + The old way was: + + {i = (xd + yd)} + +-- Stefan Pietzonke Thur 1 Feb 2024 21:18 +0100 + L1VM (2.9.0) Added "return" to compiler. See "prog/return.l1com". diff --git a/comp/main.c b/comp/main.c index 23a52557..50f19d63 100644 --- a/comp/main.c +++ b/comp/main.c @@ -596,6 +596,74 @@ S2 check_for_normal_brackets (U1 *line) } } +S2 check_for_infix_math (U1 *line) +{ + U1 buf[MAXSTRLEN]; + S4 buf_ind = 0; + S4 ind; + S4 start = 0; + S4 line_len; + U1 found_var = 0; + + if (check_for_normal_brackets (line) == 0) + { + return (0); + } + + line_len = strlen_safe ((const char *) line, MAXSTRLEN); + for (ind = 0; ind < line_len; ind++) + { + if (line[ind] == '=') + { + start = ind + 2; + break; + } + } + if (start == 0) + { + // error no equal sign found + return (2); + } + + for (ind = start; ind < line_len; ind++) + { + if (line[ind] != ' ' && line[ind] != '}') + { + //printf ("check_for_infix_math: char: %c\n", line[ind]); + + if (isOperator (line[ind]) == 1) + { + //printf ("check_for_infix_math: found operator: %c\n", line[ind]); + //printf ("check_for_infix_math: found_var: %i\n", found_var); + + if (found_var == 1) + { + // infix math found + //printf ("check_for_infix_math: found infix expression!\n"); + + return (0); + } + found_var = 0; + } + else + { + buf[buf_ind] = line[ind]; + buf_ind++; + } + } + else + { + buf[buf_ind] = '\0'; + buf_ind = 0; + + //printf ("check_for_infix_math: found var: %s\n", buf); + + found_var++; + } + } + return (1); +} + S2 parse_line (U1 *line) { S4 level, j, last_arg, last_arg_2, t, v, reg, reg2, reg3, reg4, target, e, exp; @@ -675,7 +743,7 @@ S2 parse_line (U1 *line) { // printf ("DEBUG parse_cont: '%s'\n", line); - normal_brackets = check_for_normal_brackets (line); + normal_brackets = check_for_infix_math (line); if (normal_brackets == 2) { // ERROR diff --git a/comp/main.h b/comp/main.h index 07dcd518..18821d0f 100644 --- a/comp/main.h +++ b/comp/main.h @@ -76,4 +76,7 @@ void init_data_info_var (void); S2 get_unused_var (void); +// parse-rpolish.c +S2 isOperator (char symbol); + #endif diff --git a/include/global.h b/include/global.h index 0fcfbead..b6a30ff2 100644 --- a/include/global.h +++ b/include/global.h @@ -141,7 +141,7 @@ // info strings: #define COPYRIGHT_STR " 2024/winter (C) 2017-2024 Stefan Pietzonke - software research" -#define VM_VERSION_STR "2.9.0 " // version number +#define VM_VERSION_STR "3.0.0 " // version number #define MOTTO_STR "let it snow!" // no user defined definitions below this section! ============================