Skip to content

Commit

Permalink
Remove Tlang directory and add macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Memorytaco committed May 7, 2024
1 parent fa19fea commit 933f786
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 124 deletions.
6 changes: 3 additions & 3 deletions bootstrap/bootstrap.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ library
Compiler.CodeGen
Compiler.CodeGen.LLVM
Compiler.CodeGen.LLVM.Lambda
Compiler.CodeGen.LLVM.Module
Compiler.NameChecking
Compiler.SourceParsing
Compiler.Store
Expand Down Expand Up @@ -62,9 +63,9 @@ library
JIT.FFI
JIT.LLVM
Language.Constraint.Graphic
Language.Constraint.Kind
Language.Constraint.Unification
Language.Core
Language.Core.Attribute
Language.Core.Class
Language.Core.Class.Decl
Language.Core.Constraint
Expand All @@ -75,6 +76,7 @@ library
Language.Core.Extension.Decl
Language.Core.Extension.Expr
Language.Core.Extension.Type
Language.Core.Macro
Language.Core.Module
Language.Core.Name
Language.Core.Operator
Expand All @@ -100,8 +102,6 @@ library
Language.Setting
Language.TH
Recursion.Morphism
Tlang.Codegen
Tlang.Inference.Kind
Transform.CPSTransform
Transform.Desugar
Transform.GraphType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
module Tlang.Codegen
( -- ** FIXME: this module is not useful at current time, use `Driver.Codegen`
module Compiler.CodeGen.LLVM.Module

-- ** helper
createModule
, shortString

)
where


