Add a mode for entering the object language to parse

Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
2023-12-01 11:31:38 -08:00
parent 51c78af138
commit 45a04cc59c
2 changed files with 66 additions and 17 deletions

View File

@@ -1,5 +1,7 @@
module Bergamot.ObjectLanguage exposing (..)
import Bergamot.Syntax as Syntax exposing (Metavariable)
import Parser exposing (Parser, Trailing(..), (|.), (|=))
import Set
@@ -119,3 +121,24 @@ parseExprBasic = Parser.lazy <| \() -> Parser.oneOf
, Parser.succeed Ref |= parseVariable
, parseParenthed parseExpr
]
typeToTerm : Type -> Syntax.Term Metavariable
typeToTerm t =
case t of
TInt -> Syntax.Call "tint" []
TStr -> Syntax.Call "tstr" []
TPair t1 t2 -> Syntax.Call "tpair" [ typeToTerm t1, typeToTerm t2 ]
TArr t1 t2 -> Syntax.Call "tarr" [ typeToTerm t1, typeToTerm t2 ]
exprToTerm : Expr -> Syntax.Term Metavariable
exprToTerm e =
case e of
IntLit i -> Syntax.Call "intlit" [Syntax.IntLit i]
StrLit s -> Syntax.Call "strlit" [Syntax.StringLit s]
Plus e1 e2 -> Syntax.Call "plus" [exprToTerm e1, exprToTerm e2]
Pair e1 e2 -> Syntax.Call "pair" [exprToTerm e1, exprToTerm e2]
Fst ep -> Syntax.Call "fst" [exprToTerm ep]
Snd ep -> Syntax.Call "snd" [exprToTerm ep]
Abs s t ep -> Syntax.Call "abs" [Syntax.Call s [], typeToTerm t, exprToTerm ep]
App e1 e2 -> Syntax.Call "app" [exprToTerm e1, exprToTerm e2]
Ref x -> Syntax.Call "var" [Syntax.Call x []]