-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement type system preprocessing.
- Loading branch information
Showing
14 changed files
with
216 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ Contua.cabal | |
.idea | ||
*.aux | ||
*.log | ||
out | ||
*.gz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module Parser.Parser | ||
( parseProgram | ||
, ParserError | ||
, program | ||
) where | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module Semantics.Builtins where | ||
|
||
import qualified Data.Map as Map | ||
import Data.Map (Map) | ||
import Data.List (isPrefixOf) | ||
|
||
import Parser.TypeDefs | ||
|
||
type BuiltinName = String | ||
|
||
builtinPrefix :: BuiltinName | ||
builtinPrefix = "__contua_builtin_" | ||
|
||
makeBuiltin :: String -> BuiltinName | ||
makeBuiltin = (builtinPrefix ++) | ||
|
||
isBuiltin :: String -> Bool | ||
isBuiltin s = builtinPrefix `isPrefixOf` s | ||
|
||
addName, subName, negName, mulName, consName, concName, andName, orName, eqName, leqName, notName, ifteName, matchesName :: BuiltinName | ||
addName = makeBuiltin "addition" | ||
subName = makeBuiltin "subtraction" | ||
negName = makeBuiltin "negation" | ||
mulName = makeBuiltin "multiplication" | ||
consName = makeBuiltin "list_construction" | ||
concName = makeBuiltin "concatenation" | ||
andName = makeBuiltin "logical_and" | ||
orName = makeBuiltin "logical_or" | ||
notName = makeBuiltin "logical_not" | ||
eqName = makeBuiltin "equality" | ||
leqName = makeBuiltin "less_than_or_equal" | ||
ifteName = makeBuiltin "if_then_else" | ||
matchesName = makeBuiltin "pattern_match" -- x matches y | ||
|
||
|
||
builtinTypes :: Map BuiltinName Type | ||
builtinTypes = Map.fromList | ||
[ (addName, binaryType intType) | ||
, (subName, binaryType intType) | ||
, (negName, unaryType intType) | ||
, (mulName, binaryType intType) | ||
, (consName, aType ^->^ aListType ^->^ aListType) | ||
, (concName, aListType ^->^ aListType ^->^ aListType) | ||
, (andName, binaryType boolType) | ||
, (orName, binaryType boolType) | ||
, (notName, unaryType boolType) | ||
, (eqName, intType ^->^ intType ^->^ boolType) | ||
, (leqName, intType ^->^ intType ^->^ boolType) | ||
, (ifteName, boolType ^->^ intType ^->^ intType ^->^ intType ^->^ intType) | ||
, (matchesName, TPattern ^->^ aType ^->^ boolType) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module TypeSystem.DeclarationChecker where | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module TypeSystem.Preprocessor where | ||
|
||
import TypeSystem.TypeDefs | ||
import Semantics.Builtins | ||
import Parser.TypeDefs | ||
|
||
preprocess :: AST -> IAST | ||
preprocess (AST types fns) = IAST types $ map convertFn fns | ||
where | ||
convertFn (FunDecl fnType fnName fnArgs fnBody) = IFn fnType fnName fnArgs $ desugar fnBody | ||
|
||
infixl 9 ^^$ | ||
(^^$) = IEApply | ||
|
||
desugar :: Expr -> IExpr | ||
desugar (EVar x) = IEVar x | ||
desugar (EInt x) = ILit $ LInt x | ||
desugar (ETypeName x) = IETypeCtor x | ||
desugar (EAdd e1 e2) = IEVar addName ^^$ desugar e1 ^^$ desugar e2 | ||
desugar (ESub e1 e2) = IEVar subName ^^$ desugar e1 ^^$ desugar e2 | ||
desugar (EMul e1 e2) = IEVar mulName ^^$ desugar e1 ^^$ desugar e2 | ||
desugar (EApply e1 e2) = IEApply (desugar e1) $ desugar e2 | ||
desugar (ELambda args body) = foldr IEAbstract (desugar body) args | ||
desugar (EListLiteral list) = foldr (IEApply . IEApply (IEVar consName) . desugar) (ILit LEmptyList) list | ||
desugar (ECons e1 e2) = IEVar consName ^^$ desugar e1 ^^$ desugar e2 | ||
desugar (EConcat e1 e2) = IEVar concName ^^$ desugar e1 ^^$ desugar e2 | ||
desugar (EAnd e1 e2) = IEVar andName ^^$ desugar e1 ^^$ desugar e2 | ||
desugar (EOr e1 e2) = IEVar orName ^^$ desugar e1 ^^$ desugar e2 | ||
desugar (EEq e1 e2) = IEVar eqName ^^$ desugar e1 ^^$ desugar e2 | ||
desugar (ELeq e1 e2) = IEVar leqName ^^$ desugar e1 ^^$ desugar e2 | ||
desugar (ENeg e) = IEVar negName ^^$ desugar e | ||
desugar (ENot e) = IEVar notName ^^$ desugar e | ||
desugar (ELet x e1 e2) = IELet x (desugar e1) $ desugar e2 | ||
desugar (EIf b e1 e2) = IEApply (IEApply (IEVar ifteName ^^$ desugar b) $ desugar e1) $ desugar e2 | ||
desugar (EMatch e pats) = | ||
let e' = desugar e | ||
in foldr (\(pat, expr) -> IEApply $ IEVar ifteName ^^$ (IEVar matchesName ^^$ e' ^^$ desugar pat) ^^$ desugar expr) (ILit LError) pats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module TypeSystem.TypeDefs where | ||
|
||
import Parser.TypeDefs | ||
|
||
-- | The I prefix stands for Internal (or intermediate :P) | ||
|
||
data IAST = IAST [ITDecl] [IFnDecl] deriving (Show, Eq) | ||
|
||
type ITDecl = TypeDecl | ||
|
||
data IFnDecl = IFn Type Name [Name] IExpr deriving (Show, Eq) | ||
|
||
data IExpr = | ||
IEAbstract Name IExpr | ||
| IEApply IExpr IExpr | ||
| IELet Name IExpr IExpr | ||
| IEVar Name | ||
| IETypeCtor TypeName | ||
| ILit Lit | ||
deriving (Show, Eq) | ||
|
||
data Lit = | ||
LInt Int | ||
| LEmptyList | ||
| LError | ||
deriving (Show, Eq) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module TypeSystem.TypeSystem where | ||
|
||
import Parser.Parser | ||
import Parser.TypeDefs | ||
import TypeSystem.TypeDefs | ||
|
||
typeCheck :: IAST -> Either ParserError IAST | ||
typeCheck = return |
Oops, something went wrong.