ce/ast.ml

172 lines
3.9 KiB
OCaml
Raw Normal View History

2022-01-18 16:52:33 +09:00
module Type = struct
type t =
| Int
| Float
2022-02-01 21:38:00 +09:00
| Function
| External
2022-01-20 23:36:53 +09:00
| String
2022-01-18 16:52:33 +09:00
let to_string = function
| Int -> "int"
| Float -> "float"
2022-01-20 23:36:53 +09:00
| String -> "string"
2022-02-01 21:38:00 +09:00
| Function -> "fun"
| External -> "external"
2022-01-18 16:52:33 +09:00
2022-01-25 15:28:38 +09:00
let supertype = function
| Int -> Some Float
| _ -> None
2022-01-18 16:52:33 +09:00
end
2022-02-01 21:38:00 +09:00
(* simple, untyped AST. *)
type t =
| Value of value
| Var of string
| Let of string * t
| Unary of operator * t
| Binop of t * operator * t
| Apply of t * t list (* function application *)
| Set_binop_pre of operator * t
| Get_binop_pre of operator
| Set_binop_aso of operator * string
| Get_binop_aso of operator
and value =
| Int of int
| Float of float
| String of string
| Function of string list * t
| External of string
| Nop (* return of system operations (will be deprecated) *)
and operator =
| Add | Sub | Mul | Div (* arithmetics *)
| Mod (* modular operation *)
| Exp (* exponentation *)
| Negate
2022-01-18 16:52:33 +09:00
exception Invalid_type of Type.t
module Value = struct
2022-02-01 21:38:00 +09:00
type t = value
2022-01-18 16:52:33 +09:00
let to_string = function
2022-01-20 23:36:53 +09:00
| Int n -> string_of_int n
| Float n -> string_of_float n
| String s -> "\"" ^ s ^ "\""
2022-02-01 21:38:00 +09:00
| Function (vars, _) ->
Printf.sprintf "function with %d arguments" @@ List.length vars
| External f -> "external " ^ f
2022-01-18 16:52:33 +09:00
| Nop -> "nop"
let typeof = function
| Int _ -> Type.Int
| Float _ -> Type.Float
2022-01-20 23:36:53 +09:00
| String _ -> Type.String
2022-02-01 21:38:00 +09:00
| Function _ -> Type.Function
| External _ -> Type.External
2022-01-18 16:52:33 +09:00
| Nop -> failwith "Value.typeof"
let promote = function
| Int n -> Float (float n)
| Float n -> Float n
| _ -> failwith "Value.promote"
end
2022-01-28 00:56:24 +09:00
(* operators *)
module Operator = struct
2022-02-01 21:38:00 +09:00
type t = operator
2022-01-18 16:52:33 +09:00
2022-01-25 15:28:38 +09:00
exception Unavailable of t
2022-01-18 16:52:33 +09:00
let to_string = function
| Add -> "+"
| Sub -> "-"
| Mul -> "*"
| Div -> "/"
| Mod -> "%"
| Exp -> "^"
2022-01-28 00:56:24 +09:00
| Negate -> "-"
let negate = function
2022-02-01 21:38:00 +09:00
| Int n -> Int ~-n
| Float n -> Float ~-.n
2022-01-28 00:56:24 +09:00
| _ -> failwith "Operator.negate"
2022-01-18 16:52:33 +09:00
let vi f a b =
match a, b with
| Int a, Int b -> Int (f a b)
| _ -> raise @@ Invalid_type Int
let vf f a b =
match a, b with
| Float a, Float b -> Float (f a b)
| _ -> raise @@ Invalid_type Float
let operators =
let open Type in
2022-01-25 15:28:38 +09:00
let ip = Int, Int and fp = Float, Float in
2022-01-18 16:52:33 +09:00
[
2022-01-25 15:28:38 +09:00
Add, [ip, vi Int.add; fp, vf Float.add];
Sub, [ip, vi Int.sub; fp, vf Float.sub];
Mul, [ip, vi Int.mul; fp, vf Float.mul];
Div, [ip, vi Int.div; fp, vf Float.div];
Mod, [ip, vi Int.rem; fp, vf Float.rem];
Exp, [fp, vf Float.pow];
2022-01-18 16:52:33 +09:00
]
|> List.to_seq
|> Hashtbl.of_seq
2022-01-25 15:28:38 +09:00
let get_types op =
match Hashtbl.find_opt operators op with
| None -> raise @@ Unavailable op
| Some p -> List.map fst p
2022-01-28 00:56:24 +09:00
let get_unary = function
| Negate -> negate
| op -> raise @@ Unavailable op
let get_binary op typ =
2022-01-18 16:52:33 +09:00
Hashtbl.find operators op
|> List.assoc_opt typ
end
2022-01-10 01:31:47 +09:00
let value v = Value v
2022-01-23 01:20:34 +09:00
let unary op t =
Unary (op, t)
2022-01-10 01:31:47 +09:00
let binop left op right =
Binop (left, op, right)
(* print ast LISP style. *)
let print ast =
let pr = Printf.printf in
2022-01-18 16:52:33 +09:00
let pv v = pr "%s" @@ Value.to_string v in
2022-01-10 23:11:13 +09:00
let rec aux = function
2022-01-10 01:31:47 +09:00
| Value n -> pv n
2022-01-19 14:17:04 +09:00
| Var v -> pr "%s" v
2022-01-21 00:17:01 +09:00
| Let (v, e) ->
2022-01-29 20:01:48 +09:00
pr "(let %s " v; aux e; pr ")"
| Unary (op, t) ->
2022-02-01 21:38:00 +09:00
let op = Operator.to_string op in
pr "(%s " op; aux t; pr ")"
2022-01-29 20:01:48 +09:00
| Binop (left, op, right) ->
2022-02-01 21:38:00 +09:00
let op = Operator.to_string op in
pr "(%s " op; aux left; pr " "; aux right; pr ")"
2022-01-29 20:01:48 +09:00
| Apply (f, args) ->
pr "("; List.iter aux @@ f::args; pr ")"
2022-01-10 01:31:47 +09:00
| Set_binop_pre (op, pre) ->
2022-01-28 00:56:24 +09:00
pr "(set_pre %s " (Operator.to_string op);
2022-01-10 01:31:47 +09:00
aux pre;
pr ")"
2022-01-11 01:05:29 +09:00
| Get_binop_pre op ->
2022-01-28 00:56:24 +09:00
pr "(get_pre %s)" (Operator.to_string op)
2022-01-20 23:36:53 +09:00
| Set_binop_aso (op, aso) ->
2022-01-28 00:56:24 +09:00
pr "(set_assoc %s %s)" (Operator.to_string op) aso
2022-01-20 23:36:53 +09:00
| Get_binop_aso op ->
2022-01-28 00:56:24 +09:00
pr "(get_pre %s)" (Operator.to_string op)
2022-01-10 01:31:47 +09:00
in
aux ast; pr "\n"