Skip to content

Commit

Permalink
Lang: Make comparisons a bit more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Oct 24, 2024
1 parent 4e47453 commit 5e065c8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
9 changes: 5 additions & 4 deletions src/lang/lang.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ inline const TokenDef Compile{"compile"};

namespace verona::wf
{
inline const auto cond = Eq | Neq;
inline const auto lv = Ident | Lookup;
inline const auto rv = lv | Empty | Null | String | Call | Method | Take;
inline const auto rv =
lv | Empty | Null | String | Call | Method | Take | cond;
inline const auto cmp_values = Ident | Lookup | Null | Call | Method;
inline const auto key = Ident | Lookup | String;
inline const auto operand = Lookup | Call | Method | Ident;
inline const auto cond = Eq | Neq;

inline const auto grouping = (Top <<= File) | (File <<= Body) |
(Body <<= Block) |
Expand Down Expand Up @@ -63,13 +64,13 @@ namespace verona::wf
(Func <<= Body) | (Label <<= Ident)[Ident];
}

inline const auto COND = T(Eq, Neq);
inline const auto LV = T(Ident, Lookup);
inline const auto RV =
T(Empty, Ident, Lookup, Null, String, Call, Method, Take);
T(Empty, Ident, Lookup, Null, String, Call, Method, Take, Eq, Neq);
inline const auto CMP_V = T(Ident, Lookup, Null, Call, Method);
inline const auto KEY = T(Ident, Lookup, String);
inline const auto OPERAND = T(Lookup, Call, Method, Ident);
inline const auto COND = T(Eq, Neq);

// Parsing && AST construction
Parse parser();
Expand Down
9 changes: 5 additions & 4 deletions src/lang/passes/grouping.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ PassDef grouping()
((T(Group) << (RV[Rhs] * End)) / (RV[Rhs] * End)) * End) >>
[](auto& _) { return Assign << _[Lhs] << _[Rhs]; },

// Normalize `if x:` -> `if x == True`
In(If, While) * ((T(Group) << End) * (!COND)[Op]) >>
[](auto& _) {
return Seq << Group << (Eq << _(Op) << (Ident ^ "True"));
},
COND[Op] << (Any[Lhs] * (T(Group) << CMP_V[Rhs] * End) * End) >>
[](auto& _) {
return create_from(_(Op)->type(), _(Op)) << _[Lhs] << _[Rhs];
Expand Down Expand Up @@ -97,10 +102,6 @@ PassDef grouping()
return create_from(For, _(For))
<< _(Key) << _(Value) << _(Op) << _(Block);
},
In(While) * ((T(Group) << End) * (!COND)[Op]) >>
[](auto& _) {
return Seq << Group << (Eq << _(Op) << (Ident ^ "True"));
},
(T(While) << (T(Group) * COND[Op] * (T(Group) << T(Block)[Block]))) >>
[](auto& _) { return While << _(Op) << _(Block); },

Expand Down
7 changes: 4 additions & 3 deletions src/lang/passes/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ namespace verona::wf
Group | Assign | If | Else | Block | For | Func | List | Return | While;

inline const auto parser = (Top <<= File) | (File <<= parse_groups++) |
(Assign <<= Group++) | (If <<= Group * (Op >>= (cond | Group)) * Group) |
(Assign <<= Group * (Lhs >>= (Group | cond))) |
(If <<= Group * (Op >>= (cond | Group)) * Group) |
(Else <<= Group * Group) | (Group <<= (parse_tokens | Block | List)++) |
(Block <<= (parse_tokens | parse_groups)++) | (Eq <<= Group * Group) |
(Neq <<= Group * Group) | (Lookup <<= Group) |
(For <<= Group * List * Group * Group) |
(While <<= Group * (Op >>= (cond | Group)) * Group) | (List <<= Group++) |
(Parens <<= (Group | List)++) | (Func <<= Group * Group * Group) |
(Return <<= Group++);
(Return <<= (Group | cond)++);
}

struct Indent
Expand All @@ -34,7 +35,7 @@ trieste::Parse parser()
indent->push_back({0, File});

auto update_indent = [indent](detail::Make& m, size_t this_indent) {
m.term({Assign, Return});
m.term({Eq, Neq, Assign, Return});

if (this_indent > indent->back().indent)
{
Expand Down
11 changes: 10 additions & 1 deletion tests/exprs/ifs.vpy
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Build a region
a = {}
a["self"] = a
Expand All @@ -21,5 +20,15 @@ else:
d = {}
drop d

cond = f.a != a
if cond:
dummy = "created"

def check():
c = False
return c == True
if check():
this = "is false"

drop a
drop f

0 comments on commit 5e065c8

Please sign in to comment.