From edeb52ae64403f76dce267d8f2df78b7527096a4 Mon Sep 17 00:00:00 2001 From: Ch1keen Date: Sun, 29 Jun 2025 17:37:42 +0900 Subject: [PATCH] Apply ocamlformat --- dia.ml | 330 +++++++++++--------- diaNode.ml | 24 +- main.ml | 152 +++++---- predefined.ml | 849 ++++++++++++++++++++++++++------------------------ 4 files changed, 721 insertions(+), 634 deletions(-) diff --git a/dia.ml b/dia.ml index ba1fe5b..379db58 100644 --- a/dia.ml +++ b/dia.ml @@ -1,14 +1,11 @@ exception DiaSyntaxError of string -let dia_dbgprint str = - Printf.eprintf "[dia.ml] %s\n" str +let dia_dbgprint str = Printf.eprintf "[dia.ml] %s\n" str let rec treemaker chr depth = - match depth with - | 0 -> " " - | _ -> chr ^ treemaker chr (depth - 1) + match depth with 0 -> " " | _ -> chr ^ treemaker chr (depth - 1) -let debug_type_to_string (t: DiaNode.dia_token_type) = +let debug_type_to_string (t : DiaNode.dia_token_type) = match t with | DiaFunction _ -> "DiaFunction" | DiaFunctionParam _ -> "DiaFunctionParam" @@ -23,7 +20,7 @@ let debug_type_to_string (t: DiaNode.dia_token_type) = * | _ DiaVoid -> ... * But it didn't work. *) -let type_to_string (t: DiaNode.dia_token_type) = +let type_to_string (t : DiaNode.dia_token_type) = match t with | DiaConstant DiaInteger -> "int" | DiaConstant DiaString -> "std::string" @@ -41,194 +38,245 @@ let type_to_string (t: DiaNode.dia_token_type) = | DiaFunctionParam DiaBool -> "bool" | DiaFunctionParam DiaVoid -> "void" -let rec dia_debug_function_descriptor (node : DiaNode.dia_node) depth: unit = +let rec dia_debug_function_descriptor (node : DiaNode.dia_node) depth : unit = Printf.eprintf "%sname: %s\n" (treemaker "-" depth) node.name; - Printf.eprintf "%stoken_type: %s\n" - (treemaker "-" depth) (debug_type_to_string node.token_type); - Printf.eprintf "%snum_of_parameters: %d\n" (treemaker "-" depth) node.num_of_parameters; + Printf.eprintf "%stoken_type: %s\n" (treemaker "-" depth) + (debug_type_to_string node.token_type); + Printf.eprintf "%snum_of_parameters: %d\n" (treemaker "-" depth) + node.num_of_parameters; Printf.eprintf "%sParameters:\n" (treemaker "-" depth); - List.iter (fun e -> dia_debug_function_descriptor e (depth+1)) node.parameters + List.iter + (fun e -> dia_debug_function_descriptor e (depth + 1)) + node.parameters -let generate_header = "\n\ - /* Start of Main function */\n\ - int main(int argc, char** argv) {\n\ -" +let generate_header = + "\n/* Start of Main function */\nint main(int argc, char** argv) {\n" -let generate_comment (orig_code: string) = +let generate_comment (orig_code : string) = let list_bulleted_orig_code = - String.trim orig_code - |> String.split_on_char '\n' - |> List.map (String.cat "* ") in + String.trim orig_code |> String.split_on_char '\n' + |> List.map (String.cat "* ") + in let commented_orig_code = - List.fold_left (fun x acc -> x ^ "\n" ^ acc) - (List.nth list_bulleted_orig_code 0) - (List.drop 1 list_bulleted_orig_code) + List.fold_left + (fun x acc -> x ^ "\n" ^ acc) + (List.nth list_bulleted_orig_code 0) + (List.drop 1 list_bulleted_orig_code) in - "/**\ - * This code is generated by diac, the Dia programming language compiler.\n\ - * Version Info:\n\ - * Original Source Code:\n\ - * ```dia\n" - ^ commented_orig_code ^ - "\n\ - * ```\n\ - *\n\ -*/" + "/*** This code is generated by diac, the Dia programming language compiler.\n\ + * Version Info:\n\ + * Original Source Code:\n\ + * ```dia\n" ^ commented_orig_code ^ "\n* ```\n*\n*/" (* In C, it was a static function, a simple counter function. * In each function call, it just returns "v0", "v1", ... * But it is cumbersome in OCaml, and it was also hard to fine-tune * these C++ variables. *) -let dia_create_cpp_variable (i: int) = - Printf.sprintf "v%d" i +let dia_create_cpp_variable (i : int) = Printf.sprintf "v%d" i -let rec dia_generate_code (node: DiaNode.dia_node) (custom_functions: DiaNode.dia_node list) (var_index: int): DiaNode.dia_node * int = - if node.name = "if" - then +let rec dia_generate_code (node : DiaNode.dia_node) + (custom_functions : DiaNode.dia_node list) (var_index : int) : + DiaNode.dia_node * int = + if node.name = "if" then let node, custom_functions, _ = dia_if node custom_functions var_index 0 in - node, custom_functions - else match node.token_type with - | DiaConstant _ -> node, var_index - | DiaFunctionParam _ -> ( - dia_dbgprint (Printf.sprintf "Function parameter '%s' detected" node.name); - node, var_index - ) - | DiaFunction _ -> - dia_dbgprint (Printf.sprintf "Generating code for function '%s'" node.name); - dia_debug_function_descriptor node 0; - (* Evaluate the parameters first. *) - let parameters, _var_index = dia_generate_code_parameter node.parameters custom_functions var_index in - (* Find the node name in custom created function list *) - let cfunc = List.find_opt - (fun (e: DiaNode.dia_node) -> e.name = node.name) - custom_functions - in - match cfunc with - | Some func -> List.map (fun (p: DiaNode.dia_node) -> p.name) parameters - |> String.concat "," - |> Printf.printf "auto %s = %s(%s);\n" (dia_create_cpp_variable _var_index) node.name; - { - name = dia_create_cpp_variable _var_index; - token_type = func.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, (_var_index + 1) - | None -> - (* Find given function name in predefined function list *) - let pfunc = List.find_opt - (fun (e: Predefined.dia_predefined_function) -> e.node.name = node.name) - Predefined.functions + (node, custom_functions) + else + match node.token_type with + | DiaConstant _ -> (node, var_index) + | DiaFunctionParam _ -> + dia_dbgprint + (Printf.sprintf "Function parameter '%s' detected" node.name); + (node, var_index) + | DiaFunction _ -> ( + dia_dbgprint + (Printf.sprintf "Generating code for function '%s'" node.name); + dia_debug_function_descriptor node 0; + (* Evaluate the parameters first. *) + let parameters, _var_index = + dia_generate_code_parameter node.parameters custom_functions var_index + in + (* Find the node name in custom created function list *) + let cfunc = + List.find_opt + (fun (e : DiaNode.dia_node) -> e.name = node.name) + custom_functions in - match pfunc with - | None -> raise (DiaSyntaxError ("Error: Unknown identifier '" ^ node.name ^ "'. Don't know how to generate it.")) - | Some func -> func.generate_code { - name = node.name; - token_type = node.token_type; - num_of_parameters = node.num_of_parameters; - parameters = parameters; - next_function = node.next_function; - } _var_index - -and dia_generate_code_parameter (nodes: DiaNode.dia_node list) (custom_functions: DiaNode.dia_node list) (var_index: int): DiaNode.dia_node list * int = + match cfunc with + | Some func -> + List.map (fun (p : DiaNode.dia_node) -> p.name) parameters + |> String.concat "," + |> Printf.printf "auto %s = %s(%s);\n" + (dia_create_cpp_variable _var_index) + node.name; + ( { + name = dia_create_cpp_variable _var_index; + token_type = func.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + _var_index + 1 ) + | None -> ( + (* Find given function name in predefined function list *) + let pfunc = + List.find_opt + (fun (e : Predefined.dia_predefined_function) -> + e.node.name = node.name) + Predefined.functions + in + match pfunc with + | None -> + raise + (DiaSyntaxError + ("Error: Unknown identifier '" ^ node.name + ^ "'. Don't know how to generate it.")) + | Some func -> + func.generate_code + { + name = node.name; + token_type = node.token_type; + num_of_parameters = node.num_of_parameters; + parameters; + next_function = node.next_function; + } + _var_index)) + +and dia_generate_code_parameter (nodes : DiaNode.dia_node list) + (custom_functions : DiaNode.dia_node list) (var_index : int) : + DiaNode.dia_node list * int = match nodes with | [] -> ([], var_index) | n :: ns -> - let p, vi = dia_generate_code n custom_functions var_index in - let ps, i = dia_generate_code_parameter ns custom_functions vi in - (p :: ps, i) + let p, vi = dia_generate_code n custom_functions var_index in + let ps, i = dia_generate_code_parameter ns custom_functions vi in + (p :: ps, i) -and dia_if (node: DiaNode.dia_node) (custom_functions: DiaNode.dia_node list) (var_index: int) (if_depth: int): DiaNode.dia_node * int * int = +and dia_if (node : DiaNode.dia_node) (custom_functions : DiaNode.dia_node list) + (var_index : int) (if_depth : int) : DiaNode.dia_node * int * int = dia_dbgprint "Generating code of if-else clause"; - let _, _, _if_depth = dia_if_parse_condition node custom_functions var_index if_depth in + let _, _, _if_depth = + dia_if_parse_condition node custom_functions var_index if_depth + in dia_if_parse_bodies node custom_functions var_index (_if_depth + 1) -and dia_if_parse_condition (node: DiaNode.dia_node) (custom_functions: DiaNode.dia_node list) (var_index: int) (if_depth: int): DiaNode.dia_node * int * int = - let _, _var_index = dia_generate_code (List.nth node.parameters 0) custom_functions var_index in +and dia_if_parse_condition (node : DiaNode.dia_node) + (custom_functions : DiaNode.dia_node list) (var_index : int) + (if_depth : int) : DiaNode.dia_node * int * int = + let _, _var_index = + dia_generate_code (List.nth node.parameters 0) custom_functions var_index + in let else_clause = List.nth node.parameters 2 in match else_clause.name with (* else if () *) - | "if" -> dia_if_parse_condition else_clause custom_functions _var_index (if_depth + 1) + | "if" -> + dia_if_parse_condition else_clause custom_functions _var_index + (if_depth + 1) (* else *) - | _ -> dia_dbgprint (Printf.sprintf "if_parse_condition: depth is %d" if_depth); node, var_index, if_depth + | _ -> + dia_dbgprint (Printf.sprintf "if_parse_condition: depth is %d" if_depth); + (node, var_index, if_depth) -and dia_if_parse_bodies (node: DiaNode.dia_node) (custom_functions: DiaNode.dia_node list) (var_index: int) (if_depth: int): DiaNode.dia_node * int * int = +and dia_if_parse_bodies (node : DiaNode.dia_node) + (custom_functions : DiaNode.dia_node list) (var_index : int) + (if_depth : int) : DiaNode.dia_node * int * int = (* if { }*) let _ = Printf.printf "if(%s){\n" (dia_create_cpp_variable var_index) in - let body_var_index = (* The body_var_index would hold an incremented value from the latest used variable index. *) + let body_var_index = + (* The body_var_index would hold an incremented value from the latest used variable index. *) let _node = List.nth node.parameters 1 in match _node.token_type with - | DiaConstant _ -> Printf.printf "return %s;\n} else " _node.name; (var_index + 1) - | _ -> let _, body_var_index = dia_generate_code _node custom_functions (var_index + if_depth) in - Printf.printf "return %s;\n} else " (dia_create_cpp_variable body_var_index); (body_var_index + 1) + | DiaConstant _ -> + Printf.printf "return %s;\n} else " _node.name; + var_index + 1 + | _ -> + let _, body_var_index = + dia_generate_code _node custom_functions (var_index + if_depth) + in + Printf.printf "return %s;\n} else " + (dia_create_cpp_variable body_var_index); + body_var_index + 1 in (* else *) let _node = List.nth node.parameters 2 in - dia_dbgprint (Printf.sprintf "if_parse_condition: depth is %d" body_var_index); - (* else if *) - if _node.name = "if" - then dia_if_parse_bodies _node custom_functions (var_index + 1) if_depth - (* else { }*) + dia_dbgprint (Printf.sprintf "if_parse_condition: depth is %d" body_var_index); + (* else if *) + if _node.name = "if" then + dia_if_parse_bodies _node custom_functions (var_index + 1) if_depth + (* else { }*) else - let _node = List.nth node.parameters 2 in - match _node.token_type with - | DiaConstant _ -> Printf.printf "{\nreturn %s;\n}\n" _node.name; node, (body_var_index + 1), if_depth - | _ -> let _ = print_endline "{" in - let _, body_var_index = dia_generate_code _node custom_functions body_var_index in - Printf.printf "return %s;\n}\n" (dia_create_cpp_variable (body_var_index - 1)); - node, (body_var_index + 1), if_depth - + let _node = List.nth node.parameters 2 in + match _node.token_type with + | DiaConstant _ -> + Printf.printf "{\nreturn %s;\n}\n" _node.name; + (node, body_var_index + 1, if_depth) + | _ -> + let _ = print_endline "{" in + let _, body_var_index = + dia_generate_code _node custom_functions body_var_index + in + Printf.printf "return %s;\n}\n" + (dia_create_cpp_variable (body_var_index - 1)); + (node, body_var_index + 1, if_depth) -let rec dia_generate_code_chain (node: DiaNode.dia_node) (custom_functions: DiaNode.dia_node list) (var_index: int): DiaNode.dia_node * int = +let rec dia_generate_code_chain (node : DiaNode.dia_node) + (custom_functions : DiaNode.dia_node list) (var_index : int) : + DiaNode.dia_node * int = let n, i = dia_generate_code node custom_functions var_index in match node.next_function with | Some next_func -> - let _n, j = dia_generate_code_chain next_func custom_functions (i+1) in - (_n, j) + let _n, j = dia_generate_code_chain next_func custom_functions (i + 1) in + (_n, j) | None -> (n, i) -let rec dia_debug_function_descriptor_chain (node: DiaNode.dia_node) = +let rec dia_debug_function_descriptor_chain (node : DiaNode.dia_node) = dia_debug_function_descriptor node 0; match node.next_function with | None -> () | Some next_node -> dia_debug_function_descriptor_chain next_node - -let dia_main (node: DiaNode.dia) orig_code = +let dia_main (node : DiaNode.dia) orig_code = dia_dbgprint "Lemme write down bill of main function..."; dia_debug_function_descriptor_chain node.main_function; dia_dbgprint "Available custom functions are:"; - List.map (fun (cf: DiaNode.dia_node) -> cf.name) node.custom_functions - |> String.concat ", " - |> dia_dbgprint; - (print_endline generate_header); - (print_endline (generate_comment orig_code)); - let _, _ = dia_generate_code_chain node.main_function node.custom_functions 0 in + List.map (fun (cf : DiaNode.dia_node) -> cf.name) node.custom_functions + |> String.concat ", " |> dia_dbgprint; + print_endline generate_header; + print_endline (generate_comment orig_code); + let _, _ = + dia_generate_code_chain node.main_function node.custom_functions 0 + in print_string "return 0;\n}\n/* End of Main Function */\n" -let generate_custom_func_header (node: DiaNode.dia_node) = +let generate_custom_func_header (node : DiaNode.dia_node) = Printf.printf "%s %s(" (type_to_string node.token_type) node.name; - List.map (fun (p: DiaNode.dia_node) -> Printf.sprintf "%s %s" (type_to_string p.token_type) p.name) node.parameters - |> String.concat "," - |> print_string; + List.map + (fun (p : DiaNode.dia_node) -> + Printf.sprintf "%s %s" (type_to_string p.token_type) p.name) + node.parameters + |> String.concat "," |> print_string; print_endline ") {" -let dia_custom_function (node: DiaNode.dia_node) (custom_functions: DiaNode.dia_node list) = +let dia_custom_function (node : DiaNode.dia_node) + (custom_functions : DiaNode.dia_node list) = match node.next_function with | None -> - Printf.sprintf "Function %s have no function body. Skipping generating code." node.name - |> dia_dbgprint - | Some body -> - dia_dbgprint (Printf.sprintf "Generating code for custom function '%s'" node.name); - Printf.printf "/* Start of %s Function */\n" node.name; - generate_custom_func_header node; - dia_debug_function_descriptor_chain node; - match body.token_type with - | DiaConstant _ -> - print_endline ("return " ^ body.name ^ ";"); - Printf.printf "}\n/* End of %s Function */\n" node.name - | _ -> - let _, cpp_var_count = dia_generate_code_chain body custom_functions 0 in - print_endline ("return v" ^ string_of_int (cpp_var_count - 1) ^ ";"); - Printf.printf "}\n/* End of %s Function */\n" node.name + Printf.sprintf + "Function %s have no function body. Skipping generating code." node.name + |> dia_dbgprint + | Some body -> ( + dia_dbgprint + (Printf.sprintf "Generating code for custom function '%s'" node.name); + Printf.printf "/* Start of %s Function */\n" node.name; + generate_custom_func_header node; + dia_debug_function_descriptor_chain node; + match body.token_type with + | DiaConstant _ -> + print_endline ("return " ^ body.name ^ ";"); + Printf.printf "}\n/* End of %s Function */\n" node.name + | _ -> + let _, cpp_var_count = + dia_generate_code_chain body custom_functions 0 + in + print_endline ("return v" ^ string_of_int (cpp_var_count - 1) ^ ";"); + Printf.printf "}\n/* End of %s Function */\n" node.name) diff --git a/diaNode.ml b/diaNode.ml index 9901357..919d608 100644 --- a/diaNode.ml +++ b/diaNode.ml @@ -1,9 +1,4 @@ -type dia_basic_type = - | DiaInteger - | DiaDouble - | DiaString - | DiaBool - | DiaVoid +type dia_basic_type = DiaInteger | DiaDouble | DiaString | DiaBool | DiaVoid type dia_token_type = | DiaFunction of dia_basic_type @@ -11,14 +6,11 @@ type dia_token_type = | DiaConstant of dia_basic_type type dia_node = { - name: string; - token_type: dia_token_type; - num_of_parameters: int; - parameters: dia_node list; - next_function: dia_node option; - } - -type dia = { - custom_functions: dia_node list; - main_function: dia_node; + name : string; + token_type : dia_token_type; + num_of_parameters : int; + parameters : dia_node list; + next_function : dia_node option; } + +type dia = { custom_functions : dia_node list; main_function : dia_node } diff --git a/main.ml b/main.ml index 2c2e64e..eba3ba9 100644 --- a/main.ml +++ b/main.ml @@ -3,75 +3,100 @@ open DiaNode exception DiaFileNotFoundError of string -let dia_help = "\ - Dia: The brilliance of Diamond, the elegance of the Language\n\n\ - Usage: diac [options] file.dia\n\n\ - Option:\n\ - -v[v..] Increase the level of verbosity (-v to -vvvv)\n\ - --stdout Print compiled code to terminal\n\ - --no-compile Do not compile, persist the source code.\n\ - --skip-format Skip formatting the generated code using clang-format.\n\ - --quiet Do not generate file comment.\n\ - --static Generate static binary (equivalent of --static option in C++ compilers)\n\n\ - Experimental Options:\n\ - --pure Add [[gnu::pure]] macro to all custom functions.\n\ - --constexpr Add constexpr keyword to all custom functions.\n\ - --faster-io Disable input and output buffer to increase I/O performance.\n\ - (Should be used on competitive programming)\n\ -" +let dia_help = + "Dia: The brilliance of Diamond, the elegance of the Language\n\n\ + Usage: diac [options] file.dia\n\n\ + Option:\n\ + -v[v..] Increase the level of verbosity (-v to -vvvv)\n\ + --stdout Print compiled code to terminal\n\ + --no-compile Do not compile, persist the source code.\n\ + --skip-format Skip formatting the generated code using clang-format.\n\ + --quiet Do not generate file comment.\n\ + --static Generate static binary (equivalent of --static option in C++ \ + compilers)\n\n\ + Experimental Options:\n\ + --pure Add [[gnu::pure]] macro to all custom functions.\n\ + --constexpr Add constexpr keyword to all custom functions.\n\ + --faster-io Disable input and output buffer to increase I/O performance.\n\ + (Should be used on competitive programming)\n" -let generate_header = "#include \n\ - #include \n\ - #include \n\ - #include \n\ -" +let generate_header = + "#include \n\ + #include \n\ + #include \n\ + #include \n" type dia_cli_options = { - input_file: string ref; - output_file: string ref; - verbosity: bool ref; - std_out: bool ref; - no_compile: bool ref; - skip_format: bool ref; - quiet: bool ref; - static: bool ref; - pure: bool ref; - constexpr: bool ref; - faster_io: bool ref; + input_file : string ref; + output_file : string ref; + verbosity : bool ref; + std_out : bool ref; + no_compile : bool ref; + skip_format : bool ref; + quiet : bool ref; + static : bool ref; + pure : bool ref; + constexpr : bool ref; + faster_io : bool ref; } -let cli_options = { - input_file = ref ""; - output_file = ref ""; - verbosity = ref false; - std_out = ref false; - no_compile = ref false; - skip_format = ref false; - quiet = ref false; - static = ref false; - pure = ref false; - constexpr = ref false; - faster_io = ref false; -} +let cli_options = + { + input_file = ref ""; + output_file = ref ""; + verbosity = ref false; + std_out = ref false; + no_compile = ref false; + skip_format = ref false; + quiet = ref false; + static = ref false; + pure = ref false; + constexpr = ref false; + faster_io = ref false; + } -let speclist = [ - ("-v", Arg.Set cli_options.verbosity, "Increase the level of verbosity (-v to -vvvv)"); - ("-o", Arg.Set_string cli_options.output_file, "Set explicit output file name."); - ("--stdout", Arg.Set cli_options.std_out, "Print compiled code to terminal"); - ("--no-compile", Arg.Set cli_options.no_compile, "Do not compile, persist the source code."); - ("--skip-format", Arg.Set cli_options.skip_format, "Skip formatting the generated code using clang-format."); - ("--quiet", Arg.Set cli_options.quiet, "Do not generate file comment."); - ("--static", Arg.Set cli_options.static, "Generate static binary (equivalent of --static option in C++ compilers)"); - (* Experimental Options *) - ("--pure", Arg.Set cli_options.pure, "Add [[gnu::pure]] macro to all custom functions."); - ("--constexpr", Arg.Set cli_options.constexpr, "Add constexpr keyword to all custom functions."); - ("--faster-io", Arg.Set cli_options.faster_io, "Disable input and output buffer to increase I/O performance.\n(Should be used on competitive programming)"); -] +let speclist = + [ + ( "-v", + Arg.Set cli_options.verbosity, + "Increase the level of verbosity (-v to -vvvv)" ); + ( "-o", + Arg.Set_string cli_options.output_file, + "Set explicit output file name." ); + ("--stdout", Arg.Set cli_options.std_out, "Print compiled code to terminal"); + ( "--no-compile", + Arg.Set cli_options.no_compile, + "Do not compile, persist the source code." ); + ( "--skip-format", + Arg.Set cli_options.skip_format, + "Skip formatting the generated code using clang-format." ); + ("--quiet", Arg.Set cli_options.quiet, "Do not generate file comment."); + ( "--static", + Arg.Set cli_options.static, + "Generate static binary (equivalent of --static option in C++ compilers)" + ); + (* Experimental Options *) + ( "--pure", + Arg.Set cli_options.pure, + "Add [[gnu::pure]] macro to all custom functions." ); + ( "--constexpr", + Arg.Set cli_options.constexpr, + "Add constexpr keyword to all custom functions." ); + ( "--faster-io", + Arg.Set cli_options.faster_io, + "Disable input and output buffer to increase I/O performance.\n\ + (Should be used on competitive programming)" ); + ] let _ = - let () = Arg.parse speclist (fun str -> cli_options.input_file := str) dia_help in - let ch = if cli_options.input_file.contents = "" - then raise (DiaFileNotFoundError "Dia: No input file given. Example: diac .dia") + let () = + Arg.parse speclist (fun str -> cli_options.input_file := str) dia_help + in + let ch = + if cli_options.input_file.contents = "" then + raise + (DiaFileNotFoundError + "Dia: No input file given. Example: diac .dia") else open_in cli_options.input_file.contents in let dia_file = In_channel.input_all ch in @@ -79,6 +104,7 @@ let _ = let lexbuf = Lexing.from_string dia_file in let result = Parser.dia Lexer.parse_code lexbuf in print_endline generate_header; - List.iter (fun f -> dia_custom_function f result.custom_functions) result.custom_functions; + List.iter + (fun f -> dia_custom_function f result.custom_functions) + result.custom_functions; dia_main result dia_file - diff --git a/predefined.ml b/predefined.ml index 2aa7284..0bd2971 100644 --- a/predefined.ml +++ b/predefined.ml @@ -1,16 +1,13 @@ open DiaNode type dia_predefined_function = { - node: dia_node; - generate_code: dia_node -> int -> dia_node * int; + node : dia_node; + generate_code : dia_node -> int -> dia_node * int; } let dia_dbgprint str = Printf.eprintf "[predefined.ml] %s\n" str - let var_counter_init = 0 - -let dia_create_cpp_variable (i: int) = - Printf.sprintf "v%d" i +let dia_create_cpp_variable (i : int) = Printf.sprintf "v%d" i (* The Predefined.functions is defined at the end of the file. *) @@ -50,50 +47,57 @@ let dia_create_cpp_variable (i: int) = (* * Functions - IO Start *) -let functions_io = [ - { - node = { - name = "print"; - token_type = DiaFunction DiaVoid; - num_of_parameters = -1; - parameters = []; - next_function = None; +let functions_io = + [ + { + node = + { + name = "print"; + token_type = DiaFunction DiaVoid; + num_of_parameters = -1; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + dia_dbgprint "Generating 'print' function"; + print_string "std::cout"; + List.iter (fun n -> print_string ("<<" ^ n.name)) node.parameters; + print_endline ";"; + ( { + name = "_"; + token_type = DiaConstant DiaVoid; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index )); }; - generate_code = fun node var_index -> - dia_dbgprint "Generating 'print' function"; - print_string "std::cout"; - List.iter (fun n -> print_string("<<" ^ n.name)) node.parameters; - print_endline ";"; - { - name = "_"; - token_type = DiaConstant DiaVoid; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index - }; - { - node = { - name = "puts"; - token_type = DiaFunction DiaVoid; - num_of_parameters = -1; - parameters = []; - next_function = None; + { + node = + { + name = "puts"; + token_type = DiaFunction DiaVoid; + num_of_parameters = -1; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + dia_dbgprint "Generating 'puts' function"; + print_string "std::cout"; + List.iter (fun n -> print_string ("<<" ^ n.name)) node.parameters; + print_endline "< - dia_dbgprint "Generating 'puts' function"; - print_string "std::cout"; - List.iter (fun n -> print_string("<<" ^ n.name)) node.parameters; - print_endline "< + let a1 = List.nth node.parameters 0 in + let a2 = List.nth node.parameters 1 in + dia_dbgprint "Generating 'plus' function"; + Printf.printf "auto %s=%s+%s;\n" + (dia_create_cpp_variable var_index) + a1.name a2.name; + ( { + name = dia_create_cpp_variable var_index; + token_type = a1.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index + 1 )); }; - generate_code = fun node var_index -> - let a1 = List.nth node.parameters 0 in - let a2 = List.nth node.parameters 1 - in - dia_dbgprint "Generating 'plus' function"; - Printf.printf "auto %s=%s+%s;\n" - (dia_create_cpp_variable var_index) - a1.name - a2.name; - { - name = (dia_create_cpp_variable var_index); - token_type = a1.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index+1 - }; - { - node = { - name = "minus"; - token_type = DiaFunction DiaVoid; - num_of_parameters = 2; - parameters = []; - next_function = None; + { + node = + { + name = "minus"; + token_type = DiaFunction DiaVoid; + num_of_parameters = 2; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + let a1 = List.nth node.parameters 0 in + let a2 = List.nth node.parameters 1 in + dia_dbgprint "Generating 'minus' function"; + Printf.printf "auto %s=%s-%s;\n" + (dia_create_cpp_variable var_index) + a1.name a2.name; + ( { + name = dia_create_cpp_variable var_index; + token_type = a1.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index + 1 )); }; - generate_code = fun node var_index -> - let a1 = List.nth node.parameters 0 in - let a2 = List.nth node.parameters 1 - in - dia_dbgprint "Generating 'minus' function"; - Printf.printf "auto %s=%s-%s;\n" - (dia_create_cpp_variable var_index) - a1.name - a2.name; - { - name = (dia_create_cpp_variable var_index); - token_type = a1.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index+1 - }; - { - node = { - name = "multiplies"; - token_type = DiaFunction DiaVoid; - num_of_parameters = 2; - parameters = []; - next_function = None; + { + node = + { + name = "multiplies"; + token_type = DiaFunction DiaVoid; + num_of_parameters = 2; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + let a1 = List.nth node.parameters 0 in + let a2 = List.nth node.parameters 1 in + dia_dbgprint "Generating 'multiplies' function"; + Printf.printf "auto %s=%s* %s;\n" + (dia_create_cpp_variable var_index) + a1.name a2.name; + ( { + name = dia_create_cpp_variable var_index; + token_type = a1.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index + 1 )); }; - generate_code = fun node var_index -> - let a1 = List.nth node.parameters 0 in - let a2 = List.nth node.parameters 1 - in - dia_dbgprint "Generating 'multiplies' function"; - Printf.printf "auto %s=%s* %s;\n" - (dia_create_cpp_variable var_index) - a1.name - a2.name; - { - name = (dia_create_cpp_variable var_index); - token_type = a1.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index+1 - }; - { - node = { - name = "divides"; - token_type = DiaFunction DiaVoid; - num_of_parameters = 2; - parameters = []; - next_function = None; + { + node = + { + name = "divides"; + token_type = DiaFunction DiaVoid; + num_of_parameters = 2; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + let a1 = List.nth node.parameters 0 in + let a2 = List.nth node.parameters 1 in + dia_dbgprint "Generating 'divides' function"; + Printf.printf "auto %s=%s/%s;\n" + (dia_create_cpp_variable var_index) + a1.name a2.name; + ( { + name = dia_create_cpp_variable var_index; + token_type = a1.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index + 1 )); }; - generate_code = fun node var_index -> - let a1 = List.nth node.parameters 0 in - let a2 = List.nth node.parameters 1 - in - dia_dbgprint "Generating 'divides' function"; - Printf.printf "auto %s=%s/%s;\n" - (dia_create_cpp_variable var_index) - a1.name - a2.name; - { - name = (dia_create_cpp_variable var_index); - token_type = a1.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index+1 - }; - { - node = { - name = "modulus"; - token_type = DiaFunction DiaVoid; - num_of_parameters = 2; - parameters = []; - next_function = None; + { + node = + { + name = "modulus"; + token_type = DiaFunction DiaVoid; + num_of_parameters = 2; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + let a1 = List.nth node.parameters 0 in + let a2 = List.nth node.parameters 1 in + dia_dbgprint "Generating 'modulus' function"; + Printf.printf "auto %s=%s%%%s;\n" + (dia_create_cpp_variable var_index) + a1.name a2.name; + ( { + name = dia_create_cpp_variable var_index; + token_type = a1.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index + 1 )); }; - generate_code = fun node var_index -> - let a1 = List.nth node.parameters 0 in - let a2 = List.nth node.parameters 1 - in - dia_dbgprint "Generating 'modulus' function"; - Printf.printf "auto %s=%s%%%s;\n" - (dia_create_cpp_variable var_index) - a1.name - a2.name; - { - name = (dia_create_cpp_variable var_index); - token_type = a1.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index+1 - }; - { - node = { - name = "negate"; - token_type = DiaFunction DiaVoid; - num_of_parameters = 1; - parameters = []; - next_function = None; + { + node = + { + name = "negate"; + token_type = DiaFunction DiaVoid; + num_of_parameters = 1; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + let a1 = List.nth node.parameters 0 in + dia_dbgprint "Generating 'negate' function"; + Printf.printf "auto %s=%s*(-1);\n" + (dia_create_cpp_variable var_index) + a1.name; + ( { + name = dia_create_cpp_variable var_index; + token_type = a1.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index + 1 )); }; - generate_code = fun node var_index -> - let a1 = List.nth node.parameters 0 - in - dia_dbgprint "Generating 'negate' function"; - Printf.printf "auto %s=%s*(-1);\n" - (dia_create_cpp_variable var_index) - a1.name; - { - name = (dia_create_cpp_variable var_index); - token_type = a1.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index+1 - }; -] + ] (* * Functions - Arithmetic End *) @@ -259,81 +271,86 @@ let functions_arithmetic = [ * Functions - Logic Start *) -let functions_logic = [ - { - node = { - name = "logical_and"; - token_type = DiaFunction DiaVoid; - num_of_parameters = 2; - parameters = []; - next_function = None; +let functions_logic = + [ + { + node = + { + name = "logical_and"; + token_type = DiaFunction DiaVoid; + num_of_parameters = 2; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + let a1 = List.nth node.parameters 0 in + let a2 = List.nth node.parameters 1 in + dia_dbgprint "Generating 'logical_and' function"; + Printf.printf "auto %s=%s&&%s;\n" + (dia_create_cpp_variable var_index) + a1.name a2.name; + ( { + name = dia_create_cpp_variable var_index; + token_type = a1.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index + 1 )); }; - generate_code = fun node var_index -> - let a1 = List.nth node.parameters 0 in - let a2 = List.nth node.parameters 1 - in - dia_dbgprint "Generating 'logical_and' function"; - Printf.printf "auto %s=%s&&%s;\n" - (dia_create_cpp_variable var_index) - a1.name - a2.name; - { - name = (dia_create_cpp_variable var_index); - token_type = a1.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index+1 - }; - { - node = { - name = "logical_or"; - token_type = DiaFunction DiaVoid; - num_of_parameters = 2; - parameters = []; - next_function = None; + { + node = + { + name = "logical_or"; + token_type = DiaFunction DiaVoid; + num_of_parameters = 2; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + let a1 = List.nth node.parameters 0 in + let a2 = List.nth node.parameters 1 in + dia_dbgprint "Generating 'logical_or' function"; + Printf.printf "auto %s=%s||%s;\n" + (dia_create_cpp_variable var_index) + a1.name a2.name; + ( { + name = dia_create_cpp_variable var_index; + token_type = a1.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index + 1 )); }; - generate_code = fun node var_index -> - let a1 = List.nth node.parameters 0 in - let a2 = List.nth node.parameters 1 - in - dia_dbgprint "Generating 'logical_or' function"; - Printf.printf "auto %s=%s||%s;\n" - (dia_create_cpp_variable var_index) - a1.name - a2.name; - { - name = (dia_create_cpp_variable var_index); - token_type = a1.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index+1 - }; - { - node = { - name = "logical_not"; - token_type = DiaFunction DiaVoid; - num_of_parameters = 1; - parameters = []; - next_function = None; + { + node = + { + name = "logical_not"; + token_type = DiaFunction DiaVoid; + num_of_parameters = 1; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + let a1 = List.nth node.parameters 0 in + dia_dbgprint "Generating 'logical_not' function"; + Printf.printf "auto %s=!%s;\n" + (dia_create_cpp_variable var_index) + a1.name; + ( { + name = dia_create_cpp_variable var_index; + token_type = a1.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index + 1 )); }; - generate_code = fun node var_index -> - let a1 = List.nth node.parameters 0 - in - dia_dbgprint "Generating 'logical_not' function"; - Printf.printf "auto %s=!%s;\n" - (dia_create_cpp_variable var_index) - a1.name; - { - name = (dia_create_cpp_variable var_index); - token_type = a1.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index+1 - }; -] + ] (* * Functions - Logic End @@ -343,165 +360,169 @@ let functions_logic = [ * Functions - Comparison Start *) - let functions_comparison = [ - { - node = { - name = "equal_to"; - token_type = DiaFunction DiaVoid; - num_of_parameters = 2; - parameters = []; - next_function = None; +let functions_comparison = + [ + { + node = + { + name = "equal_to"; + token_type = DiaFunction DiaVoid; + num_of_parameters = 2; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + let a1 = List.nth node.parameters 0 in + let a2 = List.nth node.parameters 1 in + dia_dbgprint "Generating 'equal_to' function"; + Printf.printf "auto %s=%s==%s;\n" + (dia_create_cpp_variable var_index) + a1.name a2.name; + ( { + name = dia_create_cpp_variable var_index; + token_type = a1.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index + 1 )); }; - generate_code = fun node var_index -> - let a1 = List.nth node.parameters 0 in - let a2 = List.nth node.parameters 1 - in - dia_dbgprint "Generating 'equal_to' function"; - Printf.printf "auto %s=%s==%s;\n" - (dia_create_cpp_variable var_index) - a1.name - a2.name; - { - name = (dia_create_cpp_variable var_index); - token_type = a1.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index+1 - }; - { - node = { - name = "not_equal_to"; - token_type = DiaFunction DiaVoid; - num_of_parameters = 2; - parameters = []; - next_function = None; + { + node = + { + name = "not_equal_to"; + token_type = DiaFunction DiaVoid; + num_of_parameters = 2; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + let a1 = List.nth node.parameters 0 in + let a2 = List.nth node.parameters 1 in + dia_dbgprint "Generating 'not_equal_to' function"; + Printf.printf "auto %s=%s!=%s;\n" + (dia_create_cpp_variable var_index) + a1.name a2.name; + ( { + name = dia_create_cpp_variable var_index; + token_type = a1.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index + 1 )); }; - generate_code = fun node var_index -> - let a1 = List.nth node.parameters 0 in - let a2 = List.nth node.parameters 1 - in - dia_dbgprint "Generating 'not_equal_to' function"; - Printf.printf "auto %s=%s!=%s;\n" - (dia_create_cpp_variable var_index) - a1.name - a2.name; - { - name = (dia_create_cpp_variable var_index); - token_type = a1.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index+1 - }; - { - node = { - name = "greater_equal"; - token_type = DiaFunction DiaVoid; - num_of_parameters = 2; - parameters = []; - next_function = None; + { + node = + { + name = "greater_equal"; + token_type = DiaFunction DiaVoid; + num_of_parameters = 2; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + let a1 = List.nth node.parameters 0 in + let a2 = List.nth node.parameters 1 in + dia_dbgprint "Generating 'greater_equal' function"; + Printf.printf "auto %s=%s>=%s;\n" + (dia_create_cpp_variable var_index) + a1.name a2.name; + ( { + name = dia_create_cpp_variable var_index; + token_type = a1.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index + 1 )); }; - generate_code = fun node var_index -> - let a1 = List.nth node.parameters 0 in - let a2 = List.nth node.parameters 1 - in - dia_dbgprint "Generating 'greater_equal' function"; - Printf.printf "auto %s=%s>=%s;\n" - (dia_create_cpp_variable var_index) - a1.name - a2.name; - { - name = (dia_create_cpp_variable var_index); - token_type = a1.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index+1 - }; - { - node = { - name = "greater"; - token_type = DiaFunction DiaVoid; - num_of_parameters = 2; - parameters = []; - next_function = None; + { + node = + { + name = "greater"; + token_type = DiaFunction DiaVoid; + num_of_parameters = 2; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + let a1 = List.nth node.parameters 0 in + let a2 = List.nth node.parameters 1 in + dia_dbgprint "Generating 'greater' function"; + Printf.printf "auto %s=%s>%s;\n" + (dia_create_cpp_variable var_index) + a1.name a2.name; + ( { + name = dia_create_cpp_variable var_index; + token_type = a1.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index + 1 )); }; - generate_code = fun node var_index -> - let a1 = List.nth node.parameters 0 in - let a2 = List.nth node.parameters 1 - in - dia_dbgprint "Generating 'greater' function"; - Printf.printf "auto %s=%s>%s;\n" - (dia_create_cpp_variable var_index) - a1.name - a2.name; - { - name = (dia_create_cpp_variable var_index); - token_type = a1.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index+1 - }; - { - node = { - name = "less_equal"; - token_type = DiaFunction DiaVoid; - num_of_parameters = 2; - parameters = []; - next_function = None; + { + node = + { + name = "less_equal"; + token_type = DiaFunction DiaVoid; + num_of_parameters = 2; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + let a1 = List.nth node.parameters 0 in + let a2 = List.nth node.parameters 1 in + dia_dbgprint "Generating 'less_equal' function"; + Printf.printf "auto %s=%s<=%s;\n" + (dia_create_cpp_variable var_index) + a1.name a2.name; + ( { + name = dia_create_cpp_variable var_index; + token_type = a1.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index + 1 )); }; - generate_code = fun node var_index -> - let a1 = List.nth node.parameters 0 in - let a2 = List.nth node.parameters 1 - in - dia_dbgprint "Generating 'less_equal' function"; - Printf.printf "auto %s=%s<=%s;\n" - (dia_create_cpp_variable var_index) - a1.name - a2.name; - { - name = (dia_create_cpp_variable var_index); - token_type = a1.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index+1 - }; - { - node = { - name = "less"; - token_type = DiaFunction DiaVoid; - num_of_parameters = 2; - parameters = []; - next_function = None; + { + node = + { + name = "less"; + token_type = DiaFunction DiaVoid; + num_of_parameters = 2; + parameters = []; + next_function = None; + }; + generate_code = + (fun node var_index -> + let a1 = List.nth node.parameters 0 in + let a2 = List.nth node.parameters 1 in + dia_dbgprint "Generating 'less' function"; + Printf.printf "auto %s=%s<%s;\n" + (dia_create_cpp_variable var_index) + a1.name a2.name; + ( { + name = dia_create_cpp_variable var_index; + token_type = a1.token_type; + num_of_parameters = 0; + parameters = []; + next_function = None; + }, + var_index + 1 )); }; - generate_code = fun node var_index -> - let a1 = List.nth node.parameters 0 in - let a2 = List.nth node.parameters 1 - in - dia_dbgprint "Generating 'less' function"; - Printf.printf "auto %s=%s<%s;\n" - (dia_create_cpp_variable var_index) - a1.name - a2.name; - { - name = (dia_create_cpp_variable var_index); - token_type = a1.token_type; - num_of_parameters = 0; - parameters = []; - next_function = None; - }, var_index+1 - }; -] + ] (* * Functions - Comparison End *) let functions = - functions_io - @ functions_arithmetic - @ functions_logic - @ functions_comparison + functions_io @ functions_arithmetic @ functions_logic @ functions_comparison