Commit fd3a6d6a authored by Tom Niget's avatar Tom Niget

Refactor builtins structure, enhance module emulation system

parent 0b91cf1f
......@@ -12,41 +12,7 @@
using namespace std::literals;
template<typename T>
concept Streamable = requires(const T &x, std::ostream &s) {
{ s << x } -> std::same_as<std::ostream &>;
};
template<Streamable T>
void print_to(const T &x, std::ostream &s) {
s << x;
}
template<typename T>
concept FunctionPointer = std::is_function_v<T>
or std::is_member_function_pointer_v<T>
or std::is_function_v<std::remove_pointer_t<T>>;
template<Streamable T>
requires (FunctionPointer<T>)
void print_to(const T &x, std::ostream &s) {
s << "<function at 0x" << std::hex << (size_t) x << ">";
}
template<typename T>
concept PyPrint = requires(const T &x, std::ostream &s) {
{ x.py_print(s) } -> std::same_as<void>;
};
template<PyPrint T>
void print_to(const T &x, std::ostream &s) {
x.py_print(s);
}
template<typename T>
concept Printable = requires(const T &x, std::ostream &s) {
{ print_to(x, s) } -> std::same_as<void>;
};
template<typename T>
concept PyIterator = requires(T t) {
......@@ -73,9 +39,6 @@ size_t len(const T &t) {
return t.py_len();
}
void print() {
std::cout << '\n';
}
bool is_cpp() {
return true;
......@@ -85,14 +48,8 @@ bool is_cpp() {
#include "builtins/complex.hpp"
#include "builtins/dict.hpp"
#include "builtins/list.hpp"
#include "builtins/print.hpp"
#include "builtins/set.hpp"
#include "builtins/str.hpp"
template<Printable T, Printable ... Args>
void print(T const &head, Args const &... args) {
print_to(head, std::cout);
(((std::cout << ' '), print_to(args, std::cout)), ...);
std::cout << '\n';
}
#endif //TYPON_BUILTINS_HPP
......@@ -7,6 +7,8 @@
#include <ostream>
#include "print.hpp"
template<>
void print_to<bool>(const bool &x, std::ostream &s) {
s << (x ? "True" : "False");
......
......@@ -8,6 +8,8 @@
#include <complex>
#include <ostream>
#include "print.hpp"
using PyComplex = std::complex<double>;
PyComplex operator+(int a, const PyComplex &b) {
......@@ -18,7 +20,8 @@ PyComplex operator-(int a, const PyComplex &b) {
return PyComplex(a) - b;
}
void print_to(const PyComplex &x, std::ostream &s) {
template<>
void print_to<PyComplex>(const PyComplex &x, std::ostream &s) {
if (x.real() == 0) {
s << x.imag() << "j";
} else {
......
//
// Created by Tom on 09/03/2023.
//
#ifndef TYPON_PRINT_HPP
#define TYPON_PRINT_HPP
#include <iostream>
#include <ostream>
template<typename T>
concept Streamable = requires(const T &x, std::ostream &s) {
{ s << x } -> std::same_as<std::ostream &>;
};
template<Streamable T>
void print_to(const T &x, std::ostream &s) {
s << x;
}
template<typename T>
concept FunctionPointer = std::is_function_v<T>
or std::is_member_function_pointer_v<T>
or std::is_function_v<std::remove_pointer_t<T>>;
template<Streamable T>
requires (FunctionPointer<T>)
void print_to(const T &x, std::ostream &s) {
s << "<function at 0x" << std::hex << (size_t) x << ">";
}
template<typename T>
concept PyPrint = requires(const T &x, std::ostream &s) {
{ x.py_print(s) } -> std::same_as<void>;
};
template<PyPrint T>
void print_to(const T &x, std::ostream &s) {
x.py_print(s);
}
template<typename T>
concept Printable = requires(const T &x, std::ostream &s) {
{ print_to(x, s) } -> std::same_as<void>;
};
template<Printable T, Printable ... Args>
void print(T const &head, Args const &... args) {
print_to(head, std::cout);
(((std::cout << ' '), print_to(args, std::cout)), ...);
std::cout << '\n';
}
void print() {
std::cout << '\n';
}
#endif //TYPON_PRINT_HPP
......@@ -7,10 +7,12 @@
#include <iostream>
struct sys_t {
static constexpr auto& stdin = std::cin;
static constexpr auto& stdout = std::cout;
static constexpr auto& stderr = std::cerr;
} sys;
namespace py_sys {
struct sys_t {
static constexpr auto &stdin = std::cin;
static constexpr auto &stdout = std::cout;
static constexpr auto &stderr = std::cerr;
} all;
}
#endif //TYPON_SYS_HPP
# coding: utf-8
from typon import is_cpp
import sys as sis
from sys import stdout as truc
test = (2 + 3) * 4
......
......@@ -387,7 +387,7 @@ class BlockVisitor(NodeVisitor):
yield ""
else:
yield from self.import_module(alias.name)
#raise NotImplementedError(node)
yield f'auto& {alias.asname or alias.name} = py_{alias.name}::all;'
def import_module(self, name: str) -> Iterable[str]:
yield f'#include "python/{name}.hpp"'
......@@ -398,7 +398,7 @@ class BlockVisitor(NodeVisitor):
else:
yield from self.import_module(node.module)
for alias in node.names:
yield f"auto& {alias.asname or alias.name} = {node.module}.{alias.name};"
yield f"auto& {alias.asname or alias.name} = py_{node.module}::all.{alias.name};"
def visit_FunctionDef(self, node: ast.FunctionDef) -> Iterable[str]:
templ, args = self.process_args(node.args)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment