1414#include < rift/nodes/unary.hpp>
1515#include < rift/nodes/value.hpp>
1616
17+ #include < rift/optimizer.hpp>
18+
1719namespace rift {
1820
1921 /* * Parser syntax:
@@ -294,12 +296,20 @@ namespace rift {
294296
295297 auto lhs = std::move (comparisons.top ());
296298 comparisons.pop ();
297- comparisons. push (std::make_unique<BinaryNode> (
299+ auto opt = optimizer::tryFoldBinary (
298300 std::move (lhs),
299- type,
300301 std::move (rhs.unwrap ()),
302+ type,
301303 start, m_currentToken.toIndex
302- ));
304+ );
305+ if (opt.isErr ()) {
306+ return geode::Err (CompileError (
307+ std::string (m_lexer.m_source ),
308+ std::move (opt.unwrapErr ()),
309+ start, m_currentToken.toIndex
310+ ));
311+ }
312+ comparisons.push (std::move (opt.unwrap ()));
303313 }
304314
305315 if (comparisons.size () == 1 ) {
@@ -335,12 +345,20 @@ namespace rift {
335345 return rhs;
336346 }
337347
338- return geode::Ok (std::make_unique<BinaryNode> (
348+ auto opt = optimizer::tryFoldBinary (
339349 std::move (res.unwrap ()),
340- op.type ,
341350 std::move (rhs.unwrap ()),
351+ op.type ,
342352 start, m_currentToken.toIndex
343- ));
353+ );
354+ if (opt.isErr ()) {
355+ return geode::Err (CompileError (
356+ std::string (m_lexer.m_source ),
357+ std::move (opt.unwrapErr ()),
358+ start, m_currentToken.toIndex
359+ ));
360+ }
361+ return geode::Ok (std::move (opt.unwrap ()));
344362 }
345363
346364 default : {
@@ -370,12 +388,20 @@ namespace rift {
370388
371389 auto lhs = std::move (terms.top ());
372390 terms.pop ();
373- terms. push (std::make_unique<BinaryNode> (
391+ auto opt = optimizer::tryFoldBinary (
374392 std::move (lhs),
375- type,
376393 std::move (rhs.unwrap ()),
394+ type,
377395 start, m_currentToken.toIndex
378- ));
396+ );
397+ if (opt.isErr ()) {
398+ return geode::Err (CompileError (
399+ std::string (m_lexer.m_source ),
400+ std::move (opt.unwrapErr ()),
401+ start, m_currentToken.toIndex
402+ ));
403+ }
404+ terms.push (std::move (opt.unwrap ()));
379405 }
380406
381407 if (terms.size () == 1 ) {
@@ -410,12 +436,20 @@ namespace rift {
410436
411437 auto lhs = std::move (terms.top ());
412438 terms.pop ();
413- terms. push (std::make_unique<BinaryNode> (
439+ auto opt = optimizer::tryFoldBinary (
414440 std::move (lhs),
415- type,
416441 std::move (rhs.unwrap ()),
442+ type,
417443 start, m_currentToken.toIndex
418- ));
444+ );
445+ if (opt.isErr ()) {
446+ return geode::Err (CompileError (
447+ std::string (m_lexer.m_source ),
448+ std::move (opt.unwrapErr ()),
449+ start, m_currentToken.toIndex
450+ ));
451+ }
452+ terms.push (std::move (opt.unwrap ()));
419453 }
420454
421455 if (terms.size () == 1 ) {
@@ -445,11 +479,19 @@ namespace rift {
445479 return res;
446480 }
447481
448- return geode::Ok (std::make_unique<UnaryNode>(
449- op.type ,
482+ auto opt = optimizer::tryFoldUnary (
450483 std::move (res.unwrap ()),
484+ op.type ,
451485 start, m_currentToken.toIndex
452- ));
486+ );
487+ if (opt.isErr ()) {
488+ return geode::Err (CompileError (
489+ std::string (m_lexer.m_source ),
490+ std::move (opt.unwrapErr ()),
491+ start, m_currentToken.toIndex
492+ ));
493+ }
494+ return geode::Ok (std::move (opt.unwrap ()));
453495 }
454496
455497 default : break ;
@@ -467,12 +509,20 @@ namespace rift {
467509 return rhs;
468510 }
469511
470- return geode::Ok (std::make_unique<BinaryNode> (
512+ auto opt = optimizer::tryFoldBinary (
471513 std::move (res.unwrap ()),
472- TokenType::CARET,
473514 std::move (rhs.unwrap ()),
515+ TokenType::CARET,
474516 start, m_currentToken.toIndex
475- ));
517+ );
518+ if (opt.isErr ()) {
519+ return geode::Err (CompileError (
520+ std::string (m_lexer.m_source ),
521+ std::move (opt.unwrapErr ()),
522+ start, m_currentToken.toIndex
523+ ));
524+ }
525+ return geode::Ok (std::move (opt.unwrap ()));
476526 }
477527
478528 return res;
@@ -618,16 +668,23 @@ namespace rift {
618668 return res;
619669 }
620670
621- auto binary = std::make_unique<BinaryNode> (
671+ auto opt = optimizer::tryFoldBinary (
622672 std::make_unique<IdentifierNode>(name),
623- static_cast <TokenType>(op.type - TokenType::PLUS_ASSIGN + TokenType::PLUS),
624673 std::move (res.unwrap ()),
674+ static_cast <TokenType>(op.type - TokenType::PLUS_ASSIGN + TokenType::PLUS),
625675 name.fromIndex , m_currentToken.toIndex
626676 );
677+ if (opt.isErr ()) {
678+ return geode::Err (CompileError (
679+ std::string (m_lexer.m_source ),
680+ std::move (opt.unwrapErr ()),
681+ name.fromIndex , m_currentToken.toIndex
682+ ));
683+ }
627684
628685 auto assign = std::make_unique<AssignNode>(
629686 std::move (name.value ),
630- std::move (binary ),
687+ std::move (opt. unwrap () ),
631688 name.fromIndex ,
632689 m_currentToken.toIndex
633690 );
0 commit comments