Skip to content

Commit

Permalink
Merge pull request #146 from andrew-johnson-4/stdlib-v1
Browse files Browse the repository at this point in the history
Stdlib v1
  • Loading branch information
andrew-johnson-4 authored Mar 13, 2024
2 parents e3e76f2 + fb33645 commit 19d6fee
Show file tree
Hide file tree
Showing 13 changed files with 13,588 additions and 11,869 deletions.
25,289 changes: 13,461 additions & 11,828 deletions BOOTSTRAP/cli.s

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lambda_mountain"
version = "1.11.17"
version = "1.11.18"
authors = ["Andrew <andrew@subarctic.org>"]
license = "MIT"
description = "Lambda Mountain"
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

nostd: prod
# ./production --nostd -o tmp.s STDLIB/default-instruction-set.lm STDLIB/default-primitives.lm tests/nostd/normal_fib.lm
as -o tmp.o scratch_area/sfz_example.s
./production --nostd -o tmp.s STDLIB/default-instruction-set.lm STDLIB/default-primitives.lm STDLIB/default-stdlib.lm tests/nostd/printU64.lm
as -o tmp.o tmp.s
ld -o tmp tmp.o
./tmp && echo $?

Expand Down
24 changes: 4 additions & 20 deletions PRODUCTION/assemble.lm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ assemble-destructure-fragment := λctx lmb args offset . (match lmb (

assemble-substitute-fragment := λsubs frag e . (match frag (
()
( Nil (
(expr::new())
))
( (App( (App( (Literal ':) fe )) ft )) (
(assemble-substitute-fragment( subs fe e ))
))
Expand Down Expand Up @@ -56,7 +59,7 @@ assemble-substitute-fragment := λsubs frag e . (match frag (
)))
))
))
( u (fail (UnknownFragment frag)))
( u (fail (UnknownSubstituteFragment frag)))
));

assemble-substitute-fragment-mode := λsubs mode frag . (match frag (
Expand Down Expand Up @@ -123,25 +126,6 @@ assemble-substitute-fragment-mode := λsubs mode frag . (match frag (
( u (fail (UnknownFragmentSubstitution u)))
));

assemble-apply-literal := λctx arrow args offset . (match arrow (
()
( (Lambda( (App( (App( (Literal :) (Variable bind) )) ltype )) rhs )) (
(match args (
()
( (App( (App( (Literal :) (Literal val) )) vtype )) (
fail (ApplyLiteral val vtype)
#(local result)
#(set result (assemble-substitute-fragment( rc rhs (expr::new()) ))
#(set result (expr::set-context result ctx))
#(set result (expr::set-offset result offset))
#result
))
( u (fail (UnknownLiteral u)) )
))
))
( u (fail (UnknownApplyLiteral u)) )
));

assemble-apply-fragment := λctx arrow args offset . (tail(
(local rc)
(set rc (assemble-destructure-fragment( ctx arrow args offset )))
Expand Down
2 changes: 1 addition & 1 deletion PRODUCTION/codegen-dynamic.lm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ compile-include-argv := λ. if compile-argv-hook () (set compile-argv-hook (comp
)));

compile-append := λoutput_e gctx e . (tail(
(set e (compile-expr( gctx e (expr::get-offset output_e) Used )))
(set e (compile-expr( gctx e (expr::get-offset output_e) Unused )))
(Expr(
((expr::get-frame output_e) (expr::get-frame e))
((expr::get-prog output_e) (expr::get-prog e))
Expand Down
50 changes: 44 additions & 6 deletions PRODUCTION/codegen-strict.lm
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,54 @@ compile-expr-strict := λctx e offset used . (tail(
()
( (App( (App( (Literal :) (Literal lval) )) ltype )) (tail(
(set e1 (expr::set-expr( (expr::new ()) lval )))
(set e1 (expr::set-prog( e1 lval )))
(set e1 (expr::set-context( e1 ctx )))
(set e1 (expr::set-offset( e1 offset )))
e1
)))
( (App( (App( (Literal :) lval )) ltype )) (tail(
(set e1 (compile-expr-strict( ctx lval offset Unused )))
e1
)))
( (App( (Variable 'tail) (App( l r )) )) (tail(
(set e1 (compile-expr-strict( ctx l offset Unused )))
(set e2 (compile-expr-strict( (expr::get-context e1) r (expr::get-offset e1) Used )))
(set e2 (expr::set-frame( e2 ( (expr::get-frame e1) (expr::get-frame e2) ))))
(set e2 (expr::set-unframe( e2 ( (expr::get-unframe e1) (expr::get-unframe e2) ))))
(set e2 (expr::set-text( e2 ( (expr::get-text e1) (expr::get-text e2) ))))
(set e2 (expr::set-data( e2 ( (expr::get-data e1) (expr::get-data e2) ))))
(set e2 (expr::set-prog( e2 ( (expr::get-prog e1) (expr::get-prog e2) ))))
e2
)))
( (App ((Variable 'gensym-label) arg)) (tail(
(set e1 (expr::set-context( (expr::new()) ctx )))
(set e1 (expr::set-offset( e1 offset )))
e1
)))
( (App ((Variable fname) arg)) (tail(
(set e1 (apply-calling-convention-stack( ctx e fname arg offset used )))
e1
)))
( Nil (
( Nil (tail(
(set e1 (expr::new()) )
(set e1 (expr::set-context( e1 ctx )))
(set e1 (expr::set-offset( e1 offset )))
e1
))
)))
( (App (l r)) (tail(
(if (eq( used Used )) (
(fail (TODO Return Strict Cons e))
) (
(set e1 (compile-expr-strict( ctx l offset Unused )))
(set e2 (compile-expr-strict( (expr::get-context e1) r (expr::get-offset e1) Used )))
(set e2 (expr::set-frame( e2 ( (expr::get-frame e1) (expr::get-frame e2) ))))
(set e2 (expr::set-unframe( e2 ( (expr::get-unframe e1) (expr::get-unframe e2) ))))
(set e2 (expr::set-text( e2 ( (expr::get-text e1) (expr::get-text e2) ))))
(set e2 (expr::set-data( e2 ( (expr::get-data e1) (expr::get-data e2) ))))
(set e2 (expr::set-prog( e2 ( (expr::get-prog e1) (expr::get-prog e2) ))))
e2
))
)))
( _ (fail (TODO CompileStrict e)))
))
))
Expand All @@ -64,7 +98,7 @@ apply-calling-convention-stack := λ ctx e fname arg offset used . (tail(
e
)))
( (StackVariable vname) (tail(
(set e1 (compile-expr( ctx arg offset used )))
(set e1 (compile-expr-strict( ctx arg offset used )))
(local l)
(set l (get-local( ctx fname )))
(if l () (fail (UnknownFunctionLocal fname)))
Expand All @@ -83,7 +117,7 @@ define-calling-convention-stack := λ ctx fname lmb . (tail(
(local e2)
(local text)
(set e1 (define-calling-convention-stack-destructure-args( ctx lhs () )))
(set e2 (compile-expr( (expr::get-context e1) rhs (expr::get-offset e1) Return)))
(set e2 (compile-expr-strict( (expr::get-context e1) rhs (expr::get-offset e1) Return)))
(set text ( text (mangle-global-function( fname lmb )) ': \n ))
(set text ( text (expr::get-frame e2) ))
(set text ( text (expr::get-prog e2) ))
Expand Down Expand Up @@ -214,6 +248,7 @@ compile-fragment := λctx e offset used . (match e (
( (App( (App( (Literal :) (Literal lval) )) ltype )) (tail(
(set ltype (typecheck-infer-type-compound ltype))
(set e (expr::set-prog( (expr::new()) lval )))
(set e (expr::set-expr( e lval )))
(set e (expr::set-context( e ctx )))
(set e (expr::set-offset( e offset )))
e
Expand All @@ -222,12 +257,13 @@ compile-fragment := λctx e offset used . (match e (
()
(get-fragment-local( ctx v offset ))
)))
( (Literal val) (
( (Literal val) (tail(
(set e (expr::set-prog( (expr::new()) val )))
(set e (expr::set-expr( e val )))
(set e (expr::set-context( e ctx )))
(set e (expr::set-offset( e offset )))
e
))
)))
( u (fail (UnknownFragment e)))
);

Expand All @@ -240,6 +276,7 @@ get-fragment-local := λctx v offset . (tail(
if r () (
if (eq(l v)) (
(set r (expr::set-prog( (expr::new()) uid )))
(set r (expr::set-expr( r uid )))
(set r (expr::set-context( r ctx )))
(set r (expr::set-offset( r offset )))
r
Expand All @@ -250,6 +287,7 @@ get-fragment-local := λctx v offset . (tail(
if r () (
if (eq(l v)) (
(set r (expr::set-prog( (expr::new()) loffset )))
(set r (expr::set-expr( r loffset )))
(set r (expr::set-context( r ctx )))
(set r (expr::set-offset( r offset )))
r
Expand Down
31 changes: 29 additions & 2 deletions PRODUCTION/preprocess.lm
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ preprocess-destructure-type := λtt . (tail(
preprocess-apply-macros := λprogram . (match program (
()
( (App( (App( (Literal :) mvar )) (Literal mtype) )) (
(App( (App( (Literal :) mvar )) (preprocess-destructure-type mtype) ))
(App( (App( (Literal :) (preprocess-apply-macros mvar) )) (preprocess-destructure-type mtype) ))
))
( (App( (App( (Literal :) mvar )) (Variable mtype) )) (
(App( (App( (Literal :) mvar )) (preprocess-destructure-type mtype) ))
(App( (App( (Literal :) (preprocess-apply-macros mvar) )) (preprocess-destructure-type mtype) ))
))
( (Literal l) (tail(
(local suffixes)
Expand Down Expand Up @@ -132,6 +132,33 @@ preprocess-apply-macros := λprogram . (match program (
)))
program
)))
( (Variable l) (tail(
(local suffixes)
(set suffixes parse-suffix)
(while suffixes (tail(
(local sfx)
(set sfx (tail (tail suffixes)) )
(if (is-suffix( l sfx )) (
(set program (
(App(
(App(
(Literal ':)
(Literal (remove-suffix( l sfx )))
))
(App(
(Literal And)
(App(
(Literal Constant)
(Literal (head (tail suffixes)))
))
))
))
))
) ())
(set suffixes (head suffixes))
)))
program
)))
( _ (tail(
(local macros)
(set macros parse-macros)
Expand Down
30 changes: 21 additions & 9 deletions PRODUCTION/typecheck.lm
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ typecheck-assert-expr := λe . (tail(
( (Literal lit) (
(typecheck-assert-one( e ))
))
( (App( (Variable 'tail) (App( l r )) )) (tail(
(typecheck-assert-expr( r ))
(typecheck-assert-expr( l ))
(typecheck-assert-one( e ))
)))
( (App( l r )) (
(typecheck-assert-expr( r ))
(typecheck-assert-expr( l ))
Expand Down Expand Up @@ -202,7 +207,7 @@ typecheck-show-ascript := λp . (tail(

typecheck-infer-prog := λp . (
(while p (
(typecheck-infer-expr( () (tail p) ))
(typecheck-infer-expr( () (tail p) Used ))
(set p (head p))
))
);
Expand Down Expand Up @@ -500,15 +505,15 @@ typecheck-infer-type-expr := λe . (match e (
(_ ())
));

typecheck-infer-expr := λctx e . (tail(
typecheck-infer-expr := λctx e used . (tail(
(match e (
()
( () () )
( (GExpr ge) (
(typecheck-infer-expr( ctx ge ))
(typecheck-infer-expr( ctx ge Unused ))
))
( (Global( gln gle )) (
(typecheck-infer-expr( ctx gle ))
(typecheck-infer-expr( ctx gle Used ))
(match (typecheck-lookup-type gle) (
()
( () () )
Expand Down Expand Up @@ -550,12 +555,12 @@ typecheck-infer-expr := λctx e . (tail(
(typecheck-ascript( e (typecheck-infer-type-compound tt) ))
))
( (App( (App( (Literal ':) asc )) tt )) (
(typecheck-infer-expr( ctx asc ))
(typecheck-infer-expr( ctx asc used ))
(typecheck-ascript( asc (typecheck-infer-type-compound tt) ))
(typecheck-ascript( e (typecheck-infer-type-compound tt) ))
))
( (App( (App( (Variable 'as) ast )) ett )) (
(typecheck-infer-expr( ctx ast ))
(typecheck-infer-expr( ctx ast used ))
(typecheck-ascript( e (typecheck-infer-type-compound ett) ))
))
( (Literal lit) (
Expand All @@ -567,9 +572,16 @@ typecheck-infer-expr := λctx e . (tail(
(typecheck-ascript( e Atom ))
))
))
( (App( (Variable 'tail) (App( l r )) )) (tail(
(set ctx (typecheck-infer-expr( ctx l used )))
(set ctx (typecheck-infer-expr( ctx r used )))
(if (typecheck-lookup-type r) (
(typecheck-ascript( e (typecheck-lookup-type r) ))
) ())
)))
( (App( l r )) (tail(
(set ctx (typecheck-infer-expr( ctx l )))
(set ctx (typecheck-infer-expr( ctx r )))
(set ctx (typecheck-infer-expr( ctx l used )))
(set ctx (typecheck-infer-expr( ctx r used )))
(match ( (typecheck-lookup-type l) (typecheck-lookup-type r) ) (
()
( (() _) () )
Expand All @@ -596,7 +608,7 @@ typecheck-infer-expr := λctx e . (tail(
)))
( (Lambda( lhs rhs )) (tail(
(set ctx (typecheck-infer-ctx( ctx lhs )))
(typecheck-infer-expr( ctx rhs ))
(typecheck-infer-expr( ctx rhs Used ))
(match (typecheck-lookup-type rhs) (
()
( () () )
Expand Down
8 changes: 8 additions & 0 deletions STDLIB/default-instruction-set.lm
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ fragment push := λ(: l Constant+I16). (: ( (.program( \t 'pushw \t '$ l \n)) (.
fragment push := λ(: l Constant+I32). (: ( (.program( \t 'pushs \t '$ l \n)) (.expression r12d) ) Nil);
fragment push := λ(: l Constant+I64). (: ( (.program( \t 'pushq \t '$ l \n)) (.expression r12 ) ) Nil);

fragment cdecl::return := λ(: l Nil). (: () Nil);
fragment cdecl::return := λ(: l Reg8). (: ( (.program( \t 'mov \t '% (.expression l) , '%al \n)) (.expression 'al) ) Nil);
fragment cdecl::return := λ(: l Reg16). (: ( (.program( \t 'mov \t '% (.expression l) , '%ax \n)) (.expression 'ax) ) Nil);
fragment cdecl::return := λ(: l Reg32). (: ( (.program( \t 'mov \t '% (.expression l) , '%eax \n)) (.expression 'eax) ) Nil);
fragment cdecl::return := λ(: l Reg64). (: ( (.program( \t 'mov \t '% (.expression l) , '%rax \n)) (.expression 'rax) ) Nil);

fragment mov := λ(: src LocalVariable+U64)(: dst Constant+Reg64). (: ( (.program( \t 'mov \t (.expression src) \[ '%rbp \] , \s '% (.expression dst) \n)) ) Nil);
fragment mov := λ(: src LocalVariable+I64)(: dst Constant+Reg64). (: ( (.program( \t 'mov \t (.expression src) \[ '%rbp \] , \s '% (.expression dst) \n)) ) Nil);

fragment cmp := λ(: imm Constant+U64)(: r Constant+Reg64). (: (.program( \t 'cmp \s '$ imm , '% (.expression r) \n )) Nil);
fragment cmp := λ(: imm Constant+I64)(: r Constant+Reg64). (: (.program( \t 'cmp \s '$ imm , '% (.expression r) \n )) Nil);

fragment jge := λ(: dst Label). (: (.program( \t 'jge \s (.expression dst) \n )) Nil);
11 changes: 11 additions & 0 deletions STDLIB/default-stdlib.lm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

print := λ(: x U64). (: (
(mov( x R15 ))
) Nil);
print := λ(: x I64). (: (tail(
(gensym-label unsigned)
(mov( x R15 ))
(cmp( 0i64 R15 ))
# (jge( unsigned ))
# (label unsigned)
)) Nil);
1 change: 1 addition & 0 deletions tests/b_semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ fn run_compile_production(mode:&str, target: &str) -> String {
.arg("STDLIB/default-rules.lm")
.arg("STDLIB/default-instruction-set.lm")
.arg("STDLIB/default-primitives.lm")
.arg("STDLIB/default-stdlib.lm")
.arg(target)
.spawn()
.expect("failed to execute process")
Expand Down
5 changes: 5 additions & 0 deletions tests/nostd/printU64.lm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

main := (
(print 123u64)
(print -456i64)
);
Empty file added tests/nostd/printU64.lm.out
Empty file.

0 comments on commit 19d6fee

Please sign in to comment.