Skip to content

Commit 215bb28

Browse files
committed
fixes inheritance, regex literals, default args, del (#3) :) [merging from test repo]
1 parent b303742 commit 215bb28

21 files changed

Lines changed: 781 additions & 40 deletions

‎shell_lite/ast_nodes.hpp‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct SliceNode; struct DbInsertNode; struct DbQueryNode; struct DbFindNode; st
2222
struct WebListenNode; struct WebRouteNode; struct WebServeNode;
2323
struct NlpAddNode; struct NlpRemoveNode; struct NlpTimerNode;
2424
struct FileWriteNode; struct FileReadNode;
25-
struct NamespaceDecl;
25+
struct NamespaceDecl; struct RegexLiteral; struct DelStmt; struct ParentInitCall;
2626
class Visitor {
2727
public:
2828
virtual ~Visitor() = default;
@@ -84,6 +84,9 @@ class Visitor {
8484
virtual void visit(FileWriteNode* node) = 0;
8585
virtual void visit(FileReadNode* node) = 0;
8686
virtual void visit(NamespaceDecl* node) = 0;
87+
virtual void visit(RegexLiteral* node) = 0;
88+
virtual void visit(DelStmt* node) = 0;
89+
virtual void visit(ParentInitCall* node) = 0;
8790
};
8891

8992
struct Node {
@@ -587,4 +590,29 @@ struct NamespaceDecl : Node {
587590
void accept(Visitor* v) override { v->visit(this); }
588591
};
589592

593+
struct RegexLiteral : Node {
594+
std::string pattern;
595+
std::string flags;
596+
RegexLiteral(std::string p = "", std::string f = "") : pattern(std::move(p)), flags(std::move(f)) {}
597+
std::string node_type() const override { return "RegexLiteral"; }
598+
bool pushes_value_as_statement() const override { return true; }
599+
void accept(Visitor* v) override { v->visit(this); }
600+
};
601+
602+
struct DelStmt : Node {
603+
Node* obj;
604+
Node* key;
605+
DelStmt(Node* o = nullptr, Node* k = nullptr) : obj(o), key(k) {}
606+
std::string node_type() const override { return "DelStmt"; }
607+
void accept(Visitor* v) override { v->visit(this); }
608+
};
609+
610+
struct ParentInitCall : Node {
611+
std::string_view parent_name;
612+
std::vector<Node*> args;
613+
std::string node_type() const override { return "ParentInitCall"; }
614+
bool pushes_value_as_statement() const override { return true; }
615+
void accept(Visitor* v) override { v->visit(this); }
616+
};
617+
590618
}

‎shell_lite/ast_printer.hpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ class AstPrinter : public Visitor {
212212
void visit(FileWriteNode* /*node*/) override { out_ << "{\"type\": \"FileWriteNode\"}"; }
213213
void visit(FileReadNode* /*node*/) override { out_ << "{\"type\": \"FileReadNode\"}"; }
214214
void visit(NamespaceDecl* /*node*/) override { out_ << "{\"type\": \"NamespaceDecl\"}"; }
215+
void visit(RegexLiteral* node) override { out_ << "{\"type\": \"RegexLiteral\", \"pattern\": \"" << escape_string(node->pattern) << "\"}"; }
216+
void visit(DelStmt* /*node*/) override { out_ << "{\"type\": \"DelStmt\"}"; }
217+
void visit(ParentInitCall* /*node*/) override { out_ << "{\"type\": \"ParentInitCall\"}"; }
215218

216219
private:
217220
std::ostream &out_;

‎shell_lite/chunk.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ bool Chunk::verify() const {
232232
case OP_LIST:
233233
case OP_DICT:
234234
case OP_LIST_APPEND:
235+
case OP_SET_DEFAULTS:
236+
case OP_CALL_PARENT_INIT:
235237
case OP_SPAWN: {
236238
if (ip + 1 > code.size()) return false;
237239
ip += 1;
@@ -299,6 +301,8 @@ bool Chunk::verify() const {
299301
case OP_LIST:
300302
case OP_DICT:
301303
case OP_LIST_APPEND:
304+
case OP_SET_DEFAULTS:
305+
case OP_CALL_PARENT_INIT:
302306
case OP_SPAWN: {
303307
ip += 1;
304308
break;

‎shell_lite/chunk.hpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ enum OpCode : uint8_t {
8282
OP_IMPORT,
8383
OP_IN,
8484
OP_NOT_IN,
85+
OP_INHERIT,
86+
OP_REGEX,
87+
OP_DEL,
88+
OP_SET_DEFAULTS,
89+
OP_CALL_PARENT_INIT,
8590
OP_HALT
8691
};
8792

‎shell_lite/compiler.cpp‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

‎shell_lite/gc.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static size_t get_object_size(GCObject* obj) {
4848
case ObjType::UPVALUE: return sizeof(ObjUpvalue);
4949
case ObjType::DATABASE: return sizeof(ObjDatabase);
5050
case ObjType::LOCK: return sizeof(ObjLock);
51+
case ObjType::REGEX: return sizeof(ObjRegex);
5152
default: return sizeof(GCObject);
5253
}
5354
}
@@ -290,6 +291,7 @@ void ObjFunction::mark_children() {
290291
if (chunk) {
291292
for (auto& v : chunk->constants) v.mark();
292293
}
294+
for (auto& v : default_args) v.mark();
293295
}
294296

295297
void ObjClosure::mark_children() {
@@ -310,6 +312,10 @@ void ObjUpvalue::mark_children() {
310312
}
311313

312314
void ObjClass::mark_children() {
315+
if (parent && !parent->marked) {
316+
parent->marked = true;
317+
parent->mark_children();
318+
}
313319
for (auto& pair : methods) {
314320
if (pair.second && !pair.second->marked) {
315321
pair.second->marked = true;

‎shell_lite/gc.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace shell_lite {
1212
class GCArena;
1313

1414
enum class ObjType : uint8_t {
15-
STRING, LIST, DICT, FUNCTION, CLOSURE, CLASS, INSTANCE, MODULE, FILE_OBJ, TASK, CHANNEL, ITERATOR, CALLABLE, UPVALUE, DATABASE, LOCK
15+
STRING, LIST, DICT, FUNCTION, CLOSURE, CLASS, INSTANCE, MODULE, FILE_OBJ, TASK, CHANNEL, ITERATOR, CALLABLE, UPVALUE, DATABASE, LOCK, REGEX
1616
};
1717

1818
static constexpr uint8_t GC_FLAG_SHARED = 1 << 0;

‎shell_lite/lexer.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const std::unordered_map<std::string_view, TokenType> KEYWORDS = {
4040
{"listen", TokenType::TOK_LISTEN}, {"port", TokenType::TOK_PORT}, {"model", TokenType::TOK_MODEL},
4141
{"create", TokenType::TOK_CREATE}, {"table", TokenType::TOK_TABLE}, {"insert", TokenType::TOK_INSERT},
4242
{"find", TokenType::TOK_FIND}, {"update", TokenType::TOK_UPDATE}, {"delete", TokenType::TOK_DELETE},
43+
{"del", TokenType::TOK_DEL},
4344
{"where", TokenType::TOK_WHERE}, {"every", TokenType::TOK_EVERY}, {"minute", TokenType::TOK_MINUTE},
4445
{"minutes", TokenType::TOK_MINUTE}, {"second", TokenType::TOK_SECOND}, {"seconds", TokenType::TOK_SECOND},
4546
{"serve", TokenType::TOK_SERVE},

‎shell_lite/lexer.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ enum class TokenType {
1616
TOK_ON, TOK_DOWNLOAD, TOK_COMPRESS, TOK_EXTRACT, TOK_FOLDER, TOK_LOAD, TOK_SAVE, TOK_CSV, TOK_COPY, TOK_PASTE,
1717
TOK_CLIPBOARD, TOK_PRESS, TOK_TYPE, TOK_CLICK, TOK_AT, TOK_NOTIFY, TOK_AFTER, TOK_BEFORE,
1818
TOK_WAIT, TOK_CONVERT, TOK_JSON, TOK_HTTP, TOK_LISTEN, TOK_PORT, TOK_MODEL, TOK_CREATE, TOK_TABLE,
19-
TOK_INSERT, TOK_FIND, TOK_UPDATE, TOK_DELETE, TOK_WHERE, TOK_EVERY, TOK_MINUTE, TOK_SECOND, TOK_PROGRESS, TOK_BOLD,
19+
TOK_INSERT, TOK_FIND, TOK_UPDATE, TOK_DELETE, TOK_DEL, TOK_WHERE, TOK_EVERY, TOK_MINUTE, TOK_SECOND, TOK_PROGRESS, TOK_BOLD,
2020
TOK_RED, TOK_GREEN, TOK_BLUE, TOK_YELLOW, TOK_CYAN, TOK_MAGENTA, TOK_SERVE, TOK_STATIC, TOK_WRITE, TOK_APPEND,
2121
TOK_READ, TOK_FILE, TOK_DB, TOK_QUERY, TOK_OPEN, TOK_CLOSE, TOK_EXEC, TOK_MIDDLEWARE, TOK_SOMEONE, TOK_VISITS,
2222
TOK_SUBMITS, TOK_START, TOK_SERVER, TOK_FILES, TOK_DEFINE, TOK_PAGE, TOK_CALLED, TOK_USING, TOK_HEADING,

‎shell_lite/objects.cpp‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Value ObjFunction::call(VM* vm, int arg_count) {
7373
void ObjFunction::serialize(std::ostream& out) const {
7474
static constexpr uint32_t SHBC_FILE_MAGIC = 0x43424853; // "SHBC"
7575
static constexpr uint16_t SHBC_VERSION_MAJOR = 1;
76-
static constexpr uint16_t SHBC_VERSION_MINOR = 0;
76+
static constexpr uint16_t SHBC_VERSION_MINOR = 2;
7777

7878
out.write(reinterpret_cast<const char*>(&SHBC_FILE_MAGIC), sizeof(SHBC_FILE_MAGIC));
7979
out.write(reinterpret_cast<const char*>(&SHBC_VERSION_MAJOR), sizeof(SHBC_VERSION_MAJOR));
@@ -91,7 +91,11 @@ void ObjFunction::serialize(std::ostream& out) const {
9191
uint32_t sl = static_cast<uint32_t>(source_file.length());
9292
out.write(reinterpret_cast<const char*>(&sl), sizeof(sl));
9393
if (sl > 0) out.write(source_file.data(), sl);
94-
94+
95+
uint32_t dc = static_cast<uint32_t>(default_args.size());
96+
out.write(reinterpret_cast<const char*>(&dc), sizeof(dc));
97+
for (const auto& d : default_args) serialize_value(out, d);
98+
9599
if (chunk) {
96100
chunk->serialize(out);
97101
} else {
@@ -140,7 +144,13 @@ ObjFunction* ObjFunction::deserialize(std::istream& in, GCArena& arena) {
140144
if (!in) throw std::runtime_error("Unexpected EOF reading source file name");
141145
}
142146
f->source_file = s;
143-
147+
148+
if (minor >= 1) {
149+
uint32_t dc = 0; in.read(reinterpret_cast<char*>(&dc), sizeof(dc));
150+
if (!in || dc > 1024) throw std::runtime_error("Invalid default arg count in bytecode");
151+
for (uint32_t i = 0; i < dc; ++i) f->default_args.push_back(deserialize_value(in, arena));
152+
}
153+
144154
f->chunk = std::unique_ptr<Chunk>(Chunk::deserialize(in, arena));
145155
return f;
146156
}
@@ -251,6 +261,7 @@ GCObject* ObjFunction::clone(GCArena& target, std::unordered_map<GCObject*, GCOb
251261
f->upvalue_count = upvalue_count;
252262
f->name = name;
253263
f->source_file = source_file;
264+
for (const auto& d : default_args) f->default_args.push_back(d.clone_val(target, clones));
254265
if (chunk) f->chunk = std::unique_ptr<Chunk>(chunk->clone(target, clones));
255266
return f;
256267
}
@@ -287,9 +298,11 @@ GCObject* ObjClass::clone(GCArena& target, std::unordered_map<GCObject*, GCObjec
287298
if (clones.count(this)) return clones[this];
288299
auto* c = target.allocate<ObjClass>(name);
289300
clones[this] = c;
301+
if (parent) c->parent = static_cast<ObjClass*>(parent->clone(target, clones));
290302
for (const auto& pair : methods) {
291303
c->methods[pair.first] = static_cast<ObjClosure*>(pair.second->clone(target, clones));
292304
}
305+
for (const auto& f : default_fields) c->default_fields.push_back(f);
293306
return c;
294307
}
295308

0 commit comments

Comments
 (0)