From ee0e9ec30516428d90735c20179b75ef942d4d82 Mon Sep 17 00:00:00 2001 From: Pierre Delaunay Date: Wed, 1 May 2024 00:18:00 -0400 Subject: [PATCH] ubuntu fixes --- src/ast/magic.h | 5 + src/cli/commands/vm.cpp | 3 + src/tide/ast_render_visitor.cpp | 5 + src/tide/convert/to_graph.cpp | 3 + src/utilities/guard.h | 2 +- src/vm/vm.cpp | 178 ++++++++++++++++++++++++++++++++ src/vm/vm.h | 28 ++++- tests/cases.cpp | 5 + tests/cases_vm.cpp | 7 ++ tests/libtest.cpp | 7 +- tests/llvm_test.cpp | 2 +- 11 files changed, 237 insertions(+), 8 deletions(-) diff --git a/src/ast/magic.h b/src/ast/magic.h index a0939c64..708a8f1d 100644 --- a/src/ast/magic.h +++ b/src/ast/magic.h @@ -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 String str(T* const& obj) { if (obj == nullptr) { diff --git a/src/cli/commands/vm.cpp b/src/cli/commands/vm.cpp index 4ca0b534..cb344938 100644 --- a/src/cli/commands/vm.cpp +++ b/src/cli/commands/vm.cpp @@ -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"; diff --git a/src/tide/ast_render_visitor.cpp b/src/tide/ast_render_visitor.cpp index b26b7336..02aa6a15 100644 --- a/src/tide/ast_render_visitor.cpp +++ b/src/tide/ast_render_visitor.cpp @@ -1395,4 +1395,9 @@ void ASTRender::arguments(Arguments& self, int depth) { } } +LY_ReturnType ASTRender::condjump(CondJump_t* n, int depth) { + return false; +} + + } // namespace lython diff --git a/src/tide/convert/to_graph.cpp b/src/tide/convert/to_graph.cpp index 55b3c5d0..f3757864 100644 --- a/src/tide/convert/to_graph.cpp +++ b/src/tide/convert/to_graph.cpp @@ -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(n); diff --git a/src/utilities/guard.h b/src/utilities/guard.h index 34abb823..49e8d34d 100644 --- a/src/utilities/guard.h +++ b/src/utilities/guard.h @@ -25,7 +25,7 @@ Guard 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 struct ElementProxy { diff --git a/src/vm/vm.cpp b/src/vm/vm.cpp index c9cc7712..be08e80d 100644 --- a/src/vm/vm.cpp +++ b/src/vm/vm.cpp @@ -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 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()) { + 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 diff --git a/src/vm/vm.h b/src/vm/vm.h index 45959184..a287931f 100644 --- a/src/vm/vm.h +++ b/src/vm/vm.h @@ -106,13 +106,35 @@ struct VMGen: public BaseVisitor }; -struct VMExec +struct VMExec: public BaseVisitor { - Value execute(Array const& program); + int ic = 0; + Value execute(Array const& program, int entry); Array variable; Array registers; Array 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 ? @@ -132,7 +154,7 @@ inline Array compile(Module* mod) { inline Value eval(Array program) { VMExec eval; - return eval.execute(program); + return eval.execute(program, 0); } } diff --git a/tests/cases.cpp b/tests/cases.cpp index ef54113a..74307148 100644 --- a/tests/cases.cpp +++ b/tests/cases.cpp @@ -238,6 +238,11 @@ Array const& Raise_examples() { return example; } +Array const& CondJump_examples() { + static Array example = {}; + return example; +} + Array const& With_examples() { static Array example = { {"with a as b, c as d:\n" diff --git a/tests/cases_vm.cpp b/tests/cases_vm.cpp index 86ca4cd6..2e78bc27 100644 --- a/tests/cases_vm.cpp +++ b/tests/cases_vm.cpp @@ -1029,4 +1029,11 @@ Array const& Exported_vm_examples() { static Array examples = {}; return examples; } + +Array const& CondJump_vm_examples() { + static Array example = {}; + return example; +} + + } // namespace lython \ No newline at end of file diff --git a/tests/libtest.cpp b/tests/libtest.cpp index 6e1808d4..337edf44 100644 --- a/tests/libtest.cpp +++ b/tests/libtest.cpp @@ -1,5 +1,6 @@ #include "libtest.h" #include "utilities/strings.h" +#include "ast/magic.h" #include #include @@ -72,7 +73,7 @@ Array 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) { @@ -151,13 +152,13 @@ Array 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(); diff --git a/tests/llvm_test.cpp b/tests/llvm_test.cpp index 56720489..d02b72c7 100644 --- a/tests/llvm_test.cpp +++ b/tests/llvm_test.cpp @@ -187,7 +187,7 @@ void run_testcases(String const& name, Array const& cases) { #define GENTEST(name) \ TEMPLATE_TEST_CASE("LLVM_" #name, #name, name) { \ - auto cases = get_test_cases(str(nodekind()), name##_vm_examples()); \ + auto cases = get_test_cases("LLVM", str(nodekind()), name##_vm_examples()); \ run_testcases(str(nodekind()), cases); \ }