Progress
This commit is contained in:
parent
a4b8d3f76f
commit
1ac96b0d60
13 changed files with 183 additions and 58 deletions
|
@ -1,6 +0,0 @@
|
||||||
(var a)
|
|
||||||
(var b)
|
|
||||||
(a 1)
|
|
||||||
(b 1)
|
|
||||||
(write (+ a b))
|
|
||||||
(newline)
|
|
|
@ -1,2 +1,2 @@
|
||||||
(import console)
|
|
||||||
(write "Hello world")
|
(write "Hello world")
|
||||||
|
(newline)
|
||||||
|
|
6
examples/tostr.flsp
Normal file
6
examples/tostr.flsp
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
(var:str a "1234")
|
||||||
|
(var:int b (:int a))
|
||||||
|
(write a)
|
||||||
|
(newline)
|
||||||
|
(write b)
|
||||||
|
(newline)
|
|
@ -160,3 +160,6 @@ static inline FnSig *get_fnsig(Value *x){
|
||||||
rval &= 0xFFFFFFFFFFFF; // 48bits - should be (mostly) fine
|
rval &= 0xFFFFFFFFFFFF; // 48bits - should be (mostly) fine
|
||||||
return (void*)rval;
|
return (void*)rval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern Tag ntag;
|
||||||
|
extern Tag atag;
|
||||||
|
|
|
@ -28,7 +28,7 @@
|
||||||
// Debug logs
|
// Debug logs
|
||||||
// 0 : None
|
// 0 : None
|
||||||
// 1 : All misc debug logs
|
// 1 : All misc debug logs
|
||||||
#define LOG 0
|
#define LOG 1
|
||||||
|
|
||||||
// TODO : Automate this
|
// TODO : Automate this
|
||||||
#define INTERPR
|
#define INTERPR
|
||||||
|
|
|
@ -252,6 +252,37 @@ void print_ast(Statement *base){
|
||||||
#endif
|
#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
|
// Lesser special functions
|
||||||
|
|
||||||
Value is(Tag tag, Value b){
|
Value is(Tag tag, Value b){
|
||||||
|
@ -298,28 +329,80 @@ Value flt_to_fix(Value x){
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char tmpbuf[256];
|
||||||
|
|
||||||
Value int_to_str(Value x){
|
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){
|
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){
|
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){
|
Value str_to_int(Value x){
|
||||||
|
return vstr_to(x,T_int);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value str_to_fix(Value x){
|
Value str_to_fix(Value x){
|
||||||
|
return vstr_to(x,T_fix);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value str_to_flt(Value x){
|
Value str_to_flt(Value x){
|
||||||
|
return vstr_to(x,T_float);
|
||||||
}
|
}
|
||||||
|
|
||||||
// [from][to]
|
// [from][to]
|
||||||
|
@ -518,6 +601,7 @@ Value pop(Value a, Value b){
|
||||||
Value write(Value a){
|
Value write(Value a){
|
||||||
switch(a.tag.type){
|
switch(a.tag.type){
|
||||||
case T_null:
|
case T_null:
|
||||||
|
case T_any:
|
||||||
printf("null");
|
printf("null");
|
||||||
break;
|
break;
|
||||||
case T_int:
|
case T_int:
|
||||||
|
@ -530,15 +614,16 @@ Value write(Value a){
|
||||||
printf("%f",a.v4B.vfl);
|
printf("%f",a.v4B.vfl);
|
||||||
break;
|
break;
|
||||||
case T_str:
|
case T_str:
|
||||||
// TODO replace strdup
|
|
||||||
char *nstr;
|
char *nstr;
|
||||||
|
nstr = malloc(a.vstr.len+1);
|
||||||
if(a.vstr.len > 4){
|
if(a.vstr.len > 4){
|
||||||
char *str = get_addr(a.vstr.pos);
|
char *str = get_addr(a.vstr.pos);
|
||||||
nstr = strndup(str, a.vstr.len);
|
memcpy(nstr, str, a.vstr.len);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
nstr = strndup(a.vstr.str, 4);
|
memcpy(nstr, a.vstr.str, a.vstr.len);
|
||||||
}
|
}
|
||||||
|
nstr[a.vstr.len] = '\0';
|
||||||
printf("%s",nstr);
|
printf("%s",nstr);
|
||||||
free(nstr);
|
free(nstr);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -46,4 +46,8 @@ Value is(Tag tag, Value b);
|
||||||
|
|
||||||
Value cast(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);
|
void symbol_map_add_bi(HashMap *map);
|
||||||
|
|
66
src/main.c
Normal file
66
src/main.c
Normal file
|
@ -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
|
||||||
|
* <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
|
@ -92,7 +92,11 @@ Statement *make_fconst(float val){
|
||||||
}
|
}
|
||||||
|
|
||||||
Statement *make_strconst(char *str){
|
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){
|
Statement *declare(i32 type, Tag vtype, char *name, Statement *assign){
|
||||||
|
|
|
@ -48,6 +48,3 @@ Statement *add_block(Statement *block, Statement *add);
|
||||||
Statement *make_operation(i32 type, Tag vtype, char *str, i32 nparam, ...);
|
Statement *make_operation(i32 type, Tag vtype, char *str, i32 nparam, ...);
|
||||||
|
|
||||||
void set_entry_point(Statement *statement);
|
void set_entry_point(Statement *statement);
|
||||||
|
|
||||||
extern Tag ntag;
|
|
||||||
extern Tag atag;
|
|
||||||
|
|
|
@ -48,7 +48,6 @@
|
||||||
|
|
||||||
\"[^"\n]*["\n] {
|
\"[^"\n]*["\n] {
|
||||||
yylval.st = make_strconst(yytext+1);
|
yylval.st = make_strconst(yytext+1);
|
||||||
printf("STRCST : %s\n",yytext);
|
|
||||||
return CST;
|
return CST;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
36
src/parser.y
36
src/parser.y
|
@ -21,6 +21,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "tests.h"
|
#include "tests.h"
|
||||||
#include "code_defs.h"
|
#include "code_defs.h"
|
||||||
|
@ -144,38 +145,3 @@ void yyerror(char *s){
|
||||||
fprintf(stderr, "Error : %s\n", s);
|
fprintf(stderr, "Error : %s\n", s);
|
||||||
exit(1);
|
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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -93,7 +93,7 @@ int t_flisp_alloc(){
|
||||||
|
|
||||||
int t_flisp_realloc(){
|
int t_flisp_realloc(){
|
||||||
int err = 0;
|
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);
|
u32 pos0 = flisp_alloc(5);
|
||||||
if(!pos0)
|
if(!pos0)
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -104,6 +104,7 @@ int t_flisp_realloc(){
|
||||||
return 1;
|
return 1;
|
||||||
ptr0 = get_addr(pos0);
|
ptr0 = get_addr(pos0);
|
||||||
err += memcmp(ptr0, ref, 5);
|
err += memcmp(ptr0, ref, 5);
|
||||||
|
flisp_free(pos0);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue