From fa2e69bf68346d653d194d863c019674ea0fd7e2 Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Wed, 8 Jan 2014 18:17:03 +0100 Subject: Premier mail emile --- asm/asmlex.mll | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 asm/asmlex.mll (limited to 'asm/asmlex.mll') diff --git a/asm/asmlex.mll b/asm/asmlex.mll new file mode 100644 index 0000000..4d78e87 --- /dev/null +++ b/asm/asmlex.mll @@ -0,0 +1,98 @@ +{ + open Asm + open Asmpars + + let keywords_ri = List.map (fun (k,o) -> (k ^ "i",o)) keywords_r + + let keywords = [ + "pop",POP; + "push",PUSH; + "incri",INCRI; + "shi",SHI; + "j",JJ; + "jal",JAL; + "jr",JR; + "jalr",JALR; + "lw",LW; + "sw",SW; + "lb",LB; + "sb",SB; + "not",NOT; + "lil",LIL; + "lilz",LILZ; + "liu",LIU; + "liuz",LIUZ; + "lra",LRA; + "li",LI; + "move",MOVE; + "jz",JZ; + "jnz",JNZ + ] + + let regs = [ + "Z",0; + "RA",6; + "F",6; + "A",1; + "B",2; + "C",3; + "D",4; + "E",5; + "G",7; + "SP",7 + ] + + let read_16 n = + let res = ref 0 in + for i = 0 to String.length n - 1 do + res := 16 * !res; + let v = + let c = Char.code n.[i] in + if c >= Char.code '0' && c <= Char.code '9' then c - (Char.code '0') + else if c >= Char.code 'a' && c <= Char.code 'f' then c - (Char.code 'a') + else c - (Char.code 'A') in + res := !res + v + done; + !res + + let read_2 n = + let res = ref 0 in + for i = 0 to String.length n - 1 do + res := 2 * !res; + let v = Char.code n.[i] - Char.code '0' in + res := !res + v + done; + !res +} + +let digit = ['0'-'9'] +let alpha = ['a'-'z' 'A'-'Z'] +let hexdigit = ['a'-'f' 'A'-'F' '0'-'9'] + +rule token = parse + | eof { EOF } + | '#' { comment lexbuf } + | ['\t' '\r' ' '] { token lexbuf } + | ':' { COLON } + | '\n' { Lexing.new_line lexbuf; token lexbuf } + | ((['a'-'z'] | '_') (alpha | digit | '_')*) as id + { try ROP (List.assoc id keywords_r) + with Not_found -> try RIOP (List.assoc id keywords_ri) + with Not_found -> try List.assoc id keywords + with Not_found -> ID id } + | "0x" (((hexdigit)+) as n) + { INT (read_16 n) } + | (digit)+ as n { INT (int_of_string n) } + | "0b" (['0' '1']+ as n) { INT (read_2 n) } + | ['A'-'Z']+ as name { REG (List.assoc name regs) } + | '$' (['0'-'7'] as n) { REG (Char.code n - (Char.code '0')) } + | ".text" { TEXT } + | ".data" { DATA } + | '-' { MINUS } + | '(' { LP } + | ')' { RP } + +and comment = parse + | eof { EOF } + | '\n' { Lexing.new_line lexbuf; token lexbuf } + | _ { comment lexbuf } -- cgit v1.2.3 From 1f228ce77e8a71475930b433fb2c72521203aa99 Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Wed, 8 Jan 2014 18:19:21 +0100 Subject: Petites modifications --- asm/asmlex.mll | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'asm/asmlex.mll') diff --git a/asm/asmlex.mll b/asm/asmlex.mll index 4d78e87..726b732 100644 --- a/asm/asmlex.mll +++ b/asm/asmlex.mll @@ -23,10 +23,14 @@ "liu",LIU; "liuz",LIUZ; "lra",LRA; + "la",LA; "li",LI; "move",MOVE; "jz",JZ; - "jnz",JNZ + "jnz",JNZ; + "byte",BYTE; + "word",WORD; + "asciiz",ASCIIZ; ] let regs = [ @@ -49,8 +53,8 @@ let v = let c = Char.code n.[i] in if c >= Char.code '0' && c <= Char.code '9' then c - (Char.code '0') - else if c >= Char.code 'a' && c <= Char.code 'f' then c - (Char.code 'a') - else c - (Char.code 'A') in + else if c >= Char.code 'a' && c <= Char.code 'f' then c - (Char.code 'a') + 10 + else c - (Char.code 'A') + 10 in res := !res + v done; !res @@ -84,7 +88,8 @@ rule token = parse { INT (read_16 n) } | (digit)+ as n { INT (int_of_string n) } | "0b" (['0' '1']+ as n) { INT (read_2 n) } - | ['A'-'Z']+ as name { REG (List.assoc name regs) } + | '"' { STR (lex_str "" lexbuf) } + | ['A'-'Z']+ as name { try REG (List.assoc name regs) with Not_found -> raise (Asm_error ("no reg " ^ name))} | '$' (['0'-'7'] as n) { REG (Char.code n - (Char.code '0')) } | ".text" { TEXT } | ".data" { DATA } @@ -96,3 +101,11 @@ and comment = parse | eof { EOF } | '\n' { Lexing.new_line lexbuf; token lexbuf } | _ { comment lexbuf } + +and lex_str q = parse + | eof { q } + | '"' { q } + | "\\\"" { lex_str (q ^ "\"") lexbuf } + | "\\\\" { lex_str (q ^ "\\") lexbuf } + | "\\n" { lex_str (q ^ "\n") lexbuf } + | _ as c { lex_str (q ^ String.make 1 c) lexbuf } -- cgit v1.2.3 From 91bc6bd5554bb4267cbaf147e603791e50462242 Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Wed, 8 Jan 2014 18:20:58 +0100 Subject: 2e mail Emile --- asm/asmlex.mll | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'asm/asmlex.mll') diff --git a/asm/asmlex.mll b/asm/asmlex.mll index 4d78e87..2bd52fc 100644 --- a/asm/asmlex.mll +++ b/asm/asmlex.mll @@ -26,7 +26,12 @@ "li",LI; "move",MOVE; "jz",JZ; - "jnz",JNZ + "jnz",JNZ; + "_clock",INT 0x4000; + "_input",INT 0x4100; + "_output",INT 0x4102; + "word",WORD; + "byte",BYTE ] let regs = [ -- cgit v1.2.3