authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-11-29 14:46:05-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-11-29 14:46:05-07:00
log9a014b52ccc22d7ec24804f90993cfe08e3d20c4
treee7e90f22ed9ad940cb3c22b6d02d70bcb2b7dd96
parent4466a4533c0f1ba33143bfead7ba99910d331531

flatten expression ast to hide operator precedence


3 files changed, 230 insertions(+), 421 deletions(-)

src/codegen.cpp+94-132
...@@ -305,17 +305,9 @@ static void find_declarations(CodeGen *g, AstNode *node) {...@@ -305,17 +305,9 @@ static void find_declarations(CodeGen *g, AstNode *node) {
305 case NodeTypeReturnExpr:305 case NodeTypeReturnExpr:
306 case NodeTypeRoot:306 case NodeTypeRoot:
307 case NodeTypeBlock:307 case NodeTypeBlock:
308 case NodeTypeBoolOrExpr:308 case NodeTypeBinOpExpr:
309 case NodeTypeFnCall:309 case NodeTypeFnCall:
310 case NodeTypeRootExportDecl:310 case NodeTypeRootExportDecl:
311 case NodeTypeBoolAndExpr:
312 case NodeTypeComparisonExpr:
313 case NodeTypeBinOrExpr:
314 case NodeTypeBinXorExpr:
315 case NodeTypeBinAndExpr:
316 case NodeTypeBitShiftExpr:
317 case NodeTypeAddExpr:
318 case NodeTypeMultExpr:
319 case NodeTypeCastExpr:311 case NodeTypeCastExpr:
320 case NodeTypePrimaryExpr:312 case NodeTypePrimaryExpr:
321 case NodeTypeGroupedExpr:313 case NodeTypeGroupedExpr:
...@@ -481,10 +473,9 @@ static void analyze_node(CodeGen *g, AstNode *node) {...@@ -481,10 +473,9 @@ static void analyze_node(CodeGen *g, AstNode *node) {
481 analyze_node(g, node->data.return_expr.expr);473 analyze_node(g, node->data.return_expr.expr);
482 }474 }
483 break;475 break;
484 case NodeTypeBoolOrExpr:476 case NodeTypeBinOpExpr:
485 analyze_node(g, node->data.bool_or_expr.op1);477 analyze_node(g, node->data.bin_op_expr.op1);
486 if (node->data.bool_or_expr.op2)478 analyze_node(g, node->data.bin_op_expr.op2);
487 analyze_node(g, node->data.bool_or_expr.op2);
488 break;479 break;
489 case NodeTypeFnCall:480 case NodeTypeFnCall:
490 {481 {
...@@ -515,30 +506,6 @@ static void analyze_node(CodeGen *g, AstNode *node) {...@@ -515,30 +506,6 @@ static void analyze_node(CodeGen *g, AstNode *node) {
515 case NodeTypeDirective:506 case NodeTypeDirective:
516 // we looked at directives in the parent node507 // we looked at directives in the parent node
517 break;508 break;
518 case NodeTypeBoolAndExpr:
519 zig_panic("TODO");
520 break;
521 case NodeTypeComparisonExpr:
522 zig_panic("TODO");
523 break;
524 case NodeTypeBinOrExpr:
525 zig_panic("TODO");
526 break;
527 case NodeTypeBinXorExpr:
528 zig_panic("TODO");
529 break;
530 case NodeTypeBinAndExpr:
531 zig_panic("TODO");
532 break;
533 case NodeTypeBitShiftExpr:
534 zig_panic("TODO");
535 break;
536 case NodeTypeAddExpr:
537 zig_panic("TODO");
538 break;
539 case NodeTypeMultExpr:
540 zig_panic("TODO");
541 break;
542 case NodeTypeCastExpr:509 case NodeTypeCastExpr:
543 zig_panic("TODO");510 zig_panic("TODO");
544 break;511 break;
...@@ -752,168 +719,138 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {...@@ -752,168 +719,138 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
752}719}
753720
754static LLVMValueRef gen_mult_expr(CodeGen *g, AstNode *node) {721static LLVMValueRef gen_mult_expr(CodeGen *g, AstNode *node) {
755 assert(node->type == NodeTypeMultExpr);722 assert(node->type == NodeTypeBinOpExpr);
756
757 LLVMValueRef val1 = gen_cast_expr(g, node->data.mult_expr.op1);
758
759 if (!node->data.mult_expr.op2)
760 return val1;
761723
762 LLVMValueRef val2 = gen_cast_expr(g, node->data.mult_expr.op2);724 LLVMValueRef val1 = gen_expr(g, node->data.bin_op_expr.op1);
725 LLVMValueRef val2 = gen_expr(g, node->data.bin_op_expr.op2);
763726
764 switch (node->data.mult_expr.mult_op) {727 switch (node->data.bin_op_expr.bin_op) {
765 case MultOpMult:728 case BinOpTypeMult:
766 // TODO types so we know float vs int729 // TODO types so we know float vs int
767 add_debug_source_node(g, node);730 add_debug_source_node(g, node);
768 return LLVMBuildMul(g->builder, val1, val2, "");731 return LLVMBuildMul(g->builder, val1, val2, "");
769 case MultOpDiv:732 case BinOpTypeDiv:
770 // TODO types so we know float vs int and signed vs unsigned733 // TODO types so we know float vs int and signed vs unsigned
771 add_debug_source_node(g, node);734 add_debug_source_node(g, node);
772 return LLVMBuildSDiv(g->builder, val1, val2, "");735 return LLVMBuildSDiv(g->builder, val1, val2, "");
773 case MultOpMod:736 case BinOpTypeMod:
774 // TODO types so we know float vs int and signed vs unsigned737 // TODO types so we know float vs int and signed vs unsigned
775 add_debug_source_node(g, node);738 add_debug_source_node(g, node);
776 return LLVMBuildSRem(g->builder, val1, val2, "");739 return LLVMBuildSRem(g->builder, val1, val2, "");
777 case MultOpInvalid:740 default:
778 zig_unreachable();741 zig_unreachable();
779 }742 }
780 zig_unreachable();743 zig_unreachable();
781}744}
782745
783static LLVMValueRef gen_add_expr(CodeGen *g, AstNode *node) {746static LLVMValueRef gen_add_expr(CodeGen *g, AstNode *node) {
784 assert(node->type == NodeTypeAddExpr);747 assert(node->type == NodeTypeBinOpExpr);
785
786 LLVMValueRef val1 = gen_mult_expr(g, node->data.add_expr.op1);
787
788 if (!node->data.add_expr.op2)
789 return val1;
790748
791 LLVMValueRef val2 = gen_mult_expr(g, node->data.add_expr.op2);749 LLVMValueRef val1 = gen_expr(g, node->data.bin_op_expr.op1);
750 LLVMValueRef val2 = gen_expr(g, node->data.bin_op_expr.op2);
792751
793 switch (node->data.add_expr.add_op) {752 switch (node->data.bin_op_expr.bin_op) {
794 case AddOpAdd:753 case BinOpTypeAdd:
795 add_debug_source_node(g, node);754 add_debug_source_node(g, node);
796 return LLVMBuildAdd(g->builder, val1, val2, "");755 return LLVMBuildAdd(g->builder, val1, val2, "");
797 case AddOpSub:756 case BinOpTypeSub:
798 add_debug_source_node(g, node);757 add_debug_source_node(g, node);
799 return LLVMBuildSub(g->builder, val1, val2, "");758 return LLVMBuildSub(g->builder, val1, val2, "");
800 case AddOpInvalid:759 default:
801 zig_unreachable();760 zig_unreachable();
802 }761 }
803 zig_unreachable();762 zig_unreachable();
804}763}
805764
806static LLVMValueRef gen_bit_shift_expr(CodeGen *g, AstNode *node) {765static LLVMValueRef gen_bit_shift_expr(CodeGen *g, AstNode *node) {
807 assert(node->type == NodeTypeBitShiftExpr);766 assert(node->type == NodeTypeBinOpExpr);
808
809 LLVMValueRef val1 = gen_add_expr(g, node->data.bit_shift_expr.op1);
810
811 if (!node->data.bit_shift_expr.op2)
812 return val1;
813767
814 LLVMValueRef val2 = gen_add_expr(g, node->data.bit_shift_expr.op2);768 LLVMValueRef val1 = gen_expr(g, node->data.bin_op_expr.op1);
769 LLVMValueRef val2 = gen_expr(g, node->data.bin_op_expr.op2);
815770
816 switch (node->data.bit_shift_expr.bit_shift_op) {771 switch (node->data.bin_op_expr.bin_op) {
817 case BitShiftOpLeft:772 case BinOpTypeBitShiftLeft:
818 add_debug_source_node(g, node);773 add_debug_source_node(g, node);
819 return LLVMBuildShl(g->builder, val1, val2, "");774 return LLVMBuildShl(g->builder, val1, val2, "");
820 case BitShiftOpRight:775 case BinOpTypeBitShiftRight:
821 // TODO implement type system so that we know whether to do776 // TODO implement type system so that we know whether to do
822 // logical or arithmetic shifting here.777 // logical or arithmetic shifting here.
823 // signed -> arithmetic, unsigned -> logical778 // signed -> arithmetic, unsigned -> logical
824 add_debug_source_node(g, node);779 add_debug_source_node(g, node);
825 return LLVMBuildLShr(g->builder, val1, val2, "");780 return LLVMBuildLShr(g->builder, val1, val2, "");
826 case BitShiftOpInvalid:781 default:
827 zig_unreachable();782 zig_unreachable();
828 }783 }
829 zig_unreachable();784 zig_unreachable();
830}785}
831786
832static LLVMValueRef gen_bin_and_expr(CodeGen *g, AstNode *node) {787static LLVMValueRef gen_bin_and_expr(CodeGen *g, AstNode *node) {
833 assert(node->type == NodeTypeBinAndExpr);788 assert(node->type == NodeTypeBinOpExpr);
834
835 LLVMValueRef val1 = gen_bit_shift_expr(g, node->data.bin_and_expr.op1);
836
837 if (!node->data.bin_and_expr.op2)
838 return val1;
839789
840 LLVMValueRef val2 = gen_bit_shift_expr(g, node->data.bin_and_expr.op2);790 LLVMValueRef val1 = gen_expr(g, node->data.bin_op_expr.op1);
791 LLVMValueRef val2 = gen_expr(g, node->data.bin_op_expr.op2);
841792
842 add_debug_source_node(g, node);793 add_debug_source_node(g, node);
843 return LLVMBuildAnd(g->builder, val1, val2, "");794 return LLVMBuildAnd(g->builder, val1, val2, "");
844}795}
845796
846static LLVMValueRef gen_bin_xor_expr(CodeGen *g, AstNode *node) {797static LLVMValueRef gen_bin_xor_expr(CodeGen *g, AstNode *node) {
847 assert(node->type == NodeTypeBinXorExpr);798 assert(node->type == NodeTypeBinOpExpr);
848799
849 LLVMValueRef val1 = gen_bin_and_expr(g, node->data.bin_xor_expr.op1);800 LLVMValueRef val1 = gen_expr(g, node->data.bin_op_expr.op1);
850801 LLVMValueRef val2 = gen_expr(g, node->data.bin_op_expr.op2);
851 if (!node->data.bin_xor_expr.op2)
852 return val1;
853
854 LLVMValueRef val2 = gen_bin_and_expr(g, node->data.bin_xor_expr.op2);
855802
856 add_debug_source_node(g, node);803 add_debug_source_node(g, node);
857 return LLVMBuildXor(g->builder, val1, val2, "");804 return LLVMBuildXor(g->builder, val1, val2, "");
858}805}
859806
860static LLVMValueRef gen_bin_or_expr(CodeGen *g, AstNode *node) {807static LLVMValueRef gen_bin_or_expr(CodeGen *g, AstNode *node) {
861 assert(node->type == NodeTypeBinOrExpr);808 assert(node->type == NodeTypeBinOpExpr);
862
863 LLVMValueRef val1 = gen_bin_xor_expr(g, node->data.bin_or_expr.op1);
864809
865 if (!node->data.bin_or_expr.op2)810 LLVMValueRef val1 = gen_expr(g, node->data.bin_op_expr.op1);
866 return val1;811 LLVMValueRef val2 = gen_expr(g, node->data.bin_op_expr.op2);
867
868 LLVMValueRef val2 = gen_bin_xor_expr(g, node->data.bin_or_expr.op2);
869812
870 add_debug_source_node(g, node);813 add_debug_source_node(g, node);
871 return LLVMBuildOr(g->builder, val1, val2, "");814 return LLVMBuildOr(g->builder, val1, val2, "");
872}815}
873816
874static LLVMIntPredicate cmp_op_to_int_predicate(CmpOp cmp_op, bool is_signed) {817static LLVMIntPredicate cmp_op_to_int_predicate(BinOpType cmp_op, bool is_signed) {
875 switch (cmp_op) {818 switch (cmp_op) {
876 case CmpOpInvalid:819 case BinOpTypeInvalid:
877 zig_unreachable();820 zig_unreachable();
878 case CmpOpEq:821 case BinOpTypeCmpEq:
879 return LLVMIntEQ;822 return LLVMIntEQ;
880 case CmpOpNotEq:823 case BinOpTypeCmpNotEq:
881 return LLVMIntNE;824 return LLVMIntNE;
882 case CmpOpLessThan:825 case BinOpTypeCmpLessThan:
883 return is_signed ? LLVMIntSLT : LLVMIntULT;826 return is_signed ? LLVMIntSLT : LLVMIntULT;
884 case CmpOpGreaterThan:827 case BinOpTypeCmpGreaterThan:
885 return is_signed ? LLVMIntSGT : LLVMIntUGT;828 return is_signed ? LLVMIntSGT : LLVMIntUGT;
886 case CmpOpLessOrEq:829 case BinOpTypeCmpLessOrEq:
887 return is_signed ? LLVMIntSLE : LLVMIntULE;830 return is_signed ? LLVMIntSLE : LLVMIntULE;
888 case CmpOpGreaterOrEq:831 case BinOpTypeCmpGreaterOrEq:
889 return is_signed ? LLVMIntSGE : LLVMIntUGE;832 return is_signed ? LLVMIntSGE : LLVMIntUGE;
833 default:
834 zig_unreachable();
890 }835 }
891 zig_unreachable();
892}836}
893837
894static LLVMValueRef gen_cmp_expr(CodeGen *g, AstNode *node) {838static LLVMValueRef gen_cmp_expr(CodeGen *g, AstNode *node) {
895 assert(node->type == NodeTypeComparisonExpr);839 assert(node->type == NodeTypeBinOpExpr);
896
897 LLVMValueRef val1 = gen_bin_or_expr(g, node->data.comparison_expr.op1);
898
899 if (!node->data.comparison_expr.op2)
900 return val1;
901840
902 LLVMValueRef val2 = gen_bin_or_expr(g, node->data.comparison_expr.op2);841 LLVMValueRef val1 = gen_expr(g, node->data.bin_op_expr.op1);
842 LLVMValueRef val2 = gen_expr(g, node->data.bin_op_expr.op2);
903843
904 // TODO implement type system so that we know whether to do signed or unsigned comparison here844 // TODO implement type system so that we know whether to do signed or unsigned comparison here
905 LLVMIntPredicate pred = cmp_op_to_int_predicate(node->data.comparison_expr.cmp_op, true);845 LLVMIntPredicate pred = cmp_op_to_int_predicate(node->data.bin_op_expr.bin_op, true);
906 add_debug_source_node(g, node);846 add_debug_source_node(g, node);
907 return LLVMBuildICmp(g->builder, pred, val1, val2, "");847 return LLVMBuildICmp(g->builder, pred, val1, val2, "");
908}848}
909849
910static LLVMValueRef gen_bool_and_expr(CodeGen *g, AstNode *node) {850static LLVMValueRef gen_bool_and_expr(CodeGen *g, AstNode *node) {
911 assert(node->type == NodeTypeBoolAndExpr);851 assert(node->type == NodeTypeBinOpExpr);
912852
913 LLVMValueRef val1 = gen_cmp_expr(g, node->data.bool_and_expr.op1);853 LLVMValueRef val1 = gen_expr(g, node->data.bin_op_expr.op1);
914
915 if (!node->data.bool_and_expr.op2)
916 return val1;
917854
918 // block for when val1 == true855 // block for when val1 == true
919 LLVMBasicBlockRef true_block = LLVMAppendBasicBlock(g->cur_fn, "BoolAndTrue");856 LLVMBasicBlockRef true_block = LLVMAppendBasicBlock(g->cur_fn, "BoolAndTrue");
...@@ -926,7 +863,7 @@ static LLVMValueRef gen_bool_and_expr(CodeGen *g, AstNode *node) {...@@ -926,7 +863,7 @@ static LLVMValueRef gen_bool_and_expr(CodeGen *g, AstNode *node) {
926 LLVMBuildCondBr(g->builder, val1_i1, false_block, true_block);863 LLVMBuildCondBr(g->builder, val1_i1, false_block, true_block);
927864
928 LLVMPositionBuilderAtEnd(g->builder, true_block);865 LLVMPositionBuilderAtEnd(g->builder, true_block);
929 LLVMValueRef val2 = gen_cmp_expr(g, node->data.bool_and_expr.op2);866 LLVMValueRef val2 = gen_expr(g, node->data.bin_op_expr.op2);
930 add_debug_source_node(g, node);867 add_debug_source_node(g, node);
931 LLVMValueRef val2_i1 = LLVMBuildICmp(g->builder, LLVMIntEQ, val2, zero, "");868 LLVMValueRef val2_i1 = LLVMBuildICmp(g->builder, LLVMIntEQ, val2, zero, "");
932869
...@@ -942,12 +879,9 @@ static LLVMValueRef gen_bool_and_expr(CodeGen *g, AstNode *node) {...@@ -942,12 +879,9 @@ static LLVMValueRef gen_bool_and_expr(CodeGen *g, AstNode *node) {
942}879}
943880
944static LLVMValueRef gen_bool_or_expr(CodeGen *g, AstNode *expr_node) {881static LLVMValueRef gen_bool_or_expr(CodeGen *g, AstNode *expr_node) {
945 assert(expr_node->type == NodeTypeBoolOrExpr);882 assert(expr_node->type == NodeTypeBinOpExpr);
946
947 LLVMValueRef val1 = gen_bool_and_expr(g, expr_node->data.bool_or_expr.op1);
948883
949 if (!expr_node->data.bool_or_expr.op2)884 LLVMValueRef val1 = gen_expr(g, expr_node->data.bin_op_expr.op1);
950 return val1;
951885
952 // block for when val1 == false886 // block for when val1 == false
953 LLVMBasicBlockRef false_block = LLVMAppendBasicBlock(g->cur_fn, "BoolOrFalse");887 LLVMBasicBlockRef false_block = LLVMAppendBasicBlock(g->cur_fn, "BoolOrFalse");
...@@ -960,7 +894,7 @@ static LLVMValueRef gen_bool_or_expr(CodeGen *g, AstNode *expr_node) {...@@ -960,7 +894,7 @@ static LLVMValueRef gen_bool_or_expr(CodeGen *g, AstNode *expr_node) {
960 LLVMBuildCondBr(g->builder, val1_i1, false_block, true_block);894 LLVMBuildCondBr(g->builder, val1_i1, false_block, true_block);
961895
962 LLVMPositionBuilderAtEnd(g->builder, false_block);896 LLVMPositionBuilderAtEnd(g->builder, false_block);
963 LLVMValueRef val2 = gen_bool_and_expr(g, expr_node->data.bool_or_expr.op2);897 LLVMValueRef val2 = gen_expr(g, expr_node->data.bin_op_expr.op2);
964 add_debug_source_node(g, expr_node);898 add_debug_source_node(g, expr_node);
965 LLVMValueRef val2_i1 = LLVMBuildICmp(g->builder, LLVMIntEQ, val2, zero, "");899 LLVMValueRef val2_i1 = LLVMBuildICmp(g->builder, LLVMIntEQ, val2, zero, "");
966900
...@@ -975,6 +909,41 @@ static LLVMValueRef gen_bool_or_expr(CodeGen *g, AstNode *expr_node) {...@@ -975,6 +909,41 @@ static LLVMValueRef gen_bool_or_expr(CodeGen *g, AstNode *expr_node) {
975 return phi;909 return phi;
976}910}
977911
912static LLVMValueRef gen_bin_op_expr(CodeGen *g, AstNode *node) {
913 switch (node->data.bin_op_expr.bin_op) {
914 case BinOpTypeInvalid:
915 zig_unreachable();
916 case BinOpTypeBoolOr:
917 return gen_bool_or_expr(g, node);
918 case BinOpTypeBoolAnd:
919 return gen_bool_and_expr(g, node);
920 case BinOpTypeCmpEq:
921 case BinOpTypeCmpNotEq:
922 case BinOpTypeCmpLessThan:
923 case BinOpTypeCmpGreaterThan:
924 case BinOpTypeCmpLessOrEq:
925 case BinOpTypeCmpGreaterOrEq:
926 return gen_cmp_expr(g, node);
927 case BinOpTypeBinOr:
928 return gen_bin_or_expr(g, node);
929 case BinOpTypeBinXor:
930 return gen_bin_xor_expr(g, node);
931 case BinOpTypeBinAnd:
932 return gen_bin_and_expr(g, node);
933 case BinOpTypeBitShiftLeft:
934 case BinOpTypeBitShiftRight:
935 return gen_bit_shift_expr(g, node);
936 case BinOpTypeAdd:
937 case BinOpTypeSub:
938 return gen_add_expr(g, node);
939 case BinOpTypeMult:
940 case BinOpTypeDiv:
941 case BinOpTypeMod:
942 return gen_mult_expr(g, node);
943 }
944 zig_unreachable();
945}
946
978static LLVMValueRef gen_return_expr(CodeGen *g, AstNode *node) {947static LLVMValueRef gen_return_expr(CodeGen *g, AstNode *node) {
979 assert(node->type == NodeTypeReturnExpr);948 assert(node->type == NodeTypeReturnExpr);
980 AstNode *param_node = node->data.return_expr.expr;949 AstNode *param_node = node->data.return_expr.expr;
...@@ -993,10 +962,12 @@ Expression : BoolOrExpression | ReturnExpression...@@ -993,10 +962,12 @@ Expression : BoolOrExpression | ReturnExpression
993*/962*/
994static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {963static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
995 switch (node->type) {964 switch (node->type) {
996 case NodeTypeBoolOrExpr:965 case NodeTypeBinOpExpr:
997 return gen_bool_or_expr(g, node);966 return gen_bin_op_expr(g, node);
998 case NodeTypeReturnExpr:967 case NodeTypeReturnExpr:
999 return gen_return_expr(g, node);968 return gen_return_expr(g, node);
969 case NodeTypeCastExpr:
970 return gen_cast_expr(g, node);
1000 case NodeTypeRoot:971 case NodeTypeRoot:
1001 case NodeTypeRootExportDecl:972 case NodeTypeRootExportDecl:
1002 case NodeTypeFnProto:973 case NodeTypeFnProto:
...@@ -1008,15 +979,6 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {...@@ -1008,15 +979,6 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
1008 case NodeTypeFnCall:979 case NodeTypeFnCall:
1009 case NodeTypeExternBlock:980 case NodeTypeExternBlock:
1010 case NodeTypeDirective:981 case NodeTypeDirective:
1011 case NodeTypeBoolAndExpr:
1012 case NodeTypeComparisonExpr:
1013 case NodeTypeBinOrExpr:
1014 case NodeTypeBinXorExpr:
1015 case NodeTypeBinAndExpr:
1016 case NodeTypeBitShiftExpr:
1017 case NodeTypeAddExpr:
1018 case NodeTypeMultExpr:
1019 case NodeTypeCastExpr:
1020 case NodeTypePrimaryExpr:982 case NodeTypePrimaryExpr:
1021 return gen_primary_expr(g, node);983 return gen_primary_expr(g, node);
1022 case NodeTypeGroupedExpr:984 case NodeTypeGroupedExpr:
src/parser.cpp+109-188
...@@ -10,43 +10,27 @@...@@ -10,43 +10,27 @@
10#include <stdarg.h>10#include <stdarg.h>
11#include <stdio.h>11#include <stdio.h>
1212
13static const char *mult_op_str(MultOp mult_op) {13static const char *bin_op_str(BinOpType bin_op) {
14 switch (mult_op) {14 switch (bin_op) {
15 case MultOpInvalid: return "(invalid)";15 case BinOpTypeInvalid: return "(invalid)";
16 case MultOpMult: return "*";16 case BinOpTypeBoolOr: return "||";
17 case MultOpDiv: return "/";17 case BinOpTypeBoolAnd: return "&&";
18 case MultOpMod: return "%";18 case BinOpTypeCmpEq: return "==";
19 }19 case BinOpTypeCmpNotEq: return "!=";
20 zig_unreachable();20 case BinOpTypeCmpLessThan: return "<";
21}21 case BinOpTypeCmpGreaterThan: return ">";
2222 case BinOpTypeCmpLessOrEq: return "<=";
23static const char *add_op_str(AddOp add_op) {23 case BinOpTypeCmpGreaterOrEq: return ">=";
24 switch (add_op) {24 case BinOpTypeBinOr: return "|";
25 case AddOpInvalid: return "(invalid)";25 case BinOpTypeBinXor: return "^";
26 case AddOpAdd: return "+";26 case BinOpTypeBinAnd: return "&";
27 case AddOpSub: return "-";27 case BinOpTypeBitShiftLeft: return "<<";
28 }28 case BinOpTypeBitShiftRight: return ">>";
29 zig_unreachable();29 case BinOpTypeAdd: return "+";
30}30 case BinOpTypeSub: return "-";
3131 case BinOpTypeMult: return "*";
32static const char *bit_shift_op_str(BitShiftOp bit_shift_op) {32 case BinOpTypeDiv: return "/";
33 switch (bit_shift_op) {33 case BinOpTypeMod: return "%";
34 case BitShiftOpInvalid: return "(invalid)";
35 case BitShiftOpLeft: return "<<";
36 case BitShiftOpRight: return ">>";
37 }
38 zig_unreachable();
39}
40
41static const char *cmp_op_str(CmpOp cmp_op) {
42 switch (cmp_op) {
43 case CmpOpInvalid: return "(invalid)";
44 case CmpOpEq: return "=";
45 case CmpOpNotEq: return "!=";
46 case CmpOpLessThan: return "<";
47 case CmpOpGreaterThan: return ">";
48 case CmpOpLessOrEq: return "<=";
49 case CmpOpGreaterOrEq: return ">=";
50 }34 }
51 zig_unreachable();35 zig_unreachable();
52}36}
...@@ -84,8 +68,8 @@ const char *node_type_str(NodeType node_type) {...@@ -84,8 +68,8 @@ const char *node_type_str(NodeType node_type) {
84 return "Type";68 return "Type";
85 case NodeTypeBlock:69 case NodeTypeBlock:
86 return "Block";70 return "Block";
87 case NodeTypeBoolOrExpr:71 case NodeTypeBinOpExpr:
88 return "BoolOrExpr";72 return "BinOpExpr";
89 case NodeTypeFnCall:73 case NodeTypeFnCall:
90 return "FnCall";74 return "FnCall";
91 case NodeTypeExternBlock:75 case NodeTypeExternBlock:
...@@ -94,22 +78,6 @@ const char *node_type_str(NodeType node_type) {...@@ -94,22 +78,6 @@ const char *node_type_str(NodeType node_type) {
94 return "Directive";78 return "Directive";
95 case NodeTypeReturnExpr:79 case NodeTypeReturnExpr:
96 return "ReturnExpr";80 return "ReturnExpr";
97 case NodeTypeBoolAndExpr:
98 return "BoolAndExpr";
99 case NodeTypeComparisonExpr:
100 return "ComparisonExpr";
101 case NodeTypeBinOrExpr:
102 return "BinOrExpr";
103 case NodeTypeBinXorExpr:
104 return "BinXorExpr";
105 case NodeTypeBinAndExpr:
106 return "BinAndExpr";
107 case NodeTypeBitShiftExpr:
108 return "BitShiftExpr";
109 case NodeTypeAddExpr:
110 return "AddExpr";
111 case NodeTypeMultExpr:
112 return "MultExpr";
113 case NodeTypeCastExpr:81 case NodeTypeCastExpr:
114 return "CastExpr";82 return "CastExpr";
115 case NodeTypePrimaryExpr:83 case NodeTypePrimaryExpr:
...@@ -214,11 +182,11 @@ void ast_print(AstNode *node, int indent) {...@@ -214,11 +182,11 @@ void ast_print(AstNode *node, int indent) {
214 fprintf(stderr, "%s\n", node_type_str(node->type));182 fprintf(stderr, "%s\n", node_type_str(node->type));
215 ast_print(node->data.fn_decl.fn_proto, indent + 2);183 ast_print(node->data.fn_decl.fn_proto, indent + 2);
216 break;184 break;
217 case NodeTypeBoolOrExpr:185 case NodeTypeBinOpExpr:
218 fprintf(stderr, "%s\n", node_type_str(node->type));186 fprintf(stderr, "%s %s\n", node_type_str(node->type),
219 ast_print(node->data.bool_or_expr.op1, indent + 2);187 bin_op_str(node->data.bin_op_expr.bin_op));
220 if (node->data.bool_or_expr.op2)188 ast_print(node->data.bin_op_expr.op1, indent + 2);
221 ast_print(node->data.bool_or_expr.op2, indent + 2);189 ast_print(node->data.bin_op_expr.op2, indent + 2);
222 break;190 break;
223 case NodeTypeFnCall:191 case NodeTypeFnCall:
224 fprintf(stderr, "%s '%s'\n", node_type_str(node->type), buf_ptr(&node->data.fn_call.name));192 fprintf(stderr, "%s '%s'\n", node_type_str(node->type), buf_ptr(&node->data.fn_call.name));
...@@ -230,58 +198,6 @@ void ast_print(AstNode *node, int indent) {...@@ -230,58 +198,6 @@ void ast_print(AstNode *node, int indent) {
230 case NodeTypeDirective:198 case NodeTypeDirective:
231 fprintf(stderr, "%s\n", node_type_str(node->type));199 fprintf(stderr, "%s\n", node_type_str(node->type));
232 break;200 break;
233 case NodeTypeBoolAndExpr:
234 fprintf(stderr, "%s\n", node_type_str(node->type));
235 ast_print(node->data.bool_and_expr.op1, indent + 2);
236 if (node->data.bool_and_expr.op2)
237 ast_print(node->data.bool_and_expr.op2, indent + 2);
238 break;
239 case NodeTypeComparisonExpr:
240 fprintf(stderr, "%s %s\n", node_type_str(node->type),
241 cmp_op_str(node->data.comparison_expr.cmp_op));
242 ast_print(node->data.comparison_expr.op1, indent + 2);
243 if (node->data.comparison_expr.op2)
244 ast_print(node->data.comparison_expr.op2, indent + 2);
245 break;
246 case NodeTypeBinOrExpr:
247 fprintf(stderr, "%s\n", node_type_str(node->type));
248 ast_print(node->data.bin_or_expr.op1, indent + 2);
249 if (node->data.bin_or_expr.op2)
250 ast_print(node->data.bin_or_expr.op2, indent + 2);
251 break;
252 case NodeTypeBinXorExpr:
253 fprintf(stderr, "%s\n", node_type_str(node->type));
254 ast_print(node->data.bin_xor_expr.op1, indent + 2);
255 if (node->data.bin_xor_expr.op2)
256 ast_print(node->data.bin_xor_expr.op2, indent + 2);
257 break;
258 case NodeTypeBinAndExpr:
259 fprintf(stderr, "%s\n", node_type_str(node->type));
260 ast_print(node->data.bin_and_expr.op1, indent + 2);
261 if (node->data.bin_and_expr.op2)
262 ast_print(node->data.bin_and_expr.op2, indent + 2);
263 break;
264 case NodeTypeBitShiftExpr:
265 fprintf(stderr, "%s %s\n", node_type_str(node->type),
266 bit_shift_op_str(node->data.bit_shift_expr.bit_shift_op));
267 ast_print(node->data.bit_shift_expr.op1, indent + 2);
268 if (node->data.bit_shift_expr.op2)
269 ast_print(node->data.bit_shift_expr.op2, indent + 2);
270 break;
271 case NodeTypeAddExpr:
272 fprintf(stderr, "%s %s\n", node_type_str(node->type),
273 add_op_str(node->data.add_expr.add_op));
274 ast_print(node->data.add_expr.op1, indent + 2);
275 if (node->data.add_expr.op2)
276 ast_print(node->data.add_expr.op2, indent + 2);
277 break;
278 case NodeTypeMultExpr:
279 fprintf(stderr, "%s %s\n", node_type_str(node->type),
280 mult_op_str(node->data.mult_expr.mult_op));
281 ast_print(node->data.mult_expr.op1, indent + 2);
282 if (node->data.mult_expr.op2)
283 ast_print(node->data.mult_expr.op2, indent + 2);
284 break;
285 case NodeTypeCastExpr:201 case NodeTypeCastExpr:
286 fprintf(stderr, "%s\n", node_type_str(node->type));202 fprintf(stderr, "%s\n", node_type_str(node->type));
287 ast_print(node->data.cast_expr.primary_expr, indent + 2);203 ast_print(node->data.cast_expr.primary_expr, indent + 2);
...@@ -709,26 +625,26 @@ static AstNode *ast_parse_cast_expression(ParseContext *pc, int *token_index, bo...@@ -709,26 +625,26 @@ static AstNode *ast_parse_cast_expression(ParseContext *pc, int *token_index, bo
709 return node;625 return node;
710}626}
711627
712static MultOp tok_to_mult_op(Token *token) {628static BinOpType tok_to_mult_op(Token *token) {
713 switch (token->id) {629 switch (token->id) {
714 case TokenIdStar: return MultOpMult;630 case TokenIdStar: return BinOpTypeMult;
715 case TokenIdSlash: return MultOpDiv;631 case TokenIdSlash: return BinOpTypeDiv;
716 case TokenIdPercent: return MultOpMod;632 case TokenIdPercent: return BinOpTypeMod;
717 default: return MultOpInvalid;633 default: return BinOpTypeInvalid;
718 }634 }
719}635}
720636
721/*637/*
722MultiplyOperator : token(Star) | token(Slash) | token(Percent)638MultiplyOperator : token(Star) | token(Slash) | token(Percent)
723*/639*/
724static MultOp ast_parse_mult_op(ParseContext *pc, int *token_index, bool mandatory) {640static BinOpType ast_parse_mult_op(ParseContext *pc, int *token_index, bool mandatory) {
725 Token *token = &pc->tokens->at(*token_index);641 Token *token = &pc->tokens->at(*token_index);
726 MultOp result = tok_to_mult_op(token);642 BinOpType result = tok_to_mult_op(token);
727 if (result == MultOpInvalid) {643 if (result == BinOpTypeInvalid) {
728 if (mandatory) {644 if (mandatory) {
729 ast_invalid_token_error(pc, token);645 ast_invalid_token_error(pc, token);
730 } else {646 } else {
731 return MultOpInvalid;647 return BinOpTypeInvalid;
732 }648 }
733 }649 }
734 *token_index += 1;650 *token_index += 1;
...@@ -744,39 +660,39 @@ static AstNode *ast_parse_mult_expr(ParseContext *pc, int *token_index, bool man...@@ -744,39 +660,39 @@ static AstNode *ast_parse_mult_expr(ParseContext *pc, int *token_index, bool man
744 return nullptr;660 return nullptr;
745661
746 Token *token = &pc->tokens->at(*token_index);662 Token *token = &pc->tokens->at(*token_index);
747 MultOp mult_op = ast_parse_mult_op(pc, token_index, false);663 BinOpType mult_op = ast_parse_mult_op(pc, token_index, false);
748 if (mult_op == MultOpInvalid)664 if (mult_op == BinOpTypeInvalid)
749 return operand_1;665 return operand_1;
750666
751 AstNode *operand_2 = ast_parse_cast_expression(pc, token_index, true);667 AstNode *operand_2 = ast_parse_cast_expression(pc, token_index, true);
752668
753 AstNode *node = ast_create_node(NodeTypeMultExpr, token);669 AstNode *node = ast_create_node(NodeTypeBinOpExpr, token);
754 node->data.mult_expr.op1 = operand_1;670 node->data.bin_op_expr.op1 = operand_1;
755 node->data.mult_expr.mult_op = mult_op;671 node->data.bin_op_expr.bin_op = mult_op;
756 node->data.mult_expr.op2 = operand_2;672 node->data.bin_op_expr.op2 = operand_2;
757673
758 return node;674 return node;
759}675}
760676
761static AddOp tok_to_add_op(Token *token) {677static BinOpType tok_to_add_op(Token *token) {
762 switch (token->id) {678 switch (token->id) {
763 case TokenIdPlus: return AddOpAdd;679 case TokenIdPlus: return BinOpTypeAdd;
764 case TokenIdDash: return AddOpSub;680 case TokenIdDash: return BinOpTypeSub;
765 default: return AddOpInvalid;681 default: return BinOpTypeInvalid;
766 }682 }
767}683}
768684
769/*685/*
770AdditionOperator : token(Plus) | token(Minus)686AdditionOperator : token(Plus) | token(Minus)
771*/687*/
772static AddOp ast_parse_add_op(ParseContext *pc, int *token_index, bool mandatory) {688static BinOpType ast_parse_add_op(ParseContext *pc, int *token_index, bool mandatory) {
773 Token *token = &pc->tokens->at(*token_index);689 Token *token = &pc->tokens->at(*token_index);
774 AddOp result = tok_to_add_op(token);690 BinOpType result = tok_to_add_op(token);
775 if (result == AddOpInvalid) {691 if (result == BinOpTypeInvalid) {
776 if (mandatory) {692 if (mandatory) {
777 ast_invalid_token_error(pc, token);693 ast_invalid_token_error(pc, token);
778 } else {694 } else {
779 return AddOpInvalid;695 return BinOpTypeInvalid;
780 }696 }
781 }697 }
782 *token_index += 1;698 *token_index += 1;
...@@ -792,39 +708,39 @@ static AstNode *ast_parse_add_expr(ParseContext *pc, int *token_index, bool mand...@@ -792,39 +708,39 @@ static AstNode *ast_parse_add_expr(ParseContext *pc, int *token_index, bool mand
792 return nullptr;708 return nullptr;
793709
794 Token *token = &pc->tokens->at(*token_index);710 Token *token = &pc->tokens->at(*token_index);
795 AddOp add_op = ast_parse_add_op(pc, token_index, false);711 BinOpType add_op = ast_parse_add_op(pc, token_index, false);
796 if (add_op == AddOpInvalid)712 if (add_op == BinOpTypeInvalid)
797 return operand_1;713 return operand_1;
798714
799 AstNode *operand_2 = ast_parse_mult_expr(pc, token_index, true);715 AstNode *operand_2 = ast_parse_mult_expr(pc, token_index, true);
800716
801 AstNode *node = ast_create_node(NodeTypeAddExpr, token);717 AstNode *node = ast_create_node(NodeTypeBinOpExpr, token);
802 node->data.add_expr.op1 = operand_1;718 node->data.bin_op_expr.op1 = operand_1;
803 node->data.add_expr.add_op = add_op;719 node->data.bin_op_expr.bin_op = add_op;
804 node->data.add_expr.op2 = operand_2;720 node->data.bin_op_expr.op2 = operand_2;
805721
806 return node;722 return node;
807}723}
808724
809static BitShiftOp tok_to_bit_shift_op(Token *token) {725static BinOpType tok_to_bit_shift_op(Token *token) {
810 switch (token->id) {726 switch (token->id) {
811 case TokenIdBitShiftLeft: return BitShiftOpLeft;727 case TokenIdBitShiftLeft: return BinOpTypeBitShiftLeft;
812 case TokenIdBitShiftRight: return BitShiftOpRight;728 case TokenIdBitShiftRight: return BinOpTypeBitShiftRight;
813 default: return BitShiftOpInvalid;729 default: return BinOpTypeInvalid;
814 }730 }
815}731}
816732
817/*733/*
818BitShiftOperator : token(BitShiftLeft | token(BitShiftRight)734BitShiftOperator : token(BitShiftLeft | token(BitShiftRight)
819*/735*/
820static BitShiftOp ast_parse_bit_shift_op(ParseContext *pc, int *token_index, bool mandatory) {736static BinOpType ast_parse_bit_shift_op(ParseContext *pc, int *token_index, bool mandatory) {
821 Token *token = &pc->tokens->at(*token_index);737 Token *token = &pc->tokens->at(*token_index);
822 BitShiftOp result = tok_to_bit_shift_op(token);738 BinOpType result = tok_to_bit_shift_op(token);
823 if (result == BitShiftOpInvalid) {739 if (result == BinOpTypeInvalid) {
824 if (mandatory) {740 if (mandatory) {
825 ast_invalid_token_error(pc, token);741 ast_invalid_token_error(pc, token);
826 } else {742 } else {
827 return BitShiftOpInvalid;743 return BinOpTypeInvalid;
828 }744 }
829 }745 }
830 *token_index += 1;746 *token_index += 1;
...@@ -840,16 +756,16 @@ static AstNode *ast_parse_bit_shift_expr(ParseContext *pc, int *token_index, boo...@@ -840,16 +756,16 @@ static AstNode *ast_parse_bit_shift_expr(ParseContext *pc, int *token_index, boo
840 return nullptr;756 return nullptr;
841757
842 Token *token = &pc->tokens->at(*token_index);758 Token *token = &pc->tokens->at(*token_index);
843 BitShiftOp bit_shift_op = ast_parse_bit_shift_op(pc, token_index, false);759 BinOpType bit_shift_op = ast_parse_bit_shift_op(pc, token_index, false);
844 if (bit_shift_op == BitShiftOpInvalid)760 if (bit_shift_op == BinOpTypeInvalid)
845 return operand_1;761 return operand_1;
846762
847 AstNode *operand_2 = ast_parse_add_expr(pc, token_index, true);763 AstNode *operand_2 = ast_parse_add_expr(pc, token_index, true);
848764
849 AstNode *node = ast_create_node(NodeTypeBitShiftExpr, token);765 AstNode *node = ast_create_node(NodeTypeBinOpExpr, token);
850 node->data.bit_shift_expr.op1 = operand_1;766 node->data.bin_op_expr.op1 = operand_1;
851 node->data.bit_shift_expr.bit_shift_op = bit_shift_op;767 node->data.bin_op_expr.bin_op = bit_shift_op;
852 node->data.bit_shift_expr.op2 = operand_2;768 node->data.bin_op_expr.op2 = operand_2;
853769
854 return node;770 return node;
855}771}
...@@ -870,9 +786,10 @@ static AstNode *ast_parse_bin_and_expr(ParseContext *pc, int *token_index, bool...@@ -870,9 +786,10 @@ static AstNode *ast_parse_bin_and_expr(ParseContext *pc, int *token_index, bool
870786
871 AstNode *operand_2 = ast_parse_bit_shift_expr(pc, token_index, true);787 AstNode *operand_2 = ast_parse_bit_shift_expr(pc, token_index, true);
872788
873 AstNode *node = ast_create_node(NodeTypeBinAndExpr, token);789 AstNode *node = ast_create_node(NodeTypeBinOpExpr, token);
874 node->data.bin_and_expr.op1 = operand_1;790 node->data.bin_op_expr.op1 = operand_1;
875 node->data.bin_and_expr.op2 = operand_2;791 node->data.bin_op_expr.bin_op = BinOpTypeBinAnd;
792 node->data.bin_op_expr.op2 = operand_2;
876793
877 return node;794 return node;
878}795}
...@@ -892,9 +809,10 @@ static AstNode *ast_parse_bin_xor_expr(ParseContext *pc, int *token_index, bool...@@ -892,9 +809,10 @@ static AstNode *ast_parse_bin_xor_expr(ParseContext *pc, int *token_index, bool
892809
893 AstNode *operand_2 = ast_parse_bin_and_expr(pc, token_index, true);810 AstNode *operand_2 = ast_parse_bin_and_expr(pc, token_index, true);
894811
895 AstNode *node = ast_create_node(NodeTypeBinXorExpr, token);812 AstNode *node = ast_create_node(NodeTypeBinOpExpr, token);
896 node->data.bin_xor_expr.op1 = operand_1;813 node->data.bin_op_expr.op1 = operand_1;
897 node->data.bin_xor_expr.op2 = operand_2;814 node->data.bin_op_expr.bin_op = BinOpTypeBinXor;
815 node->data.bin_op_expr.op2 = operand_2;
898816
899 return node;817 return node;
900}818}
...@@ -914,33 +832,34 @@ static AstNode *ast_parse_bin_or_expr(ParseContext *pc, int *token_index, bool m...@@ -914,33 +832,34 @@ static AstNode *ast_parse_bin_or_expr(ParseContext *pc, int *token_index, bool m
914832
915 AstNode *operand_2 = ast_parse_bin_xor_expr(pc, token_index, true);833 AstNode *operand_2 = ast_parse_bin_xor_expr(pc, token_index, true);
916834
917 AstNode *node = ast_create_node(NodeTypeBinOrExpr, token);835 AstNode *node = ast_create_node(NodeTypeBinOpExpr, token);
918 node->data.bin_or_expr.op1 = operand_1;836 node->data.bin_op_expr.op1 = operand_1;
919 node->data.bin_or_expr.op2 = operand_2;837 node->data.bin_op_expr.bin_op = BinOpTypeBinOr;
838 node->data.bin_op_expr.op2 = operand_2;
920839
921 return node;840 return node;
922}841}
923842
924static CmpOp tok_to_cmp_op(Token *token) {843static BinOpType tok_to_cmp_op(Token *token) {
925 switch (token->id) {844 switch (token->id) {
926 case TokenIdCmpEq: return CmpOpEq;845 case TokenIdCmpEq: return BinOpTypeCmpEq;
927 case TokenIdCmpNotEq: return CmpOpNotEq;846 case TokenIdCmpNotEq: return BinOpTypeCmpNotEq;
928 case TokenIdCmpLessThan: return CmpOpLessThan;847 case TokenIdCmpLessThan: return BinOpTypeCmpLessThan;
929 case TokenIdCmpGreaterThan: return CmpOpGreaterThan;848 case TokenIdCmpGreaterThan: return BinOpTypeCmpGreaterThan;
930 case TokenIdCmpLessOrEq: return CmpOpLessOrEq;849 case TokenIdCmpLessOrEq: return BinOpTypeCmpLessOrEq;
931 case TokenIdCmpGreaterOrEq: return CmpOpGreaterOrEq;850 case TokenIdCmpGreaterOrEq: return BinOpTypeCmpGreaterOrEq;
932 default: return CmpOpInvalid;851 default: return BinOpTypeInvalid;
933 }852 }
934}853}
935854
936static CmpOp ast_parse_comparison_operator(ParseContext *pc, int *token_index, bool mandatory) {855static BinOpType ast_parse_comparison_operator(ParseContext *pc, int *token_index, bool mandatory) {
937 Token *token = &pc->tokens->at(*token_index);856 Token *token = &pc->tokens->at(*token_index);
938 CmpOp result = tok_to_cmp_op(token);857 BinOpType result = tok_to_cmp_op(token);
939 if (result == CmpOpInvalid) {858 if (result == BinOpTypeInvalid) {
940 if (mandatory) {859 if (mandatory) {
941 ast_invalid_token_error(pc, token);860 ast_invalid_token_error(pc, token);
942 } else {861 } else {
943 return CmpOpInvalid;862 return BinOpTypeInvalid;
944 }863 }
945 }864 }
946 *token_index += 1;865 *token_index += 1;
...@@ -956,16 +875,16 @@ static AstNode *ast_parse_comparison_expr(ParseContext *pc, int *token_index, bo...@@ -956,16 +875,16 @@ static AstNode *ast_parse_comparison_expr(ParseContext *pc, int *token_index, bo
956 return nullptr;875 return nullptr;
957876
958 Token *token = &pc->tokens->at(*token_index);877 Token *token = &pc->tokens->at(*token_index);
959 CmpOp cmp_op = ast_parse_comparison_operator(pc, token_index, false);878 BinOpType cmp_op = ast_parse_comparison_operator(pc, token_index, false);
960 if (cmp_op == CmpOpInvalid)879 if (cmp_op == BinOpTypeInvalid)
961 return operand_1;880 return operand_1;
962881
963 AstNode *operand_2 = ast_parse_bin_or_expr(pc, token_index, true);882 AstNode *operand_2 = ast_parse_bin_or_expr(pc, token_index, true);
964883
965 AstNode *node = ast_create_node(NodeTypeComparisonExpr, token);884 AstNode *node = ast_create_node(NodeTypeBinOpExpr, token);
966 node->data.comparison_expr.op1 = operand_1;885 node->data.bin_op_expr.op1 = operand_1;
967 node->data.comparison_expr.cmp_op = cmp_op;886 node->data.bin_op_expr.bin_op = cmp_op;
968 node->data.comparison_expr.op2 = operand_2;887 node->data.bin_op_expr.op2 = operand_2;
969888
970 return node;889 return node;
971}890}
...@@ -985,9 +904,10 @@ static AstNode *ast_parse_bool_and_expr(ParseContext *pc, int *token_index, bool...@@ -985,9 +904,10 @@ static AstNode *ast_parse_bool_and_expr(ParseContext *pc, int *token_index, bool
985904
986 AstNode *operand_2 = ast_parse_comparison_expr(pc, token_index, true);905 AstNode *operand_2 = ast_parse_comparison_expr(pc, token_index, true);
987906
988 AstNode *node = ast_create_node(NodeTypeBoolAndExpr, token);907 AstNode *node = ast_create_node(NodeTypeBinOpExpr, token);
989 node->data.bool_and_expr.op1 = operand_1;908 node->data.bin_op_expr.op1 = operand_1;
990 node->data.bool_and_expr.op2 = operand_2;909 node->data.bin_op_expr.bin_op = BinOpTypeBoolAnd;
910 node->data.bin_op_expr.op2 = operand_2;
991911
992 return node;912 return node;
993}913}
...@@ -1024,9 +944,10 @@ static AstNode *ast_parse_bool_or_expr(ParseContext *pc, int *token_index, bool...@@ -1024,9 +944,10 @@ static AstNode *ast_parse_bool_or_expr(ParseContext *pc, int *token_index, bool
1024944
1025 AstNode *operand_2 = ast_parse_bool_and_expr(pc, token_index, true);945 AstNode *operand_2 = ast_parse_bool_and_expr(pc, token_index, true);
1026946
1027 AstNode *node = ast_create_node(NodeTypeBoolOrExpr, token);947 AstNode *node = ast_create_node(NodeTypeBinOpExpr, token);
1028 node->data.bool_or_expr.op1 = operand_1;948 node->data.bin_op_expr.op1 = operand_1;
1029 node->data.bool_or_expr.op2 = operand_2;949 node->data.bin_op_expr.bin_op = BinOpTypeBoolOr;
950 node->data.bin_op_expr.op2 = operand_2;
1030951
1031 return node;952 return node;
1032}953}
src/parser.hpp+27-101
...@@ -28,15 +28,7 @@ enum NodeType {...@@ -28,15 +28,7 @@ enum NodeType {
28 NodeTypeExternBlock,28 NodeTypeExternBlock,
29 NodeTypeDirective,29 NodeTypeDirective,
30 NodeTypeReturnExpr,30 NodeTypeReturnExpr,
31 NodeTypeBoolOrExpr,31 NodeTypeBinOpExpr,
32 NodeTypeBoolAndExpr,
33 NodeTypeComparisonExpr,
34 NodeTypeBinOrExpr,
35 NodeTypeBinXorExpr,
36 NodeTypeBinAndExpr,
37 NodeTypeBitShiftExpr,
38 NodeTypeAddExpr,
39 NodeTypeMultExpr,
40 NodeTypeCastExpr,32 NodeTypeCastExpr,
41 NodeTypePrimaryExpr,33 NodeTypePrimaryExpr,
42 NodeTypeGroupedExpr,34 NodeTypeGroupedExpr,
...@@ -96,9 +88,32 @@ struct AstNodeReturnExpr {...@@ -96,9 +88,32 @@ struct AstNodeReturnExpr {
96 AstNode *expr;88 AstNode *expr;
97};89};
9890
99struct AstNodeBoolOrExpr {91enum BinOpType {
92 BinOpTypeInvalid,
93 // TODO: include assignment?
94 BinOpTypeBoolOr,
95 BinOpTypeBoolAnd,
96 BinOpTypeCmpEq,
97 BinOpTypeCmpNotEq,
98 BinOpTypeCmpLessThan,
99 BinOpTypeCmpGreaterThan,
100 BinOpTypeCmpLessOrEq,
101 BinOpTypeCmpGreaterOrEq,
102 BinOpTypeBinOr,
103 BinOpTypeBinXor,
104 BinOpTypeBinAnd,
105 BinOpTypeBitShiftLeft,
106 BinOpTypeBitShiftRight,
107 BinOpTypeAdd,
108 BinOpTypeSub,
109 BinOpTypeMult,
110 BinOpTypeDiv,
111 BinOpTypeMod,
112};
113
114struct AstNodeBinOpExpr {
100 AstNode *op1;115 AstNode *op1;
101 // if op2 is non-null, do boolean or, otherwise nothing116 BinOpType bin_op;
102 AstNode *op2;117 AstNode *op2;
103};118};
104119
...@@ -122,87 +137,6 @@ struct AstNodeRootExportDecl {...@@ -122,87 +137,6 @@ struct AstNodeRootExportDecl {
122 Buf name;137 Buf name;
123};138};
124139
125struct AstNodeBoolAndExpr {
126 AstNode *op1;
127 // if op2 is non-null, do boolean and, otherwise nothing
128 AstNode *op2;
129};
130
131enum CmpOp {
132 CmpOpInvalid,
133 CmpOpEq,
134 CmpOpNotEq,
135 CmpOpLessThan,
136 CmpOpGreaterThan,
137 CmpOpLessOrEq,
138 CmpOpGreaterOrEq,
139};
140
141struct AstNodeComparisonExpr {
142 AstNode *op1;
143 CmpOp cmp_op;
144 // if op2 is non-null, do cmp_op, otherwise nothing
145 AstNode *op2;
146};
147
148struct AstNodeBinOrExpr {
149 AstNode *op1;
150 // if op2 is non-null, do binary or, otherwise nothing
151 AstNode *op2;
152};
153
154struct AstNodeBinXorExpr {
155 AstNode *op1;
156 // if op2 is non-null, do binary xor, otherwise nothing
157 AstNode *op2;
158};
159
160struct AstNodeBinAndExpr {
161 AstNode *op1;
162 // if op2 is non-null, do binary and, otherwise nothing
163 AstNode *op2;
164};
165
166enum BitShiftOp {
167 BitShiftOpInvalid,
168 BitShiftOpLeft,
169 BitShiftOpRight,
170};
171
172struct AstNodeBitShiftExpr {
173 AstNode *op1;
174 BitShiftOp bit_shift_op;
175 // if op2 is non-null, do bit_shift_op, otherwise nothing
176 AstNode *op2;
177};
178
179enum AddOp {
180 AddOpInvalid,
181 AddOpAdd,
182 AddOpSub,
183};
184
185struct AstNodeAddExpr {
186 AstNode *op1;
187 AddOp add_op;
188 // if op2 is non-null, do add_op, otherwise nothing
189 AstNode *op2;
190};
191
192enum MultOp {
193 MultOpInvalid,
194 MultOpMult,
195 MultOpDiv,
196 MultOpMod,
197};
198
199struct AstNodeMultExpr {
200 AstNode *op1;
201 MultOp mult_op;
202 // if op2 is non-null, do mult_op, otherwise nothing
203 AstNode *op2;
204};
205
206struct AstNodeCastExpr {140struct AstNodeCastExpr {
207 AstNode *primary_expr;141 AstNode *primary_expr;
208 // if type is non-null, do cast, otherwise nothing142 // if type is non-null, do cast, otherwise nothing
...@@ -249,18 +183,10 @@ struct AstNode {...@@ -249,18 +183,10 @@ struct AstNode {
249 AstNodeParamDecl param_decl;183 AstNodeParamDecl param_decl;
250 AstNodeBlock block;184 AstNodeBlock block;
251 AstNodeReturnExpr return_expr;185 AstNodeReturnExpr return_expr;
252 AstNodeBoolOrExpr bool_or_expr;186 AstNodeBinOpExpr bin_op_expr;
253 AstNodeFnCall fn_call;187 AstNodeFnCall fn_call;
254 AstNodeExternBlock extern_block;188 AstNodeExternBlock extern_block;
255 AstNodeDirective directive;189 AstNodeDirective directive;
256 AstNodeBoolAndExpr bool_and_expr;
257 AstNodeComparisonExpr comparison_expr;
258 AstNodeBinOrExpr bin_or_expr;
259 AstNodeBinXorExpr bin_xor_expr;
260 AstNodeBinAndExpr bin_and_expr;
261 AstNodeBitShiftExpr bit_shift_expr;
262 AstNodeAddExpr add_expr;
263 AstNodeMultExpr mult_expr;
264 AstNodeCastExpr cast_expr;190 AstNodeCastExpr cast_expr;
265 AstNodePrimaryExpr primary_expr;191 AstNodePrimaryExpr primary_expr;
266 AstNodeGroupedExpr grouped_expr;192 AstNodeGroupedExpr grouped_expr;