#include "financial_api.h" #include #include #include namespace sleela { namespace financial { namespace { ExprP V(const std::string&n){return std::make_unique(n);} ExprP D(double n){return std::make_unique(n);} ExprP B(const char*o,ExprP a,ExprP b){return std::make_unique(o,std::move(a),std::move(b));} std::unique_ptr C(const std::string&n){return std::make_unique(n);} ExprP C1(const std::string&n,ExprP a){auto c=C(n);c->args.push_back(std::move(a));return c;} ExprP C2(const std::string&n,ExprP a,ExprP b){auto c=C(n);c->args.push_back(std::move(a));c->args.push_back(std::move(b));return c;} ExprP C3(const std::string&n,ExprP a,ExprP b,ExprP d){auto c=C(n);c->args.push_back(std::move(a));c->args.push_back(std::move(b));c->args.push_back(std::move(d));return c;} StmtP Ret(ExprP e){auto s=std::make_unique();s->value=std::move(e);return s;} Method M(const char*r,const char*n,std::initializer_list>p){Method m;m.retType=r;m.name=n;m.body=std::make_unique();for(auto x:p)m.params.push_back({x.first,x.second});return m;} static const std::set& financialAllowed(){static const std::seta={"future_value","present_value","annuity_present","annuity_future","npv4","bond_price","capm","wacc","determinant2","solve2x2_x","solve2x2_y","quadratic_discriminant","quadratic_root_plus","quadratic_root_minus","ratio"};return a;} void lowerExpr(ExprP&e){if(!e)return;if(auto*c=dynamic_cast(e.get())){for(auto&a:c->args)lowerExpr(a);const std::string p="financial.";if(c->callee.rfind(p,0)==0){std::string n=c->callee.substr(p.size());if(!financialAllowed().count(n))throw std::runtime_error("Semantic error: unsupported executable financial call '"+c->callee+"'");c->callee="__native_financial_"+n;}return;}if(auto*mc=dynamic_cast(e.get())){if(auto*recv=dynamic_cast(mc->receiver.get())){if(recv->name=="financial"){if(!financialAllowed().count(mc->method))throw std::runtime_error("Semantic error: unsupported executable financial call 'financial."+mc->method+"'");auto call=std::make_unique("__native_financial_"+mc->method);for(auto&a:mc->args){lowerExpr(a);call->args.push_back(std::move(a));}e=std::move(call);return;}}lowerExpr(mc->receiver);for(auto&a:mc->args)lowerExpr(a);return;}if(auto*b=dynamic_cast(e.get())){lowerExpr(b->lhs);lowerExpr(b->rhs);}else if(auto*u=dynamic_cast(e.get()))lowerExpr(u->operand);} void lowerStmt(StmtP&s){if(!s)return;if(auto*lb=dynamic_cast(s.get())){lowerStmt(lb->body);return;}if(auto*v=dynamic_cast(s.get()))lowerExpr(v->init);else if(auto*a=dynamic_cast(s.get()))lowerExpr(a->value);else if(auto*e=dynamic_cast(s.get()))lowerExpr(e->expr);else if(auto*p=dynamic_cast(s.get()))lowerExpr(p->expr);else if(auto*r=dynamic_cast(s.get()))lowerExpr(r->value);else if(auto*b=dynamic_cast(s.get()))for(auto&x:b->stmts)lowerStmt(x);else if(auto*i=dynamic_cast(s.get())){lowerExpr(i->cond);lowerStmt(i->thenS);lowerStmt(i->elseS);}else if(auto*w=dynamic_cast(s.get())){lowerExpr(w->cond);lowerStmt(w->body);}else if(auto*f=dynamic_cast(s.get())){lowerStmt(f->init);lowerExpr(f->cond);lowerStmt(f->update);lowerStmt(f->body);}} void add(Program&p){ClassDecl c;c.name="__NativeFinancial"; auto m=M("double","__native_financial_future_value",{{"double","p"},{"double","r"},{"double","n"}});m.body->stmts.push_back(Ret(B("*",V("p"),C2("__native_math_pow",B("+",D(1),V("r")),V("n")))));c.methods.push_back(std::move(m)); m=M("double","__native_financial_present_value",{{"double","f"},{"double","r"},{"double","n"}});m.body->stmts.push_back(Ret(B("/",V("f"),C2("__native_math_pow",B("+",D(1),V("r")),V("n")))));c.methods.push_back(std::move(m)); m=M("double","__native_financial_annuity_present",{{"double","p"},{"double","r"},{"double","n"}});{auto i=std::make_unique();i->cond=B("==",V("r"),D(0));i->thenS=Ret(B("*",V("p"),V("n")));i->elseS=Ret(B("*",V("p"),B("/",B("-",D(1),B("/",D(1),C2("__native_math_pow",B("+",D(1),V("r")),V("n")))),V("r"))));m.body->stmts.push_back(std::move(i));}c.methods.push_back(std::move(m)); m=M("double","__native_financial_annuity_future",{{"double","p"},{"double","r"},{"double","n"}});{auto i=std::make_unique();i->cond=B("==",V("r"),D(0));i->thenS=Ret(B("*",V("p"),V("n")));i->elseS=Ret(B("*",V("p"),B("/",B("-",C2("__native_math_pow",B("+",D(1),V("r")),V("n")),D(1)),V("r"))));m.body->stmts.push_back(std::move(i));}c.methods.push_back(std::move(m)); m=M("double","__native_financial_npv4",{{"double","r"},{"double","c1"},{"double","c2"},{"double","c3"},{"double","c4"}});m.body->stmts.push_back(Ret(B("+",B("+",B("+",B("/",V("c1"),B("+",D(1),V("r"))),B("/",V("c2"),C2("__native_math_pow",B("+",D(1),V("r")),D(2)))),B("/",V("c3"),C2("__native_math_pow",B("+",D(1),V("r")),D(3)))),B("/",V("c4"),C2("__native_math_pow",B("+",D(1),V("r")),D(4))))));c.methods.push_back(std::move(m)); m=M("double","__native_financial_bond_price",{{"double","face"},{"double","coupon"},{"double","yield"},{"double","n"}});m.body->stmts.push_back(Ret(B("+",B("*",B("*",V("face"),V("coupon")),B("/",B("-",D(1),B("/",D(1),C2("__native_math_pow",B("+",D(1),V("yield")),V("n")))),V("yield"))),B("/",V("face"),C2("__native_math_pow",B("+",D(1),V("yield")),V("n"))))));c.methods.push_back(std::move(m)); m=M("double","__native_financial_capm",{{"double","rf"},{"double","beta"},{"double","rm"}});m.body->stmts.push_back(Ret(B("+",V("rf"),B("*",V("beta"),B("-",V("rm"),V("rf"))))));c.methods.push_back(std::move(m)); m=M("double","__native_financial_wacc",{{"double","ew"},{"double","dw"},{"double","ce"},{"double","cd"},{"double","tax"}});m.body->stmts.push_back(Ret(B("+",B("*",V("ew"),V("ce")),B("*",B("*",V("dw"),V("cd")),B("-",D(1),V("tax"))))));c.methods.push_back(std::move(m)); m=M("double","__native_financial_determinant2",{{"double","a"},{"double","b"},{"double","c"},{"double","d"}});m.body->stmts.push_back(Ret(B("-",B("*",V("a"),V("d")),B("*",V("b"),V("c")))));c.methods.push_back(std::move(m)); m=M("double","__native_financial_ratio",{{"double","n"},{"double","d"}});{auto i=std::make_unique();i->cond=B("==",V("d"),D(0));i->thenS=Ret(D(0));i->elseS=Ret(B("/",V("n"),V("d")));m.body->stmts.push_back(std::move(i));}c.methods.push_back(std::move(m)); m=M("double","__native_financial_solve2x2_x",{{"double","a"},{"double","b"},{"double","c"},{"double","d"},{"double","e"},{"double","f"}});m.body->stmts.push_back(Ret(C2("__native_financial_ratio",B("-",B("*",V("e"),V("d")),B("*",V("b"),V("f"))),B("-",B("*",V("a"),V("d")),B("*",V("b"),V("c"))))));c.methods.push_back(std::move(m)); m=M("double","__native_financial_solve2x2_y",{{"double","a"},{"double","b"},{"double","c"},{"double","d"},{"double","e"},{"double","f"}});m.body->stmts.push_back(Ret(C2("__native_financial_ratio",B("-",B("*",V("a"),V("f")),B("*",V("e"),V("c"))),B("-",B("*",V("a"),V("d")),B("*",V("b"),V("c"))))));c.methods.push_back(std::move(m)); m=M("double","__native_financial_quadratic_discriminant",{{"double","a"},{"double","b"},{"double","c"}});m.body->stmts.push_back(Ret(B("-",B("*",V("b"),V("b")),B("*",D(4),B("*",V("a"),V("c"))))));c.methods.push_back(std::move(m)); m=M("double","__native_financial_quadratic_root_plus",{{"double","a"},{"double","b"},{"double","c"}});m.body->stmts.push_back(Ret(B("/",B("+",B("-",D(0),V("b")),C1("__native_math_sqrt",C3("__native_financial_quadratic_discriminant",V("a"),V("b"),V("c")))),B("*",D(2),V("a")))));c.methods.push_back(std::move(m)); m=M("double","__native_financial_quadratic_root_minus",{{"double","a"},{"double","b"},{"double","c"}});m.body->stmts.push_back(Ret(B("/",B("-",B("-",D(0),V("b")),C1("__native_math_sqrt",C3("__native_financial_quadratic_discriminant",V("a"),V("b"),V("c")))),B("*",D(2),V("a")))));c.methods.push_back(std::move(m)); // Pure functions, no instance state: mark static so a qualified call // (financial.future_value(...)) matches the parameter count and the VM call // frame stays aligned across calls (as math does at the end of addMath). for(auto& method:c.methods)method.isStatic=true; p.classes.push_back(std::move(c));} } // namespace void lowerProgram(Program&program){bool imported=false;bool math=false;for(const auto&m:program.imports){if(m=="financial")imported=true;if(m=="math")math=true;}if(!imported)return;if(!math)throw std::runtime_error("Semantic error: financial requires 'import math;' for its numerical kernels");bool helper=false;for(const auto&c:program.classes)if(c.name=="__NativeFinancial")helper=true;if(!helper)add(program);for(auto&c:program.classes){if(c.name=="__NativeFinancial")continue;for(auto&m:c.methods)for(auto&s:m.body->stmts)lowerStmt(s);for(auto&f:c.fields)lowerExpr(f.init);}for(auto&c:program.classes)if(c.java.qualifiedName.empty())c.java.qualifiedName=c.name;} }} // namespace financial, sleela