use ocamlformat
This commit is contained in:
parent
5224133837
commit
47adc5c8ed
7 changed files with 45 additions and 44 deletions
1
.ocamlformat
Normal file
1
.ocamlformat
Normal file
|
@ -0,0 +1 @@
|
|||
profile = janestreet
|
9
ast.ml
9
ast.ml
|
@ -1,10 +1,11 @@
|
|||
module Syntax = struct
|
||||
type expr =
|
||||
| Int of { value: int
|
||||
; pos: Lexing.position }
|
||||
| Int of
|
||||
{ value : int
|
||||
; pos : Lexing.position
|
||||
}
|
||||
end
|
||||
|
||||
module IR = struct
|
||||
type expr =
|
||||
| Int of int
|
||||
type expr = Int of int
|
||||
end
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
open Ast
|
||||
|
||||
module Env = Map.Make(String)
|
||||
module Env = Map.Make (String)
|
||||
|
||||
let _types_ = Env.empty
|
||||
|
||||
let builtins = []
|
||||
|
|
10
compiler.ml
10
compiler.ml
|
@ -1,12 +1,10 @@
|
|||
open Ast.IR
|
||||
open Mips
|
||||
|
||||
module Env = Map.Make(String)
|
||||
module Env = Map.Make (String)
|
||||
|
||||
let rec compile_expr e =
|
||||
match e with
|
||||
| Int n -> [ Li (V0, n) ]
|
||||
| Int n -> [ Li (V0, n) ]
|
||||
;;
|
||||
|
||||
let compile ir =
|
||||
{ text = Baselib.builtins @ compile_expr ir
|
||||
; data = [] }
|
||||
let compile ir = { text = Baselib.builtins @ compile_expr ir; data = [] }
|
||||
|
|
27
main.ml
27
main.ml
|
@ -4,27 +4,30 @@ open Lexing
|
|||
open Ast
|
||||
|
||||
let err msg pos =
|
||||
Printf.eprintf "Error on line %d col %d: %s.\n"
|
||||
pos.pos_lnum (pos.pos_cnum - pos.pos_bol) msg ;
|
||||
Printf.eprintf
|
||||
"Error on line %d col %d: %s.\n"
|
||||
pos.pos_lnum
|
||||
(pos.pos_cnum - pos.pos_bol)
|
||||
msg;
|
||||
exit 1
|
||||
;;
|
||||
|
||||
let () =
|
||||
if (Array.length Sys.argv) != 2 then begin
|
||||
Printf.eprintf "Usage: %s <file>\n" Sys.argv.(0) ;
|
||||
exit 1
|
||||
end;
|
||||
if Array.length Sys.argv != 2
|
||||
then (
|
||||
Printf.eprintf "Usage: %s <file>\n" Sys.argv.(0);
|
||||
exit 1);
|
||||
let f = open_in Sys.argv.(1) in
|
||||
let buf = Lexing.from_channel f in
|
||||
try
|
||||
let parsed = Parser.prog Lexer.token buf in
|
||||
close_in f ;
|
||||
close_in f;
|
||||
let ast = Semantics.analyze parsed in
|
||||
let asm = Compiler.compile ast in
|
||||
Mips.emit Stdlib.stdout asm
|
||||
with
|
||||
| Lexer.Error c ->
|
||||
err (Printf.sprintf "unrecognized char '%c'" c) (Lexing.lexeme_start_p buf)
|
||||
| Parser.Error ->
|
||||
err "syntax error" (Lexing.lexeme_start_p buf)
|
||||
| Semantics.Error (msg, pos) ->
|
||||
err msg pos
|
||||
err (Printf.sprintf "unrecognized char '%c'" c) (Lexing.lexeme_start_p buf)
|
||||
| Parser.Error -> err "syntax error" (Lexing.lexeme_start_p buf)
|
||||
| Semantics.Error (msg, pos) -> err msg pos
|
||||
;;
|
||||
|
|
34
mips.ml
34
mips.ml
|
@ -1,32 +1,32 @@
|
|||
type reg =
|
||||
| V0
|
||||
|
||||
type reg = V0
|
||||
type label = string
|
||||
|
||||
type instr =
|
||||
| Li of reg * int
|
||||
|
||||
type directive =
|
||||
| Asciiz of string
|
||||
|
||||
type instr = Li of reg * int
|
||||
type directive = Asciiz of string
|
||||
type decl = label * directive
|
||||
|
||||
type asm = { text: instr list ; data: decl list }
|
||||
type asm =
|
||||
{ text : instr list
|
||||
; data : decl list
|
||||
}
|
||||
|
||||
let ps = Printf.sprintf (* alias raccourci *)
|
||||
|
||||
let fmt_reg = function
|
||||
| V0 -> "$v0"
|
||||
| V0 -> "$v0"
|
||||
;;
|
||||
|
||||
let fmt_instr = function
|
||||
| Li (r, i) -> ps " li %s, %d" (fmt_reg r) i
|
||||
;;
|
||||
|
||||
let fmt_dir = function
|
||||
| Asciiz (s) -> ps ".asciiz \"%s\"" s
|
||||
| Asciiz s -> ps ".asciiz \"%s\"" s
|
||||
;;
|
||||
|
||||
let emit oc asm =
|
||||
Printf.fprintf oc ".text\n.globl main\nmain:\n" ;
|
||||
List.iter (fun i -> Printf.fprintf oc "%s\n" (fmt_instr i)) asm.text ;
|
||||
Printf.fprintf oc " move $a0, $v0\n li $v0, 1\n syscall\n jr $ra\n" ;
|
||||
Printf.fprintf oc "\n.data\n" ;
|
||||
Printf.fprintf oc ".text\n.globl main\nmain:\n";
|
||||
List.iter (fun i -> Printf.fprintf oc "%s\n" (fmt_instr i)) asm.text;
|
||||
Printf.fprintf oc " move $a0, $v0\n li $v0, 1\n syscall\n jr $ra\n";
|
||||
Printf.fprintf oc "\n.data\n";
|
||||
List.iter (fun (l, d) -> Printf.fprintf oc "%s: %s\n" l (fmt_dir d)) asm.data
|
||||
;;
|
||||
|
|
|
@ -7,6 +7,6 @@ exception Error of string * Lexing.position
|
|||
let rec analyze_expr expr env =
|
||||
match expr with
|
||||
| Syntax.Int n -> Int n.value
|
||||
;;
|
||||
|
||||
let analyze parsed =
|
||||
analyze_expr parsed Baselib._types_
|
||||
let analyze parsed = analyze_expr parsed Baselib._types_
|
||||
|
|
Reference in a new issue