Make array access, cast, if statements, conditional statements, sizeof, and struct record access convert properly

This commit is contained in:
Joey Yakimowich-Payne 2018-06-03 16:50:17 +09:00
commit ded58071a1

View file

@ -21,6 +21,8 @@ let gen_indent num_spaces =
in in
aux "" num_spaces aux "" num_spaces
let replace search sub str =
Str.global_replace (Str.regexp search) sub str
let indent ?(level=2) str = let indent ?(level=2) str =
let indent_str = gen_indent level let indent_str = gen_indent level
@ -346,6 +348,12 @@ and process_unaryOp expr =
| Not -> "not " ^ expr | Not -> "not " ^ expr
| GetRefLabel -> failwith "Ref labels are not supported in flitter!" | GetRefLabel -> failwith "Ref labels are not supported in flitter!"
and process_assignOp =
function
| SimpleAssign -> "="
| OpAssign arith ->
process_arithOp arith
and process_exprbis toks = and process_exprbis toks =
function function
| Id ((name, info)) -> | Id ((name, info)) ->
@ -360,22 +368,21 @@ and process_exprbis toks =
let name = process_expression expr let name = process_expression expr
and args = process_paren (process_comma_list process_argument) args in and args = process_paren (process_comma_list process_argument) args in
name ^ args name ^ args
| CondExpr ((v1, v2, v3)) -> | CondExpr ((binary, first_option, second_option)) ->
(*let v1 = vof_expression v1 let bin = process_expression binary
and v2 = Ocaml.vof_option vof_expression v2 and first_op = process_option process_expression first_option
and v3 = vof_expression v3*) and second_op = process_expression second_option in
"" "if " ^ bin ^ ": " ^ first_op ^ " else: " ^ second_op
| Sequence ((v1, v2)) -> | Sequence ((v1, v2)) ->
(*let v1 = vof_expression v1 (*let v1 = process_expression v1
and v2 = vof_expression v2 and v2 = process_expression v2
in Ocaml.VSum (("Sequence", [ v1; v2 ]))*) in Ocaml.VSum (("Sequence", [ v1; v2 ]))*)
"" ""
| Assignment ((v1, v2, v3)) -> | Assignment ((left, op, right)) ->
(*let v1 = vof_expression v1 let left = process_expression left
and v2 = vof_assignOp v2 and op = process_assignOp op
and v3 = vof_expression v3 and right = process_expression right in
in Ocaml.VSum (("Assignment", [ v1; v2; v3 ]))*) left ^ " " ^ op ^ " " ^ right
""
| Postfix ((expr, op)) -> | Postfix ((expr, op)) ->
let expr = process_expression expr in let expr = process_expression expr in
let op_expr = let op_expr =
@ -397,83 +404,83 @@ and process_exprbis toks =
and op = process_binaryOp op and op = process_binaryOp op
and right = process_expression right and right = process_expression right
in left ^ " " ^ op ^ " " ^ right in left ^ " " ^ op ^ " " ^ right
| ArrayAccess ((v1, v2)) -> | ArrayAccess ((expr, bracket_expr)) ->
(*let v1 = vof_expression v1 let expr = process_expression expr
and v2 = vof_bracket vof_expression v2 and bracket_expr = process_bracket process_expression bracket_expr in
in Ocaml.VSum (("ArrayAccess", [ v1; v2 ]))*) expr ^ bracket_expr
"" | RecordAccess ((expr, field)) ->
| RecordAccess ((v1, v2)) -> let expr = process_expression expr
(*let v1 = vof_expression v1 and field = process_name field in
and v2 = vof_name v2 expr ^ "." ^ field
in Ocaml.VSum (("RecordAccess", [ v1; v2 ]))*) | RecordPtAccess ((expr, field)) ->
"" let expr = process_expression expr
| RecordPtAccess ((v1, v2)) -> and field = process_name field in
(*let v1 = vof_expression v1 expr ^ "." ^ field
and v2 = vof_name v2 | RecordStarAccess ((left, right)) ->
in Ocaml.VSum (("RecordPtAccess", [ v1; v2 ]))*) (*
"" * Not sure what this is, exactly. The nim code
| RecordStarAccess ((v1, v2)) -> * will have it commented out for now.
(*let v1 = vof_expression v1 *)
and v2 = vof_expression v2 let left = process_expression left
in Ocaml.VSum (("RecordStarAccess", [ v1; v2 ]))*) and right = process_expression right in
"" "# " ^ left ^ process_list process_token toks ^ right
| RecordPtStarAccess ((v1, v2)) -> | RecordPtStarAccess ((left, right)) ->
(*let v1 = vof_expression v1 (*
and v2 = vof_expression v2 * Not sure what this is, exactly. The nim code
in Ocaml.VSum (("RecordPtStarAccess", [ v1; v2 ]))*) * will have it commented out for now.
"" *)
| SizeOfExpr ((v1, v2)) -> let left = process_expression left
(*let v1 = vof_tok v1 and right = process_expression right in
and v2 = vof_expression v2 "# " ^ left ^ process_list process_token toks ^ right
in Ocaml.VSum (("SizeOfExpr", [ v1; v2 ]))*) | SizeOfExpr ((sizeof, expr)) ->
"" let expr = process_expression expr in
| SizeOfType ((v1, v2)) -> "sizeof(" ^ expr ^ ")"
(*let v1 = vof_tok v1 | SizeOfType ((sizeof, fullType)) ->
and v2 = vof_paren vof_fullType v2 let ty = process_paren process_fullType fullType in
in Ocaml.VSum (("SizeOfType", [ v1; v2 ]))*) "sizeof" ^ ty
"" | Cast (((left, fullType, right), expr)) ->
| Cast ((v1, v2)) -> let fullType = process_fullType fullType
(*let v1 = vof_paren vof_fullType v1 and expr = process_expression expr in
and v2 = vof_expression v2 "cast[" ^ fullType ^ "](" ^ expr ^ ")"
in Ocaml.VSum (("Cast", [ v1; v2 ]))*)
""
| StatementExpr v1 -> | StatementExpr v1 ->
process_paren process_compound v1 process_paren process_compound v1
| GccConstructor ((v1, v2)) -> | GccConstructor ((v1, v2)) ->
(*let v1 = vof_paren vof_fullType v1 (*
and v2 = vof_brace (vof_comma_list vof_initialiser) v2 * Not sure what this is, exactly. The nim code
in Ocaml.VSum (("GccConstructor", [ v1; v2 ]))*) * will have it commented out for now.
"" *)
let v1 = process_paren process_fullType v1
and v2 = process_brace (process_comma_list process_initialiser) v2 in
"# " ^ v1 ^ process_list process_token toks ^ v2
| This v1 -> | This v1 ->
(*let v1 = vof_tok v1 in Ocaml.VSum (("This", [ v1 ]))*) (*let v1 = process_tok v1 in Ocaml.VSum (("This", [ v1 ]))*)
""
| ConstructedObject ((v1, v2)) ->
(*let v1 = vof_fullType v1
and v2 = vof_paren (vof_comma_list vof_argument) v2
in Ocaml.VSum (("ConstructedObject", [ v1; v2 ]))*)
"" ""
| ConstructedObject ((fullType, params)) ->
let fullType = process_fullType fullType
and params = process_paren (process_comma_list process_argument) params in
fullType ^ params
| TypeId ((v1, v2)) -> | TypeId ((v1, v2)) ->
(*let v1 = vof_tok v1 (*let v1 = process_tok v1
and v2 = vof_paren vof_either_ft_or_expr v2 and v2 = process_paren process_either_ft_or_expr v2
in Ocaml.VSum (("TypeId", [ v1; v2 ]))*) in Ocaml.VSum (("TypeId", [ v1; v2 ]))*)
"" ""
| CplusplusCast ((v1, v2, v3)) -> | CplusplusCast ((cast_op, fullType, expr)) ->
(*let v1 = vof_wrap2 vof_cast_operator v1 (*let v1 = process_wrap2 process_cast_operator v1
and v2 = vof_angle vof_fullType v2 and v2 = process_angle process_fullType v2
and v3 = vof_paren vof_expression v3 and v3 = process_paren process_expression v3
in Ocaml.VSum (("CplusplusCast", [ v1; v2; v3 ]))*) in Ocaml.VSum (("CplusplusCast", [ v1; v2; v3 ]))*)
"" ""
| New ((v1, v2, v3, v4, v5)) -> | New ((v1, v2, v3, v4, v5)) ->
(*let v1 = Ocaml.vof_option vof_tok v1 (*let v1 = Ocaml.process_option process_tok v1
and v2 = vof_tok v2 and v2 = process_tok v2
and v3 = Ocaml.vof_option (vof_paren (vof_comma_list vof_argument)) v3 and v3 = Ocaml.process_option (process_paren (process_comma_list process_argument)) v3
and v4 = vof_fullType v4 and v4 = process_fullType v4
and v5 = Ocaml.vof_option (vof_paren (vof_comma_list vof_argument)) v5 and v5 = Ocaml.process_option (process_paren (process_comma_list process_argument)) v5
in Ocaml.VSum (("New", [ v1; v2; v3; v4; v5 ]))*) in Ocaml.VSum (("New", [ v1; v2; v3; v4; v5 ]))*)
"" ""
| Delete ((v1, v2)) -> | Delete ((v1, v2)) ->
(*let v1 = Ocaml.vof_option vof_tok v1 (*let v1 = Ocaml.process_option process_tok v1
and v2 = vof_expression v2 and v2 = process_expression v2
in Ocaml.VSum (("Delete", [ v1; v2 ]))*) in Ocaml.VSum (("Delete", [ v1; v2 ]))*)
"" ""
| DeleteArray ((tok, expr)) -> | DeleteArray ((tok, expr)) ->
@ -488,13 +495,14 @@ and process_exprbis toks =
and process_selection = and process_selection =
function function
| If ((v1, v2, v3, v4, v5)) -> | If ((if_tok, paren_expr, stmt1, else_tok, stmt2)) ->
let v1 = process_token v1 let paren_expr = process_paren process_expression paren_expr
and v2 = process_paren process_expression v2 and stmt1 = process_statement stmt1
and v3 = process_statement v3 and else_tok = process_option process_token else_tok
and v4 = process_option process_token v4 and stmt2 = process_statement stmt2 in
and v5 = process_statement v5 "if" ^ paren_expr ^
in v1 ^ v2 ^ v3 ^ v4 ^v5 stmt1 ^ "\n" ^
replace "elseif" "elif" (else_tok ^ stmt2)
| Switch ((v1, v2, v3)) -> | Switch ((v1, v2, v3)) ->
let v1 = process_token v1 let v1 = process_token v1
and v2 = process_paren process_expression v2 and v2 = process_paren process_expression v2
@ -534,14 +542,14 @@ and process_iteration =
and process_jump = and process_jump =
function function
| Goto goto -> "# XXX goto not supported: " ^ goto | Goto goto -> failwith "# XXX goto not supported: " ^ goto
| Continue -> "continue" | Continue -> "continue"
| Break -> "break" | Break -> "break"
| Return -> "return" | Return -> "return"
| ReturnExpr ret_expr -> | ReturnExpr ret_expr ->
"return " ^ process_expression ret_expr "return " ^ process_expression ret_expr
| GotoComputed goto_comp -> | GotoComputed goto_comp ->
"#[ XXX goto not supported: " ^ process_expression goto_comp ^ "]#" failwith "#[ XXX goto not supported: " ^ process_expression goto_comp ^ "]#"
and process_handler (v1, v2, v3) = and process_handler (v1, v2, v3) =
let v1 = process_token v1 let v1 = process_token v1
@ -707,7 +715,7 @@ and process_statement stmt = wrap process_statementbis stmt
and process_statementbis = and process_statementbis =
function function
| Compound comp -> | Compound comp ->
process_compound comp ":\n" ^ indent (process_compound comp)
| ExprStatement expr -> | ExprStatement expr ->
process_exprStatement expr process_exprStatement expr
| Labeled labeled -> | Labeled labeled ->
@ -888,43 +896,43 @@ and process_declaration =
| Func func -> | Func func ->
process_func_or_else func process_func_or_else func
| TemplateDecl (v1, v2, v3) -> | TemplateDecl (v1, v2, v3) ->
(*let v1 = vof_tok v1 (*let v1 = process_tok v1
and v2 = vof_template_parameters v2 and v2 = process_template_parameters v2
and v3 = vof_declaration v3 and v3 = process_declaration v3
in Ocaml.VSum (("TemplateDecl", [ v1; v2; v3 ]))*) in Ocaml.VSum (("TemplateDecl", [ v1; v2; v3 ]))*)
"" ""
| TemplateSpecialization ((v1, v2, v3)) -> | TemplateSpecialization ((v1, v2, v3)) ->
(*let v1 = vof_tok v1 (*let v1 = process_tok v1
and v2 = vof_angle Ocaml.vof_unit v2 and v2 = process_angle Ocaml.process_unit v2
and v3 = vof_declaration v3 and v3 = process_declaration v3
in Ocaml.VSum (("TemplateSpecialization", [ v1; v2; v3 ]))*) in Ocaml.VSum (("TemplateSpecialization", [ v1; v2; v3 ]))*)
"" ""
| ExternC ((v1, v2, v3)) -> | ExternC ((v1, v2, v3)) ->
(*let v1 = vof_tok v1 (*let v1 = process_tok v1
and v2 = vof_tok v2 and v2 = process_tok v2
and v3 = vof_declaration v3 and v3 = process_declaration v3
in Ocaml.VSum (("ExternC", [ v1; v2; v3 ]))*) in Ocaml.VSum (("ExternC", [ v1; v2; v3 ]))*)
"" ""
| ExternCList ((v1, v2, v3)) -> | ExternCList ((v1, v2, v3)) ->
(*let v1 = vof_tok v1 (*let v1 = process_tok v1
and v2 = vof_tok v2 and v2 = process_tok v2
and v3 = vof_brace (Ocaml.vof_list vof_declaration_sequencable) v3 and v3 = process_brace (Ocaml.process_list process_declaration_sequencable) v3
in Ocaml.VSum (("ExternCList", [ v1; v2; v3 ]))*) in Ocaml.VSum (("ExternCList", [ v1; v2; v3 ]))*)
"" ""
| NameSpace ((v1, v2, v3)) -> | NameSpace ((v1, v2, v3)) ->
(*let v1 = vof_tok v1 (*let v1 = process_tok v1
and v2 = vof_wrap2 Ocaml.vof_string v2 and v2 = process_wrap2 Ocaml.process_string v2
and v3 = vof_brace (Ocaml.vof_list vof_declaration_sequencable) v3 and v3 = process_brace (Ocaml.process_list process_declaration_sequencable) v3
in Ocaml.VSum (("NameSpace", [ v1; v2; v3 ]))*) in Ocaml.VSum (("NameSpace", [ v1; v2; v3 ]))*)
"" ""
| NameSpaceExtend ((v1, v2)) -> | NameSpaceExtend ((v1, v2)) ->
(*let v1 = Ocaml.vof_string v1 (*let v1 = Ocaml.process_string v1
and v2 = Ocaml.vof_list vof_declaration_sequencable v2 and v2 = Ocaml.process_list process_declaration_sequencable v2
in Ocaml.VSum (("NameSpaceExtend", [ v1; v2 ]))*) in Ocaml.VSum (("NameSpaceExtend", [ v1; v2 ]))*)
"" ""
| NameSpaceAnon ((v1, v2)) -> | NameSpaceAnon ((v1, v2)) ->
(*let v1 = vof_tok v1 (*let v1 = process_tok v1
and v2 = vof_brace (Ocaml.vof_list vof_declaration_sequencable) v2 and v2 = process_brace (Ocaml.process_list process_declaration_sequencable) v2
in Ocaml.VSu:m (("NameSpaceAnon", [ v1; v2 ]))*) in Ocaml.VSu:m (("NameSpaceAnon", [ v1; v2 ]))*)
"" ""
| EmptyDef def -> "" | EmptyDef def -> ""