import * as Immutable from "immutable" import { Unit, Fun, Prod, Sum, unit, absurd, fst, snd, defun, fun, inl, inr, apply, apply_pair, id, constant, curry, uncurry, lazy, swap_prod, swap_sum, compose_pair } from "ts-bccc" import * as CCC from "ts-bccc" import * as St from "ts-bccc" import { mk_state, State } from "ts-bccc" // import { mk_coroutine, Coroutine, suspend, co_unit, co_run, co_error } from "ts-bccc" // import * as Co from "ts-bccc" import { StmtRt, ExprRt, Interface, MemRt, ErrVal, Val, Lambda, Bool, ValueName, HeapRef, set_arr_el_rt, set_arr_el_expr_rt, set_class_def_rt, set_fun_def_rt, set_heap_v_rt, set_v_rt, set_v_expr_rt, set_highlighting_rt, runtime_error, mk_bool_val, mk_int_val, mk_float_val, mk_lambda_val, new_arr_rt, new_obj_rt, mk_arr_val, ArrayVal, empty_memory_rt, empty_scope_val, mk_obj_val, mk_ref_val, Scope, mk_string_val, mk_unit_val, get_arr_len_rt, get_arr_el_rt, get_class_def_rt, get_fun_def_rt, get_heap_v_rt, get_v_rt, pop_scope_rt, push_scope_rt, mk_tuple_val, RenderSurface } from "./memory" import { SourceRange } from "../source_range"; import { mk_render_surface_val, RenderSurfaceOperation, mk_render_surface_operation_val, mk_circle_op, mk_square_op, mk_rectangle_op, mk_ellipse_op, mk_other_surface_op, mk_sprite_op, mk_line_op, mk_polygon_op, mk_text_op } from "./python"; // import { comm_list_coroutine } from "../ccc_aux"; import { co_error, Coroutine, mk_coroutine, co_unit, co_suspend, co_get_state, co_set_state, comm_list_coroutine } from "../fast_coroutine"; export interface BoolCat extends Fun> {} export let FalseCat:BoolCat = unit().then(inl()) export let TrueCat:BoolCat = unit().then(inr()) export let bool_to_boolcat : Fun = fun(b => b ? TrueCat : FalseCat) export let unit_expr = () => (co_unit>(apply(inl(),mk_unit_val))) export let str_expr = (s:string) => (co_unit>(apply(inl(), mk_string_val(s)))) export let float_expr = (n:number) => (co_unit>(apply(inl(), mk_float_val(n)))) export let int_expr = (n:number) => (co_unit>(apply(inl(), mk_int_val(n)))) export let arr_expr = (a:ArrayVal) => (co_unit>(apply(inl(), mk_arr_val(a)))) export let tuple_expr = (a:Array) => (co_unit>(apply(inl(), mk_tuple_val(a)))) export let bool_expr = (s:boolean) => (co_unit>(apply(inl(), mk_bool_val(s)))) export let lambda_expr = (l:Lambda) => (co_unit>(apply(inl(), mk_lambda_val(l)))) export let obj_expr = (o:Scope) => (co_unit>(apply(inl(), mk_obj_val(o)))) export let ref_expr = (r:ValueName) => (co_unit>(apply(inl(), mk_ref_val(r)))) export let val_expr = (v:Sum) => (co_unit>(v)) export let render_surface_expr = (v:RenderSurface) => (co_unit>(apply(inl(), mk_render_surface_val(v)))) export let render_surface_operation_expr = (v:RenderSurfaceOperation) => (co_unit>(apply(inl(), mk_render_surface_operation_val(v)))) let lift_binary_operation = function (r:SourceRange,a: ExprRt>, b:ExprRt>, check_types:(ab:Prod) => Sum, Unit>, actual_operation:(_:Prod)=>Val, operator_name?:string): ExprRt> { return a.then(a_val => b.then(b_val => apply(fun(check_types).then((fun(actual_operation).then(inl()).then(fun, ExprRt>>(co_unit))).plus( constant>>(runtime_error(r, `Type error: cannot perform ${operator_name} on ${a_val.value.v} and ${b_val.value.v}.`)))), { fst:a_val.value, snd:b_val.value }))) } export let tuple_expr_rt = (args:Array>>) : ExprRt> => { let eval_args = comm_list_coroutine(Immutable.List>>(args)) return eval_args.then(arg_vals => (co_unit>(apply(inl(), mk_tuple_val(arg_vals.toArray().map(a => a.value)))))) } export let tuple_get_rt = (r:SourceRange, t:ExprRt>, item_index:number) : ExprRt> => { return t.then(t_val => t_val.value.k == "tuple" ? co_unit(apply(inl(), t_val.value.v[item_index])) : runtime_error(r, `Type error (tuple): cannot lookup item ${item_index} on non-tuple value ${t_val.value}.`)) } export let record_get_rt = (r:SourceRange, t:ExprRt>, F_name:string) : ExprRt> => { return t.then(t_val => t_val.value.k == "record" && t_val.value.v.has(F_name) ? co_unit(apply(inl(), t_val.value.v.get(F_name))) : runtime_error(r, `Type error (record): cannot lookup item ${F_name} on ${JSON.stringify( t_val.value)}.`)) } export let render_surface_plus_rt = function (sr:SourceRange, r: ExprRt>, p:ExprRt>): ExprRt> { return lift_binary_operation(sr, r, p, ab => ab.fst.k != "render surface" || ab.snd.k != "render surface operation" ? inr, Unit>().f({}) : inl, Unit>().f({ fst:ab.fst.v, snd:ab.snd.v }), ab_val => mk_render_surface_val({...ab_val.fst, operations:ab_val.fst.operations.push(ab_val.snd) }), "(+)") } export let mk_empty_render_surface_rt = function (r:SourceRange,width: ExprRt>, height:ExprRt>, color:ExprRt>): ExprRt> { return width.then(w => height.then(h => color.then(col => w.value.k == "f" && h.value.k == "f" && col.value.k == "s" ? render_surface_expr({ width:w.value.v, height:h.value.v, operations:Immutable.List([{ kind:"rectangle", x:0, y:0, width:w.value.v, height:h.value.v, color:col.value.v, rotation:0 }]) }) : runtime_error(r, `Type error: cannot create empty render surface with ${w.value.v}, ${h.value.v}, and ${col.value.v}.`) ))) } export let mk_circle_rt = function (sr:SourceRange, x: ExprRt>, y:ExprRt>, r:ExprRt>, color:ExprRt>): ExprRt> { return x.then(x_v => y.then(y_v => r.then(r_v => color.then(col => x_v.value.k == "f" && y_v.value.k == "f" && r_v.value.k == "f" && col.value.k == "s" ? render_surface_operation_expr(mk_circle_op(x_v.value.v, y_v.value.v, r_v.value.v, col.value.v)) : runtime_error(sr, `Type error: cannot create circle with ${x_v.value.v}, ${y_v.value.v}, ${r_v.value.v} and ${col.value.v}.`) )))) } export let mk_square_rt = function (r:SourceRange, x: ExprRt>, y:ExprRt>, s:ExprRt>, color:ExprRt>, rot:ExprRt>): ExprRt> { return x.then(x_v => y.then(y_v => s.then(s_v => color.then(col => rot.then(rot_v => x_v.value.k == "f" && y_v.value.k == "f" && s_v.value.k == "f" && col.value.k == "s" && rot_v.value.k == "f" ? render_surface_operation_expr(mk_square_op(x_v.value.v, y_v.value.v, s_v.value.v, col.value.v, rot_v.value.v)) : runtime_error(r, `Type error: cannot create square with ${x_v.value.v}, ${y_v.value.v}, ${s_v.value.v} and ${col.value.v}.`) ))))) } export let mk_rectangle_rt = function (r:SourceRange, x: ExprRt>, y:ExprRt>, w:ExprRt>, h:ExprRt>, color:ExprRt>, rot:ExprRt>): ExprRt> { return x.then(x_v => y.then(y_v => w.then(w_v => h.then(h_v => color.then(col => rot.then(rot_v => x_v.value.k == "f" && y_v.value.k == "f" && w_v.value.k == "f" && h_v.value.k == "f" && col.value.k == "s" && rot_v.value.k == "f" ? render_surface_operation_expr(mk_rectangle_op(x_v.value.v, y_v.value.v, w_v.value.v, h_v.value.v, col.value.v, rot_v.value.v)) : runtime_error(r, `Type error: cannot create rectangle with ${x_v.value.v}, ${y_v.value.v}, ${w_v.value.v}, ${h_v.value.v} and ${col.value.v}.`) )))))) } export let mk_line_rt = function (r:SourceRange, x1: ExprRt>, y1:ExprRt>, x2: ExprRt>, y2:ExprRt>, w:ExprRt>, color:ExprRt>, rot:ExprRt>): ExprRt> { return x1.then(x1_v => y1.then(y1_v => x2.then(x2_v => y2.then(y2_v => w.then(w_v => color.then(col => rot.then(rot_v => x1_v.value.k == "f" && y1_v.value.k == "f" && x2_v.value.k == "f" && y2_v.value.k == "f" && w_v.value.k == "f" && col.value.k == "s" && rot_v.value.k == "f" ? render_surface_operation_expr(mk_line_op(x1_v.value.v, y1_v.value.v, x2_v.value.v, y2_v.value.v, w_v.value.v, col.value.v, rot_v.value.v)) : runtime_error(r, `Type error: cannot create line with ${x1_v.value.v}, ${y1_v.value.v}, ${x2_v.value.v}, ${y2_v.value.v}, ${w_v.value.v}, and ${col.value.v}.`) ))))))) } export let mk_polygon_rt = function(r:SourceRange, points:ExprRt>, col:ExprRt>, rot:ExprRt>) : ExprRt> { return points.then(points_v => col.then(col_v => rot.then(rot_v => points_v.value.k != "ref" ? runtime_error(r, `Type error: cannot create polygon with ${points_v.value.v}, ${col_v.value.v}, and ${rot_v.value.v}.`) : get_heap_v_rt(r, points_v.value.v).then(points_arr_v => col_v.value.k == "s" && rot_v.value.k == "f" && points_arr_v.value.k == "arr" ? render_surface_operation_expr( mk_polygon_op( points_arr_v.value.v.elements.toArray().map(e => e.k == "tuple" && e.v[0].k == "f" && e.v[1].k == "f" ? ({ x:e.v[0].v as number, y:e.v[1].v as number }) : ({ x:0, y:0 })), col_v.value.v, rot_v.value.v)) : runtime_error(r, `Type error: cannot create polygon with ${points_v.value.v}, ${col_v.value.v}, and ${rot_v.value.v}.`) )))) } export let mk_text_rt = function (r:SourceRange, text:ExprRt>, x: ExprRt>, y:ExprRt>, s:ExprRt>, color:ExprRt>, rotation:ExprRt>): ExprRt> { return text.then(t_v => x.then(x_v => y.then(y_v => s.then(s_v => color.then(col => rotation.then(rot => t_v.value.k == "s" && x_v.value.k == "f" && y_v.value.k == "f" && s_v.value.k == "f" && col.value.k == "s" && rot.value.k == "f" ? render_surface_operation_expr(mk_text_op(t_v.value.v, x_v.value.v, y_v.value.v, s_v.value.v, col.value.v, rot.value.v)) : runtime_error(r, `Type error: cannot create text with ${t_v.value.v}, ${x_v.value.v}, ${y_v.value.v}, ${s_v.value.v}, ${col.value.v} and ${rot.value.v}.`) )))))) } export let mk_sprite_rt = function (r:SourceRange, sprite: ExprRt>, x: ExprRt>, y:ExprRt>, w:ExprRt>, h:ExprRt>, rot:ExprRt>): ExprRt> { return sprite.then(sprite_v => x.then(x_v => y.then(y_v => w.then(w_v => h.then(h_v => rot.then(rot_v => sprite_v.value.k == "s" && x_v.value.k == "f" && y_v.value.k == "f" && w_v.value.k == "f" && h_v.value.k == "f" && rot_v.value.k == "f" ? render_surface_operation_expr(mk_sprite_op(sprite_v.value.v, x_v.value.v, y_v.value.v, w_v.value.v, h_v.value.v, rot_v.value.v)) : runtime_error(r, `Type error: cannot create sprite with ${x_v.value.v}, ${y_v.value.v}, ${w_v.value.v}, ${h_v.value.v} and ${rot_v.value.v}.`) )))))) } export let mk_ellipse_rt = function (r:SourceRange, x: ExprRt>, y:ExprRt>, w:ExprRt>, h:ExprRt>, color:ExprRt>, rot:ExprRt>): ExprRt> { return x.then(x_v => y.then(y_v => w.then(w_v => h.then(h_v => color.then(col => rot.then(rot_v => x_v.value.k == "f" && y_v.value.k == "f" && w_v.value.k == "f" && h_v.value.k == "f" && col.value.k == "s" && rot_v.value.k == "f" ? render_surface_operation_expr(mk_ellipse_op(x_v.value.v, y_v.value.v, w_v.value.v, h_v.value.v, col.value.v, rot_v.value.v)) : runtime_error(r, `Type error: cannot create ellipse with ${x_v.value.v}, ${y_v.value.v}, ${w_v.value.v}, ${h_v.value.v} and ${col.value.v}.`) )))))) } export let mk_other_surface_rt = function (r:SourceRange, s:ExprRt>, dx: ExprRt>, dy:ExprRt>, sx:ExprRt>, sy:ExprRt>, rot:ExprRt>): ExprRt> { return dx.then(dx_v => dy.then(dy_v => sx.then(sx_v => sy.then(sy_v => s.then(s_v => rot.then(rot_v => dx_v.value.k == "f" && dy_v.value.k == "f" && sx_v.value.k == "f" && sy_v.value.k == "f" && s_v.value.k == "render surface" && rot_v.value.k == "f" ? render_surface_operation_expr(mk_other_surface_op(s_v.value.v, dx_v.value.v, dy_v.value.v, sx_v.value.v, sy_v.value.v, rot_v.value.v)) : runtime_error(r, `Type error: cannot create other surface with ${dx_v.value.v}, ${dy_v.value.v}, ${sx_v.value.v}, ${sy_v.value.v}, ${s_v.value.v}, and ${rot_v.value.v}.`) )))))) } let lift_unary_operation = function (r:SourceRange, a: ExprRt>, check_type:(a:Val) => Sum, actual_operation:(_:a)=>Val, operator_name?:string): ExprRt> { return a.then(a_val => apply(fun(check_type).then((fun(actual_operation).then(inl()).then(fun, ExprRt>>(co_unit))).plus(constant>>( runtime_error(r, `Type error: cannot perform ${operator_name} on value ${a_val.value.v}.`)))), a_val.value)) } export let int_minus_unary_rt = function (r:SourceRange, a: ExprRt>): ExprRt> { return lift_unary_operation(r, a, ab => ab.k != "i" ? inr().f({}) : inl().f(ab.v), ab_val => mk_int_val(-ab_val), "(-)") } export let float_minus_unary_rt = function (r:SourceRange, a: ExprRt>): ExprRt> { return lift_unary_operation(r, a, ab => ab.k != "f" ? inr().f({}) : inl().f(ab.v), ab_val => mk_float_val(-ab_val), "(-)") } export let string_length_rt = function (r:SourceRange, a: ExprRt>): ExprRt> { return lift_unary_operation(r, a, ab => ab.k != "s" ? inr().f({}) : inl().f(ab.v), ab_val => mk_int_val(ab_val.length), "(length)") }