Skip to content

Commit

Permalink
🔥 named expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
fennecdjay committed Nov 7, 2024
1 parent 33c78d8 commit bf2ac16
Show file tree
Hide file tree
Showing 11 changed files with 1,666 additions and 1,605 deletions.
29 changes: 20 additions & 9 deletions include/absyn.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef __ABSYN
#define __ABSYN

#include <inttypes.h>
#define AST_NEW(type, name, ...) type new_##name(MemPool p, __VA_ARGS__)

#define AST_FREE(type, name) void free_##name(MemPool p NUSED, type a NUSED)
Expand Down Expand Up @@ -117,8 +118,8 @@ enum fbflag {

/** a dot expression. @code object.member @endcode */
typedef struct {
Exp* base;
Symbol xid;
Tag tag;
Exp *base;
} Exp_Dot;

typedef struct Capture {
Expand Down Expand Up @@ -200,7 +201,8 @@ typedef enum {
ae_exp_if,
ae_exp_dot,
ae_exp_lambda,
ae_exp_td
ae_exp_td,
ae_exp_named,
} ae_exp_t;
typedef enum { ae_meta_var, ae_meta_value, ae_meta_protect } ae_Exp_Meta;

Expand Down Expand Up @@ -325,6 +327,14 @@ typedef struct {
enum unary_type unary_type;
} Exp_Unary;

typedef struct {
Exp *exp;
Tag tag;
bool is_arg;
} Exp_Named;

AST_NEW(Exp*, exp_named, Exp *exp, const Tag tag, const loc_t loc);

enum exp_state {
exp_state_meta, // ae_meta_value
exp_state_prot, // ae_meta_protect
Expand Down Expand Up @@ -353,6 +363,7 @@ struct Exp {
Exp_Slice exp_slice;
Exp_Lambda exp_lambda;
Type_Decl * exp_td;
Exp_Named exp_named;
} d;
struct Type_ *type;
struct Type_ *cast_to;
Expand Down Expand Up @@ -454,7 +465,7 @@ ANEW AST_NEW(Exp*, exp_call, Exp*, Exp* args, const loc_t);
ANEW ANN AST_NEW(Exp*, exp_cast, Type_Decl *, Exp*, const loc_t);
ANN2(1, 2, 4)
ANEW AST_NEW(Exp*, exp_if, Exp*, Exp*, Exp*, const loc_t);
ANEW ANN AST_NEW(Exp*, exp_dot, Exp*, struct Symbol_ *,
ANEW ANN AST_NEW(Exp*, exp_dot, Exp*, const Tag tag,
const loc_t);
ANEW ANN AST_NEW(Exp*, exp_unary, const Symbol, Exp*, const loc_t);
ANEW ANN2(1,2,3) AST_NEW(Exp*, exp_unary2, const Symbol, Type_Decl *,
Expand Down Expand Up @@ -649,11 +660,11 @@ ANEW ANN AST_NEW(Type_Def, type_def, Type_Decl *, const Symbol, const loc_t);
ANN void free_type_def(MemPool p, Type_Def);

typedef struct Union_Def_ {
Variable_List l;
Tag tag;
struct Type_ * type;
Tmpl * tmpl;
ae_flag flag;
Variable_List l;
Tag tag;
struct Type_ *type;
Tmpl * tmpl;
ae_flag flag;
} * Union_Def;
ANEW ANN AST_NEW(Union_Def, union_def, const Variable_List, const loc_t);
ANN void free_union_def(MemPool p, Union_Def);
Expand Down
1 change: 1 addition & 0 deletions include/ast_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ static inline bool dummy_func(const void *a NUSED, const void *b NUSED) {
(_##prefix##_exp_func)prefix##_exp_dot, \
(_##prefix##_exp_func)prefix##_exp_lambda, \
(_##prefix##_exp_func)prefix##_exp_td, \
(_##prefix##_exp_func)prefix##_exp_named, \
};

#define DECL_STMT_FUNC(prefix, type, arg) \
Expand Down
2 changes: 1 addition & 1 deletion include/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ extern int yylex \
#undef yyTABLES_NAME
#endif

#line 445 "src/gwion.l"
#line 446 "src/gwion.l"


#line 723 "include/lexer.h"
Expand Down
20 changes: 14 additions & 6 deletions pass/xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ ANN static bool xxx_exp_td(XXX *a, Type_Decl *b) {
return xxx_type_decl(a, b);
}

ANN static bool xxx_exp_named(XXX *a, Exp_Named *b) {
return xxx_exp(a, b->exp);
}

DECL_EXP_FUNC(xxx, bool, XXX*)
ANN static bool xxx_exp(XXX *a, Exp* b) {
bool ret = b->poison
Expand Down Expand Up @@ -405,8 +409,8 @@ ANN static bool xxx_stmt_spread(XXX *a, Spread_Def b) {

ANN static bool xxx_stmt_using(XXX *a, Stmt_Using b) {
bool ret = true;
if(b->alias.sym) {
CHECK_RET(xxx_tag(a, &b->alias), ret);
if(b->tag.sym) {
CHECK_RET(xxx_tag(a, &b->tag), ret);
CHECK_RET(xxx_exp(a, b->d.exp), ret);
} else
CHECK_RET(xxx_type_decl(a, b->d.td), ret);
Expand All @@ -418,10 +422,12 @@ ANN static bool xxx_stmt_import(XXX *a, Stmt_Import b) {
CHECK_RET(xxx_tag(a, &b->tag), ret);
if(b->selection) {
for(uint32_t i = 0; i < b->selection->len; i++) {
ImportItem ii = *mp_vector_at(b->selection, ImportItem, i);
CHECK_RET(xxx_tag(a, &ii.tag), ret);
if(ii.exp)
CHECK_RET(xxx_exp(a, ii.exp), ret);
Stmt_Using using = mp_vector_at(b->selection, struct Stmt_Using_, i);
CHECK_RET(xxx_tag(a, &using->tag), ret);
if(using->tag.sym)
CHECK_RET(xxx_exp(a, using->d.exp), ret);
else
CHECK_RET(xxx_type_decl(a, using->d.td), ret);
}
}
return ret;
Expand Down Expand Up @@ -516,13 +522,15 @@ ANN static bool xxx_enum_list(XXX *a, EnumValue_List b) {

ANN static bool xxx_enum_def(XXX *a, Enum_Def b) {
bool ret = true;
if(b->ext) CHECK_RET(xxx_type_decl(a, b->ext), ret);
CHECK_RET(xxx_enum_list(a, b->list), ret);
CHECK_RET(xxx_tag(a, &b->tag), ret);
return ret;
}

ANN static bool xxx_union_def(XXX *a, Union_Def b) {
bool ret = true;
if(b->ext) CHECK_RET(xxx_type_decl(a, b->ext), ret);
CHECK_RET(xxx_variable_list(a, b->l), ret);
CHECK_RET(xxx_tag(a, &b->tag), ret);
if(b->tmpl) CHECK_RET(xxx_tmpl(a, b->tmpl), ret);
Expand Down
12 changes: 6 additions & 6 deletions src/ast_cpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ANN static Stmt_List cpy_stmt_list(MemPool p, Stmt_List src);

ANN static void cpy_exp_dot(MemPool p, Exp_Dot *a, const Exp_Dot *src) {
a->base = cpy_exp(p, src->base);
a->xid = src->xid;
a->tag = src->tag;
}

ANN static void cpy_exp_lambda(MemPool p, Exp_Lambda *a,
Expand Down Expand Up @@ -265,6 +265,8 @@ ANN Exp* cpy_exp(MemPool p, const Exp* src) {
case ae_exp_td:
a->d.exp_td = cpy_type_decl(p, src->d.exp_td);
break;
case ae_exp_named:
a->d.exp_named.exp = cpy_exp(p, src->d.exp_named.exp);
}
a->exp_type = src->exp_type;
a->emit_var = src->emit_var;
Expand Down Expand Up @@ -423,11 +425,9 @@ ANN Variable_List cpy_variable_list(MemPool p, const Variable_List src) {
ANN Union_Def cpy_union_def(MemPool p, const Union_Def src) {
Union_Def a = mp_calloc(p, Union_Def);
a->tag = src->tag;
a->l = cpy_variable_list(p, src->l); // 1
// if (src->xid) a->xid = src->xid; // 1
if (src->tmpl) a->tmpl = cpy_tmpl(p, src->tmpl); // 1
// a->pos = src->pos;
a->flag = src->flag; // 1
a->l = cpy_variable_list(p, src->l);
if (src->tmpl) a->tmpl = cpy_tmpl(p, src->tmpl);
a->flag = src->flag;
return a;
}

Expand Down
4 changes: 4 additions & 0 deletions src/ast_free.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ ANN static AST_FREE(Exp_Primary *, prim) {
free_range(p, a->d.range);
}

ANN static AST_FREE(Exp_Named *, exp_named) {
free_exp(p, a->exp);
}

ANN static AST_FREE(Type_Decl **, exp_td) { free_type_decl(p, *a); }
DECL_EXP_FUNC(free, void, MemPool)

Expand Down
13 changes: 10 additions & 3 deletions src/ast_new.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ AST_NEW(Exp*, exp_td, Type_Decl *td, const loc_t loc) {
return a;
}

AST_NEW(Exp*, exp_named, Exp *exp, const Tag tag, const loc_t loc) {
Exp* a = new_exp(p, ae_exp_named, loc);
a->d.exp_named.exp = exp;
a->d.exp_named.tag = tag;
return a;
}


static AST_NEW(Exp*, prim, const loc_t loc) {
Exp* a = new_exp(p, ae_exp_primary, loc);
exp_setmeta(a, 1);
Expand Down Expand Up @@ -302,11 +310,11 @@ AST_NEW(Exp*, exp_call, Exp* base, Exp* args,
return a;
}

AST_NEW(Exp*, exp_dot, Exp* base, struct Symbol_ *xid,
AST_NEW(Exp*, exp_dot, Exp* base, const Tag tag,
const loc_t loc) {
Exp* a = new_exp(p, ae_exp_dot, loc);
a->d.exp_dot.base = base;
a->d.exp_dot.xid = xid;
a->d.exp_dot.tag = tag;
return a;
}
/*
Expand Down Expand Up @@ -395,7 +403,6 @@ AST_NEW(Enum_Def, enum_def, const EnumValue_List list, struct Symbol_ *xid,
Enum_Def a = mp_calloc(p, Enum_Def);
a->tag = MK_TAG(xid, loc);
a->list = list;
// vector_init(&a->values);
return a;
}

Expand Down
3 changes: 2 additions & 1 deletion src/gwion.l
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ static inline Macro scan_has_macro(Scanner *scan, const m_str id);
vector_add(&scan->hashes, yyleng);
scan->interp_state++;
BEGIN(interp);
return INTERP_START;
return (vector_size(&scan->hashes) != (m_uint)scan->interp_state) ? STRING_LIT : INTERP_START;
// return INTERP_START;
}

<interp>\"#* {
Expand Down
25 changes: 13 additions & 12 deletions src/gwion.y
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ ANN void lex_spread(void *data);
%type<sym>opt_id
%type<vector>func_effects _func_effects
%type<var_decl> var_decl arg_decl
%type<type_decl> type_decl_tmpl type_decl_base type_decl_noflag type_decl_opt type_decl type_decl_array class_ext
%type<type_decl> type_decl_tmpl type_decl_base type_decl_noflag type_decl_opt type_decl type_decl_array extends
%type<exp> prim_exp decl_exp binary_exp call_paren interp interp_exp
%type<exp> opt_exp con_exp log_or_exp log_and_exp inc_or_exp exc_or_exp and_exp eq_exp
%type<exp> rel_exp shift_exp add_exp mul_exp dur_exp unary_exp dict_list
Expand Down Expand Up @@ -170,7 +170,7 @@ ANN void lex_spread(void *data);
%type<tmplarg> tmplarg;
%type<tmplarg_list> tmplarg_list call_template
%type<variable> variable
%type<variable_list> variable_list
%type<variable_list> variable_list
%type<import_list> import_list
%type<prim_def> prim_def
%type<ast> ast section_list
Expand Down Expand Up @@ -227,7 +227,7 @@ section

class_flag: flag modifier { $$ = $1 | $2; }
class_def
: "class" class_flag ID decl_template class_ext traits class_body
: "class" class_flag ID decl_template extends traits class_body
{
$$ = new_class_def(mpool(arg), $2, MK_TAG($3, @3), $5, $7);
if($4)
Expand Down Expand Up @@ -269,11 +269,11 @@ prim_def: "primitive" class_flag ID decimal ";"
{
$$ = new_prim_def(mpool(arg), $3, $4.num, @3, $2);
}
class_ext : "extends" type_decl_array { $$ = $2; } | %empty { $$ = NULL; };
extends : "extends" type_decl_array { $$ = $2; } | %empty { $$ = NULL; };
traits: %empty { $$ = NULL; } | ":" id_list { $$ = $2; };

extend_def: "extends" type_decl_array ":" id_list ";" {
$$ = new_extend_def(mpool(arg), $2, $4);
extend_def: extends ":" id_list ";" {
$$ = new_extend_def(mpool(arg), $1, $3);
}


Expand Down Expand Up @@ -413,7 +413,7 @@ stmt
| import_stmt
;

spread_stmt: "..." ID ":" id_list "{" {lex_spread(((Scanner*)scan));} SPREAD {
spread_stmt: "..." ID ":" id_list "{" {lex_spread(((Scanner*)scan));} "}..." {
struct Spread_Def_ spread = {
.tag = MK_TAG($2, @2),
.list = $4,
Expand Down Expand Up @@ -494,6 +494,7 @@ enum_value: ID { $$ = (EnumValue) { .tag = MK_TAG($1, @1) }; }

enum_list: enum_value { YYLIST_INI(EnumValue, $$, $1); }
| enum_list "," enum_value { YYLIST_END(EnumValue, $$, $1, $3) ;}
| stmt_pp { return 0;}

enum_def
: "enum" flag ID "{" enum_list opt_comma "}" {
Expand Down Expand Up @@ -653,7 +654,7 @@ exp:

binary_exp
: decl_exp
// | ID "=" binary_exp {exit(10);}
| ID "=" binary_exp { $$ = new_exp_named(mpool(arg), $3, MK_TAG($1, @1), @$);}
| binary_exp "@" decl_exp { $$ = new_exp_binary(mpool(arg), $1, $2, $3, @2); }
| binary_exp DYNOP decl_exp { $$ = new_exp_binary(mpool(arg), $1, $2, $3, @2); }
| binary_exp OPTIONS decl_exp { $$ = new_exp_binary(mpool(arg), $1, $2, $3, @2); }
Expand Down Expand Up @@ -860,8 +861,8 @@ variable:
$$ = MK_VAR(td, (Var_Decl){ .tag = MK_TAG($1, @1)});
}
| type_decl_array ID ";" { $$ = MK_VAR($1, (Var_Decl){ .tag = MK_TAG($2, @2)});}

variable_list: variable { YYLIST_INI(Variable, $$, $1); }
| stmt_pp { return 0;}
variable_list: variable { YYLIST_INI(Variable, $$, $1); }
| variable_list variable { YYLIST_END(Variable, $$, $1, $2); }

union_def
Expand Down Expand Up @@ -940,7 +941,7 @@ lambda_list:

lambda_arg: "\\" lambda_list { $$ = $2; } | BACKSLASH { $$ = NULL; }

tmplarg_exp: basic_exp;
tmplarg_exp: basic_exp | interp;
tmplarg: type_decl_array {
$$ = (TmplArg) { .d = { .td = $1}, .type = tmplarg_td};
}
Expand All @@ -958,7 +959,7 @@ call_paren :
post_op : "++" | "--";

dot_exp: post_exp "." ID {
$$ = new_exp_dot(mpool(arg), $1, $3, @$);
$$ = new_exp_dot(mpool(arg), $1, MK_TAG($3, @3), @$);
};

post_exp: prim_exp
Expand Down
Loading

0 comments on commit bf2ac16

Please sign in to comment.