Skip to content

Commit d3cf14b

Browse files
authored
Merge pull request #98 from Xiddoc/feature-classes
Feature classes
2 parents 11b603e + 42c30a9 commit d3cf14b

27 files changed

+715
-366
lines changed

examples/test_class.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#include <iostream>
3+
4+
void print(auto print_string);
5+
6+
void print(auto print_string) {
7+
std::cout<<print_string<<std::endl;
8+
}
9+
10+
11+
12+
int main() {
13+
/* Transpiled with ComPy */
14+
class TestClass {
15+
public:
16+
int test_field = 0;
17+
TestClass() {
18+
int test_var = 123;
19+
print(test_var);
20+
};
21+
void increment_test() {
22+
test_field += 1;
23+
};
24+
};
25+
TestClass test_instance = TestClass();
26+
print(test_instance.test_field);
27+
test_instance.increment_test();
28+
print(test_instance.test_field);
29+
return 0;
30+
}

examples/test_class.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Testing a class in Python.
3+
"""
4+
5+
6+
# noinspection PyMissingOrEmptyDocstring
7+
class TestClass:
8+
9+
test_field: int = 0
10+
11+
def __init__(self) -> None:
12+
# Initialize new temporary variable
13+
test_var: int = 123
14+
print(test_var)
15+
16+
def increment_test(self) -> None:
17+
self.test_field += 1
18+
19+
20+
# Initialize the class
21+
test_instance: TestClass = TestClass()
22+
23+
# Get a public field
24+
print(test_instance.test_field)
25+
26+
# Execute a public method
27+
test_instance.increment_test()
28+
29+
# The value changed!
30+
print(test_instance.test_field)

src/compiler/Constants.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
"""
44
from _ast import Constant, BinOp, operator, Add, Sub, Mult, AnnAssign, AST, Expr, Name, Call, FunctionDef, arg, \
55
Return, Assign, Module, IfExp, If, cmpop, Eq, Compare, Lt, Gt, BoolOp, NotEq, Or, And, boolop, Div, Mod, FloorDiv, \
6-
Pass, GtE, LtE, While, AugAssign, Break, For
6+
Pass, GtE, LtE, While, AugAssign, Break, For, ClassDef, Attribute
77
from json import dumps
88
from typing import Dict, Type, Any, Callable
99

1010
from src.pyexpressions.abstract.PyExpression import PyExpression
1111
from src.pyexpressions.concrete.PyAnnAssign import PyAnnAssign
1212
from src.pyexpressions.concrete.PyArg import PyArg
1313
from src.pyexpressions.concrete.PyAssign import PyAssign
14+
from src.pyexpressions.concrete.PyAttribute import PyAttribute
1415
from src.pyexpressions.concrete.PyAugAssign import PyAugAssign
1516
from src.pyexpressions.concrete.PyBinOp import PyBinOp
1617
from src.pyexpressions.concrete.PyBoolOp import PyBoolOp
1718
from src.pyexpressions.concrete.PyBreak import PyBreak
1819
from src.pyexpressions.concrete.PyCall import PyCall
20+
from src.pyexpressions.concrete.PyClassDef import PyClassDef
1921
from src.pyexpressions.concrete.PyCompare import PyCompare
2022
from src.pyexpressions.concrete.PyConstant import PyConstant
2123
from src.pyexpressions.concrete.PyExpr import PyExpr
@@ -32,12 +34,14 @@
3234
AST_EXPR_TO_PYEXPR: Dict[Type[AST], Type[PyExpression]] = {
3335
AnnAssign: PyAnnAssign,
3436
Assign: PyAssign,
37+
Attribute: PyAttribute,
3538
AugAssign: PyAugAssign,
3639
arg: PyArg,
3740
BinOp: PyBinOp,
3841
BoolOp: PyBoolOp,
3942
Break: PyBreak,
4043
Call: PyCall,
44+
ClassDef: PyClassDef,
4145
Compare: PyCompare,
4246
Constant: PyConstant,
4347
Expr: PyExpr,

src/pybuiltins/PyPortFunction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class PyPortFunction(PyExpression):
1515
Port a native function or object to Python.
1616
"""
1717

18-
# __func has a type of PyFunctionDef, but you
18+
# __obj has a type of PyFunctionDef, but you
1919
# can't specify it here without a circular import
2020
# error so we will (overwrite) type hint in the constructor instead.
2121
__func: PyFunctionDef
@@ -65,7 +65,7 @@ def get_function_name(self) -> str:
6565
"""
6666
:return: The native name of the ported function.
6767
"""
68-
return self.get_interface_function().get_func_name()
68+
return self.get_interface_function().get_id()
6969

7070
def _transpile(self) -> str:
7171
"""

0 commit comments

Comments
 (0)