Skip to content

Commit

Permalink
Merge pull request #1295 from andrew-johnson-4/ansi-c-frontend-rqre
Browse files Browse the repository at this point in the history
Ansi c frontend rqre
  • Loading branch information
andrew-johnson-4 authored Feb 27, 2025
2 parents fa88084 + 2c92a75 commit b919087
Show file tree
Hide file tree
Showing 11 changed files with 26,013 additions and 25,142 deletions.
50,984 changes: 25,846 additions & 25,138 deletions BOOTSTRAP/cli.c

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
CC = cc

dev: install-production
lm --stripdebug tests/c/main.c
cp tests/c/main.c original.c
lm --stripdebug tests/c/return.c
cp tests/c/return.c original.c
cat original.c | python3 -c 'import sys; print("".join(sys.stdin.read().split()))' > diff1.c
cat tmp.c | python3 -c 'import sys; print("".join(sys.stdin.read().split()))' > diff2.c
diff diff1.c diff2.c
Expand Down
6 changes: 5 additions & 1 deletion PLATFORM/C/LIB/c-primitives.lsts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import PLATFORM/C/LIB/blob.lm;
import PLATFORM/C/LIB/primitives.lm;
import PLATFORM/C/LIB/primitives.lsts;

let :Blob $"c::compound"(ts: Cons<?,last>): last = (
let :Blob $"c::compound"(ts: Cons<Any,last>): last = (
);

let :Blob $"c::compound"(ts: last): last = (
);

let :Blob $"c::return"(ts: x): x = (
l"return "; ts; l";";
);
4 changes: 4 additions & 0 deletions PLUGINS/BACKEND/C/index-index.lm
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ import PLUGINS/BACKEND/C/std-c-mangle-declaration.lsts;
import PLUGINS/BACKEND/C/std-c-compile-function-args.lsts;
import PLUGINS/BACKEND/C/std-c-compile-expr.lsts;
import PLUGINS/BACKEND/C/std-c-print.lsts;
import PLUGINS/BACKEND/C/std-c-chain.lsts;
import PLUGINS/BACKEND/C/std-c-compile-call.lsts;
import PLUGINS/BACKEND/C/std-c-compile-constructor.lsts;
import PLUGINS/BACKEND/C/std-c-compile-args.lsts;
5 changes: 5 additions & 0 deletions PLUGINS/BACKEND/C/std-c-chain.lsts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

let std-c-chain(f1: Fragment, f2: Fragment): Fragment = (
f2 = f2.set( c"expression", f1.get(c"expression") + f2.get(c"expression") );
f2
);
86 changes: 86 additions & 0 deletions PLUGINS/BACKEND/C/std-c-compile-args.lsts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@


let std-c-compile-args(callee-ctx: FContext, caller-ctx: FContext, lhs: AST, rhs: AST): Tuple<FContext,FContext> = (
match lhs {
App{lhs-rst=left, right:App{ left:Lit{key:c":"}, right:App{left:Var{k=key}, right:AType{kt=tt}} }} => (
if typeof(rhs).is-t(c"Cons") {
match rhs {
App{le=left, re=right} => (
if kt.is-t(c"...") {
kt = kt.slot(c"...").r1;
if can-unify(kt, typeof(re)) {
if kt.is-open { callee-ctx = union(callee-ctx, unify(kt, typeof(re))); };
let c1 = std-c-compile-expr(caller-ctx, re);
caller-ctx = open(c1.context);
callee-ctx = bind-vararg(callee-ctx, k, kt, c1);
(callee-ctx, caller-ctx) = std-c-compile-args(callee-ctx, caller-ctx, lhs, le);
} else {
(callee-ctx, caller-ctx) = std-c-compile-args(callee-ctx, caller-ctx, lhs-rst, rhs);
}
} else {
if kt.is-open { callee-ctx = union(callee-ctx, unify(kt, typeof(re))); };
let c1 = std-c-compile-expr(caller-ctx, re);
caller-ctx = open(c1.context);
callee-ctx = callee-ctx.bind(k, kt, c1);
(callee-ctx, caller-ctx) = std-c-compile-args(callee-ctx, caller-ctx, lhs-rst, le);
}
);
}
} else {
if kt.is-t(c"...") {
kt = kt.slot(c"...").r1;
if can-unify(kt, typeof(rhs)) {
if kt.is-open { callee-ctx = union(callee-ctx, unify(kt, typeof(rhs))); };
let c1 = std-c-compile-expr(caller-ctx, rhs);
caller-ctx = open(c1.context);
callee-ctx = bind-vararg(callee-ctx, k, kt, c1);
} else {
(callee-ctx, caller-ctx) = std-c-compile-args(callee-ctx, caller-ctx, lhs-rst, rhs);
}
} else {
if kt.is-open { callee-ctx = union(callee-ctx, unify(kt, typeof(rhs))); };
let c1 = std-c-compile-expr(caller-ctx, rhs);
caller-ctx = open(c1.context);
callee-ctx = callee-ctx.bind(k, kt, c1);
}
};
(callee-ctx, caller-ctx)
);
App{ left:Lit{key:c":"}, right:App{left:Var{k=key}, right:AType{kt=tt}} } => (
if typeof(rhs).is-t(c"Cons") {
match rhs {
App{le=left, re=right} => (
if kt.is-t(c"...") {
kt = kt.slot(c"...").r1;
if kt.is-open { callee-ctx = union(callee-ctx, unify(kt, typeof(re))); };
let c1 = std-c-compile-expr(caller-ctx, re);
caller-ctx = open(c1.context);
callee-ctx = bind-vararg(callee-ctx, k, kt, c1);
(callee-ctx, caller-ctx) = std-c-compile-args(callee-ctx, caller-ctx, lhs, le);
} else {
fail("std-c-compile-args unexpected RHS, vararg mismatch");
}
);
}
} else {
if kt.is-t(c"...") {
kt = kt.slot(c"...").r1;
if can-unify(kt, typeof(rhs)) {
if kt.is-open { callee-ctx = union(callee-ctx, unify(kt, typeof(rhs))); };
let c1 = std-c-compile-expr(caller-ctx, rhs);
caller-ctx = open(c1.context);
callee-ctx = bind-vararg(callee-ctx, k, kt, c1);
} else {
(callee-ctx, caller-ctx) = std-c-compile-args(callee-ctx, caller-ctx, lhs, rhs);
}
} else {
if kt.is-open { callee-ctx = union(callee-ctx, unify(kt, typeof(rhs))); };
let c1 = std-c-compile-expr(caller-ctx, rhs);
caller-ctx = open(c1.context);
callee-ctx = callee-ctx.bind(k, kt, c1);
}
};
(callee-ctx, caller-ctx)
);
}
);
17 changes: 17 additions & 0 deletions PLUGINS/BACKEND/C/std-c-compile-call.lsts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

let std-c-compile-call(ctx: FContext, fname: CString, fterm: AST, args: AST): Fragment = (
if typeof(fterm).is-t(c"Blob") {
let f = ctx.lookup(fname, typeof(args), args);
let r = mk-fragment();
match f.term {
Abs{lhs=lhs, rhs=rhs} => (
(let callee-ctx, let caller-ctx) = std-c-compile-args(global-ctx, ctx, lhs, args);
r = blob-render(callee-ctx, rhs, r, 0);
r.context = close(caller-ctx);
);
};
r
} else {
fail("TODO std-c-compile-call Function"); mk-fragment();
}
);
4 changes: 4 additions & 0 deletions PLUGINS/BACKEND/C/std-c-compile-constructor.lsts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

let std-c-compile-constructor(ctx: FContext, fname: CString, fterm: AST, args: AST): Fragment = (
mk-fragment();
);
41 changes: 40 additions & 1 deletion PLUGINS/BACKEND/C/std-c-compile-expr.lsts
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@

let std-c-compile-expr(ctx: FContext, t: AST): Fragment = (
match t {
App{ left:Lit{key:c":"}, right:App{ left:Lit{val=key}, right:AType{tt=tt} } } => (
if tt.is-t(c"C") && tt.r1.is-t(c"uint8_t") then mk-expression(val)
else if tt.is-t(c"C") && tt.r1.is-t(c"uint16_t") then mk-expression(val)
else if tt.is-t(c"C") && tt.r1.is-t(c"uint32_t") then mk-expression(val)
else if tt.is-t(c"C") && tt.r1.is-t(c"uint64_t") then mk-expression(val)
else if tt.is-t(c"C") && tt.r1.is-t(c"int8_t") then mk-expression(val)
else if tt.is-t(c"C") && tt.r1.is-t(c"int16_t") then mk-expression(val)
else if tt.is-t(c"C") && tt.r1.is-t(c"int32_t") then mk-expression(val)
else if tt.is-t(c"C") && tt.r1.is-t(c"int64_t") then mk-expression(val)
else (fail("Unknown C Literal Type: \{tt}"); mk-fragment());
);
App{ left:Lit{key:c":"}, right:App{ inner-term=left, right:AType{tt=tt} } } => (
std-c-compile-expr(ctx, inner-term);
);
App{ left:Var{key:c"c::compound"}, terms=right } => (
let e = mk-fragment();
e = e.set(c"expression", SAtom{c"{}"});
let terms-list = [] :: List<AST>;
while non-zero(terms) {match terms {
App{left=left, right=right} => (
terms-list = cons( right, terms-list );
terms = left;
);
_ => terms = ASTEOF;
}};
for ct in terms-list.reverse {
let f = std-c-compile-expr(ctx, ct);
ctx = open(f.context);
e = std-c-chain( e, f );
};
e = e.set(c"expression", SAtom{c"{"} + e.get(c"expression") + SAtom{c"}"} );
e
);
App{ is-cons=is-cons, left=left, right=right } => (
if not(is-cons) && typeof(left).is-arrow {
match left {
Var{fname=key} => std-c-compile-call(ctx, fname, left, right);
App{ left:Var{fname=key}, right:App{ inner-term=left, right:AType{tt=tt} } } => std-c-compile-call(ctx, fname, left, inner-term);
Lit{fname=key} => std-c-compile-constructor(ctx, fname, left, right);
App{ left:Lit{fname=key}, right:App{ inner-term=left, right:AType{tt=tt} } } => std-c-compile-constructor(ctx, fname, left, inner-term);
_ => (
print("TODO: std-c-misc-call "); std-c-print(right); print("\n"); exit(1); mk-fragment();
);
}
} else {
fail("TODO: std-c-compile-expr cons"); mk-fragment();
}
);
_ => (
print("TODO: std-c-compile-expr\n");
std-c-print(t); exit(1); mk-fragment();
Expand Down
4 changes: 4 additions & 0 deletions tests/c/return.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

int main() {
return 0;
}
Empty file added tests/c/return.c.out
Empty file.

0 comments on commit b919087

Please sign in to comment.