Skip to content

Commit

Permalink
Func: Add print and ignore return on stmts
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Oct 1, 2024
1 parent 963d7c1 commit b384aad
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/lang/bytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ inline const trieste::TokenDef Neq{"!="};
/// Stack: <arg_0>, <arg_1>, <arg_2>, <func_obj>
/// For `function(a, b, c)` the stack would be: `a, b, c, function`
inline const trieste::TokenDef Call{"call"};
/// This clears any potentual values from the current stack.
inline const trieste::TokenDef ClearStack{"clear_stack"};
/// Unconditional Jump
inline const trieste::TokenDef Jump{"jump", trieste::flag::print};
/// Jump if the current stack frame is `False`
Expand Down
11 changes: 11 additions & 0 deletions src/lang/interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,17 @@ std::tuple<bool, std::optional<trieste::Location>> run_stmt(trieste::Node& node,
return {false, {}};
}

if (node == ClearStack) {
if (!stack.empty()) {
std::cout << "clearning " << stack.size() << " objects from the stack" << std::endl;
while (!stack.empty()) {
remove_reference(objects::get_frame(), stack.back());
stack.pop_back();
}
}
return {false, {}};
}

std::cerr << "unhandled bytecode" << std::endl;
node->str(std::cerr);
std::abort();
Expand Down
50 changes: 38 additions & 12 deletions src/lang/lang.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ inline const TokenDef Lhs{"lhs"};
inline const TokenDef Key{"key"};
inline const TokenDef Value{"value"};

inline const Node RetrunIdent = Ident ^ "return";

namespace verona::wf {
using namespace trieste::wf;

Expand All @@ -43,13 +41,13 @@ inline const auto parser =
| (Assign <<= Group++)
| (If <<= Group * Eq * Group)
| (Else <<= Group * Group)
| (Group <<= (parse_tokens | Block)++)
| (Group <<= (parse_tokens | Block | List)++)
| (Block <<= (parse_tokens | parse_groups)++)
| (Eq <<= Group * Group)
| (Lookup <<= Group)
| (For <<= Group * List * Group * Group)
| (List <<= Group++)
| (Parens <<= List++)
| (Parens <<= (Group | List)++)
| (Func <<= Group * Group * Group)
| (Return <<= Group * Group)
;
Expand Down Expand Up @@ -78,6 +76,9 @@ inline const auto grouping =
| (List <<= rv++)
| (Params <<= Ident++)
;
inline const auto stmt_prints =
grouping
| (Block <<= (Freeze | Region | Assign | If | For | Func | Return | Call | ClearStack | Print)++);
} // namespace verona::wf

struct Indent {
Expand Down Expand Up @@ -155,9 +156,8 @@ trieste::Parse parser() {
m.push(Parens);
},
"\\)" >> [](auto &m) {
m.term({List});
m.extend_before(Parens);
m.term({Parens});
m.term({List, Parens});
m.extend(Parens);
},
"return" >> [](auto &m) {
m.seq(Return);
Expand Down Expand Up @@ -390,6 +390,29 @@ PassDef grouping() {
return p;
}

PassDef stmt_prints() {
PassDef p{
"stmt_prints",
verona::wf::stmt_prints,
dir::bottomup | dir::once,
{
In(Block) * T(Call)[Call] >>
[](auto &_) {
return Seq
<< _(Call)
<< ClearStack
<< create_print(_(Call));
},
}
};

return p;
}

Node return_ident() {
return Ident ^ "return";
}

inline const TokenDef Compile{"compile"};

namespace verona::wf {
Expand All @@ -399,7 +422,8 @@ inline const trieste::wf::Wellformed flatten =
| (File <<= Body)
| (Body <<= (Freeze | Region | Assign | Eq | Neq | Label | Jump | JumpFalse |
Print | StoreFrame | LoadFrame | CreateObject | Ident | IterNext |
Create | StoreField | Lookup | String | Call | PushFrame | PopFrame)++)
Create | StoreField | Lookup | String | Call | PushFrame | PopFrame |
ClearStack)++)
| (CreateObject <<= (KeyIter | String | Dictionary | Func))
| (Func <<= Compile)
| (Compile <<= Body)
Expand Down Expand Up @@ -536,7 +560,7 @@ PassDef flatten() {
auto block = _[Block];
for (auto stmt : block) {
if (stmt == Return) {
body << (create_from(Assign, stmt) << RetrunIdent << stmt->at(0));
body << (create_from(Assign, stmt) << return_ident() << stmt->at(0));
body << (Jump ^ return_label);
} else {
body << stmt;
Expand Down Expand Up @@ -565,7 +589,8 @@ using namespace trieste::wf;
inline const trieste::wf::Wellformed bytecode =
empty | (Body <<= (LoadFrame | StoreFrame | LoadField | StoreField | Drop | Null |
CreateObject | CreateRegion | FreezeObject | IterNext | Print |
Eq | Neq | Jump | JumpFalse | Label | Call | PushFrame | PopFrame)++)
Eq | Neq | Jump | JumpFalse | Label | Call | PushFrame | PopFrame |
ClearStack)++)
| (CreateObject <<= (Dictionary | String | KeyIter | Proto | Func))
| (Top <<= Body)
| (Func <<= Body)
Expand Down Expand Up @@ -621,7 +646,8 @@ PassDef bytecode() {
// The node doesn't require additional processing and should be copied
T(Compile) << T(
Null, Label, Print, Jump, JumpFalse, CreateObject,
StoreFrame, LoadFrame, IterNext, StoreField, PushFrame, PopFrame)[Op] >>
StoreFrame, LoadFrame, IterNext, StoreField, PushFrame,
PopFrame, ClearStack)[Op] >>
[](auto &_) -> Node { return _(Op); },

T(Compile) << (T(Eq, Neq)[Op] << (Any[Lhs] * Any[Rhs])) >>
Expand Down Expand Up @@ -737,7 +763,7 @@ struct CLIOptions : trieste::Options
int load_trieste(int argc, char **argv) {
CLIOptions options;
auto [extract_bytecode, result] = extract_bytecode_pass();
trieste::Reader reader{"verona_dyn", {grouping(), flatten(), bytecode(), extract_bytecode}, parser()};
trieste::Reader reader{"verona_dyn", {grouping(), stmt_prints(), flatten(), bytecode(), extract_bytecode}, parser()};
trieste::Driver driver{reader, &options};
auto build_res = driver.run(argc, argv);

Expand Down
12 changes: 10 additions & 2 deletions tests/funcs.vpy
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ def no_args():
a = "string value"
return a

def b_literal():
return "B"

_ = no_args()
# Store the return value
value = no_args()
drop value

three_args("A", "B", "C")
# Ignore the return value
no_args()

three_args("A", b_literal(), "C")

a = {}
drop a

0 comments on commit b384aad

Please sign in to comment.