Skip to content

Commit

Permalink
classes
Browse files Browse the repository at this point in the history
  • Loading branch information
xensik committed Feb 17, 2024
1 parent 78db94e commit 039a88f
Show file tree
Hide file tree
Showing 11 changed files with 3,068 additions and 2,445 deletions.
2 changes: 1 addition & 1 deletion gen/jup/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ clean:
rm -rf ./parser2.hpp
rm -rf ./parser2.cpp

arc: parser.ypp
arc: parser2.ypp
bison parser2.ypp -Wcounterexamples
mv parser2.hpp ../../include/xsk/arc/
mv parser2.cpp ../../src/arc/
76 changes: 73 additions & 3 deletions gen/jup/parser2.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ namespace xsk::arc
%token USINGTREE "#using_animtree"
%token NAMESPACE "#namespace"
%token ANIMTREE "#animtree"
%token VAR "var"
%token CLASS "class"
%token CONSTRUCTOR "constructor"
%token DESTRUCTOR "destructor"
%token FUNCTION "function"
%token AUTOEXEC "autoexec"
%token CODECALL "codecall"
Expand Down Expand Up @@ -184,6 +188,10 @@ namespace xsk::arc
%type <decl_namespace::ptr> decl_namespace
%type <decl_usingtree::ptr> decl_usingtree
%type <decl_function::ptr> decl_function
%type <decl_variable::ptr> decl_variable
%type <decl_class::ptr> decl_class
%type <decl_list::ptr> decl_list
%type <expr_identifier::ptr> opt_class_base
%type <stmt::ptr> stmt
%type <stmt::ptr> stmt_or_dev
%type <stmt_list::ptr> stmt_list
Expand Down Expand Up @@ -226,10 +234,12 @@ namespace xsk::arc
%type <expr_complement::ptr> expr_complement
%type <expr_negate::ptr> expr_negate
%type <expr_not::ptr> expr_not
%type <expr_new::ptr> expr_new
%type <expr_call::ptr> expr_call
%type <expr_method::ptr> expr_method
%type <call::ptr> expr_function
%type <call::ptr> expr_pointer
%type <call::ptr> expr_member
%type <expr_parameters::ptr> expr_parameters
%type <expr::ptr> expr_parameters_default
%type <expr_arguments::ptr> expr_arguments
Expand Down Expand Up @@ -306,11 +316,11 @@ namespace xsk::arc
%precedence PREINC PREDEC
%precedence POSTINC POSTDEC

%start root
%start main

%%

root
main
: program { ast = std::move($1); }
| { ast = program::make(@$); }
;
Expand Down Expand Up @@ -366,10 +376,11 @@ declaration
| decl_namespace { $$ = std::move($1); }
| decl_usingtree { $$ = std::move($1); }
| decl_function { $$ = std::move($1); }
| decl_class { $$ = std::move($1); }
;

decl_namespace
: NAMESPACE expr_string SEMICOLON
: NAMESPACE expr_identifier SEMICOLON
{ ppr.ban_header(@$); $$ = decl_namespace::make(@$, std::move($2)); }
;

Expand All @@ -389,6 +400,36 @@ decl_function
{ ppr.ban_header(@$); $$ = decl_function::make(@$, expr_identifier::make(@$, ""), std::move($3), std::move($5), std::move($7), export_flags::export_private); }
;

decl_variable
: VAR expr_identifier SEMICOLON
{ $$ = decl_variable::make(@$, std::move($2)); }
;

decl_class
: CLASS expr_identifier opt_class_base LBRACE decl_list RBRACE
{ ppr.ban_header(@$); $$ = decl_class::make(@$, std::move($2), std::move($3), std::move($5)); }
;

decl_list
: decl_list CONSTRUCTOR LPAREN RPAREN stmt_comp
{ $$ = std::move($1); $$->list.push_back(decl_function::make(@$, expr_identifier::make(@$, ""), expr_identifier::make(@$, "constructor"), expr_parameters::make(@$), std::move($5), export_flags::export_none)); }
| decl_list DESTRUCTOR LPAREN RPAREN stmt_comp
{ $$ = std::move($1); $$->list.push_back(decl_function::make(@$, expr_identifier::make(@$, ""), expr_identifier::make(@$, "destructor"), expr_parameters::make(@$), std::move($5), export_flags::export_none)); }
| decl_list decl_function
{ $$ = std::move($1); $$->list.push_back(std::move($2)); }
| decl_list decl_variable
{ $$ = std::move($1); $$->list.push_back(std::move($2)); }
|
{ $$ = decl_list::make(@$); }
;

opt_class_base
: COLON expr_identifier
{ $$ = std::move($2); }
|
{ $$ = expr_identifier::make(@$, ""); }
;

stmt
: stmt_comp { $$ = std::move($1); }
| stmt_call { $$ = std::move($1); }
Expand Down Expand Up @@ -714,6 +755,7 @@ expr_primitive
: expr_complement { $$ = std::move($1); }
| expr_negate { $$ = std::move($1); }
| expr_not { $$ = std::move($1); }
| expr_new { $$ = std::move($1); }
| expr_call { $$ = std::move($1); }
| expr_method { $$ = std::move($1); }
| expr_getnextarraykey { $$ = std::move($1); }
Expand Down Expand Up @@ -780,9 +822,15 @@ expr_not
{ $$ = expr_not::make(@$, std::move($2)); }
;

expr_new
: NEW expr_identifier LPAREN RPAREN
{ $$ = expr_new::make(@$, std::move($2)); }
;

expr_call
: expr_function { $$ = expr_call::make(@$, std::move($1)); }
| expr_pointer { $$ = expr_call::make(@$, std::move($1)); }
| expr_member { $$ = expr_call::make(@$, std::move($1)); }
;

expr_method
Expand All @@ -800,6 +848,13 @@ expr_method

$$ = expr_method::make(@$, std::move($1), std::move($2));
}
| expr_object expr_member
{
if ($1->loc().begin.line != $2->loc().begin.line)
error($2->loc(), "missing ';' ?");

$$ = expr_method::make(@$, std::move($1), std::move($2));
}
;

expr_function
Expand All @@ -820,6 +875,13 @@ expr_pointer
{ $$ = expr_pointer::make(@$, std::move($4), std::move($8), call::mode::thread); }
;

expr_member
: LBRACKET LBRACKET expr RBRACKET RBRACKET ARROW expr_identifier LPAREN expr_arguments RPAREN
{ $$ = expr_member::make(@$, std::move($3), std::move($7), std::move($9), call::mode::normal); }
| THREAD LBRACKET LBRACKET expr RBRACKET RBRACKET ARROW expr_identifier LPAREN expr_arguments RPAREN
{ $$ = expr_member::make(@$, std::move($4), std::move($8), std::move($10), call::mode::thread); }
;

expr_parameters
: expr_parameters COMMA expr_parameters_default
{ $$ = std::move($1); $$->list.push_back(std::move($3)); }
Expand Down Expand Up @@ -1306,6 +1368,10 @@ std::unordered_map<token::kind, parser2::token::token_kind_type> const tok_to_pa
{ token::NAMESPACE, parser2::token::NAMESPACE },
{ token::USINGTREE, parser2::token::USINGTREE },
{ token::ANIMTREE, parser2::token::ANIMTREE },
{ token::VAR, parser2::token::VAR },
{ token::CLASS, parser2::token::CLASS },
{ token::CONSTRUCTOR, parser2::token::CONSTRUCTOR },
{ token::DESTRUCTOR, parser2::token::DESTRUCTOR },
{ token::FUNCTION, parser2::token::FUNCTION },
{ token::AUTOEXEC, parser2::token::AUTOEXEC },
{ token::CODECALL, parser2::token::CODECALL },
Expand Down Expand Up @@ -1370,6 +1436,10 @@ std::unordered_map<token::kind, parser2::token::token_kind_type> const tok_to_pa

std::unordered_map<std::string_view, parser2::token::token_kind_type> const keyword_map2
{{
{ "var", parser2::token::VAR },
{ "class", parser2::token::CLASS },
{ "constructor", parser2::token::CONSTRUCTOR },
{ "destructor", parser2::token::DESTRUCTOR },
{ "function", parser2::token::FUNCTION },
{ "autoexec", parser2::token::AUTOEXEC },
{ "codecall", parser2::token::CODECALL },
Expand Down
39 changes: 37 additions & 2 deletions include/xsk/arc/common/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ struct node
stmt_jmp_endswitch,
stmt_jmp_dev,
decl_empty,
decl_list,
decl_class,
decl_variable,
decl_function,
decl_usingtree,
decl_namespace,
Expand Down Expand Up @@ -1296,6 +1299,38 @@ struct decl_empty : public decl
XSK_ARC_AST_MAKE(decl_empty)
};

struct decl_list : public decl
{
using ptr = std::unique_ptr<decl_list>;

std::vector<decl::ptr> list;

decl_list(location const& loc);
XSK_ARC_AST_MAKE(decl_list)
};

struct decl_class : public decl
{
using ptr = std::unique_ptr<decl_class>;

expr_identifier::ptr name;
expr_identifier::ptr base;
decl_list::ptr body;

decl_class(location const& loc, expr_identifier::ptr name, expr_identifier::ptr base, decl_list::ptr body);
XSK_ARC_AST_MAKE(decl_class)
};

struct decl_variable : public decl
{
using ptr = std::unique_ptr<decl_variable>;

expr_identifier::ptr name;

decl_variable(location const& loc, expr_identifier::ptr name);
XSK_ARC_AST_MAKE(decl_variable)
};

struct decl_function : public decl
{
using ptr = std::unique_ptr<decl_function>;
Expand Down Expand Up @@ -1324,9 +1359,9 @@ struct decl_namespace : public decl
{
using ptr = std::unique_ptr<decl_namespace>;

expr_string::ptr name;
expr_identifier::ptr name;

decl_namespace(location const& loc, expr_string::ptr name);
decl_namespace(location const& loc, expr_identifier::ptr name);
XSK_ARC_AST_MAKE(decl_namespace)
};

Expand Down
1 change: 1 addition & 0 deletions include/xsk/arc/common/token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct token
CONST, ISDEFINED, VECTORSCALE, ANGLESTOUP, ANGLESTORIGHT, ANGLESTOFORWARD, ANGLECLAMP180,
VECTORTOANGLES, ABS, GETTIME, GETDVAR, GETDVARINT, GETDVARFLOAT, GETDVARVECTOR, GETDVARCOLORRED,
GETDVARCOLORGREEN, GETDVARCOLORBLUE, GETDVARCOLORALPHA, GETFIRSTARRAYKEY, GETNEXTARRAYKEY,
CLASS, CONSTRUCTOR, DESTRUCTOR, VAR,

HASH, NEWLINE, EOS, DEFINED, MACROBEGIN, MACROEND, MACROARG, MACROVAOPT, MACROVAARGS, STRINGIZE, PASTE
};
Expand Down
Loading

0 comments on commit 039a88f

Please sign in to comment.