From d2d6829d4b0684275a662ac4ca5b0aaac8667b03 Mon Sep 17 00:00:00 2001 From: crumbtoo Date: Tue, 7 Nov 2023 15:00:21 -0700 Subject: [PATCH] core AST --- meson.build | 5 +-- rlp/Core.java | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ rlp/RLPC.java | 22 +++++++++++++ src/RLPC.java | 8 ----- 4 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 rlp/Core.java create mode 100644 rlp/RLPC.java delete mode 100644 src/RLPC.java diff --git a/meson.build b/meson.build index a018a48..eb3d906 100644 --- a/meson.build +++ b/meson.build @@ -7,7 +7,8 @@ project( rlpc_jar = jar( 'rlpc', - 'src/RLPC.java', - main_class: 'RLPC' + 'rlp/RLPC.java', + 'rlp/Core.java', + main_class: 'rlp.rlpc.RLPC' ) diff --git a/rlp/Core.java b/rlp/Core.java new file mode 100644 index 0000000..158176a --- /dev/null +++ b/rlp/Core.java @@ -0,0 +1,88 @@ +package rlp; +/*------------------------------------------------------------------------------*/ +import java.util.function.BiFunction; +/*------------------------------------------------------------------------------*/ + +public class Core +{ + /* day 1: boy do i miss Haskell */ + public sealed interface Expr permits App, TyApp, Abs, TyAbs, Var {} + public record App(Expr f, Expr x) implements Expr {} + public record TyApp(Expr f, Ty x) implements Expr {} + public record Abs(Name n, Ty t, Expr m) implements Expr {} + public record TyAbs(Name n, Expr m) implements Expr {} + public record Var(Name n) implements Expr {} + + sealed interface Ty permits TyVar, Forall, TyFunc {} + public record TyVar(Name n) implements Ty {} + public record Forall(Name n, Ty t) implements Ty {} + public record TyFunc(Ty a, Ty b) implements Ty {} + + public final static class Name + { + public final String s; + public Name(String s) + { + // TODO: verify that `s` is a valid identifier + this.s = s; + } + } + + public static String showExpr(Expr e) + { + return showExprP(0, e); + } + + static String showExprP(int p, Expr e) + { + return switch(e) + { + /* prec: 1 */ + case App a -> + wrap(p, 1, showExprP(2, a.f) + " " + showExprP(2, a.x)); + + /* prec: 1 */ + case TyApp a -> + wrap(p, 1, showExprP(2, a.f) + " " + showTyP(2, a.x)); + + /* prec: 0 */ + case Abs a -> + wrap(p, 0, String.format + ( "λ(%s:%s).%s" + , a.n.s + , showTyP(0, a.t) + , showExprP(0, a.m))); + + /* prec: 0 */ + case TyAbs a -> + wrap(p, 0, String.format + ( "Λ%s.%s" + , a.n.s + , showExprP(0, a.m))); + + case Var a -> + a.n.s; + }; + } + + static String showTy(Ty t) + { + return showTyP(0, t); + } + + static String showTyP(int p, Ty t) + { + return switch(t) + { + case TyVar a -> a.n.s; + case Forall a -> ""; + case TyFunc a -> ""; + }; + } + + static String wrap(int p, int n, String s) + { + return (p <= n) ? s : "(" + s + ")"; + } +} + diff --git a/rlp/RLPC.java b/rlp/RLPC.java new file mode 100644 index 0000000..a2db769 --- /dev/null +++ b/rlp/RLPC.java @@ -0,0 +1,22 @@ +package rlp.rlpc; +/*----------------------------------------------------------------------------*/ +import rlp.Core; +/*----------------------------------------------------------------------------*/ + +class RLPC +{ + public static void main(String[] argv) + { + // final Core.Expr e = new Core.App(new Core.Var(new Core.Name("f")), new Core.Var(new Core.Name("x"))); + final Core.Expr e = + new Core.App + ( new Core.Abs + ( new Core.Name("x") + , new Core.TyVar(new Core.Name("α")) + , new Core.Var(new Core.Name("x")) + ) + , new Core.Var(new Core.Name("y"))); + System.out.println(Core.showExpr(e)); + } +} + diff --git a/src/RLPC.java b/src/RLPC.java deleted file mode 100644 index e593dd5..0000000 --- a/src/RLPC.java +++ /dev/null @@ -1,8 +0,0 @@ -class RLPC -{ - public static void main(String[] argv) - { - System.out.println("hello worms"); - } -} -