Commits

Paweł Wieczorek committed a32e682

infix operators works good

  • Participants
  • Parent commits 6877375

Comments (0)

Files changed (2)

         | Binop(op, e1, e2) -> 
             let assoc = assoc_binop op in
             let prio  = prio_binop op in
-            psp_infix prio assoc (psp_binop op) (psp_expr e1) (psp_expr e2)
+            psp_infix assoc prio (psp_binop op) (psp_expr e1) (psp_expr e2)
                 
 
         | Unop (op, e1) -> psp_nested 0
             ]
 
 
-    let expr1 = Int 5
-    let expr2 = Int 10
-    let expr3 = Int 15
+    let expr_a = Int 5
+    let expr_b = Int 10
+    let expr_c = Int 15
 
-    (* (5 + 10) + 15 *)
-    let expr_12   = Binop(Add, expr1,   expr2)
-    let expr_12_3 = Binop(Add, expr_12, expr3)
+    let expr_aADDb   = Binop(Add, expr_a,   expr_b)
+    let expr_bADDc   = Binop(Add, expr_b, expr_c)
 
-    (* 5 + (10 + 15) *)
-    let expr_23   = Binop(Add, expr2, expr3)
-    let expr_1_23 = Binop(Add, expr1, expr_23)
+    let expr_aSUBb   = Binop(Sub, expr_a,   expr_b)
+    let expr_bSUBc   = Binop(Sub, expr_b, expr_c)
+
+    let expr_aMULb   = Binop(Mul, expr_a,   expr_b)
+    let expr_bMULc   = Binop(Mul, expr_b, expr_c)
+
+
+
+
+    let exprs =
+        let mk_pair a b = (a,b) in
+        [ mk_pair
+            "(5 + 10) + 15"
+            (Binop(Add, expr_aADDb, expr_c))
+
+        ; mk_pair
+            "5 + (10 + 15)"
+            (Binop(Add, expr_a, expr_bADDc))
+
+        ; mk_pair
+            "(5 - 10) - 15"
+            (Binop(Sub, expr_aSUBb, expr_c))
+
+        ; mk_pair
+            "5 - (10 - 15)"
+            (Binop(Sub, expr_a, expr_bSUBc))
+
+        ; mk_pair
+            "(5 - 10) + 15"
+            (Binop(Add, expr_aSUBb, expr_c))
+
+        ; mk_pair
+            "5 + (10 - 15)"
+            (Binop(Add, expr_a, expr_bSUBc))
+
+        ; mk_pair
+            "(5 + 10) - 15"
+            (Binop(Sub, expr_aADDb, expr_c))
+
+        ; mk_pair
+            "5 - (10 + 15)"
+            (Binop(Sub, expr_a, expr_bADDc))
+
+        ; mk_pair
+            "(5 * 10) + 15"
+            (Binop(Add, expr_aMULb, expr_c))
+
+        ; mk_pair
+            "5 + (10 * 15)"
+            (Binop(Add, expr_a, expr_bMULc))
+
+        ; mk_pair
+            "(5 + 10) * 15"
+            (Binop(Mul, expr_aADDb, expr_c))
+
+        ; mk_pair
+            "5 * (10 + 15)"
+            (Binop(Mul, expr_a, expr_bADDc))
+
+       ]
 
 end
 
 open ArithmeticExpressions
-
 ;;
 
+List.iter (fun (s,x) -> print_string ">> "; print_endline s; print_painter_nl (psp_expr x); print_newline ()) exprs
 
-print_painter_nl (psp_expr expr_12_3 );;
-print_painter_nl (psp_expr expr_1_23 )
-

src/Prioritized.ml

 
 let psp_reprioritize n (_, sp) = (n, sp)
 
+let psp_max_priority = pred max_int
+
 (*--------------------------------------------------------------------------------------------------------------------
  * Basic combinators
  *)
 
-let psp_word            = embed_sp1 0 Engine.sp_word
-let psp_keyword         = embed_sp1 0 Engine.sp_keyword
-let psp_value           = embed_sp1 0 Engine.sp_value
-let psp_value_keyword   = embed_sp1 0 Engine.sp_value_keyword
-let psp_operator        = embed_sp1 0 Engine.sp_operator
-let psp_syntax          = embed_sp1 0 Engine.sp_syntax
+let psp_word            = embed_sp1 psp_max_priority Engine.sp_word
+let psp_keyword         = embed_sp1 psp_max_priority Engine.sp_keyword
+let psp_value           = embed_sp1 psp_max_priority Engine.sp_value
+let psp_value_keyword   = embed_sp1 psp_max_priority Engine.sp_value_keyword
+let psp_operator        = embed_sp1 psp_max_priority Engine.sp_operator
+let psp_syntax          = embed_sp1 psp_max_priority Engine.sp_syntax
 
-let psp_value_int       = embed_sp1 0 Basic.sp_value_int
+let psp_value_int       = embed_sp1 psp_max_priority Basic.sp_value_int
 
 
 (*--------------------------------------------------------------------------------------------------------------------
     ; psp_closing
     ]
 
-let psp_std_bracket    = psp_bracket (psp_syntax "(") (psp_syntax ")") 0
-let psp_square_bracket = psp_bracket (psp_syntax "[") (psp_syntax "]") 0
+let psp_std_bracket    = psp_bracket (psp_syntax "(") (psp_syntax ")") psp_max_priority
+let psp_square_bracket = psp_bracket (psp_syntax "[") (psp_syntax "]") psp_max_priority
 let psp_no_bracket     = psp_reprioritize 0
 
 let psp_decide_bracket brackets = function
         ]
 
 let psp_left_associative_infix = psp_associative_infix
-    (fun prio_left  prio -> prio < prio_left)
-    (fun prio prio_right -> prio < succ prio_right)
+    (fun prio_left  prio -> prio_left  < prio)
+    (fun prio prio_right -> prio_right < succ prio)
 
 let psp_right_associative_infix = psp_associative_infix
-    (fun prio_left  prio -> prio < succ prio_left)
-    (fun prio_right prio -> prio < prio_right)
+    (fun prio_left  prio -> prio_left  < succ prio)
+    (fun prio prio_right -> prio_right < prio)
 
+let psp_no_associative_infix = psp_associative_infix
+    (fun prio_left  prio -> prio_left  < succ prio)
+    (fun prio prio_right -> prio_right < succ prio)
 
 
-let psp_infix prio assoc oper =
-    match assoc with
-        | LeftAssociative  -> psp_left_associative_infix prio oper
-        | RightAssociative -> psp_right_associative_infix prio oper
-        | NoAssociative    -> psp_left_associative_infix prio oper 
+let psp_infix = function
+    | LeftAssociative  -> psp_left_associative_infix 
+    | RightAssociative -> psp_right_associative_infix 
+    | NoAssociative    -> psp_no_associative_infix 
 
 (*--------------------------------------------------------------------------------------------------------------------
  * Output