Add string and float literals to the object language

Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
Danila Fedorin 2023-12-26 13:15:56 -08:00
parent b828c73e43
commit 0b3469b49a

View File

@ -1,6 +1,7 @@
module Bergamot.ObjectLanguage exposing (..)
import Bergamot.Syntax as Syntax exposing (Metavariable)
import Bergamot.Parser exposing (strLit)
import Parser exposing (Parser, Trailing(..), (|.), (|=))
import Set
@ -13,6 +14,7 @@ type Type
type Expr
= IntLit Int
| FloatLit Float
| StrLit String
| Plus Expr Expr
| Pair Expr Expr
@ -99,7 +101,15 @@ exprApps =
exprBasic : Parser Expr
exprBasic = Parser.lazy <| \() -> Parser.oneOf
[ Parser.backtrackable (Parser.succeed IntLit |= Parser.int)
[ Parser.backtrackable <|
Parser.number
{ int = Just IntLit
, hex = Nothing
, octal = Nothing
, binary = Nothing
, float = Just FloatLit
}
, Parser.backtrackable (Parser.succeed StrLit |= strLit)
, Parser.backtrackable <| Parser.map (\(a, b) -> Pair a b) <| pair expr
, Parser.succeed Fst
|. Parser.keyword "fst"
@ -137,6 +147,7 @@ exprToTerm : Expr -> Syntax.Term Metavariable
exprToTerm e =
case e of
IntLit i -> Syntax.Call "intlit" [Syntax.IntLit i]
FloatLit f -> Syntax.Call "floatlit" [Syntax.FloatLit f]
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]