Add or operator
This commit is contained in:
parent
72d4ab4217
commit
51150269cc
5 changed files with 14 additions and 3 deletions
|
@ -17,6 +17,7 @@ let _types_ =
|
|||
; "%slt", Func_t (Bool_t, [ Int_t; Int_t ])
|
||||
; "%sne", Func_t (Bool_t, [ Int_t; Int_t ])
|
||||
; "%and", Func_t (Int_t, [ Int_t; Int_t ])
|
||||
; "%or", Func_t (Int_t, [ Int_t; Int_t ])
|
||||
; "puti", Func_t (Void_t, [ Int_t ])
|
||||
; "puts", Func_t (Void_t, [ Str_t ])
|
||||
; "geti", Func_t (Int_t, [])
|
||||
|
@ -40,6 +41,7 @@ let builtins =
|
|||
; "%slt", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Slt (V0, T0, T1) ]
|
||||
; "%sne", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Sne (V0, T0, T1) ]
|
||||
; "%and", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); And (V0, T0, T1) ]
|
||||
; "%or", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Or (V0, T0, T1) ]
|
||||
; "puti", [ Lw (A0, Mem (SP, 0)); Li (V0, Syscall.print_int); Syscall ]
|
||||
; "puts", [ Lw (A0, Mem (SP, 0)); Li (V0, Syscall.print_str); Syscall ]
|
||||
; "geti", [ Lw (A0, Mem (SP, 0)); Li (V0, Syscall.read_int); Syscall ]
|
||||
|
|
|
@ -42,6 +42,7 @@ rule token = parse
|
|||
| "<" { Lslt }
|
||||
| "!=" { Lsne }
|
||||
| '&' { Land }
|
||||
| '|' { Lor }
|
||||
| '"' { read_string (Buffer.create 16) lexbuf }
|
||||
| ident as i { Lvar i }
|
||||
| '#' { comment lexbuf }
|
||||
|
|
3
mips.ml
3
mips.ml
|
@ -56,6 +56,7 @@ type instr =
|
|||
| Slt of reg * reg * reg
|
||||
| Sne of reg * reg * reg
|
||||
| And of reg * reg * reg
|
||||
| Or of reg * reg * reg
|
||||
| Syscall
|
||||
| B of label
|
||||
| Beq of reg * reg * label
|
||||
|
@ -151,6 +152,8 @@ let fmt_instr ?(indent = " ") = function
|
|||
Printf.sprintf "%ssne %s, %s, %s" indent (fmt_reg rd) (fmt_reg rs) (fmt_reg rt)
|
||||
| And (rd, rs, rt) ->
|
||||
Printf.sprintf "%sand %s, %s, %s" indent (fmt_reg rd) (fmt_reg rs) (fmt_reg rt)
|
||||
| Or (rd, rs, rt) ->
|
||||
Printf.sprintf "%sor %s, %s, %s" indent (fmt_reg rd) (fmt_reg rs) (fmt_reg rt)
|
||||
| Syscall -> Printf.sprintf "%ssyscall" indent
|
||||
| B l -> Printf.sprintf "%sb %s" indent l
|
||||
| Beq (rs, rt, l) ->
|
||||
|
|
|
@ -12,11 +12,11 @@
|
|||
%token Lbracedeb Lbracefin
|
||||
%token Lpardeb Lparfin Lcomma
|
||||
%token Ladd Lsub Lmul Ldiv Lrem Lseq Lsge Lsgt Lsle Lslt Lsne
|
||||
%token Land
|
||||
%token Land Lor
|
||||
%token Lif Lelse Lwhile
|
||||
|
||||
%left Ladd Lsub Lmul Ldiv Lrem Lseq Lsge Lsgt Lsle Lslt Lsne
|
||||
%left Land
|
||||
%left Land Lor
|
||||
|
||||
%left Lbracedeb Lparfin Lbracefin Lreturn
|
||||
%left Ltype Lbool Lint Lvar Lstr
|
||||
|
@ -218,6 +218,11 @@ expr:
|
|||
Call { func = "%and" ; args = [ a ; b ] ; pos = $startpos($2) }
|
||||
}
|
||||
|
||||
/* e || e */
|
||||
| a = expr ; Lor ; Lor ; b = expr {
|
||||
Call { func = "%or" ; args = [ a ; b ] ; pos = $startpos($2) }
|
||||
}
|
||||
|
||||
/* function(a */
|
||||
| f = Lvar ; Lpardeb ; a = args_expr {
|
||||
Call { func = f ; args = a ; pos = $startpos(a) }
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
void main () {
|
||||
if (1 && 3 < 4) {
|
||||
if (1 && 3 < 4 || 1) {
|
||||
puti(1);
|
||||
} else {
|
||||
puti(0);
|
||||
|
|
Reference in a new issue