<commit_before>signature TRANSLATE = 
sig
	type level 
	type access (*not same as Frame.access*)
  type exp

	val outermost : level
	val newLevel : {parent: level, name: Temp.label, formals: bool list} -> level
	val formals : level -> access list
	val allocLocal : level -> bool -> access
	
  val procEntryExit : {level: level, body: exp} -> unit
  val getResult : unit -> FRAME.frag list

  val simpleVar : access * level -> exp
end


structure Translate : TRANSLATE = 

  structure Te = Temp
  structure Tr = Tree
  structure F = MipsFrame
  structure A = Absyn
  val err = ErrorMsg.error
  exception ErrMsg

  type frag = FRAME.frag

  datatype level = ROOT
                  | CHILD of {parent: level, frame: F.frame}
  type access = level * F.access

  val outermost = ROOT

  val fragList = ref []

  fun newLevel {parent: level, name: Te.label, formals: bool list} = 
    let 
      val formals' = true::formals
      val newframe = F.newFrame(name, formals')
    in
      CHILD(parent, newframe)
    end

  fun formals(level) = 
    let
      val theFrame = (#frame level)
      val theFormals = F.formals(theFrame)
    in
      case theFormals 
        of a::rest => rest
    end

  fun allocLocal(level) = 
    let 
      fun allocL escape:bool = 
        let
          val theFrame = (#frame level)
          val frameAccess = F.allocLocal theFrame escape
        in
          (level, frameAccess)
        end
    in
      allocL
    end


  datatype exp = Ex of Tr.exp
               | Nx of Tr.stm
               | Cx of Te.label * Te.label -> Tr.stm

  fun unEx (Ex e) = e
    | unEx (Nx s) = Tr.ESEQ(s, Tr.CONST 0)
    | unEx (Cx genstm) = 
      let
        val r = Te.newtemp()
        val t = Te.newlabel() and f = Te.newlabel()
      in
        Tr.ESEQ(seq[Tr.MOVE(Tr.TEMP r, Tr.CONST 1),
                    genstm(t, f),
                    Tr.LABEL f,
                    Tr.MOVE(Tr.TEMP r, Tr.CONST 0),
                    Tr.LABEL t],
                Tr.TEMP r)
      end

  fun unNx (Ex e) = Tr.EXP(e)
    | unNx (Nx s) = s
    | unNx (Cx genstm) = Tr.EXP(unEx(genstm)) (*evaluate the exp for side-effects and discard the result*)

  fun unCx (Cx genstm) = genstm
    | unCx (Nx _) = ErrorMsg.impossible "Error: cannot unCx(Nx stm)!"
    | unCx (Tr.CONST 0) = fn (t, f) => Tr.JUMP(Tr.NAME(f), [f])
    | unCx (Tr.CONST 1) = fn (t, f) => Tr.JUMP(Tr.NAME(t), [t])
    | unCx (Ex e) = fn (t, f) => Tr.CJUMP(Tr.NE, e, Tr.CONST 0, t, f)

  (*how is the current level useful here? I guess static links come into play here...*)
  fun simpleVar((varL: level, fa: F.access), l: level) =
    let
      f = (#frame varL) (*this is the frame the variable was declared*)
    in
      Ex (f.exp(fa) Tr.TEMP(f.FP))
    end

  fun fieldVar(varExp, index) = Ex (Tr.MEM(Tr.BINOP(Tr.PLUS, varExp, Tr.CONST (index*F.wordSize))))

  fun subscriptVar(varExp, Tr.CONST index) = Ex (Tr.MEM(Tr.BINOP(Tr.PLUS, varExp, Tr.CONST (index*F.wordSize))))

  fun procEntryExit(level, body) = 
    let
      val funFrame = (#frame level)
      val addedSteps = F.procEntryExit1(funFrame, unNx(body))
      val moveStm = Tr.MOVE((Tr.TEMP funFrame.RV), body)
      val addedMove = Tr.SEQ(addedSteps, moveStm)
    in
      fragList := (F.PROC {body = addedMove, frame = funFrame})::(!fragList)
    end

  fun getResult() = !fragList

  fun nilExp () = Ex (Tr.CONST (0))
  fun intExp (i) = Ex (Tr.CONST (i))

  (*all kinds of transformations*)

  fun strExp str =
  let
    val label = Te.newlabel()
  in
    (fragList := F.STRING (label, str) :: !fragList;
    Ex (Tr.NAME label)) (*does string uses Tr.NAME???*)
  end

  fun seq stm:[] = stm
    | seq [stm::rest] = Tr.SEQ(stm, seq(rest))

  fun seqExp [] = Ex (Tr.CONST 0)
    | seqExp [exp] = exp
    | seqExp (exp :: exps) =
        Ex (Tr.ESEQ (unNx exp, unEx (seqExp exps)))
<commit_msg>translate syntax debug<commit_after>signature TRANSLATE = 
sig
	type level 
	type access (*not same as Frame.access*)
  type exp

	val outermost : level
	val newLevel : {parent: level, name: Temp.label, formals: bool list} -> level
	val formals : level -> access list
	val allocLocal : level -> bool -> access
	
  val procEntryExit : {level: level, body: exp} -> unit
  val getResult : unit -> FRAME.frag list

  val simpleVar : access * level -> exp
end


structure Translate :> TRANSLATE = 
struct

  structure Te = Temp
  structure Tr = Tree
  structure F = MipsFrame
  structure A = Absyn
  val err = ErrorMsg.error
  exception ErrMsg

  type frag = FRAME.frag

  datatype level = ROOT
                  | CHILD of {parent: level, frame: F.frame}
  type access = level * F.access

  val outermost = ROOT

  val fragList = ref []

  fun newLevel {parent: level, name: Te.label, formals: bool list} = 
    let 
      val formals' = true::formals
      val newframe = F.newFrame(name, formals')
    in
      CHILD(parent, newframe)
    end

  fun formals(level) = 
    let
      val theFrame = (#frame level)
      val theFormals = F.formals(theFrame)
    in
      case theFormals 
        of a::rest => rest
    end

  fun allocLocal(level) = 
    let 
      fun allocL escape:bool = 
        let
          val theFrame = (#frame level)
          val frameAccess = F.allocLocal theFrame escape
        in
          (level, frameAccess)
        end
    in
      allocL
    end


  datatype exp = Ex of Tr.exp
               | Nx of Tr.stm
               | Cx of Te.label * Te.label -> Tr.stm

  fun unEx (Ex e) = e
    | unEx (Nx s) = Tr.ESEQ(s, Tr.CONST 0)
    | unEx (Cx genstm) = 
      let
        val r = Te.newtemp()
        val t = Te.newlabel() and f = Te.newlabel()
      in
        Tr.ESEQ(seq[Tr.MOVE(Tr.TEMP r, Tr.CONST 1),
                    genstm(t, f),
                    Tr.LABEL f,
                    Tr.MOVE(Tr.TEMP r, Tr.CONST 0),
                    Tr.LABEL t],
                Tr.TEMP r)
      end

  fun unNx (Ex e) = Tr.EXP(e)
    | unNx (Nx s) = s
    | unNx (Cx genstm) = Tr.EXP(unEx(genstm)) (*evaluate the exp for side-effects and discard the result*)

  fun unCx (Cx genstm) = genstm
    | unCx (Nx _) = (ErrorMsg.impossible "Error: cannot unCx(Nx stm)!"; (fn (t, f) => Tr.JUMP(Tr.NAME(f), [f])))
    | unCx (Tr.CONST 0) = (fn (t, f) => Tr.JUMP(Tr.NAME(f), [f]))
    | unCx (Tr.CONST 1) = (fn (t, f) => Tr.JUMP(Tr.NAME(t), [t]))
    | unCx (Ex e) = (fn (t, f) => Tr.CJUMP(Tr.NE, e, Tr.CONST 0, t, f))

  (*how is the current level useful here? I guess static links come into play here...*)
  fun simpleVar((varL: level, fa: F.access), l: level) =
    let
      val f = (#frame varL) (*this is the frame the variable was declared*)
    in
      Ex (f.exp(fa) Tr.TEMP(f.FP))
    end

  fun fieldVar(varExp, index) = Ex (Tr.MEM(Tr.BINOP(Tr.PLUS, varExp, Tr.CONST (index*F.wordSize))))

  fun subscriptVar(varExp, Tr.CONST index) = Ex (Tr.MEM(Tr.BINOP(Tr.PLUS, varExp, Tr.CONST (index*F.wordSize))))

  fun procEntryExit(level, body) = 
    let
      val funFrame = (#frame level)
      val addedSteps = F.procEntryExit1(funFrame, unNx(body))
      val moveStm = Tr.MOVE((Tr.TEMP funFrame.RV), body)
      val addedMove = Tr.SEQ(addedSteps, moveStm)
    in
      fragList := (F.PROC {body = addedMove, frame = funFrame})::(!fragList)
    end

  fun getResult() = !fragList

  fun nilExp () = Ex (Tr.CONST (0))
  fun intExp (i) = Ex (Tr.CONST (i))

  (*all kinds of transformations*)

  fun strExp str =
  let
    val label = Te.newlabel()
  in
    (fragList := F.STRING (label, str) :: !fragList;
    Ex (Tr.NAME label)) (*does string uses Tr.NAME???*)
  end

  fun seq stm::[] = stm
    | seq [stm::rest] = Tr.SEQ(stm, seq(rest))

  fun seqExp [] = Ex (Tr.CONST 0)
    | seqExp [exp] = exp
    | seqExp (exp :: exps) =
        Ex (Tr.ESEQ (unNx exp, unEx (seqExp exps)))

{'fraction_non_alphanumeric': 0.13151428940902626, 'fraction_numerical': 0.002327686538212854, 'mean_word_length': 2.218476903870162, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7191632', 'n_tokens_mistral': 2896, 'n_tokens_neox': 2749, 'n_words': 859}