package ostap

  1. Overview
  2. Docs

Source file PrioReorderer.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
(*
 * PrioReorderer: reordering expression trees by priorities.
 * Copyright (C) 2008
 * Dmitri Boulytchev, St.Petersburg State University
 * 
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License version 2, as published by the Free Software Foundation.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * 
 * See the GNU Library General Public License version 2 for more details
 * (enclosed in the file COPYING).
 *)

module type Expression =
  sig

    type t              
   
    val discover : t -> [`Infix of int * t * t | `Other]
    val replace  : t -> t -> t -> t
    val map      : (t -> t) -> t -> t
	  
  end
      
module Make (E : Expression) = 
  struct
      
    let rec sort expr =
      let rec reduce p ((oper, opnd) as stacks) = 
        match oper with
        | (t, p') :: oper' when (p <= p') ->
            let r::l::tl = opnd in
            reduce p (oper', (E.replace t l r) :: tl)
        | _ -> stacks
      in
      let rec putin t ((oper, opnd) as stacks) =
	match E.discover t with
	| `Infix (p, l, r) ->
            let oper', opnd' = reduce p (putin l stacks) in
            putin r ((t, p) :: oper', opnd')
	| `Other -> (oper, (E.map sort t) :: opnd)
      in 
      let _, result::_ = reduce (-1) (putin expr ([], [])) in
      result    

  end