From 1ac96b0d603bf87be1025f6cc9ebcd2ac4df02ef Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Tue, 18 Mar 2025 13:52:14 +0100 Subject: [PATCH] Progress --- examples/1p1.flsp | 6 --- examples/hello_world.flsp | 2 +- examples/tostr.flsp | 6 +++ src/byte_defs.h | 3 ++ src/config.h | 2 +- src/exec_common.c | 103 ++++++++++++++++++++++++++++++++++---- src/exec_common.h | 4 ++ src/main.c | 66 ++++++++++++++++++++++++ src/parse_utils.c | 6 ++- src/parser.h | 3 -- src/parser.l | 1 - src/parser.y | 36 +------------ src/tests.c | 3 +- 13 files changed, 183 insertions(+), 58 deletions(-) delete mode 100644 examples/1p1.flsp create mode 100644 examples/tostr.flsp create mode 100644 src/main.c diff --git a/examples/1p1.flsp b/examples/1p1.flsp deleted file mode 100644 index 568f4e7..0000000 --- a/examples/1p1.flsp +++ /dev/null @@ -1,6 +0,0 @@ -(var a) -(var b) -(a 1) -(b 1) -(write (+ a b)) -(newline) diff --git a/examples/hello_world.flsp b/examples/hello_world.flsp index 467e9f1..4a71bd8 100755 --- a/examples/hello_world.flsp +++ b/examples/hello_world.flsp @@ -1,2 +1,2 @@ -(import console) (write "Hello world") +(newline) diff --git a/examples/tostr.flsp b/examples/tostr.flsp new file mode 100644 index 0000000..1685e20 --- /dev/null +++ b/examples/tostr.flsp @@ -0,0 +1,6 @@ +(var:str a "1234") +(var:int b (:int a)) +(write a) +(newline) +(write b) +(newline) diff --git a/src/byte_defs.h b/src/byte_defs.h index 91f07a1..4462b2d 100755 --- a/src/byte_defs.h +++ b/src/byte_defs.h @@ -160,3 +160,6 @@ static inline FnSig *get_fnsig(Value *x){ rval &= 0xFFFFFFFFFFFF; // 48bits - should be (mostly) fine return (void*)rval; } + +extern Tag ntag; +extern Tag atag; diff --git a/src/config.h b/src/config.h index 02bb349..bf05a9d 100755 --- a/src/config.h +++ b/src/config.h @@ -28,7 +28,7 @@ // Debug logs // 0 : None // 1 : All misc debug logs -#define LOG 0 +#define LOG 1 // TODO : Automate this #define INTERPR diff --git a/src/exec_common.c b/src/exec_common.c index 69a4329..88a18e4 100644 --- a/src/exec_common.c +++ b/src/exec_common.c @@ -252,6 +252,37 @@ void print_ast(Statement *base){ #endif } +Value str_to_val(char *str, i32 len){ + if(len < 0) + len = strlen(str); + Value retv; + retv.tag = (Tag){.is_array = 0, .type = T_str}; +#if LOG + printf("str (%d) : %s\n", len, str); +#endif + if(len > 4){ + u32 nstr = flisp_alloc(len); + if(!nstr){ + retv.tag = ntag; + return retv; + } + retv.vstr.pos = nstr; + memcpy(get_addr(nstr),str,len); + } + else { + for(i32 i = 0; i < len; i++) + retv.vstr.str[i] = str[i]; + } + retv.vstr.len = len; + return retv; +} + +void clean_strval(Value str){ + if(str.vstr.len < 5) + return; + flisp_free(str.vstr.pos); +} + // Lesser special functions Value is(Tag tag, Value b){ @@ -298,28 +329,80 @@ Value flt_to_fix(Value x){ return x; } +char tmpbuf[256]; + Value int_to_str(Value x){ - runtime_err("TODO : int_to_str"); + i32 len = snprintf(tmpbuf, 256, "%d", x.v4B.value); + len = len > 256 ? 256:len; + Value retv = str_to_val(tmpbuf, len); + return retv; } Value fix_to_str(Value x){ - runtime_err("TODO : fix_to_str"); + i32 len = snprintf(tmpbuf, 256, "%f", f2float(x.v4B.value)); + len = len > 256 ? 256:len; + Value retv = str_to_val(tmpbuf, len); + return retv; } Value flt_to_str(Value x){ - runtime_err("TODO : flt_to_str"); + i32 len = snprintf(tmpbuf, 256, "%f", x.v4B.vfl); + len = len > 256 ? 256:len; + Value retv = str_to_val(tmpbuf, len); + return retv; +} + +Value vstr_to(Value x, i32 type){ + char *str; + Value retv; + if(x.vstr.len > 4) + str = get_addr(x.vstr.pos); + else + str = x.vstr.str; + char *nstr = malloc(x.vstr.len+1); + if(!nstr){ + retv.tag = ntag; + clean_strval(x); + return retv; + } + memcpy(nstr, str, x.vstr.len); + clean_strval(x); + nstr[x.vstr.len] = '\0'; + i32 ret = 0; + switch(type){ + case T_int: + ret = sscanf(nstr, "%d", &retv.v4B.value); + break; + case T_fix: + ret = sscanf(nstr, "%f", &retv.v4B.vfl); + x.v4B.value = fix(x.v4B.vfl); + break; + case T_float: + ret = sscanf(nstr, "%f", &retv.v4B.vfl); + break; + default: + break; + } + free(nstr); + if(!ret){ + retv.tag = ntag; + return retv; + } + retv.tag = (Tag){.is_array = 0, .type = type}; + + return retv; } Value str_to_int(Value x){ - + return vstr_to(x,T_int); } Value str_to_fix(Value x){ - + return vstr_to(x,T_fix); } Value str_to_flt(Value x){ - + return vstr_to(x,T_float); } // [from][to] @@ -518,6 +601,7 @@ Value pop(Value a, Value b){ Value write(Value a){ switch(a.tag.type){ case T_null: + case T_any: printf("null"); break; case T_int: @@ -530,15 +614,16 @@ Value write(Value a){ printf("%f",a.v4B.vfl); break; case T_str: - // TODO replace strdup char *nstr; + nstr = malloc(a.vstr.len+1); if(a.vstr.len > 4){ char *str = get_addr(a.vstr.pos); - nstr = strndup(str, a.vstr.len); + memcpy(nstr, str, a.vstr.len); } else{ - nstr = strndup(a.vstr.str, 4); + memcpy(nstr, a.vstr.str, a.vstr.len); } + nstr[a.vstr.len] = '\0'; printf("%s",nstr); free(nstr); break; diff --git a/src/exec_common.h b/src/exec_common.h index f3b35bb..c416280 100644 --- a/src/exec_common.h +++ b/src/exec_common.h @@ -46,4 +46,8 @@ Value is(Tag tag, Value b); Value cast(Tag tag, Value b); +// Makes a copy of the string +// If len < 0, will copy up to a '\0' +Value str_to_val(char *str, i32 len); + void symbol_map_add_bi(HashMap *map); diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..58e0dec --- /dev/null +++ b/src/main.c @@ -0,0 +1,66 @@ +/* +* The fLisp parser and interpreter +* Copyright (C) 2025 Fcalva +* +* This program 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 2 +* of the License, or (at your option) any later version. +* +* This program 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 this program; if not, see +* . +*/ + +#include +#include +#include + +#include "types.h" +#include "tests.h" +#include "code_defs.h" +#include "parser.h" +#include "interpreter.h" +#include "exec_common.h" + +#include "y.tab.h" + +extern FILE *yyin; + +int main(int argc, char *argv[]){ + i32 fpos = 1; + + if(argc > 1){ + if(!strcmp(argv[1], "--tests")){ + fpos++; + do_tests(); + } + } + if(argc > fpos) { + yyin = fopen(argv[fpos], "r"); + if(!yyin){ + fprintf(stderr,"Failed to open file \"%s\"", argv[1]); + exit(1); + } + } + else { + printf("fLisp interactive env - v0.1\nFcalva 2025\n"); + printf("Under the terms of the GPL v3.0 license\n"); + yyin = stdin; + } + + alloc_init(); + + make_stack(); + yyparse(); + free_stack(); + + fclose(yyin); + + return 0; +} diff --git a/src/parse_utils.c b/src/parse_utils.c index eaa1ecb..5a9fd34 100644 --- a/src/parse_utils.c +++ b/src/parse_utils.c @@ -92,7 +92,11 @@ Statement *make_fconst(float val){ } Statement *make_strconst(char *str){ - + Statement *stat = &stack->statements[stack->curr_statement++]; + stat->type = ST_Const; + i32 len = strcspn(str, "\""); + stat->cons = str_to_val(str, len); + return stat; } Statement *declare(i32 type, Tag vtype, char *name, Statement *assign){ diff --git a/src/parser.h b/src/parser.h index 5fe1da4..bd9beb8 100644 --- a/src/parser.h +++ b/src/parser.h @@ -48,6 +48,3 @@ Statement *add_block(Statement *block, Statement *add); Statement *make_operation(i32 type, Tag vtype, char *str, i32 nparam, ...); void set_entry_point(Statement *statement); - -extern Tag ntag; -extern Tag atag; diff --git a/src/parser.l b/src/parser.l index 71ad0d6..a31d251 100644 --- a/src/parser.l +++ b/src/parser.l @@ -48,7 +48,6 @@ \"[^"\n]*["\n] { yylval.st = make_strconst(yytext+1); - printf("STRCST : %s\n",yytext); return CST; } diff --git a/src/parser.y b/src/parser.y index 1d0f4cd..25be0ac 100644 --- a/src/parser.y +++ b/src/parser.y @@ -21,6 +21,7 @@ #include #include #include + #include "types.h" #include "tests.h" #include "code_defs.h" @@ -144,38 +145,3 @@ void yyerror(char *s){ fprintf(stderr, "Error : %s\n", s); exit(1); } - -extern FILE *yyin; - -int main(int argc, char *argv[]){ - i32 fpos = 1; - - if(argc > 1){ - if(!strcmp(argv[1], "--tests")){ - fpos++; - do_tests(); - } - } - if(argc > fpos) { - yyin = fopen(argv[fpos], "r"); - if(!yyin){ - fprintf(stderr,"Failed to open file \"%s\"", argv[1]); - exit(1); - } - } - else { - printf("fLisp interactive env - v0.1\nFcalva 2025\n"); - printf("Under the terms of the GPL v3.0 license\n"); - yyin = stdin; - } - - alloc_init(); - - make_stack(); - yyparse(); - free_stack(); - - fclose(yyin); - - return 0; -} diff --git a/src/tests.c b/src/tests.c index a72b484..2b68c42 100644 --- a/src/tests.c +++ b/src/tests.c @@ -93,7 +93,7 @@ int t_flisp_alloc(){ int t_flisp_realloc(){ int err = 0; - u8 ref[10] = {1,2,3,4,5,6,7,8,9,10}; + u8 ref[5] = {1,2,3,4,5}; u32 pos0 = flisp_alloc(5); if(!pos0) return 1; @@ -104,6 +104,7 @@ int t_flisp_realloc(){ return 1; ptr0 = get_addr(pos0); err += memcmp(ptr0, ref, 5); + flisp_free(pos0); return err; }