import LLVM.AST
( defaultModule, Module(moduleSourceFileName, moduleName) )

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
{-# OPTIONS_GHC -Wno-incomplete-patterns #-}
{-# OPTIONS_GHC -Wno-unused-matches #-}
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
module Tlang.Inference.Kind
{- | TODO: add kind constraint to solver after finishing the theroy part
-- -}
module Language.Constraint.Kind
(
KindUnifyError (..)
, shift
Expand Down Expand Up @@ -193,3 +195,4 @@ genConstraint = cata go
-- k <- freshName
-- return ((k1 :<> foldr (::>) k kn) : s1 <> sn, TypCon term1 termn `annotate` k)
-- go (TypRepF mr) = return . pure $ TypRep r `annotate` KindType

29 changes: 22 additions & 7 deletions bootstrap/src/Language/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ module Language.Core
, ExprSurface
, ExprSurfaceExt

-- ** surface macro lang
, MacroSurfaceAttr
, MacroSurfaceAttrExt

-- ** parsed declaration structure
, DeclSurface
, DeclsSurface
Expand Down Expand Up @@ -78,7 +82,7 @@ module Language.Core
, module Decl
, module Pattern
, module Name
, module Attribute
, module Macro
, module Class
, module Constraint
, module Utility
Expand All @@ -94,7 +98,7 @@ import Language.Core.Operator as Operator
import Language.Core.Decl as Decl
import Language.Core.Pattern as Pattern
import Language.Core.Name as Name
import Language.Core.Attribute as Attribute
import Language.Core.Macro as Macro
import Language.Core.Constraint as Constraint
import Language.Core.Class as Class
import Language.Core.Utility as Utility
Expand Down Expand Up @@ -140,17 +144,28 @@ type GPatSurfaceLitExt = PatSurfaceLitExt
-- | Surface Expr
type ExprSurface typ = Expr (ExprSurfaceExt typ) Name
type ExprSurfaceExt typ =
( LetGrp (PatSurface typ) -- plain pattern match via "let" binding
LetGrp (PatSurface typ) -- plain pattern match via "let" binding
:+: Let (Binder (Name @: Maybe typ)) -- desugared non-recursive "let" binding
:+: Letrec (Binder (Name @: Maybe typ)) -- desugared possible-recrusive "let" binding group
:+: Equation (GPatSurface typ) (Prefixes Name typ) -- heavy lambda
:+: Equation (Grp (PatSurface typ)) (Prefixes Name typ) -- light lambda
:+: Apply -- application, term beta-reduction or type application
:+: Apply -- application, term beta-reduction or type application
:+: Value typ -- unlifted type value
:+: (@:) typ -- type annotation
:+: LiteralText :+: LiteralInteger :+: LiteralNumber
:+: Tuple :+: Record Label :+: Selector Label :+: Constructor Label
)


-------------------
-- ** Surface Attribute Macro
-------------------
type MacroSurfaceAttr a = Macro MacroSurfaceAttrExt a
type MacroSurfaceAttrExt =
LiteralText -- allow string value in macro
:+: LiteralInteger -- allow integer value in macro
:+: List -- allow list value in macro
:+: Constructor Name -- allow application in macro
:+: Record Name -- allow key value pair in macro

----------------------------------
-- ** Surface toplevel declaration
Expand Down Expand Up @@ -183,15 +198,15 @@ type ModuleSurfaceExt typ expr = DeclSurfaceExt typ expr

-- | Graph representation of type
type GraphicTypeSurface = CoreG GraphicNodesSurface GraphicEdgesSurface Int
type GraphicNodesSurface =
type GraphicNodesSurface =
NodePht :+: T NodeBot :+: T (NodeLit Integer) :+: T (NodeLit Text)
:+: T NodeTup :+: T NodeSum :+: T NodeRec :+: T (NodeRef Name)
:+: T NodeApp :+: T (NodeHas Label) :+: T NodeArr
type GraphicEdgesSurface = T Sub :+: T (Binding Name)

-- | Graph representation of constraint
type ConstraintSurface = CoreG ConstraintNodesSurface ConstraintEdgesSurface Int
type ConstraintNodesSurface =
type ConstraintNodesSurface =
GraphicNodesSurface
:+: NDOrder :+: Histo :+: G
type ConstraintEdgesSurface =
Expand Down
16 changes: 0 additions & 16 deletions bootstrap/src/Language/Core/Attribute.hs

This file was deleted.

7 changes: 6 additions & 1 deletion bootstrap/src/Language/Core/Extension/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Language.Core.Extension.Common
, Binder (..)
, Cast (..)
, Literal (..)
, List (..)
)
where

Expand Down Expand Up @@ -38,7 +39,7 @@ instance (Pretty label, Pretty a) => Pretty (Record label a) where
instance (Show label, Show a) => Show (Record label a) where
show (Record vs) = "{" <> intercalate ", " ((\(a, b) -> show a <> " = " <> show b) <$> vs) <> "}"

-- | Inject any constant into the expression
-- | inject any other thing into the expression, e.g. @type injection
newtype Value val a = Value val
deriving (Show, Eq, Ord, Functor, Foldable, Traversable)
deriving (Pretty) via val
Expand All @@ -61,3 +62,7 @@ newtype Literal c a = Literal { getLiteral :: c }
deriving (Eq, Ord, Functor, Foldable, Traversable)
deriving (Show, Pretty) via c

-- | add @[]@ list constructor to language
newtype List a = List { unList :: [a] }
deriving (Eq, Ord, Functor, Foldable, Traversable)
deriving (Show, Pretty) via [a]
2 changes: 1 addition & 1 deletion bootstrap/src/Language/Core/Extension/Decl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module Language.Core.Extension.Decl
where

import Language.Core.Operator
import Language.Core.Attribute
import Language.Core.Macro
import Language.Core.Constraint (Prefixes (..))
import Language.Core.Class.Decl

Expand Down
50 changes: 50 additions & 0 deletions bootstrap/src/Language/Core/Macro.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Language.Core.Macro
( Attr (..)
, Macro (..)
, MacroF (..)
)
where

import Data.Functor.Foldable.TH
import Data.Functor.Foldable (Recursive)
import Language.TH (fixQ)

import Data.Text (Text)

-- | Structure is defined by macro functionality `f` and
-- result is defined by parameter `inst`, meaning
-- instruction. `inst` can also be used for other
-- temporary purpose for convenience.
--
-- Macro may or may not be inline, block or
-- special one. It may or may not involve
-- customized user syntax or reading environment
-- information.
data Macro f inst
= Inf inst
| Macro (f (Macro f inst))
deriving (Functor, Foldable)

instance Functor f => Applicative (Macro f) where
pure = Inf
(Inf f) <*> (Inf a) = Inf (f a)
f <*> Macro fv = Macro ((f <*>) <$> fv)
(Macro fv) <*> a = Macro ((<*> a) <$> fv)

instance Functor f => Monad (Macro f) where
Inf a >>= f = f a
Macro fma >>= f = Macro ((>>= f) <$> fma)


-- | a small expression for defining attributes
data Attr
= AttrI Integer -- ^ Integer value
| AttrT Text -- ^ literal value
| AttrS [Attr] -- ^ sequence items, take a list form
| AttrC Text [Attr] -- ^ constructor value, with optional arguments
| AttrP [(Text, Attr)] -- ^ associated value, take a record form
deriving (Show, Eq, Ord)

makeBaseFunctor $ fixQ [d|
instance (Functor f) => Recursive (Macro f a)
|]
2 changes: 2 additions & 0 deletions bootstrap/src/Language/Core/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ instance
pretty (TypVar name) = pretty name
pretty (Type t) = pretty t

-- TODO: refine kind definition in `Language.Core.Type` module

infixr 3 :->

-- | type kind representation, any kind, normal kind (*) and higher kind
Expand Down
39 changes: 33 additions & 6 deletions bootstrap/src/Language/DataLayout.hs
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
{- | reexport submodules
{- | * Language runtime and datalayout
--
-- This module defines everything we need to
-- wrap language program into a low a level
-- middle language, which is closing
-- to C programming language but is specialised to
-- data layout.
--
-- This acts like a translation layer between
-- abstraction code and real machine code. It aims
-- to handle some common translation idioms which
-- may repeat itself so many times that we decide
-- to put a marker to it so we can deal with
-- it in a uniform way.
-}


module Language.DataLayout
(
-- ** runtime representation
( -- ** runtime representation
Rep (..)
, _Rep

-- ** re-export
, module Class
, module Prim
, module Primitive
, module DataRep
)
where

import Language.DataLayout.Class as Class
import Language.DataLayout.Primitive as Prim
import Language.DataLayout.Primitive as Primitive
import Language.DataLayout.DataRep as DataRep

import Control.Lens
import Prettyprinter (Pretty)

-- | runtime representation of types in language.
--
-- Its semantic is basically interpreted by parameter `a` in `Rep a`.
--
-- e.g. If we take `a` as something like high level type
-- system (let's say T), then `Rep T` can mean "it is in the
-- middle of representing type system T". And `Rep T` can also
-- be part of `T`, where "runtime representation" is comming from.
--
-- If T is something real, like assembly language type and we can
-- say we are in the middle of translating representation to
-- real thing running on computer.
--
-- This type is originally designed to target at LLVM type
-- system, so it should not be more complex than a
-- direct mapping to translate it into real LLVM type.
newtype Rep a = Rep { __Rep :: DataRep (PrimitiveT SeqT) a}
deriving newtype (Show, Eq, Ord, Functor, Pretty)

Expand Down
1 change: 1 addition & 0 deletions bootstrap/src/Language/DataLayout/Class.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- TODO: refine DataLayout.Class module and introduce lowlevel name mangle here
module Language.DataLayout.Class
( TypeMangle (..)
, EncodeLLVMType (..)
Expand Down
14 changes: 8 additions & 6 deletions bootstrap/src/Language/DataLayout/DataRep.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{- | * runtime type representation
This module introduces `DataRep` constructor to help
translating high level type into machine type, which is
LLVM IR type in this situation.
-- This module introduces `DataRep` constructor to help
-- translating high level type into machine type.
-- It is originally designed to target at LLVM IR.
-}
{-# LANGUAGE QuantifiedConstraints #-}
Expand All @@ -25,9 +25,9 @@ import Prettyprinter (Pretty (..), encloseSep, (<+>))

-- | runtime representation for type
data DataRep t a where
-- | We have PrimitiveT contained, and it is a concrete type
-- | We have PrimitiveT contained, and it is a concrete type.
RepLift :: t (DataRep t a) -> DataRep t a
-- | Introduce whatever type system using `f`
-- | Introduce whatever type system using `t`, and this is a direct encoding.
DataRep :: a -> DataRep t a
deriving (Functor)

Expand All @@ -41,7 +41,9 @@ instance (Show (t (DataRep t a)), Show a) => Show (DataRep t a) where
deriving instance (Eq (t (DataRep t a)), Eq a) => Eq (DataRep t a)
deriving instance (Ord (t (DataRep t a)), Ord a) => Ord (DataRep t a)

-- | A hint provided by user to determin which type we will use, but the result is not guarenteed
-- | A hint provided by user to determin which type we will use, but the result is not guarenteed.
--
-- If we need other sequence alike primitive container, we can add it here.
data SeqT a where
SeqVector :: a -> Integer -> SeqT a
SeqArray :: a -> Maybe Integer -> SeqT a
Expand Down
11 changes: 6 additions & 5 deletions bootstrap/src/Language/DataLayout/Primitive.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{- | * Language primitive types
it is the fundamental building blocks of native type.
--
-- it is the fundamental building blocks of native type.
-}

module Language.DataLayout.Primitive
Expand Down Expand Up @@ -36,17 +36,18 @@ import Prettyprinter ( encloseSep, comma, Pretty(pretty) )
-- we can use it to do a direct translation between host type and llvm IR type.
data PrimitiveT seq a where
-- | void to mean empty value inhabitance of type
VoidT :: PrimitiveT seq a
VoidT :: PrimitiveT seq a
-- | see `PrimScalaType`
Scala :: ScalaType -> PrimitiveT seq a
-- | pointer type
-- | pointer type should only accept `PrimitiveT` type. The integer is
-- used for memory namespace.
Ptr :: PrimitiveT seq a -> Maybe Integer -> PrimitiveT seq a
-- | raw sequential type, array or vector
Seq :: seq (PrimitiveT seq a) -> PrimitiveT seq a
-- | Aggregate type
Struct :: [PrimitiveT seq a] -> Bool -> PrimitiveT seq a
-- | Allow other elements to be merged into primitive type
Embed :: a -> PrimitiveT seq a
Embed :: a -> PrimitiveT seq a
deriving (Functor, Foldable, Traversable)

deriving instance (Eq a, Eq (t (PrimitiveT t a))) => Eq (PrimitiveT t a)
Expand Down
1 change: 1 addition & 0 deletions bootstrap/src/Language/Parser/Decl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ instance (ParserM e m, Rule (WithDecl e m a) (Decl decl info) m, Rule (WithDecl
rule _ end = try (parseRule @(WithDecl e m a) end) <|> parseRule @(WithDecl e m b) end

-- | foreign interface
-- TODO: replace `Attr` with `Macro`
instance (ParserM e m, PrattToken proxy typ m, info ~ Name, Item (FFI typ Name) :<: decl)
=> Rule (WithDecl e m (Layer "ffi" proxy typ)) (Decl decl info) m where
rule _ end = do
Expand Down
Loading

0 comments on commit 933f786

Please sign in to comment.