Skip to content

Commit

Permalink
* compiler rewritten in ficus sort of works; all the unit tests (exce…
Browse files Browse the repository at this point in the history
…pt 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
  • Loading branch information
vpisarev committed Mar 24, 2021
1 parent 071401b commit 28ca132
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
14 changes: 7 additions & 7 deletions compiler/C_gen_code.fx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions compiler/C_pp.fx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions compiler/K_form.fx
Original file line number Diff line number Diff line change
Expand Up @@ -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})")
Expand Down
6 changes: 3 additions & 3 deletions lib/Builtins.fx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
4 changes: 3 additions & 1 deletion lib/Set.fx
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
2 changes: 1 addition & 1 deletion test/test_all.fx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 28ca132

Please sign in to comment.