Skip to content

Commit ae22371

Browse files
authored
Merge pull request #4 from Ultimaker/NP-1167
Richer support for python syntax
2 parents fcffc87 + 8b110d4 commit ae22371

33 files changed

Lines changed: 1130 additions & 79 deletions

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
cmake_minimum_required(VERSION 3.26)
22
project(cura-formulae-engine)
33

4+
include(CTest)
5+
46
find_package(standardprojectsettings REQUIRED)
57
option(ENABLE_TESTS "Build with unit test" ON)
68
option(EXTENSIVE_WARNINGS "Build with all warnings" ON)
79
option(ENABLE_APPS "Build apps example" ON)
810

11+
if (ENABLE_TESTS AND NOT BUILD_TESTING)
12+
message(STATUS "ENABLE_TESTS is ON, forcing BUILD_TESTING=ON so CTest can discover tests")
13+
set(BUILD_TESTING ON CACHE BOOL "Enable CTest" FORCE)
14+
endif ()
15+
916
find_package(spdlog REQUIRED)
1017
find_package(lexy REQUIRED)
1118
find_package(zeus_expected REQUIRED)
@@ -59,6 +66,7 @@ set(CURA_FORMULAE_ENGINE__SRC
5966
src/ast/slice_expr.cpp
6067
src/ast/tuple_expr.cpp
6168
src/ast/variable_expr.cpp
69+
src/ast/property_access_expr.cpp
6270
src/parser/parser.cpp
6371
src/ast/binary_expr/binary_expr.cpp
6472
src/ast/binary_expr/add_expr.cpp

conandata.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version: "1.1.0"
1+
version: "1.2.0"

include/cura-formulae-engine/ast/binary_expr/binary_expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct BinaryExpr : Expr
1212
ExprPtr lhs;
1313
ExprPtr rhs;
1414

15-
BinaryExpr(ExprPtr lhs, ExprPtr rhs)
15+
BinaryExpr(ExprPtr&& lhs, ExprPtr&& rhs)
1616
: lhs(std::move(lhs))
1717
, rhs(std::move(rhs))
1818
{

include/cura-formulae-engine/ast/condition_expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct ConditionExpr final : Expr
1212
ExprPtr condition;
1313
ExprPtr else_expr;
1414

15-
ConditionExpr(ExprPtr then_expr, ExprPtr condition, ExprPtr else_expr)
15+
ConditionExpr(ExprPtr&& then_expr, ExprPtr&& condition, ExprPtr&& else_expr)
1616
: then_expr(std::move(then_expr))
1717
, condition(std::move(condition))
1818
, else_expr(std::move(else_expr))

include/cura-formulae-engine/ast/expr_ptr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct ExprPtr final : Expr
1616

1717
std::unique_ptr<Expr> ptr;
1818

19-
ExprPtr(std::unique_ptr<Expr> ptr)
19+
ExprPtr(std::unique_ptr<Expr>&& ptr)
2020
: ptr(std::move(ptr))
2121
{
2222
}

include/cura-formulae-engine/ast/fn_application_expr.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,25 @@ namespace CuraFormulaeEngine::ast
88

99
struct FnApplicationExpr final : Expr
1010
{
11+
struct KeywordArg
12+
{
13+
std::string name;
14+
ExprPtr value;
15+
16+
[[nodiscard]] bool deepEq(const KeywordArg& other) const noexcept
17+
{
18+
return name == other.name && value.deepEq(other.value);
19+
}
20+
};
21+
1122
ExprPtr fn;
1223
std::vector<ExprPtr> args;
24+
std::vector<KeywordArg> kwargs;
1325

14-
FnApplicationExpr(ExprPtr fn, std::vector<ExprPtr> args)
26+
FnApplicationExpr(ExprPtr&& fn, std::vector<ExprPtr>&& args, std::vector<KeywordArg>&& kwargs = {})
1527
: fn(std::move(fn))
1628
, args(std::move(args))
29+
, kwargs(std::move(kwargs))
1730
{
1831
}
1932

include/cura-formulae-engine/ast/index_expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct IndexExpr final : Expr
1919
ExprPtr array;
2020
ExprPtr index;
2121

22-
IndexExpr(ExprPtr array, ExprPtr index)
22+
IndexExpr(ExprPtr&& array, ExprPtr&& index)
2323
: array(std::move(array))
2424
, index(std::move(index))
2525
{

include/cura-formulae-engine/ast/list_comprehension_expr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct ListComprehensionExpr final : Expr
2121
ExprPtr iterable;
2222
std::vector<ExprPtr> conditions;
2323

24-
loop(ExprPtr iterator_key, ExprPtr iterable, std::vector<ExprPtr> conditions)
24+
loop(ExprPtr&& iterator_key, ExprPtr&& iterable, std::vector<ExprPtr>&& conditions)
2525
: iterator_key(std::move(iterator_key))
2626
, iterable(std::move(iterable))
2727
, conditions(std::move(conditions))
@@ -32,7 +32,7 @@ struct ListComprehensionExpr final : Expr
3232
ExprPtr iterator;
3333
std::vector<loop> loops;
3434

35-
ListComprehensionExpr(ExprPtr iterator, std::vector<loop> loops)
35+
ListComprehensionExpr(ExprPtr&& iterator, std::vector<loop>&& loops)
3636
: iterator(std::move(iterator))
3737
, loops(std::move(loops))
3838
{

include/cura-formulae-engine/ast/list_expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct ListExpr final : Expr
1313
{
1414
std::vector<ExprPtr> elements;
1515

16-
ListExpr(std::vector<ExprPtr> elements)
16+
ListExpr(std::vector<ExprPtr>&& elements)
1717
: elements(std::move(elements))
1818
{
1919
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include "cura-formulae-engine/ast/ast.h"
4+
#include "expr_ptr.h"
5+
6+
#include <string>
7+
8+
namespace CuraFormulaeEngine::ast
9+
{
10+
11+
struct PropertyAccessExpr final : Expr
12+
{
13+
ExprPtr object;
14+
std::string property;
15+
16+
PropertyAccessExpr(ExprPtr&& object, std::string&& property)
17+
: object(std::move(object))
18+
, property(std::move(property))
19+
{
20+
}
21+
22+
[[nodiscard]] std::string toString() const noexcept final;
23+
24+
[[nodiscard]] eval::Result evaluate(const env::Environment* environment) const noexcept final;
25+
26+
[[nodiscard]] std::unordered_set<std::string> freeVariables() const noexcept final;
27+
28+
[[nodiscard]] bool deepEq(const Expr& other) const noexcept final;
29+
30+
void visitAll(std::function<void(const Expr&)> visitor) const noexcept final;
31+
};
32+
33+
} // namespace CuraFormulaeEngine::ast

0 commit comments

Comments
 (0)