Skip to content

Commit

Permalink
prepare for user generated tables
Browse files Browse the repository at this point in the history
  • Loading branch information
meepen committed Jul 8, 2021
1 parent 3a4df93 commit db85c3e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/parser/expressions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ namespace lorelai {
bool accept(visitor &visit, std::shared_ptr<node> &container) override;
string tostring() override;
public:
std::vector<std::pair<std::shared_ptr<node>, std::shared_ptr<node>>> tabledata;
std::vector<std::shared_ptr<node>> arraypart;
std::vector<std::pair<std::shared_ptr<node>, std::shared_ptr<node>>> hashpart;
};


Expand Down
11 changes: 6 additions & 5 deletions src/parser/expressions/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ tableexpression::tableexpression(lexer &lex) {
do {
word = lex.lookahead().value_or("");

if (word == "[") {
if (word == "[") { // TODO: maybe make this a sub-expression?
// `[` exp `]` `=` exp
// consume `[`
lex.read();
Expand All @@ -44,7 +44,7 @@ tableexpression::tableexpression(lexer &lex) {
lex.wasexpected("<expression>", "table constructor");
}

tabledata.push_back(std::make_pair(key, value));
hashpart.push_back(std::make_pair(key, value));
children.push_back(key);
children.push_back(value);
}
Expand Down Expand Up @@ -72,7 +72,7 @@ tableexpression::tableexpression(lexer &lex) {
}

children.push_back(value);
tabledata.push_back(std::make_pair(key, value));
arraypart.push_back(value);
}
}
while (lex.read(",") || lex.read(";"));
Expand All @@ -98,7 +98,7 @@ bool tableexpression::accept(visitor &visit, std::shared_ptr<node> &container) {
}
}

for (auto &pair : tabledata) {
for (auto &pair : hashpart) {
auto firstdeleted = std::find(deleted.begin(), deleted.end(), pair.first);
auto seconddeleted = std::find(deleted.begin(), deleted.end(), pair.second);

Expand All @@ -115,10 +115,11 @@ bool tableexpression::accept(visitor &visit, std::shared_ptr<node> &container) {
}

for (auto &key : keys) {
tabledata.erase(std::remove(tabledata.begin(), tabledata.end(), key), tabledata.end());
hashpart.erase(std::remove(hashpart.begin(), hashpart.end(), key), hashpart.end());
}

for (auto &del : deleted) {
arraypart.erase(std::remove(arraypart.begin(), arraypart.end(), del));
children.erase(std::remove(children.begin(), children.end(), del), children.end());
}

Expand Down
1 change: 1 addition & 0 deletions src/vm/bytecode/expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ std::unordered_map<std::type_index, expressiongenerator> bytecode::expressionmap
ADD(binopexpression),
ADD(unopexpression),
// ADD(functioncallexpression),
ADD(tableexpression),
ADD(indexexpression),
ADD(dotexpression),
ADD(nameexpression),
Expand Down
15 changes: 3 additions & 12 deletions src/vm/proto/src/bytecode.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ syntax = "proto3";

package lorelai.bytecode;

message debugdata {
uint32 linenumber = 1;
uint32 linecolumn = 2;
optional string filename = 3;
optional string note = 4;
}

message instruction {
enum opcode {

Expand Down Expand Up @@ -73,13 +66,11 @@ message instruction {
optional uint32 a = 2;
optional uint32 b = 3;
optional uint32 c = 4;

optional debugdata debug = 5;
}

message tablevalue {
enum valuetype {
NIL = 0;
CONSTANT = 0;
NUMBER = 1;
STRING = 2;
TABLE = 3;
Expand All @@ -89,14 +80,14 @@ message tablevalue {
optional uint32 index = 2;
}

message tablekeyvalue {
message hashpartvalue {
tablevalue key = 1;
tablevalue value = 2;
}

message table {
repeated tablevalue arraypart = 1;
repeated tablevalue keyvaluepart = 2;
repeated hashpartvalue hashpart = 2;
}

message upvaluereference {
Expand Down
4 changes: 4 additions & 0 deletions tests/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local t = {}
t.a = 1

print(t.a)

0 comments on commit db85c3e

Please sign in to comment.