From 28ca132ddfc7b2499c4d5685780514a00c26a98f Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Thu, 25 Mar 2021 03:38:34 +0800 Subject: [PATCH] * compiler rewritten in ficus sort of works; all the unit tests (except for the temporarily disabled re2 tests) pass, all the examples work well. * now need to fix the remaining glitches and try to bootstrap the compiler --- compiler/C_gen_code.fx | 14 +++++++------- compiler/C_pp.fx | 12 ++++++------ compiler/K_form.fx | 10 +++++----- lib/Builtins.fx | 6 +++--- lib/Set.fx | 4 +++- test/test_all.fx | 2 +- 6 files changed, 25 insertions(+), 23 deletions(-) diff --git a/compiler/C_gen_code.fx b/compiler/C_gen_code.fx index d5843f3..dae8d01 100644 --- a/compiler/C_gen_code.fx +++ b/compiler/C_gen_code.fx @@ -86,7 +86,7 @@ from Ast import * from K_form import * from C_form import * import K_remove_unused, K_annotate, K_mangle -import C_gen_types, C_gen_fdecls +import C_gen_types, C_gen_fdecls, C_pp import Map, Set @@ -1088,17 +1088,17 @@ fun gen_ccode(cmods: cmodule_t list, kmod: kmodule_t, c_fdecls: ccode_t, mod_ini val ifor_loc = get_cexp_loc(n_exp) val init_exps = [: make_assign(i_exp, make_int_exp(0, ifor_loc)) :] val check_exp = CExpBinary(COpCmp(CmpLT), i_exp, n_exp, (CTypBool, ifor_loc)) - val incr_exps = [: CExpUnary(COpSuffixInc, i_exp, (CTypInt, ifor_loc)) :] - val (check_exp, incr_exps) = + val incr_exps_i = [: CExpUnary(COpSuffixInc, i_exp, (CTypInt, ifor_loc)) :] + val (check_exp, incr_exps_i) = if k_final > 0 { - (check_exp, incr_exps) + (check_exp, incr_exps_i) } else { val fold check_exp = check_exp for e <- for_checks.rev() { CExpBinary(COpLogicAnd, check_exp, e, (CTypBool, ifor_loc)) } - (check_exp, incr_exps + incr_exps.rev()) + (check_exp, incr_exps_i + incr_exps.rev()) } - (k_final + 1, (Some(CTypInt), init_exps, Some(check_exp), incr_exps) :: for_headers) + (k_final + 1, (Some(CTypInt), init_exps, Some(check_exp), incr_exps_i) :: for_headers) } /* if we have open loop or loop over lists (i.e. i_exps and n_exps are empty lists), we still need to form the for-loop statement */ @@ -1464,7 +1464,7 @@ fun gen_ccode(cmods: cmodule_t list, kmod: kmodule_t, c_fdecls: ccode_t, mod_ini match (a, get_cexp_typ(c_exp)) { | (AtomLit(KLitChar c), CTypUniChar) => make_call(get_id("FX_MAKE_STR1"), - [: make_lit_exp(KLitChar(c), kloc) :], CTypString, kloc) + [: make_lit_exp(KLitString(string(c)), kloc) :], CTypString, kloc) | (AtomId n, CTypUniChar) => make_call(get_id("FX_MAKE_VAR_STR1"), [: c_exp :], CTypString, kloc) | _ => c_exp diff --git a/compiler/C_pp.fx b/compiler/C_pp.fx index dd11ff4..cbfec28 100644 --- a/compiler/C_pp.fx +++ b/compiler/C_pp.fx @@ -197,7 +197,7 @@ type assoc_t = AssocLeft | AssocRight | CExpLit(l, (_, loc)) => val s = match l { | KLitNil _ => "0" - | KLitChar c => "FX_CHAR({repr(c)})" + | KLitChar c => f"(char_){ord(c)}" | _ => K_form.klit2str(l, true, loc) } pp.str(s) @@ -268,7 +268,7 @@ type assoc_t = AssocLeft | AssocRight | CExpTyp (t, loc) => pp.begin(); pp_ctyp_(pp, t, None, loc); pp.end() | CExpCCode (ccode, l) => - pp.begin(); pp.str(ccode); pp.end() + pp.begin(); pp.str("\n"+ccode.strip()+"\n"); pp.end() } @private fun pp_elist(pp: PP.t, el: cexp_t list) @@ -542,7 +542,7 @@ type assoc_t = AssocLeft | AssocRight pp.str("#pragma "); pp.str(s); pp.end() } -fun pprint_ctyp(t: ctyp_t, loc: loc_t) +fun pp_ctyp(t: ctyp_t, loc: loc_t) { File.stdout.flush() val pp = PP.pprint_to_stdout(ccode_margin, default_indent=default_indent) @@ -551,7 +551,7 @@ fun pprint_ctyp(t: ctyp_t, loc: loc_t) File.stdout.flush() } -fun pprint_cexp(e: cexp_t) +fun pp_cexp(e: cexp_t) { File.stdout.flush() val pp = PP.pprint_to_stdout(ccode_margin, default_indent=default_indent) @@ -560,7 +560,7 @@ fun pprint_cexp(e: cexp_t) File.stdout.flush() } -fun pprint_cstmt(s: cstmt_t) +fun pp_cstmt(s: cstmt_t) { File.stdout.flush() val pp = PP.pprint_to_stdout(ccode_margin, default_indent=default_indent) @@ -573,7 +573,7 @@ fun pprint_top(code: ccode_t) { File.stdout.flush() val pp = PP.pprint_to_stdout(ccode_margin, default_indent=default_indent) - pp.beginv() + pp.beginv(0) for s@i <- code { if i != 0 { pp.break0() } pp_cstmt_(pp, s) diff --git a/compiler/K_form.fx b/compiler/K_form.fx index 201646f..45d5ad3 100644 --- a/compiler/K_form.fx +++ b/compiler/K_form.fx @@ -1141,14 +1141,14 @@ fun klit2str(lit: klit_t, cmode: bool, loc: loc_t): string match lit { | KLitInt(v) => f"{v}" | KLitSInt(64, v) => - if cmode { f"{v}iLL" } else { f"{v}ii{64}" } + if cmode { f"{v}LL" } else { f"{v}i{64}" } | KLitUInt(64, v) => - if cmode { f"{v}iULL" } else { f"{v}ii{64}" } + if cmode { f"{v}ULL" } else { f"{v}i{64}" } | KLitSInt(b, v) => - if cmode { f"{v}i" } else { f"{v}ii{b}" } + if cmode { f"{v}" } else { f"{v}i{b}" } | KLitUInt(b, v) => - if cmode { f"{v}uU" } else { f"{v}uu{b}" } - | KLitFloat(16, v) => f"{v}h" + if cmode { f"{v}U" } else { f"{v}u{b}" } + | KLitFloat(16, v) => f"{v}f" | KLitFloat(32, v) => f"{v}f" | KLitFloat(64, v) => f"{v}" | KLitFloat(b, v) => throw compile_err(loc, f"invalid literal LitFloat({b}, {v})") diff --git a/lib/Builtins.fx b/lib/Builtins.fx index e21451d..310e071 100644 --- a/lib/Builtins.fx +++ b/lib/Builtins.fx @@ -834,9 +834,9 @@ fun new_uniform_rng(seed: uint64) { var state = if seed != 0UL {seed} else {0xffffffffUL} fun (a: int, b: int) { state = (state :> uint32) * 4197999714UL + (state >> 32) - val (a, b) = (min(a, b), max(a, b)) - val diff = b - a - val x = (state :> uint32) % diff + a + val a1 = min(a, b), b1 = max(a, b) + val diff = b1 - a1 + val x = (state :> uint32) % diff + a1 (x :> int) } } diff --git a/lib/Set.fx b/lib/Set.fx index 3688946..fbdfbd9 100644 --- a/lib/Set.fx +++ b/lib/Set.fx @@ -324,7 +324,9 @@ fun filter(s: 't Set.t, f: 't -> bool): 't Set.t fun filter_(t: 't tree_t, f: 't -> bool, res: 't Set.t): 't Set.t = match t { | Node(_, l, x, r) => - if f(x) { add(res, x) } else { res } + val res = filter_(l, f, res) + val res = if f(x) { add(res, x) } else { res } + filter_(r, f, res) | _ => res } filter_(s.root, f, t {root=Empty, size=0, cmp=s.cmp}) diff --git a/test/test_all.fx b/test/test_all.fx index 01e0ae3..af212ac 100644 --- a/test/test_all.fx +++ b/test/test_all.fx @@ -8,7 +8,7 @@ import test_json import test_spectralnorm import test_mandelbrot import test_closure -import test_re2 +//import test_re2 import test_ds import test_deque import test_filename