Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include as an MLang expression #703

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions stdlib/mlang/ast-builder.mc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ recursive let mlang_bindF_ = use MLangAst in
bindF_ (lam letexpr. lam expr.
match letexpr with TmUse t then
TmUse {t with inexpr = mlang_bindF_ f t.inexpr expr}
else match letexpr with TmInclude t then
TmInclude {t with inexpr = mlang_bindF_ f t.inexpr expr}
else
f letexpr expr -- Insert at the end of the chain
) letexpr expr
Expand All @@ -34,6 +36,10 @@ let use_ = use UseAst in
lam s.
nuse_ (nameNoSym s)

let include_ = use IncludeAst in
lam p.
TmInclude {path = p, inexpr = uunit_, ty = tyunknown_, info = NoInfo {}}


-- Declarations --

Expand Down
28 changes: 27 additions & 1 deletion stdlib/mlang/ast.mc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,32 @@ lang UseAst = Ast
(acc, TmUse {t with inexpr = inexpr})
end

--- TmInclude --
lang IncludeAst = Ast
syn Expr =
| TmInclude {path : String,
inexpr : Expr,
ty : Type,
info : Info}

sem infoTm =
| TmInclude t -> t.info

sem tyTm =
| TmInclude t -> t.ty

sem withInfo (info : Info) =
| TmInclude t -> TmInclude {t with info = info}

sem withType (ty : Type) =
| TmInclude t -> TmInclude {t with ty = ty}

sem smapAccumL_Expr_Expr (f : acc -> Expr -> (acc, Expr)) (acc : acc) =
| TmInclude t ->
match f acc t.inexpr with (acc, inexpr) in
(acc, TmInclude {t with inexpr = inexpr})
end

-- Base fragment for MLang declarations --
lang DeclAst = Ast
syn Decl = -- intentionally left blank
Expand Down Expand Up @@ -142,7 +168,7 @@ lang MLangAst =
MLangTopLevel

-- Additional expressions
+ UseAst
+ UseAst + IncludeAst

-- Declarations
+ LangDeclAst + SynDeclAst + SemDeclAst + LetDeclAst + TypeDeclAst
Expand Down
15 changes: 14 additions & 1 deletion stdlib/mlang/pprint.mc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ lang MLangIdentifierPrettyPrint = IdentifierPrettyPrint
(env, s)
end

lang IncludePrettyPrint = PrettyPrint + IncludeAst
sem isAtomic =
| TmInclude _ -> false

sem pprintCode (indent : Int) (env: PprintEnv) =
| TmInclude t ->
match pprintCode indent env t.inexpr with (env,inexpr) in
(env, join ["include \"", escapeString t.path, "\"", pprintNewline indent,
"in", pprintNewline indent,
inexpr])
end


lang UsePrettyPrint = PrettyPrint + UseAst + MLangIdentifierPrettyPrint
sem isAtomic =
Expand Down Expand Up @@ -207,7 +219,7 @@ end
lang MLangPrettyPrint = MExprPrettyPrint +

-- Extended expressions
UsePrettyPrint +
UsePrettyPrint + IncludePrettyPrint +

-- Declarations
DeclPrettyPrint + LangDeclPrettyPrint + SynDeclPrettyPrint +
Expand Down Expand Up @@ -244,6 +256,7 @@ let prog: MLangProgram = {
(pcon_ "Pear" (pvar_ "fs"),
bindall_ [
ulet_ "strJoin" (unit_),
include_ "string.mc",
appf2_ (var_ "strJoin")
(var_ "x")
(appf2_ (var_ "map") (var_ "float2string") (var_ "fs"))
Expand Down