123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216(*
* This file is part of the Watson Conversation Service OCaml API project.
*
* Copyright 2016-2017 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)openSpel_tletescape(istop:bool)s=ifistopthen"<?"^s^"?>"elsesletparenprec_outprec_ins=ifprec_out>prec_inthen"("^s^")"elsesletrecovermsge=beginmatche.expr_textwith|Sometext->text|None->"[ERROR] "^msgendletprint_lit(istop:bool)v:string=beginmatchvwith|L_strings->ifistopthenselse"'"^s^"'"|L_intn->string_of_intn|L_realn->string_of_floatn|L_booleanb->ifbthen"true"else"false"|L_null->"null"endletprint_typet:string=beginmatchtwith|T_string->"String"|T_int->"int"|T_real->"double"|T_boolean->"boolean"|T_object->"Object"endletop_prec(op:Spel_t.op):int=beginmatchopwith|Op_eq|Op_ne->8|Op_lt|Op_le|Op_gt|Op_ge->9|Op_not->14|Op_and->4|Op_or->3|Op_plus|Op_minus->11|Op_uminus->14|Op_mult|Op_div|Op_mod->12|Op_concat->11|Op_toString->14endletprint_op(prec:int)(op:Spel_t.op)(l:stringlist):string=beginmatchop,lwith|Op_eq,[v1;v2]->parenprec8(v1^" == "^v2)|Op_ne,[v1;v2]->parenprec8(v1^" != "^v2)|Op_lt,[v1;v2]->parenprec9(v1^" < "^v2)|Op_le,[v1;v2]->parenprec9(v1^" <= "^v2)|Op_gt,[v1;v2]->parenprec9(v1^" > "^v2)|Op_ge,[v1;v2]->parenprec9(v1^" >= "^v2)|Op_not,[v]->parenprec14("!"^v)|Op_and,[v1;v2]->parenprec4(v1^" && "^v2)|Op_or,[v1;v2]->parenprec3(v1^" || "^v2)|Op_plus,[v1;v2]->parenprec11(v1^" + "^v2)|Op_minus,[v1;v2]->parenprec11(v1^" - "^v2)|Op_uminus,[v1]->parenprec14("-"^v1)|Op_mult,[v1;v2]->parenprec12(v1^" * "^v2)|Op_div,[v1;v2]->parenprec12(v1^" / "^v2)|Op_mod,[v1;v2]->parenprec12(v1^" % "^v2)|Op_concat,(v::l)->parenprec11(List.fold_left(funaccv->acc^" + "^v)vl)|Op_toString,[v1]->parenprec14(v1^".toString()")|_->"[bad number of arguments for operator]"endletprint_dimdim=beginmatchdimwith|Somed->"["^(string_of_intd)^"]"|None->"[]"endletprint_dimsdims:string=String.concat""(List.mapprint_dimdims)letrecprint_expr_aux(prec:int)(istop:bool)e:string=beginmatche.expr_descwith|E_litv->print_litistopv|E_prop(e,x)->parenprec14(escapeistop((print_expr_aux14falsee)^"."^x))|E_prop_catch(e,x)->parenprec14(escapeistop((print_expr_aux14falsee)^"?."^x))|E_listl->escapeistop("{"^(String.concat","(List.map(print_expr_aux0false)l))^"}")|E_get(e1,e2)->escapeistop((print_expr_aux0falsee1)^"["^(print_expr_aux0falsee2)^"]")|E_new_array(t,dims,Someinit)->parenprec13(escapeistop("new "^(print_typet)^(print_dimsdims)^"{"^(String.concat","(List.map(print_expr_aux0false)init))^"}"))|E_new_array(t,dims,None)->parenprec13(escapeistop("new "^(print_typet)^(print_dimsdims)))|E_new(x,el)->parenprec13(escapeistop("new"^x^"("^(String.concat","(List.map(print_expr_aux0false)el))^")"))|E_call(None,x,el)->parenprec16(escapeistop(x^".("^(String.concat","(List.map(print_expr_aux0false)el))^")"))|E_call(Somee,x,el)->parenprec16(escapeistop((print_expr_aux16falsee)^"."^x^"("^(String.concat","(List.map(print_expr_aux0false)el))^")"))|E_call_catch(None,x,el)->parenprec16(escapeistop(x^".("^(String.concat","(List.map(print_expr_aux0false)el))^")"))|E_call_catch(Somee,x,el)->parenprec16(escapeistop((print_expr_aux16falsee)^"?."^x^"("^(String.concat","(List.map(print_expr_aux0false)el))^")"))|E_op(Op_concat,[e1;e2])->ifistopthen(print_expr_aux0istope1)^(print_expr_aux0istope2)elseparenprec11((print_expr_aux11istope1)^" + "^(print_expr_aux11istope2))|E_op(op,l)->escapeistop(print_opprecop(List.map(print_expr_aux(op_precop)false)l))|E_conditional(e1,e2,e3)->parenprec2((print_expr_aux2istope1)^"?"^(print_expr_aux2istope2)^":"^(print_expr_aux2istope3))|E_identx->x|E_anything_else->"anything_else"|E_context->"context"|E_conversation_start->"conversation_start"|E_entities->"entities"|E_input->"input"|E_intents->"intents"|E_output->"output"|E_variable(x,None)->"$"^x|E_variable(x,Somey)->"$"^x^":"^"("^y^")"|E_intentx->"#"^x|E_entity(x,None)->"@"^x|E_entity(x,Somey)->"@"^x^":"^"("^y^")"|E_errormsg->recovermsgeendletlift_constantse=`Stringeletprint_expression_commonistope=lift_constants(print_expr_aux0istope)(** {6 Top level printer for Spel expressions} *)letto_stringe:string=print_expr_aux0falsee(** {6 Auxiliary printer for text expressions} *)letto_texte=print_expr_aux0truee