Test for strconst, general fixes, start of scoped variables
This commit is contained in:
parent
dc208aed7c
commit
909badb818
10 changed files with 72 additions and 43 deletions
|
@ -8,16 +8,17 @@ The code in examples/ is public domain
|
||||||
## Building
|
## Building
|
||||||
|
|
||||||
Pre-requisites:
|
Pre-requisites:
|
||||||
- yacc
|
- Bison
|
||||||
- lex
|
- lex
|
||||||
- A C99 compiler
|
- A C99 compiler
|
||||||
- GNU Make
|
- GNU Make
|
||||||
|
|
||||||
### Unix
|
### Unix
|
||||||
Running `make` (or `gmake` for BSDs) should work on most Unix systems.
|
Running `make` (or `gmake` for BSDs), maybe twice depending on your version of make,
|
||||||
|
should work on most Unix systems.
|
||||||
It produces an executable called `flisp.amd64`
|
It produces an executable called `flisp.amd64`
|
||||||
|
|
||||||
### Windows
|
### Windows
|
||||||
To meet the previously mentionned pre-requisites, you need to use mingw.
|
To meet the previously mentionned pre-requisites, you need to use mingw.
|
||||||
I recommend installing w64devkit, then compiling Bison and Flex from source.
|
I recommend installing w64devkit, then compiling Bison and Flex from source.
|
||||||
Run `make win`
|
Run `make win`, twice
|
||||||
|
|
8
TODO.md
8
TODO.md
|
@ -1,13 +1,5 @@
|
||||||
tmp:
|
|
||||||
- fix realloc
|
|
||||||
|
|
||||||
- Benchmarks
|
- Benchmarks
|
||||||
- Tests
|
- Tests
|
||||||
- Rework assign/call to work better
|
|
||||||
- Finish internal allocator
|
|
||||||
- Strings
|
|
||||||
- String casts
|
|
||||||
- Core functions
|
|
||||||
- Type checks
|
- Type checks
|
||||||
- Scopes
|
- Scopes
|
||||||
|
|
||||||
|
|
|
@ -141,6 +141,7 @@ typedef Value (bi_fn_t)(void);
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
||||||
Tag params[8];
|
Tag params[8];
|
||||||
|
Tag output;
|
||||||
i16 n_param;
|
i16 n_param;
|
||||||
i16 is_builtin;
|
i16 is_builtin;
|
||||||
union {
|
union {
|
||||||
|
|
|
@ -93,12 +93,15 @@ typedef struct Statement Statement;
|
||||||
// In practice, this is allocated in parse.c
|
// In practice, this is allocated in parse.c
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
||||||
// Kept for debug messages
|
// Debug messages
|
||||||
|
// TODO
|
||||||
i32 curr_line;
|
i32 curr_line;
|
||||||
i32 curr_column;
|
i32 curr_column;
|
||||||
|
|
||||||
i32 max_statement; // Unused while parsing
|
//i32 max_statement; // Unused while parsing
|
||||||
i32 curr_statement;
|
//i32 curr_statement;
|
||||||
|
|
||||||
|
i32 curr_scope; // Current local scope (id)
|
||||||
|
|
||||||
HashMap symbol_map;
|
HashMap symbol_map;
|
||||||
|
|
||||||
|
|
|
@ -260,9 +260,6 @@ Value str_to_val(char *str, i32 len){
|
||||||
}
|
}
|
||||||
Value retv;
|
Value retv;
|
||||||
retv.tag = (Tag){.is_array = 0, .type = T_str};
|
retv.tag = (Tag){.is_array = 0, .type = T_str};
|
||||||
#if LOG
|
|
||||||
printf("str (%d) : %s\n", len, str);
|
|
||||||
#endif
|
|
||||||
if(len > 4){
|
if(len > 4){
|
||||||
u32 nstr = flisp_alloc(len);
|
u32 nstr = flisp_alloc(len);
|
||||||
if(!nstr){
|
if(!nstr){
|
||||||
|
@ -731,24 +728,24 @@ const Tag tvec = {1,0,T_any};
|
||||||
#define BICAST(x) ((bi_fn_t*)(x))
|
#define BICAST(x) ((bi_fn_t*)(x))
|
||||||
|
|
||||||
FnSig builtins[] = {
|
FnSig builtins[] = {
|
||||||
{{tany,tany},2,1,{.bi = BICAST(add)}},
|
{{tany,tany},tany,2,1,{.bi = BICAST(add)}},
|
||||||
{{tany,tany},2,1,{.bi = BICAST(sub)}},
|
{{tany,tany},tany,2,1,{.bi = BICAST(sub)}},
|
||||||
{{tany,tany},2,1,{.bi = BICAST(mul)}},
|
{{tany,tany},tany,2,1,{.bi = BICAST(mul)}},
|
||||||
{{tany,tany},2,1,{.bi = BICAST(divv)}},
|
{{tany,tany},tany,2,1,{.bi = BICAST(divv)}},
|
||||||
{{tany,tany},2,1,{.bi = BICAST(mod)}},
|
{{tany,tany},tany,2,1,{.bi = BICAST(mod)}},
|
||||||
{{tany,tany},2,1,{.bi = BICAST(sml)}},
|
{{tany,tany},tint,2,1,{.bi = BICAST(sml)}},
|
||||||
{{tany,tany},2,1,{.bi = BICAST(sml_eq)}},
|
{{tany,tany},tint,2,1,{.bi = BICAST(sml_eq)}},
|
||||||
{{tany,tany},2,1,{.bi = BICAST(eq)}},
|
{{tany,tany},tint,2,1,{.bi = BICAST(eq)}},
|
||||||
{{tany,tany},2,1,{.bi = BICAST(gt_eq)}},
|
{{tany,tany},tint,2,1,{.bi = BICAST(gt_eq)}},
|
||||||
{{tany,tany},2,1,{.bi = BICAST(gt)}},
|
{{tany,tany},tint,2,1,{.bi = BICAST(gt)}},
|
||||||
{{tany} ,1,1,{.bi = BICAST(not)}},
|
{{tany} ,tint,1,1,{.bi = BICAST(not)}},
|
||||||
{{tany,tany},2,1,{.bi = BICAST(and)}},
|
{{tany,tany},tint,2,1,{.bi = BICAST(and)}},
|
||||||
{{tany,tany},2,1,{.bi = BICAST(or)}},
|
{{tany,tany},tint,2,1,{.bi = BICAST(or)}},
|
||||||
{{tvec} ,1,1,{.bi = BICAST(len)}},
|
{{tvec} ,tint,1,1,{.bi = BICAST(len)}},
|
||||||
{{tvec,tany},2,1,{.bi = BICAST(push)}},
|
{{tvec,tany},tany,2,1,{.bi = BICAST(push)}},
|
||||||
{{tvec,tint},2,1,{.bi = BICAST(pop)}},
|
{{tvec,tint},tany,2,1,{.bi = BICAST(pop)}},
|
||||||
{{tany} ,1,1,{.bi = BICAST(fl_write)}},
|
{{tany} ,tany,1,1,{.bi = BICAST(fl_write)}},
|
||||||
{{tany} ,0,1,{.bi = BICAST(nwline)}}
|
{{tany} ,tany,0,1,{.bi = BICAST(nwline)}}
|
||||||
};
|
};
|
||||||
|
|
||||||
char *bi_names[] = {
|
char *bi_names[] = {
|
||||||
|
|
|
@ -39,7 +39,8 @@ void assign(Value *dst, Value src){
|
||||||
#endif
|
#endif
|
||||||
if(dst->tag.is_array != src.tag.is_array)
|
if(dst->tag.is_array != src.tag.is_array)
|
||||||
runtime_err("Invalid assignement");
|
runtime_err("Invalid assignement");
|
||||||
if((dst->tag.type == src.tag.type || !dst->tag.is_strong) || dst->tag.type == T_any)
|
if(dst->tag.type == src.tag.type || !dst->tag.is_strong
|
||||||
|
|| dst->tag.type == T_any)
|
||||||
*dst = src;
|
*dst = src;
|
||||||
else{
|
else{
|
||||||
runtime_err("Invalid assignement");
|
runtime_err("Invalid assignement");
|
||||||
|
|
|
@ -51,7 +51,7 @@ int main(int argc, char *argv[]){
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
printf("fLisp interactive env - v0.1\nFcalva 2025\n");
|
printf("fLisp interactive env - v0.1\nFcalva 2025\n");
|
||||||
printf("Under the terms of the GPL v3.0 license\n");
|
printf("Under the terms of the GPL v2.0 license\n");
|
||||||
yyin = stdin;
|
yyin = stdin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -73,6 +73,25 @@ Tag get_type(char *str){
|
||||||
return (Tag){0,0,0};
|
return (Tag){0,0,0};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void mangle_name(i32 scope, char *name){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Syntaxically, there can only be one active fnlist at a time
|
||||||
|
Tag _fnlist[8];
|
||||||
|
i32 _fnl_pos;
|
||||||
|
|
||||||
|
void make_fnlist(char *name, Tag type){
|
||||||
|
_fnl_pos = 1;
|
||||||
|
_fnlist[0] = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
void append_fnlist(char *name, Tag type){
|
||||||
|
if(_fnl_pos >= 8)
|
||||||
|
yyerror("Too many parameters to function");
|
||||||
|
_fnlist[fnl_pos++] = type;
|
||||||
|
}
|
||||||
|
|
||||||
Statement *make_iconst(i32 val){
|
Statement *make_iconst(i32 val){
|
||||||
Statement *stat = &stack->statements[stack->curr_statement++];
|
Statement *stat = &stack->statements[stack->curr_statement++];
|
||||||
stat->type = ST_Const;
|
stat->type = ST_Const;
|
||||||
|
@ -94,6 +113,7 @@ Statement *make_fconst(float val){
|
||||||
Statement *make_strconst(char *str){
|
Statement *make_strconst(char *str){
|
||||||
Statement *stat = &stack->statements[stack->curr_statement++];
|
Statement *stat = &stack->statements[stack->curr_statement++];
|
||||||
stat->type = ST_Const;
|
stat->type = ST_Const;
|
||||||
|
stat->is_const = 1;
|
||||||
i32 len = strcspn(str, "\"");
|
i32 len = strcspn(str, "\"");
|
||||||
stat->cons = str_to_val(str, len);
|
stat->cons = str_to_val(str, len);
|
||||||
return stat;
|
return stat;
|
||||||
|
|
|
@ -56,7 +56,7 @@
|
||||||
return G_IDENT;
|
return G_IDENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
[0-9]+"."[0-9]+ {
|
[0-9]*"."[0-9]+ {
|
||||||
yylval.st = make_fconst(atof(yytext));
|
yylval.st = make_fconst(atof(yytext));
|
||||||
return CST;
|
return CST;
|
||||||
}
|
}
|
||||||
|
|
20
src/tests.c
20
src/tests.c
|
@ -150,9 +150,23 @@ int t_make_fconst(){
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
|
||||||
int t_make_strconst(){
|
int t_make_strconst(){
|
||||||
return 0;
|
int err = 0;
|
||||||
|
Statement *st = NULL;
|
||||||
|
st = make_strconst("Hello world");
|
||||||
|
if(!st)
|
||||||
|
return 1;
|
||||||
|
err += st->type != ST_Const || !st->is_const || st->child_n;
|
||||||
|
err += st->cons.vstr.len != 11 || st->cons.tag.type != T_str;
|
||||||
|
err += strncmp(get_addr(st->cons.vstr.pos), "Hello world", 11);
|
||||||
|
st = make_strconst("Gatt");
|
||||||
|
if(!st)
|
||||||
|
return 1;
|
||||||
|
err += st->type != ST_Const || !st->is_const || st->child_n;
|
||||||
|
err += st->cons.vstr.len != 4 || st->cons.tag.type != T_str;
|
||||||
|
err += strncmp(st->cons.vstr.str, "Gatt", 4);
|
||||||
|
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
int t_free_stack(){
|
int t_free_stack(){
|
||||||
|
@ -191,7 +205,7 @@ char *names[] = {
|
||||||
"make_stack",
|
"make_stack",
|
||||||
"make_iconst",
|
"make_iconst",
|
||||||
"make_fconst",
|
"make_fconst",
|
||||||
"make_strconst (TODO)",
|
"make_strconst",
|
||||||
|
|
||||||
"free_stack"
|
"free_stack"
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue