@@ -603,6 +603,16 @@ class ProperCompiler : public Visitor {
603603 // spin up sub-compiler for function body and emit closure
604604 void visit (FunctionDef *node) override {
605605 update_loc (node);
606+ bool seen_default = false ;
607+ for (auto &arg : node->args ) {
608+ bool has_default = std::get<1 >(arg) != nullptr ;
609+ if (has_default) {
610+ seen_default = true ;
611+ } else if (seen_default) {
612+ throw CompileError (" default arguments must be trailing" ,
613+ {source_file, current_line, current_col});
614+ }
615+ }
606616 CompilerState *sub =
607617 new CompilerState (state, std::string (node->name ), source_file, vm);
608618 CompilerState *old = state;
@@ -639,6 +649,18 @@ class ProperCompiler : public Visitor {
639649 emit_short (uv.index );
640650 }
641651
652+ int default_count = 0 ;
653+ for (auto &arg : node->args ) {
654+ if (std::get<1 >(arg) != nullptr ) {
655+ std::get<1 >(arg)->accept (this );
656+ default_count++;
657+ }
658+ }
659+ if (default_count > 0 ) {
660+ emit_byte (OP_SET_DEFAULTS );
661+ emit_byte ((uint8_t )default_count);
662+ }
663+
642664 if (!compiling_method) {
643665 std::string fn_name =
644666 current_namespace.empty ()
@@ -1132,10 +1154,43 @@ class ProperCompiler : public Visitor {
11321154 emit_byte (OP_METHOD );
11331155 emit_short (make_string_constant (std::string (m->name )));
11341156 }
1157+ if (node->parent ) {
1158+ emit_byte (OP_GET_GLOBAL );
1159+ emit_short (make_string_constant (std::string (*node->parent )));
1160+ emit_byte (OP_INHERIT );
1161+ }
11351162 emit_byte (OP_DEFINE_GLOBAL );
11361163 emit_short (make_string_constant (std::string (node->name )));
11371164 }
11381165
1166+ void visit (RegexLiteral *node) override {
1167+ update_loc (node);
1168+ emit_string_constant (node->pattern );
1169+ emit_string_constant (node->flags );
1170+ emit_byte (OP_REGEX );
1171+ }
1172+
1173+ void visit (DelStmt *node) override {
1174+ update_loc (node);
1175+ node->obj ->accept (this );
1176+ node->key ->accept (this );
1177+ emit_byte (OP_DEL );
1178+ }
1179+
1180+ void visit (ParentInitCall *node) override {
1181+ update_loc (node);
1182+ emit_byte (OP_GET_GLOBAL );
1183+ emit_short (make_string_constant (std::string (node->parent_name )));
1184+ if (node->args .size () > 254 ) {
1185+ throw CompileError (" Too many arguments for super() call (max 254)" ,
1186+ {source_file, current_line, current_col});
1187+ }
1188+ for (auto *arg_node : node->args )
1189+ arg_node->accept (this );
1190+ emit_byte (OP_CALL_PARENT_INIT );
1191+ emit_byte ((uint8_t )node->args .size ());
1192+ }
1193+
11391194 // new class instance and call constructor
11401195 void visit (Instantiation *node) override {
11411196 update_loc (node);
0 commit comments