Skip to content

Commit

Permalink
ubuntu fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Delaunay committed May 1, 2024
1 parent aac6827 commit ee0e9ec
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/ast/magic.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ String str(T const& obj) {
return ss.str();
}

inline
String str(std::string const& s) {
return String(s.c_str());
}

template <typename T>
String str(T* const& obj) {
if (obj == nullptr) {
Expand Down
3 changes: 3 additions & 0 deletions src/cli/commands/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ int VMCmd::main(argparse::ArgumentParser const& args)
}


VMExec exec;
exec.execute(gen.program, 0);

//
// std::cout << "\nExec\n";
// std::cout << "====\n";
Expand Down
5 changes: 5 additions & 0 deletions src/tide/ast_render_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1395,4 +1395,9 @@ void ASTRender::arguments(Arguments& self, int depth) {
}
}

LY_ReturnType ASTRender::condjump(CondJump_t* n, int depth) {
return false;
}


} // namespace lython
3 changes: 3 additions & 0 deletions src/tide/convert/to_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ using PatRet = ToGraph::PatRet;
ExprRet ToGraph::exported(Exported_t* n, int depth) {
return nullptr;
}
ExprRet ToGraph::condjump(CondJump_t* n, int depth) {
return nullptr;
}

ExprRet ToGraph::boolop(BoolOp_t* n, int depth) {
GraphNodeBase* graph = new_object<GraphNode>(n);
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/guard.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Guard<Exit, Args...> guard(Exit fun, Args... args) {
#define KW_IDT(name) KW_STR(name, __LINE__)

#define KW_DEFERRED(fun, ...) \
auto KW_IDT(_) = guard(fun, __VA_ARGS__);
auto KW_IDT(_) = guard(fun __VA_OPT__(,) __VA_ARGS__);

template<typename T>
struct ElementProxy {
Expand Down
178 changes: 178 additions & 0 deletions src/vm/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,182 @@ ModRet VMGen::interactive(Interactive_t* n, int depth) { return ModRet(); }
ModRet VMGen::functiontype(FunctionType_t* n, int depth) { return ModRet(); }
ModRet VMGen::expression(Expression_t* n, int depth) { return ModRet(); }


//

Value VMExec::execute(Array<Instruction> const& program, int entry) {
ic = entry;

while (true) {
if (ic >= program.size() || ic < 0) {
return Value();
}

Instruction const& inst = program[ic];
exec(inst.stmt, 0);
ic += 1;
}
}


ExprRet VMExec::boolop(BoolOp_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::namedexpr(NamedExpr_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::compare(Compare_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::binop(BinOp_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::unaryop(UnaryOp_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::lambda(Lambda_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::ifexp(IfExp_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::dictexpr(DictExpr_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::setexpr(SetExpr_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::listcomp(ListComp_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::generateexpr(GeneratorExp_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::setcomp(SetComp_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::dictcomp(DictComp_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::await(Await_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::yield(Yield_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::yieldfrom(YieldFrom_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::call(Call_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::joinedstr(JoinedStr_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::formattedvalue(FormattedValue_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::constant(Constant_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::attribute(Attribute_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::subscript(Subscript_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::starred(Starred_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::name(Name_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::listexpr(ListExpr_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::tupleexpr(TupleExpr_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::slice(Slice_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::dicttype(DictType_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::arraytype(ArrayType_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::arrow(Arrow_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::builtintype(BuiltinType_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::tupletype(TupleType_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::settype(SetType_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::classtype(ClassType_t* n, int depth) { return ExprRet(); }
ExprRet VMExec::comment(Comment_t* n, int depth) { return ExprRet(); }

// Leaves
StmtRet VMExec::invalidstmt(InvalidStatement_t* n, int depth) {
kwerror(outlog(), "Invalid statement");
return StmtRet();
}
StmtRet VMExec::returnstmt(Return_t* n, int depth) {
return StmtRet();
}
StmtRet VMExec::deletestmt(Delete_t* n, int depth) {
return StmtRet();
}
StmtRet VMExec::assign(Assign_t* n, int depth) {
return StmtRet();
}
StmtRet VMExec::augassign(AugAssign_t* n, int depth) {
return StmtRet();
}
StmtRet VMExec::annassign(AnnAssign_t* n, int depth) {
return StmtRet();
}
StmtRet VMExec::exprstmt(Expr_t* n, int depth) {
//
return StmtRet();
}
StmtRet VMExec::pass(Pass_t* n, int depth) {
return StmtRet();
}
StmtRet VMExec::breakstmt(Break_t* n, int depth) {
return StmtRet();
}
StmtRet VMExec::continuestmt(Continue_t* n, int depth) {
return StmtRet();
}
StmtRet VMExec::assertstmt(Assert_t* n, int depth) {
return StmtRet();
}
StmtRet VMExec::raise(Raise_t* n, int depth) {
// this is an implicit jump out to an unknown location
//

return StmtRet();
}
StmtRet VMExec::global(Global_t* n, int depth) {
return StmtRet();
}
StmtRet VMExec::nonlocal(Nonlocal_t* n, int depth) {
return StmtRet();
}

StmtRet VMExec::condjump(CondJump_t* n, int depth) {
Value val = exec(n->condition, depth);
ic = n->then_jmp - 1;
if (val.as<bool>()) {
ic = n->else_jmp - 1;
}
return StmtRet();
}

StmtRet VMExec::import(Import_t* n, int depth) {
return StmtRet();
}
StmtRet VMExec::importfrom(ImportFrom_t* n, int depth) {
return StmtRet();
}

StmtRet VMExec::inlinestmt(Inline_t* n, int depth) {
return StmtRet();
}
StmtRet VMExec::functiondef(FunctionDef_t* n, int depth) {
return StmtRet();
}
StmtRet VMExec::classdef(ClassDef_t* n, int depth) {
return StmtRet();
}

StmtRet VMExec::forstmt(For_t* n, int depth) {
return StmtRet();
}

StmtRet VMExec::whilestmt(While_t* n, int depth) {
return StmtRet();
}

StmtRet VMExec::ifstmt(If_t* n, int depth) {
return StmtRet();
}

StmtRet VMExec::with(With_t* n, int depth) {
return StmtRet();
}
StmtRet VMExec::trystmt(Try_t* n, int depth) {
return StmtRet();
}

StmtRet VMExec::exported(Exported_t* n, int depth) {
return StmtRet();
}

StmtRet VMExec::placeholder(Placeholder_t* n, int depth) {
return StmtRet();
}

StmtRet VMExec::match(Match_t* n, int depth) { return StmtRet(); }

PatRet VMExec::matchvalue(MatchValue_t* n, int depth) { return PatRet(); }
PatRet VMExec::matchsingleton(MatchSingleton_t* n, int depth) { return PatRet(); }
PatRet VMExec::matchsequence(MatchSequence_t* n, int depth) { return PatRet(); }
PatRet VMExec::matchmapping(MatchMapping_t* n, int depth) { return PatRet(); }
PatRet VMExec::matchclass(MatchClass_t* n, int depth) { return PatRet(); }
PatRet VMExec::matchstar(MatchStar_t* n, int depth) { return PatRet(); }
PatRet VMExec::matchas(MatchAs_t* n, int depth) { return PatRet(); }
PatRet VMExec::matchor(MatchOr_t* n, int depth) { return PatRet(); }

ModRet VMExec::module(Module_t* n, int depth) {
for(auto* stmt: n->body) {
exec(stmt, depth);
}
return ModRet();
};
ModRet VMExec::interactive(Interactive_t* n, int depth) { return ModRet(); }
ModRet VMExec::functiontype(FunctionType_t* n, int depth) { return ModRet(); }
ModRet VMExec::expression(Expression_t* n, int depth) { return ModRet(); }


} // namespace lython
28 changes: 25 additions & 3 deletions src/vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,35 @@ struct VMGen: public BaseVisitor<VMGen, false, VMGenTrait>
};


struct VMExec
struct VMExec: public BaseVisitor<VMExec, false, VMGenTrait>
{
Value execute(Array<Instruction> const& program);
int ic = 0;
Value execute(Array<Instruction> const& program, int entry);

Array<Value> variable;
Array<Value> registers;
Array<lython::StackTrace> stacktrace;

#define FUNCTION_GEN(name, fun) Value fun(name##_t* n, int depth);

#define X(name, _)
#define SSECTION(name)
#define MOD(name, fun) FUNCTION_GEN(name, fun)
#define EXPR(name, fun) FUNCTION_GEN(name, fun)
#define STMT(name, fun) FUNCTION_GEN(name, fun)
#define MATCH(name, fun) FUNCTION_GEN(name, fun)

NODEKIND_ENUM(X, SSECTION, EXPR, STMT, MOD, MATCH)

#undef X
#undef SSECTION
#undef EXPR
#undef STMT
#undef MOD
#undef MATCH

#undef FUNCTION_GEN

};

// Assemble programs together ?
Expand All @@ -132,7 +154,7 @@ inline Array<Instruction> compile(Module* mod) {

inline Value eval(Array<Instruction> program) {
VMExec eval;
return eval.execute(program);
return eval.execute(program, 0);
}

}
Expand Down
5 changes: 5 additions & 0 deletions tests/cases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ Array<TestCase> const& Raise_examples() {
return example;
}

Array<TestCase> const& CondJump_examples() {
static Array<TestCase> example = {};
return example;
}

Array<TestCase> const& With_examples() {
static Array<TestCase> example = {
{"with a as b, c as d:\n"
Expand Down
7 changes: 7 additions & 0 deletions tests/cases_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,4 +1029,11 @@ Array<VMTestCase> const& Exported_vm_examples() {
static Array<VMTestCase> examples = {};
return examples;
}

Array<VMTestCase> const& CondJump_vm_examples() {
static Array<TestCase> example = {};
return example;
}


} // namespace lython
7 changes: 4 additions & 3 deletions tests/libtest.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "libtest.h"
#include "utilities/strings.h"
#include "ast/magic.h"

#include <regex>
#include <iostream>
Expand Down Expand Up @@ -72,7 +73,7 @@ Array<VMTestCase> load_cases(std::istream& in) {
int i = 0;
VMTestCase currentcase{"", ""};
CaseSection section = CaseSection::None;
String line;
std::string line;

auto add_case = [&]() {
if (i > 0) {
Expand Down Expand Up @@ -151,13 +152,13 @@ Array<VMTestCase> load_cases(std::istream& in) {
}

if (std::regex_match(line, match, content_regex)) {
buffer.push_back(String(match[1].str().c_str()));
buffer.push_back(str(match[1].str()));
push();
section = CaseSection::None;
continue;
}

buffer.push_back(line);
buffer.push_back(str(line));
}
push();
add_case();
Expand Down
2 changes: 1 addition & 1 deletion tests/llvm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void run_testcases(String const& name, Array<VMTestCase> const& cases) {

#define GENTEST(name) \
TEMPLATE_TEST_CASE("LLVM_" #name, #name, name) { \
auto cases = get_test_cases(str(nodekind<TestType>()), name##_vm_examples()); \
auto cases = get_test_cases("LLVM", str(nodekind<TestType>()), name##_vm_examples()); \
run_testcases(str(nodekind<TestType>()), cases); \
}

Expand Down

0 comments on commit ee0e9ec

Please sign in to comment.