Skip to content

Commit

Permalink
Refactor VM tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Delaunay committed Apr 21, 2024
1 parent 2abfc8e commit 53dff64
Show file tree
Hide file tree
Showing 31 changed files with 1,707 additions and 541 deletions.
65 changes: 65 additions & 0 deletions code/vm/VM_FunctionDef.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# >>> case: 0
# >>> code
def fun(a: i32) -> i32:
return a
# <<<


# >>> call
fun(1)# <<<


# >>> expected
1# <<<


# >>> case: 1
# >>> code
def fun(a: i32) -> i32:
return a + 1
# <<<


# >>> call
fun(1)# <<<


# >>> expected
2# <<<


# >>> case: 2
# >>> code
def fun(a: i32, b: i32) -> i32:
b += 1
if a == 0:
return b
return fun(a - 1, b)
# <<<


# >>> call
fun(10, 0)# <<<


# >>> expected
11# <<<


# >>> case: 3
# >>> code
def fun(a: i32) -> i32:
if a == 0:
return 0
return fun(a - 1) + 1
# <<<


# >>> call
fun(10)# <<<


# >>> expected
10# <<<


13 changes: 13 additions & 0 deletions code/vm/dummy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# >>> case: 0
# >>> code
from nmodule import Point# <<<


# >>> call
Point(1, 2).add(Point(1, 2)).y# <<<


# >>> expected
4# <<<


1 change: 0 additions & 1 deletion src/dependencies/formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ struct fmt::formatter<lython::StringRef>: formatter<string_view> {
}
};


template <>
struct fmt::formatter<lython::NodeKind>: formatter<string_view> {
auto format(lython::NodeKind c, format_context& ctx) const {
Expand Down
14 changes: 11 additions & 3 deletions src/sema/sema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1601,10 +1601,18 @@ TypeExpr* SemanticAnalyser::annassign(AnnAssign* n, int depth) {
return constraint;
}
TypeExpr* SemanticAnalyser::forstmt(For* n, int depth) {
auto* iter_t = exec(n->iter, depth);
// This assume a function call
// type could be an object with a __iter__ + __next__
auto* iter_return_t = exec(n->iter, depth);

// TODO: use iter_t to set target types
exec_with_ctx(ExprContext::Store, n->target, depth);
// exec_with_ctx(ExprContext::Store, n->target, depth);

// Handle unpacking here
if (Name* name = cast<Name>(n->target)) {
add_name(name, nullptr, iter_return_t);
}

// kwdebug(semalog, "iter type: {}: {}", str(n->iter), str(iter_t));

// TODO: check consistency of return types
auto return_t1 = exec<TypeExpr*>(n->body, depth);
Expand Down
32 changes: 26 additions & 6 deletions src/utilities/guard.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,40 @@

namespace lython {
// Execute function upon destruction
template <typename Exit>
template <typename Exit, typename... Args>
struct Guard {
Guard(Exit fun): on_exit(fun) {}
Guard(Exit fun, Args... args): fun_args(std::make_tuple(args...)), on_exit(fun) {}

~Guard() { on_exit(); }
~Guard() {
std::apply(on_exit, fun_args);
}

Tuple<Args...> fun_args;
Exit on_exit;
};

template <typename Exit>
Guard<Exit> guard(Exit fun) {
return Guard(fun);
template <typename Exit, typename... Args>
Guard<Exit, Args...> guard(Exit fun, Args... args) {
return Guard<Exit, Args...>(fun, args...);
}

#define KW_STR_(x, y) x ## y
#define KW_STR(x, y) KW_STR_(x, y)
#define KW_IDT(name) KW_STR(name, __LINE__)

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

template<typename T>
struct ElementProxy {
int i;
Array<T>* holder;

T& operator->() {
return (*holder)[i];
}
};

template <typename T, typename U>
struct PopGuard {
PopGuard(T& array, U const& v): array(array), oldsize(array.size()) { array.push_back(v); }
Expand Down
20 changes: 20 additions & 0 deletions src/utilities/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ String strip(String const& v) {
return result;
}


String strip_repeating(char c, String const& s)
{
String cleaned;
cleaned.reserve(s.size());
int repeating = 0;
for(auto elem: s) {
if (elem == c) {
repeating += 1;
} else {
repeating = 0;
}
if (repeating <= 1) {
cleaned.push_back(elem);
}
}
return cleaned;
}


Array<String> split(char sep, String const& text) {
int count = 1;
for (auto c: text) {
Expand Down
Loading

0 comments on commit 53dff64

Please sign in to comment.