Compare commits
218 Commits
new-comman
...
autoparser
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb26293157 | ||
|
|
c0289f238f | ||
|
|
7afb9d47fc | ||
|
|
2431a074b0 | ||
|
|
3bfd251a68 | ||
|
|
a033c82d13 | ||
|
|
c176c1c918 | ||
|
|
aa40b985f3 | ||
|
|
64a3705e35 | ||
|
|
7e23e40a2f | ||
|
|
4c88a7ada6 | ||
|
|
367719d408 | ||
|
|
2e80045750 | ||
|
|
35c67f73c3 | ||
|
|
da9aa1e29d | ||
|
|
ca67f9b4fe | ||
|
|
60cce3fe9c | ||
|
|
4eb22f94d0 | ||
|
|
355c8170a4 | ||
|
|
e3671a579d | ||
|
|
08ca48b2ba | ||
|
|
fea9b9575b | ||
|
|
276dad56d7 | ||
|
|
695e733584 | ||
|
|
9bfd751db6 | ||
|
|
b058e47d79 | ||
|
|
5be53dc847 | ||
|
|
a0bea0d55a | ||
|
|
9747374e8a | ||
|
|
9ab1ca28f8 | ||
|
|
66cd51a355 | ||
|
|
1056be12e7 | ||
|
|
48e7c0be03 | ||
|
|
6e82d1207e | ||
|
|
f0e7c9906e | ||
|
|
57c7858c87 | ||
|
|
a105c84943 | ||
|
|
2b8d63d9cc | ||
|
|
c807c20292 | ||
|
|
a643c8a792 | ||
|
|
69200048fa | ||
|
|
55e372a670 | ||
|
|
c50626241e | ||
|
|
232bec97a7 | ||
|
|
ce1d967f08 | ||
|
|
daa0062108 | ||
|
|
3e7c7a50b4 | ||
|
|
2574a1b9c0 | ||
|
|
c285ee182e | ||
|
|
f7659a5598 | ||
|
|
1064d9993a | ||
|
|
0e3320e183 | ||
|
|
89a2be19f4 | ||
|
|
d9e96398a4 | ||
|
|
a564ffa1ce | ||
|
|
b3fff100d2 | ||
|
|
cfd6df7ba5 | ||
|
|
bb2e1ae27a | ||
|
|
4333563d03 | ||
|
|
e7cabb2a79 | ||
|
|
5da7c809b2 | ||
|
|
d229a57837 | ||
|
|
0dd8861f83 | ||
|
|
4ab900d601 | ||
|
|
501b975fb6 | ||
|
|
83315e97ac | ||
|
|
6259a0808c | ||
|
|
0c69476fd0 | ||
|
|
1caccc6ae2 | ||
|
|
23af2b1455 | ||
|
|
61795b0331 | ||
|
|
a1b874c891 | ||
|
|
e7103b925b | ||
|
|
c35401da65 | ||
|
|
d51a9a73d7 | ||
|
|
fddd43b86e | ||
|
|
12f55fa844 | ||
|
|
bb0fb716e4 | ||
|
|
687d482853 | ||
|
|
628eb28deb | ||
|
|
4c8b4c8c71 | ||
|
|
c674148772 | ||
|
|
7b4f69dce5 | ||
|
|
98caf1cac3 | ||
|
|
457799e0f7 | ||
|
|
681d767855 | ||
|
|
ef4620e90a | ||
|
|
1f2a4c706f | ||
|
|
a452bccd1c | ||
|
|
eca2218f6a | ||
|
|
83aedb0efb | ||
|
|
76841de784 | ||
|
|
c0574ff1ef | ||
|
|
faa5c6ab42 | ||
|
|
9c2d2190b0 | ||
|
|
21511f5120 | ||
|
|
8bd399f97a | ||
|
|
30a6d0929a | ||
|
|
bec8aedc22 | ||
|
|
1b642c6321 | ||
|
|
559306ffc8 | ||
|
|
540ffde4bc | ||
|
|
fa8d46e3d7 | ||
|
|
4598802999 | ||
|
|
5ea83e2da6 | ||
|
|
23c0f54042 | ||
|
|
8618de313b | ||
|
|
cd23b23a91 | ||
|
|
e6475a1262 | ||
|
|
e6f81b28f9 | ||
|
|
c20d75faf1 | ||
|
|
85aabed344 | ||
|
|
aa821e720a | ||
|
|
0e25720927 | ||
|
|
c101610cde | ||
|
|
1217f6e143 | ||
|
|
6f41167402 | ||
|
|
cf0af7e0c9 | ||
|
|
a7fd515e7b | ||
|
|
c6509338d8 | ||
|
|
192a6bf6e1 | ||
|
|
7e8c4267c2 | ||
|
|
25527bbdf0 | ||
|
|
5e7aef1040 | ||
|
|
3386fcc505 | ||
|
|
18a839bb91 | ||
|
|
92d641fca0 | ||
|
|
9b4499c5ac | ||
|
|
07e19cbfa2 | ||
|
|
2016fcab41 | ||
|
|
0e7b6f25b3 | ||
|
|
7e7aa55d6e | ||
|
|
cc79565fb3 | ||
|
|
5659bab684 | ||
|
|
405f91a770 | ||
|
|
faed1d6f25 | ||
|
|
f6d047e3b8 | ||
|
|
fcd980f148 | ||
|
|
c3919daa66 | ||
|
|
f8152f68ad | ||
|
|
9273773bf4 | ||
|
|
844cef36c7 | ||
|
|
0e17e45f3e | ||
|
|
18d8ca7bd5 | ||
|
|
2ee14bf740 | ||
|
|
502497687a | ||
|
|
107897ec97 | ||
|
|
8534fb4118 | ||
|
|
a1e38aba8e | ||
|
|
2f8ef99b08 | ||
|
|
2bb55b6cca | ||
|
|
bcd70ff538 | ||
|
|
728393671f | ||
|
|
9c3e223e51 | ||
|
|
2738119f17 | ||
|
|
630ead289c | ||
|
|
485e869c90 | ||
|
|
9e8a3d1f08 | ||
|
|
b1da524a8f | ||
|
|
787b6d51a4 | ||
|
|
1dae4443cd | ||
|
|
210a45c92e | ||
|
|
815e0401f2 | ||
|
|
753247ee83 | ||
|
|
6223fc20f3 | ||
|
|
da928db351 | ||
|
|
93d0cfe5b8 | ||
|
|
687b28d1d1 | ||
|
|
b62f618256 | ||
|
|
f25b76ea11 | ||
|
|
6b2736348d | ||
|
|
69d5f38ea1 | ||
|
|
a6f8616839 | ||
|
|
cdcb55e3b8 | ||
|
|
74ac26841f | ||
|
|
8fd29b5090 | ||
|
|
5ebc96daa7 | ||
|
|
277e039251 | ||
|
|
6e8f57e54f | ||
|
|
ae02391270 | ||
|
|
9379485713 | ||
|
|
910522537c | ||
|
|
98e1a5235a | ||
|
|
e054c4b27f | ||
|
|
e3b0f4a51e | ||
|
|
911f26e9c6 | ||
|
|
677e3ae0a9 | ||
|
|
9611770bb3 | ||
|
|
4c256cb5f7 | ||
|
|
688e1c7f5d | ||
|
|
26c9c72bcc | ||
|
|
2d614aa17a | ||
|
|
ecb2eb0f87 | ||
|
|
4c4004d3ac | ||
|
|
9b4a23c4f2 | ||
|
|
936c168cef | ||
|
|
db835f42aa | ||
|
|
cd5fc36c37 | ||
|
|
d7a33c974e | ||
|
|
b2288206d2 | ||
|
|
d962e2c27a | ||
|
|
4534c1d3d6 | ||
|
|
f79dc0b1e3 | ||
|
|
4928fc0019 | ||
|
|
d735e45688 | ||
|
|
b4208b696d | ||
|
|
ff3dbbcbc6 | ||
|
|
e3261be8a0 | ||
|
|
f131105b50 | ||
|
|
1089a33634 | ||
|
|
6c60794485 | ||
|
|
2f18529bcc | ||
|
|
c68e09d89d | ||
|
|
d9e8178a90 | ||
|
|
57536e6399 | ||
|
|
32e077c407 | ||
|
|
33d0d49d30 | ||
|
|
76a9367284 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,4 +2,3 @@ Cargo.lock
|
||||
target
|
||||
.schala_repl
|
||||
.schala_history
|
||||
rusty-tags.vi
|
||||
|
||||
13
Cargo.toml
13
Cargo.toml
@@ -5,11 +5,14 @@ authors = ["greg <greg.shuflin@protonmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
|
||||
schala-repl = { path = "schala-repl" }
|
||||
schala-lang = { path = "schala-lang/language" }
|
||||
# maaru-lang = { path = "maaru" }
|
||||
# rukka-lang = { path = "rukka" }
|
||||
# robo-lang = { path = "robo" }
|
||||
llvm-sys = "*"
|
||||
take_mut = "0.1.3"
|
||||
itertools = "0.5.8"
|
||||
lazy_static = "0.2.8"
|
||||
maplit = "*"
|
||||
colored = "1.5"
|
||||
|
||||
schala-lib = { path = "schala-lib" }
|
||||
|
||||
[build-dependencies]
|
||||
includedir_codegen = "0.2.0"
|
||||
|
||||
31
Grammar
Normal file
31
Grammar
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
|
||||
<program> := <statements> EOF
|
||||
|
||||
<statements> := <statement>
|
||||
| <statement> SEP <statements>
|
||||
|
||||
<statement> := let <id> = <expr>
|
||||
| <expr>
|
||||
| <fn_block>
|
||||
|
||||
<fn_block> := fn <id> ( <arg_list> ) <statements> end
|
||||
|
||||
<arg_list> := e
|
||||
| <id>
|
||||
| <id> , <arg_list>
|
||||
|
||||
<expr> := if <expr> then <statements> end
|
||||
| if <expr> then <statements> else <statements> end
|
||||
| while <expr> SEP <statements> end
|
||||
| ( <expr> )
|
||||
| <binop>
|
||||
|
||||
<binop> := <simple_expr>
|
||||
| <simple_expr> <id> <binop>
|
||||
|
||||
<simple_expr> := <id>
|
||||
| <number>
|
||||
| <string>
|
||||
|
||||
|
||||
920
HindleyMilner.hs
920
HindleyMilner.hs
@@ -1,920 +0,0 @@
|
||||
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||
{-# LANGUAGE LambdaCase #-}
|
||||
{-# LANGUAGE OverloadedLists #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
|
||||
|
||||
-- | This module is an extensively documented walkthrough for typechecking a
|
||||
-- basic functional language using the Hindley-Damas-Milner algorithm.
|
||||
--
|
||||
-- In the end, we'll be able to infer the type of expressions like
|
||||
--
|
||||
-- @
|
||||
-- find (λx. (>) x 0)
|
||||
-- :: [Integer] -> Either () Integer
|
||||
-- @
|
||||
--
|
||||
-- It can be used in multiple different forms:
|
||||
--
|
||||
-- * The source is written in literate programming style, so you can almost
|
||||
-- read it from top to bottom, minus some few references to later topics.
|
||||
-- * /Loads/ of doctests (runnable and verified code examples) are included
|
||||
-- * The code is runnable in GHCi, all definitions are exposed.
|
||||
-- * A small main module that gives many examples of what you might try out in
|
||||
-- GHCi is also included.
|
||||
-- * The Haddock output yields a nice overview over the definitions given, with
|
||||
-- a nice rendering of a truckload of Haddock comments.
|
||||
|
||||
module HindleyMilner where
|
||||
|
||||
import Control.Monad.Trans
|
||||
import Control.Monad.Trans.Except
|
||||
import Control.Monad.Trans.State
|
||||
import Data.Map (Map)
|
||||
import qualified Data.Map as M
|
||||
import Data.Monoid
|
||||
import Data.Set (Set)
|
||||
import qualified Data.Set as S
|
||||
import Data.String
|
||||
import Data.Text (Text)
|
||||
import qualified Data.Text as T
|
||||
|
||||
|
||||
|
||||
-- $setup
|
||||
--
|
||||
-- For running doctests:
|
||||
--
|
||||
-- >>> :set -XOverloadedStrings
|
||||
-- >>> :set -XOverloadedLists
|
||||
-- >>> :set -XLambdaCase
|
||||
-- >>> import qualified Data.Text.IO as T
|
||||
-- >>> let putPprLn = T.putStrLn . ppr
|
||||
|
||||
|
||||
|
||||
-- #############################################################################
|
||||
-- #############################################################################
|
||||
-- * Preliminaries
|
||||
-- #############################################################################
|
||||
-- #############################################################################
|
||||
|
||||
|
||||
|
||||
-- #############################################################################
|
||||
-- ** Prettyprinting
|
||||
-- #############################################################################
|
||||
|
||||
|
||||
|
||||
-- | A prettyprinter class. Similar to 'Show', but with a focus on having
|
||||
-- human-readable output as opposed to being valid Haskell.
|
||||
class Pretty a where
|
||||
ppr :: a -> Text
|
||||
|
||||
|
||||
|
||||
-- #############################################################################
|
||||
-- ** Names
|
||||
-- #############################################################################
|
||||
|
||||
|
||||
|
||||
-- | A 'name' is an identifier in the language we're going to typecheck.
|
||||
-- Variables on both the term and type level have 'Name's, for example.
|
||||
newtype Name = Name Text
|
||||
deriving (Eq, Ord, Show)
|
||||
|
||||
-- | >>> "lorem" :: Name
|
||||
-- Name "lorem"
|
||||
instance IsString Name where
|
||||
fromString = Name . T.pack
|
||||
|
||||
-- | >>> putPprLn (Name "var")
|
||||
-- var
|
||||
instance Pretty Name where
|
||||
ppr (Name n) = n
|
||||
|
||||
|
||||
|
||||
-- #############################################################################
|
||||
-- ** Monotypes
|
||||
-- #############################################################################
|
||||
|
||||
|
||||
|
||||
-- | A monotype is an unquantified/unparametric type, in other words it contains
|
||||
-- no @forall@s. Monotypes are the inner building blocks of all types. Examples
|
||||
-- of monotypes are @Int@, @a@, @a -> b@.
|
||||
--
|
||||
-- In formal notation, 'MType's are often called τ (tau) types.
|
||||
data MType = TVar Name -- ^ @a@
|
||||
| TFun MType MType -- ^ @a -> b@
|
||||
| TConst Name -- ^ @Int@, @()@, …
|
||||
|
||||
-- Since we can't declare our own types in our simple type system
|
||||
-- here, we'll hard-code certain basic ones so we can typecheck some
|
||||
-- familar functions that use them later.
|
||||
| TList MType -- ^ @[a]@
|
||||
| TEither MType MType -- ^ @Either a b@
|
||||
| TTuple MType MType -- ^ @(a,b)@
|
||||
deriving Show
|
||||
|
||||
-- | >>> putPprLn (TFun (TEither (TVar "a") (TVar "b")) (TFun (TVar "c") (TVar "d")))
|
||||
-- Either a b → c → d
|
||||
--
|
||||
-- Using the 'IsString' instance:
|
||||
--
|
||||
-- >>> putPprLn (TFun (TEither "a" "b") (TFun "c" "d"))
|
||||
-- Either a b → c → d
|
||||
instance Pretty MType where
|
||||
ppr = go False
|
||||
where
|
||||
go _ (TVar name) = ppr name
|
||||
go _ (TList a) = "[" <> ppr a <> "]"
|
||||
go _ (TEither l r) = "Either " <> ppr l <> " " <> ppr r
|
||||
go _ (TTuple a b) = "(" <> ppr a <> ", " <> ppr b <> ")"
|
||||
go _ (TConst name) = ppr name
|
||||
go parenthesize (TFun a b)
|
||||
| parenthesize = "(" <> lhs <> " → " <> rhs <> ")"
|
||||
| otherwise = lhs <> " → " <> rhs
|
||||
where lhs = go True a
|
||||
rhs = go False b
|
||||
|
||||
-- | >>> "var" :: MType
|
||||
-- TVar (Name "var")
|
||||
instance IsString MType where
|
||||
fromString = TVar . fromString
|
||||
|
||||
|
||||
|
||||
-- | The free variables of an 'MType'. This is simply the collection of all the
|
||||
-- individual type variables occurring inside of it.
|
||||
--
|
||||
-- __Example:__ The free variables of @a -> b@ are @a@ and @b@.
|
||||
freeMType :: MType -> Set Name
|
||||
freeMType = \case
|
||||
TVar a -> [a]
|
||||
TFun a b -> freeMType a <> freeMType b
|
||||
TList a -> freeMType a
|
||||
TEither l r -> freeMType l <> freeMType r
|
||||
TTuple a b -> freeMType a <> freeMType b
|
||||
TConst _ -> []
|
||||
|
||||
|
||||
|
||||
-- | Substitute all the contained type variables mentioned in the substitution,
|
||||
-- and leave everything else alone.
|
||||
instance Substitutable MType where
|
||||
applySubst s = \case
|
||||
TVar a -> let Subst s' = s
|
||||
in M.findWithDefault (TVar a) a s'
|
||||
TFun f x -> TFun (applySubst s f) (applySubst s x)
|
||||
TList a -> TList (applySubst s a)
|
||||
TEither l r -> TEither (applySubst s l) (applySubst s r)
|
||||
TTuple a b -> TTuple (applySubst s a) (applySubst s b)
|
||||
c@TConst {} -> c
|
||||
|
||||
|
||||
|
||||
-- #############################################################################
|
||||
-- ** Polytypes
|
||||
-- #############################################################################
|
||||
|
||||
-- | A polytype is a monotype universally quantified over a number of type
|
||||
-- variables. In Haskell, all definitions have polytypes, but since the @forall@
|
||||
-- is implicit they look a bit like monotypes, maybe confusingly so. For
|
||||
-- example, the type of @1 :: Int@ is actually @forall <nothing>. Int@, and the
|
||||
-- type of @id@ is @forall a. a -> a@, although GHC displays it as @a -> a@.
|
||||
--
|
||||
-- A polytype claims to work "for all imaginable type parameters", very similar
|
||||
-- to how a lambda claims to work "for all imaginable value parameters". We can
|
||||
-- insert a value into a lambda's parameter to evaluate it to a new value, and
|
||||
-- similarly we'll later insert types into a polytype's quantified variables to
|
||||
-- gain new types.
|
||||
--
|
||||
-- __Example:__ in a definition @id :: forall a. a -> a@, the @a@ after the
|
||||
-- ∀ ("forall") is the collection of type variables, and @a -> a@ is the 'MType'
|
||||
-- quantified over. When we have such an @id@, we also have its specialized
|
||||
-- version @Int -> Int@ available. This process will be the topic of the type
|
||||
-- inference/unification algorithms.
|
||||
--
|
||||
-- In formal notation, 'PType's are often called σ (sigma) types.
|
||||
--
|
||||
-- The purpose of having monotypes and polytypes is that we'd like to only have
|
||||
-- universal quantification at the top level, restricting our language to rank-1
|
||||
-- polymorphism, where type inferece is total (all types can be inferred) and
|
||||
-- simple (only a handful of typing rules). Weakening this constraint would be
|
||||
-- easy: if we allowed universal quantification within function types we would
|
||||
-- get rank-N polymorphism. Taking it even further to allow it anywhere,
|
||||
-- effectively replacing all occurrences of 'MType' with 'PType', yields
|
||||
-- impredicative types. Both these extensions make the type system
|
||||
-- *significantly* more complex though.
|
||||
data PType = Forall (Set Name) MType -- ^ ∀{α}. τ
|
||||
|
||||
-- | >>> putPprLn (Forall ["a"] (TFun "a" "a"))
|
||||
-- ∀a. a → a
|
||||
instance Pretty PType where
|
||||
ppr (Forall qs mType) = "∀" <> pprUniversals <> ". " <> ppr mType
|
||||
where
|
||||
pprUniversals
|
||||
| S.null qs = "∅"
|
||||
| otherwise = (T.intercalate " " . map ppr . S.toList) qs
|
||||
|
||||
|
||||
|
||||
-- | The free variables of a 'PType' are the free variables of the contained
|
||||
-- 'MType', except those universally quantified.
|
||||
--
|
||||
-- >>> let sigma = Forall ["a"] (TFun "a" (TFun (TTuple "b" "a") "c"))
|
||||
-- >>> putPprLn sigma
|
||||
-- ∀a. a → (b, a) → c
|
||||
-- >>> let display = T.putStrLn . T.intercalate ", " . foldMap (\x -> [ppr x])
|
||||
-- >>> display (freePType sigma)
|
||||
-- b, c
|
||||
freePType :: PType -> Set Name
|
||||
freePType (Forall qs mType) = freeMType mType `S.difference` qs
|
||||
|
||||
|
||||
|
||||
-- | Substitute all the free type variables.
|
||||
instance Substitutable PType where
|
||||
applySubst (Subst subst) (Forall qs mType) =
|
||||
let qs' = M.fromSet (const ()) qs
|
||||
subst' = Subst (subst `M.difference` qs')
|
||||
in Forall qs (applySubst subst' mType)
|
||||
|
||||
|
||||
|
||||
-- #############################################################################
|
||||
-- ** The environment
|
||||
-- #############################################################################
|
||||
|
||||
|
||||
|
||||
-- | The environment consists of all the values available in scope, and their
|
||||
-- associated polytypes. Other common names for it include "(typing) context",
|
||||
-- and because of the commonly used symbol for it sometimes directly
|
||||
-- \"Gamma"/@"Γ"@.
|
||||
--
|
||||
-- There are two kinds of membership in an environment,
|
||||
--
|
||||
-- - @∈@: an environment @Γ@ can be viewed as a set of @(value, type)@ pairs,
|
||||
-- and we can test whether something is /literally contained/ by it via
|
||||
-- x:σ ∈ Γ
|
||||
-- - @⊢@, pronounced /entails/, describes all the things that are well-typed,
|
||||
-- given an environment @Γ@. @Γ ⊢ x:τ@ can thus be seen as a judgement that
|
||||
-- @x:τ@ is /figuratively contained/ in @Γ@.
|
||||
--
|
||||
-- For example, the environment @{x:Int}@ literally contains @x@, but given
|
||||
-- this, it also entails @λy. x@, @λy z. x@, @let id = λy. y in id x@ and so on.
|
||||
--
|
||||
-- In Haskell terms, the environment consists of all the things you currently
|
||||
-- have available, or that can be built by comining them. If you import the
|
||||
-- Prelude, your environment entails
|
||||
--
|
||||
-- @
|
||||
-- id → ∀a. a→a
|
||||
-- map → ∀a b. (a→b) → [a] → [b]
|
||||
-- putStrLn → ∀∅. String → IO ()
|
||||
-- …
|
||||
-- id map → ∀a b. (a→b) → [a] → [b]
|
||||
-- map putStrLn → ∀∅. [String] -> [IO ()]
|
||||
-- …
|
||||
-- @
|
||||
newtype Env = Env (Map Name PType)
|
||||
|
||||
-- | >>> :{
|
||||
-- putPprLn (Env
|
||||
-- [ ("id", Forall ["a"] (TFun "a" "a"))
|
||||
-- , ("const", Forall ["a", "b"] (TFun "a" (TFun "b" "a"))) ])
|
||||
-- :}
|
||||
-- Γ = { const : ∀a b. a → b → a
|
||||
-- , id : ∀a. a → a }
|
||||
instance Pretty Env where
|
||||
ppr (Env env) = "Γ = { " <> T.intercalate "\n , " pprBindings <> " }"
|
||||
where
|
||||
bindings = M.assocs env
|
||||
pprBinding (name, pType) = ppr name <> " : " <> ppr pType
|
||||
pprBindings = map pprBinding bindings
|
||||
|
||||
|
||||
|
||||
-- | The free variables of an 'Env'ironment are all the free variables of the
|
||||
-- 'PType's it contains.
|
||||
freeEnv :: Env -> Set Name
|
||||
freeEnv (Env env) = let allPTypes = M.elems env
|
||||
in S.unions (map freePType allPTypes)
|
||||
|
||||
|
||||
|
||||
-- | Performing a 'Subst'itution in an 'Env'ironment means performing that
|
||||
-- substituion on all the contained 'PType's.
|
||||
instance Substitutable Env where
|
||||
applySubst s (Env env) = Env (M.map (applySubst s) env)
|
||||
|
||||
|
||||
|
||||
-- #############################################################################
|
||||
-- ** Substitutions
|
||||
-- #############################################################################
|
||||
|
||||
|
||||
|
||||
-- | A substitution is a mapping from type variables to 'MType's. Applying a
|
||||
-- substitution means applying those replacements. For example, the substitution
|
||||
-- @a -> Int@ applied to @a -> a@ yields the result @Int -> Int@.
|
||||
--
|
||||
-- A key concept behind Hindley-Milner is that once we dive deeper into an
|
||||
-- expression, we learn more about our type variables. We might learn that @a@
|
||||
-- has to be specialized to @b -> b@, and then later on that @b@ is actually
|
||||
-- @Int@. Substitutions are an organized way of carrying this information along.
|
||||
newtype Subst = Subst (Map Name MType)
|
||||
|
||||
|
||||
|
||||
-- | We're going to apply substitutions to a variety of other values that
|
||||
-- somehow contain type variables, so we overload this application operation in
|
||||
-- a class here.
|
||||
--
|
||||
-- Laws:
|
||||
--
|
||||
-- @
|
||||
-- 'applySubst' 'mempty' ≡ 'id'
|
||||
-- 'applySubst' (s1 '<>' s2) ≡ 'applySubst' s1 . 'applySubst' s2
|
||||
-- @
|
||||
class Substitutable a where
|
||||
applySubst :: Subst -> a -> a
|
||||
|
||||
instance (Substitutable a, Substitutable b) => Substitutable (a,b) where
|
||||
applySubst s (x,y) = (applySubst s x, applySubst s y)
|
||||
|
||||
-- | @'applySubst' s1 s2@ applies one substitution to another, replacing all the
|
||||
-- bindings in the second argument @s2@ with their values mentioned in the first
|
||||
-- one (@s1@).
|
||||
instance Substitutable Subst where
|
||||
applySubst s (Subst target) = Subst (fmap (applySubst s) target)
|
||||
|
||||
-- | >>> :{
|
||||
-- putPprLn (Subst
|
||||
-- [ ("a", TFun "b" "b")
|
||||
-- , ("b", TEither "c" "d") ])
|
||||
-- :}
|
||||
-- { a ––> b → b
|
||||
-- , b ––> Either c d }
|
||||
instance Pretty Subst where
|
||||
ppr (Subst s) = "{ " <> T.intercalate "\n, " [ ppr k <> " ––> " <> ppr v | (k,v) <- M.toList s ] <> " }"
|
||||
|
||||
-- | Combine two substitutions by applying all substitutions mentioned in the
|
||||
-- first argument to the type variables contained in the second.
|
||||
instance Monoid Subst where
|
||||
-- Considering that all we can really do with a substitution is apply it, we
|
||||
-- can use the one of 'Substitutable's laws to show that substitutions
|
||||
-- combine associatively,
|
||||
--
|
||||
-- @
|
||||
-- applySubst (compose s1 (compose s2 s3))
|
||||
-- = applySubst s1 . applySubst (compose s2 s3)
|
||||
-- = applySubst s1 . applySubst s2 . applySubst s3
|
||||
-- = applySubst (compose s1 s2) . applySubst s3
|
||||
-- = applySubst (compose (compose s1 s2) s3)
|
||||
-- @
|
||||
mappend subst1 subst2 = Subst (s1 `M.union` s2)
|
||||
where
|
||||
Subst s1 = subst1
|
||||
Subst s2 = applySubst subst1 subst2
|
||||
|
||||
mempty = Subst M.empty
|
||||
|
||||
|
||||
|
||||
-- #############################################################################
|
||||
-- #############################################################################
|
||||
-- * Typechecking
|
||||
-- #############################################################################
|
||||
-- #############################################################################
|
||||
|
||||
-- $ Typechecking does two things:
|
||||
--
|
||||
-- 1. If two types are not immediately identical, attempt to 'unify' them
|
||||
-- to get a type compatible with both of them
|
||||
-- 2. 'infer' the most general type of a value by comparing the values in its
|
||||
-- definition with the 'Env'ironment
|
||||
|
||||
|
||||
|
||||
-- #############################################################################
|
||||
-- ** Inference context
|
||||
-- #############################################################################
|
||||
|
||||
|
||||
|
||||
-- | The inference type holds a supply of unique names, and can fail with a
|
||||
-- descriptive error if something goes wrong.
|
||||
--
|
||||
-- /Invariant:/ the supply must be infinite, or we might run out of names to
|
||||
-- give to things.
|
||||
newtype Infer a = Infer (ExceptT InferError (State [Name]) a)
|
||||
deriving (Functor, Applicative, Monad)
|
||||
|
||||
-- | Errors that can happen during the type inference process.
|
||||
data InferError =
|
||||
-- | Two types that don't match were attempted to be unified.
|
||||
--
|
||||
-- For example, @a -> a@ and @Int@ do not unify.
|
||||
--
|
||||
-- >>> putPprLn (CannotUnify (TFun "a" "a") (TConst "Int"))
|
||||
-- Cannot unify a → a with Int
|
||||
CannotUnify MType MType
|
||||
|
||||
-- | A 'TVar' is bound to an 'MType' that already contains it.
|
||||
--
|
||||
-- The canonical example of this is @λx. x x@, where the first @x@
|
||||
-- in the body has to have type @a -> b@, and the second one @a@. Since
|
||||
-- they're both the same @x@, this requires unification of @a@ with
|
||||
-- @a -> b@, which only works if @a = a -> b = (a -> b) -> b = …@, yielding
|
||||
-- an infinite type.
|
||||
--
|
||||
-- >>> putPprLn (OccursCheckFailed "a" (TFun "a" "a"))
|
||||
-- Occurs check failed: a already appears in a → a
|
||||
| OccursCheckFailed Name MType
|
||||
|
||||
-- | The value of an unknown identifier was read.
|
||||
--
|
||||
-- >>> putPprLn (UnknownIdentifier "a")
|
||||
-- Unknown identifier: a
|
||||
| UnknownIdentifier Name
|
||||
deriving Show
|
||||
|
||||
-- | >>> putPprLn (CannotUnify (TEither "a" "b") (TTuple "a" "b"))
|
||||
-- Cannot unify Either a b with (a, b)
|
||||
instance Pretty InferError where
|
||||
ppr = \case
|
||||
CannotUnify t1 t2 ->
|
||||
"Cannot unify " <> ppr t1 <> " with " <> ppr t2
|
||||
OccursCheckFailed name ty ->
|
||||
"Occurs check failed: " <> ppr name <> " already appears in " <> ppr ty
|
||||
UnknownIdentifier name ->
|
||||
"Unknown identifier: " <> ppr name
|
||||
|
||||
|
||||
|
||||
-- | Evaluate a value in an 'Infer'ence context.
|
||||
--
|
||||
-- >>> let expr = EAbs "f" (EAbs "g" (EAbs "x" (EApp (EApp "f" "x") (EApp "g" "x"))))
|
||||
-- >>> putPprLn expr
|
||||
-- λf g x. f x (g x)
|
||||
-- >>> let inferred = runInfer (infer (Env []) expr)
|
||||
-- >>> let demonstrate = \case Right (_, ty) -> T.putStrLn (":: " <> ppr ty)
|
||||
-- >>> demonstrate inferred
|
||||
-- :: (c → e → f) → (c → e) → c → f
|
||||
runInfer :: Infer a -- ^ Inference data
|
||||
-> Either InferError a
|
||||
runInfer (Infer inf) =
|
||||
evalState (runExceptT inf) (map Name (infiniteSupply alphabet))
|
||||
where
|
||||
|
||||
alphabet = map T.singleton ['a'..'z']
|
||||
|
||||
-- [a, b, c] ==> [a,b,c, a1,b1,c1, a2,b2,c2, …]
|
||||
infiniteSupply supply = supply <> addSuffixes supply (1 :: Integer)
|
||||
where
|
||||
addSuffixes xs n = map (\x -> addSuffix x n) xs <> addSuffixes xs (n+1)
|
||||
addSuffix x n = x <> T.pack (show n)
|
||||
|
||||
|
||||
|
||||
-- | Throw an 'InferError' in an 'Infer'ence context.
|
||||
--
|
||||
-- >>> case runInfer (throw (UnknownIdentifier "var")) of Left err -> putPprLn err
|
||||
-- Unknown identifier: var
|
||||
throw :: InferError -> Infer a
|
||||
throw = Infer . throwE
|
||||
|
||||
|
||||
|
||||
-- #############################################################################
|
||||
-- ** Unification
|
||||
-- #############################################################################
|
||||
|
||||
-- $ Unification describes the process of making two different types compatible
|
||||
-- by specializing them where needed. A desirable property to have here is being
|
||||
-- able to find the most general unifier. Luckily, we'll be able to do that in
|
||||
-- our type system.
|
||||
|
||||
|
||||
|
||||
-- | The unification of two 'MType's is the most general substituion that can be
|
||||
-- applied to both of them in order to yield the same result.
|
||||
--
|
||||
-- >>> let m1 = TFun "a" "b"
|
||||
-- >>> putPprLn m1
|
||||
-- a → b
|
||||
-- >>> let m2 = TFun "c" (TEither "d" "e")
|
||||
-- >>> putPprLn m2
|
||||
-- c → Either d e
|
||||
-- >>> let inferSubst = unify (m1, m2)
|
||||
-- >>> case runInfer inferSubst of Right subst -> putPprLn subst
|
||||
-- { a ––> c
|
||||
-- , b ––> Either d e }
|
||||
unify :: (MType, MType) -> Infer Subst
|
||||
unify = \case
|
||||
(TFun a b, TFun x y) -> unifyBinary (a,b) (x,y)
|
||||
(TVar v, x) -> v `bindVariableTo` x
|
||||
(x, TVar v) -> v `bindVariableTo` x
|
||||
(TConst a, TConst b) | a == b -> pure mempty
|
||||
(TList a, TList b) -> unify (a,b)
|
||||
(TEither a b, TEither x y) -> unifyBinary (a,b) (x,y)
|
||||
(TTuple a b, TTuple x y) -> unifyBinary (a,b) (x,y)
|
||||
(a, b) -> throw (CannotUnify a b)
|
||||
|
||||
where
|
||||
|
||||
-- Unification of binary type constructors, such as functions and Either.
|
||||
-- Unification is first done for the first operand, and assuming the
|
||||
-- required substitution, for the second one.
|
||||
unifyBinary :: (MType, MType) -> (MType, MType) -> Infer Subst
|
||||
unifyBinary (a,b) (x,y) = do
|
||||
s1 <- unify (a, x)
|
||||
s2 <- unify (applySubst s1 (b, y))
|
||||
pure (s1 <> s2)
|
||||
|
||||
|
||||
|
||||
-- | Build a 'Subst'itution that binds a 'Name' of a 'TVar' to an 'MType'. The
|
||||
-- resulting substitution should be idempotent, i.e. applying it more than once
|
||||
-- to something should not be any different from applying it only once.
|
||||
--
|
||||
-- - In the simplest case, this just means building a substitution that just
|
||||
-- does that.
|
||||
-- - Substituting a 'Name' with a 'TVar' with the same name unifies a type
|
||||
-- variable with itself, and the resulting substitution does nothing new.
|
||||
-- - If the 'Name' we're trying to bind to an 'MType' already occurs in that
|
||||
-- 'MType', the resulting substitution would not be idempotent: the 'MType'
|
||||
-- would be replaced again, yielding a different result. This is known as the
|
||||
-- Occurs Check.
|
||||
bindVariableTo :: Name -> MType -> Infer Subst
|
||||
|
||||
bindVariableTo name (TVar v) | boundToSelf = pure mempty
|
||||
where
|
||||
boundToSelf = name == v
|
||||
|
||||
bindVariableTo name mType | name `occursIn` mType = throw (OccursCheckFailed name mType)
|
||||
where
|
||||
n `occursIn` ty = n `S.member` freeMType ty
|
||||
|
||||
bindVariableTo name mType = pure (Subst (M.singleton name mType))
|
||||
|
||||
|
||||
|
||||
-- #############################################################################
|
||||
-- ** Type inference
|
||||
-- #############################################################################
|
||||
|
||||
-- $ Type inference is the act of finding out a value's type by looking at the
|
||||
-- environment it is in, in order to make it compatible with it.
|
||||
--
|
||||
-- In literature, the Hindley-Damas-Milner inference algorithm ("Algorithm W")
|
||||
-- is often presented in the style of logical formulas, and below you'll find
|
||||
-- that version along with code that actually does what they say.
|
||||
--
|
||||
-- These formulas look a bit like fractions, where the "numerator" is a
|
||||
-- collection of premises, and the denominator is the consequence if all of them
|
||||
-- hold.
|
||||
--
|
||||
-- __Example:__
|
||||
--
|
||||
-- @
|
||||
-- Γ ⊢ even : Int → Bool Γ ⊢ 1 : Int
|
||||
-- –––––––––––––––––––––––––––––––––––
|
||||
-- Γ ⊢ even 1 : Bool
|
||||
-- @
|
||||
--
|
||||
-- means that if we have a value of type @Int -> Bool@ called "even" and a value
|
||||
-- of type @Int@ called @1@, then we also have a value of type @Bool@ via
|
||||
-- @even 1@ available to us.
|
||||
--
|
||||
-- The actual inference rules are polymorphic versions of this example, and
|
||||
-- the code comments will explain each step in detail.
|
||||
|
||||
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- *** The language: typed lambda calculus
|
||||
-- -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
-- | The syntax tree of the language we'd like to typecheck. You can view it as
|
||||
-- a close relative to simply typed lambda calculus, having only the most
|
||||
-- necessary syntax elements.
|
||||
--
|
||||
-- Since 'ELet' is non-recursive, the usual fixed-point function
|
||||
-- @fix : (a → a) → a@ can be introduced to allow recursive definitions.
|
||||
data Exp = ELit Lit -- ^ True, 1
|
||||
| EVar Name -- ^ @x@
|
||||
| EApp Exp Exp -- ^ @f x@
|
||||
| EAbs Name Exp -- ^ @λx. e@
|
||||
| ELet Name Exp Exp -- ^ @let x = e in e'@ (non-recursive)
|
||||
deriving Show
|
||||
|
||||
|
||||
|
||||
-- | Literals we'd like to support. Since we can't define new data types in our
|
||||
-- simple type system, we'll have to hard-code the possible ones here.
|
||||
data Lit = LBool Bool
|
||||
| LInteger Integer
|
||||
deriving Show
|
||||
|
||||
|
||||
|
||||
-- | >>> putPprLn (EAbs "f" (EAbs "g" (EAbs "x" (EApp (EApp "f" "x") (EApp "g" "x")))))
|
||||
-- λf g x. f x (g x)
|
||||
instance Pretty Exp where
|
||||
ppr (ELit lit) = ppr lit
|
||||
|
||||
ppr (EVar name) = ppr name
|
||||
|
||||
ppr (EApp f x) = pprApp1 f <> " " <> pprApp2 x
|
||||
where
|
||||
pprApp1 = \case
|
||||
eLet@ELet{} -> "(" <> ppr eLet <> ")"
|
||||
eLet@EAbs{} -> "(" <> ppr eLet <> ")"
|
||||
e -> ppr e
|
||||
pprApp2 = \case
|
||||
eApp@EApp{} -> "(" <> ppr eApp <> ")"
|
||||
e -> pprApp1 e
|
||||
|
||||
ppr x@EAbs{} = pprAbs True x
|
||||
where
|
||||
pprAbs True (EAbs name expr) = "λ" <> ppr name <> pprAbs False expr
|
||||
pprAbs False (EAbs name expr) = " " <> ppr name <> pprAbs False expr
|
||||
pprAbs _ expr = ". " <> ppr expr
|
||||
|
||||
ppr (ELet name value body) =
|
||||
"let " <> ppr name <> " = " <> ppr value <> " in " <> ppr body
|
||||
|
||||
-- | >>> putPprLn (LBool True)
|
||||
-- True
|
||||
--
|
||||
-- >>> putPprLn (LInteger 127)
|
||||
-- 127
|
||||
instance Pretty Lit where
|
||||
ppr = \case
|
||||
LBool b -> showT b
|
||||
LInteger i -> showT i
|
||||
where
|
||||
showT :: Show a => a -> Text
|
||||
showT = T.pack . show
|
||||
|
||||
-- | >>> "var" :: Exp
|
||||
-- EVar (Name "var")
|
||||
instance IsString Exp where
|
||||
fromString = EVar . fromString
|
||||
|
||||
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- *** Some useful definitions
|
||||
-- -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
-- | Generate a fresh 'Name' in a type 'Infer'ence context. An example use case
|
||||
-- of this is η expansion, which transforms @f@ into @λx. f x@, where "x" is a
|
||||
-- new name, i.e. unbound in the current context.
|
||||
fresh :: Infer MType
|
||||
fresh = drawFromSupply >>= \case
|
||||
Right name -> pure (TVar name)
|
||||
Left err -> throw err
|
||||
|
||||
where
|
||||
|
||||
drawFromSupply :: Infer (Either InferError Name)
|
||||
drawFromSupply = Infer (do
|
||||
s:upply <- lift get
|
||||
lift (put upply)
|
||||
pure (Right s) )
|
||||
|
||||
|
||||
|
||||
-- | Add a new binding to the environment.
|
||||
--
|
||||
-- The Haskell equivalent would be defining a new value, for example in module
|
||||
-- scope or in a @let@ block. This corresponds to the "comma" operation used in
|
||||
-- formal notation,
|
||||
--
|
||||
-- @
|
||||
-- Γ, x:σ ≡ extendEnv Γ (x,σ)
|
||||
-- @
|
||||
extendEnv :: Env -> (Name, PType) -> Env
|
||||
extendEnv (Env env) (name, pType) = Env (M.insert name pType env)
|
||||
|
||||
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- *** Inferring the types of all language constructs
|
||||
-- -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
-- | Infer the type of an 'Exp'ression in an 'Env'ironment, resulting in the
|
||||
-- 'Exp's 'MType' along with a substitution that has to be done in order to reach
|
||||
-- this goal.
|
||||
--
|
||||
-- This is widely known as /Algorithm W/.
|
||||
infer :: Env -> Exp -> Infer (Subst, MType)
|
||||
infer env = \case
|
||||
ELit lit -> inferLit lit
|
||||
EVar name -> inferVar env name
|
||||
EApp f x -> inferApp env f x
|
||||
EAbs x e -> inferAbs env x e
|
||||
ELet x e e' -> inferLet env x e e'
|
||||
|
||||
|
||||
|
||||
-- | Literals such as 'True' and '1' have their types hard-coded.
|
||||
inferLit :: Lit -> Infer (Subst, MType)
|
||||
inferLit lit = pure (mempty, TConst litTy)
|
||||
where
|
||||
litTy = case lit of
|
||||
LBool {} -> "Bool"
|
||||
LInteger {} -> "Integer"
|
||||
|
||||
|
||||
|
||||
-- | Inferring the type of a variable is done via
|
||||
--
|
||||
-- @
|
||||
-- x:σ ∈ Γ τ = instantiate(σ)
|
||||
-- –––––––––––––––––––––––––––– [Var]
|
||||
-- Γ ⊢ x:τ
|
||||
-- @
|
||||
--
|
||||
-- This means that if @Γ@ /literally contains/ (@∈@) a value, then it also
|
||||
-- /entails it/ (@⊢@) in all its instantiations.
|
||||
inferVar :: Env -> Name -> Infer (Subst, MType)
|
||||
inferVar env name = do
|
||||
sigma <- lookupEnv env name -- x:σ ∈ Γ
|
||||
tau <- instantiate sigma -- τ = instantiate(σ)
|
||||
-- ------------------
|
||||
pure (mempty, tau) -- Γ ⊢ x:τ
|
||||
|
||||
|
||||
|
||||
-- | Look up the 'PType' of a 'Name' in the 'Env'ironment.
|
||||
--
|
||||
-- This checks whether @x:σ@ is /literally contained/ in @Γ@. For more details
|
||||
-- about this, see the documentation of 'Env'.
|
||||
--
|
||||
-- To give a Haskell analogon, looking up @id@ when @Prelude@ is loaded, the
|
||||
-- resulting 'PType' would be @id@'s type, namely @forall a. a -> a@.
|
||||
lookupEnv :: Env -> Name -> Infer PType
|
||||
lookupEnv (Env env) name = case M.lookup name env of
|
||||
Just x -> pure x
|
||||
Nothing -> throw (UnknownIdentifier name)
|
||||
|
||||
|
||||
|
||||
-- | Bind all quantified variables of a 'PType' to 'fresh' type variables.
|
||||
--
|
||||
-- __Example:__ instantiating @forall a. a -> b -> a@ results in the 'MType'
|
||||
-- @c -> b -> c@, where @c@ is a fresh name (to avoid shadowing issues).
|
||||
--
|
||||
-- You can picture the 'PType' to be the prototype converted to an instantiated
|
||||
-- 'MType', which can now be used in the unification process.
|
||||
--
|
||||
-- Another way of looking at it is by simply forgetting which variables were
|
||||
-- quantified, carefully avoiding name clashes when doing so.
|
||||
--
|
||||
-- 'instantiate' can also be seen as the opposite of 'generalize', which we'll
|
||||
-- need later to convert an 'MType' to a 'PType'.
|
||||
instantiate :: PType -> Infer MType
|
||||
instantiate (Forall qs t) = do
|
||||
subst <- substituteAllWithFresh qs
|
||||
pure (applySubst subst t)
|
||||
|
||||
where
|
||||
-- For each given name, add a substitution from that name to a fresh type
|
||||
-- variable to the result.
|
||||
substituteAllWithFresh :: Set Name -> Infer Subst
|
||||
substituteAllWithFresh xs = do
|
||||
let freshSubstActions = M.fromSet (const fresh) xs
|
||||
freshSubsts <- sequenceA freshSubstActions
|
||||
pure (Subst freshSubsts)
|
||||
|
||||
|
||||
|
||||
-- | Function application captures the fact that if we have a function and an
|
||||
-- argument we can give to that function, we also have the result value of the
|
||||
-- result type available to us.
|
||||
--
|
||||
-- @
|
||||
-- Γ ⊢ f : fτ Γ ⊢ x : xτ fxτ = fresh unify(fτ, xτ → fxτ)
|
||||
-- ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– [App]
|
||||
-- Γ ⊢ f x : fxτ
|
||||
-- @
|
||||
--
|
||||
-- This rule says that given a function and a value with a type, the function
|
||||
-- type has to unify with a function type that allows the value type to be its
|
||||
-- argument.
|
||||
inferApp
|
||||
:: Env
|
||||
-> Exp -- ^ __f__ x
|
||||
-> Exp -- ^ f __x__
|
||||
-> Infer (Subst, MType)
|
||||
inferApp env f x = do
|
||||
(s1, fTau) <- infer env f -- f : fτ
|
||||
(s2, xTau) <- infer (applySubst s1 env) x -- x : xτ
|
||||
fxTau <- fresh -- fxτ = fresh
|
||||
s3 <- unify (applySubst s2 fTau, TFun xTau fxTau) -- unify (fτ, xτ → fxτ)
|
||||
let s = s3 <> s2 <> s1 -- --------------------
|
||||
pure (s, applySubst s3 fxTau) -- f x : fxτ
|
||||
|
||||
|
||||
|
||||
-- | Lambda abstraction is based on the fact that when we introduce a new
|
||||
-- variable, the resulting lambda maps from that variable's type to the type of
|
||||
-- the body.
|
||||
--
|
||||
-- @
|
||||
-- τ = fresh σ = ∀∅. τ Γ, x:σ ⊢ e:τ'
|
||||
-- ––––––––––––––––––––––––––––––––––––– [Abs]
|
||||
-- Γ ⊢ λx.e : τ→τ'
|
||||
-- @
|
||||
--
|
||||
-- Here, @Γ, x:τ@ is @Γ@ extended by one additional mapping, namely @x:τ@.
|
||||
--
|
||||
-- Abstraction is typed by extending the environment by a new 'MType', and if
|
||||
-- under this assumption we can construct a function mapping to a value of that
|
||||
-- type, we can say that the lambda takes a value and maps to it.
|
||||
inferAbs
|
||||
:: Env
|
||||
-> Name -- ^ λ__x__. e
|
||||
-> Exp -- ^ λx. __e__
|
||||
-> Infer (Subst, MType)
|
||||
inferAbs env x e = do
|
||||
tau <- fresh -- τ = fresh
|
||||
let sigma = Forall [] tau -- σ = ∀∅. τ
|
||||
env' = extendEnv env (x, sigma) -- Γ, x:σ …
|
||||
(s, tau') <- infer env' e -- … ⊢ e:τ'
|
||||
-- ---------------
|
||||
pure (s, TFun (applySubst s tau) tau') -- λx.e : τ→τ'
|
||||
|
||||
|
||||
|
||||
-- | A let binding allows extending the environment with new bindings in a
|
||||
-- principled manner. To do this, we first have to typecheck the expression to
|
||||
-- be introduced. The result of this is then generalized to a 'PType', since let
|
||||
-- bindings introduce new polymorphic values, which are then added to the
|
||||
-- environment. Now we can finally typecheck the body of the "in" part of the
|
||||
-- let binding.
|
||||
--
|
||||
-- Note that in our simple language, let is non-recursive, but recursion can be
|
||||
-- introduced as usual by adding a primitive @fix : (a → a) → a@ if desired.
|
||||
--
|
||||
-- @
|
||||
-- Γ ⊢ e:τ σ = gen(Γ,τ) Γ, x:σ ⊢ e':τ'
|
||||
-- ––––––––––––––––––––––––––––––––––––––– [Let]
|
||||
-- Γ ⊢ let x = e in e' : τ'
|
||||
-- @
|
||||
inferLet
|
||||
:: Env
|
||||
-> Name -- ^ let __x__ = e in e'
|
||||
-> Exp -- ^ let x = __e__ in e'
|
||||
-> Exp -- ^ let x = e in __e'__
|
||||
-> Infer (Subst, MType)
|
||||
inferLet env x e e' = do
|
||||
(s1, tau) <- infer env e -- Γ ⊢ e:τ
|
||||
let env' = applySubst s1 env
|
||||
let sigma = generalize env' tau -- σ = gen(Γ,τ)
|
||||
let env'' = extendEnv env' (x, sigma) -- Γ, x:σ
|
||||
(s2, tau') <- infer env'' e' -- Γ ⊢ …
|
||||
-- --------------------------
|
||||
pure (s2 <> s1, tau') -- … let x = e in e' : τ'
|
||||
|
||||
|
||||
|
||||
-- | Generalize an 'MType' to a 'PType' by universally quantifying over all the
|
||||
-- type variables contained in it, except those already free in the environment.
|
||||
--
|
||||
-- >>> let tau = TFun "a" (TFun "b" "a")
|
||||
-- >>> putPprLn tau
|
||||
-- a → b → a
|
||||
-- >>> putPprLn (generalize (Env [("x", Forall [] "b")]) tau)
|
||||
-- ∀a. a → b → a
|
||||
--
|
||||
-- In more formal notation,
|
||||
--
|
||||
-- @
|
||||
-- gen(Γ,τ) = ∀{α}. τ
|
||||
-- where {α} = free(τ) – free(Γ)
|
||||
-- @
|
||||
--
|
||||
-- 'generalize' can also be seen as the opposite of 'instantiate', which
|
||||
-- converts a 'PType' to an 'MType'.
|
||||
generalize :: Env -> MType -> PType
|
||||
generalize env mType = Forall qs mType
|
||||
where
|
||||
qs = freeMType mType `S.difference` freeEnv env
|
||||
185
Main.hs
185
Main.hs
@@ -1,185 +0,0 @@
|
||||
{-# LANGUAGE OverloadedLists #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
module Main where
|
||||
|
||||
|
||||
|
||||
import qualified Data.Map as M
|
||||
import Data.Monoid
|
||||
import Data.Text (Text)
|
||||
import qualified Data.Text.IO as T
|
||||
|
||||
import HindleyMilner
|
||||
|
||||
|
||||
|
||||
-- #############################################################################
|
||||
-- #############################################################################
|
||||
-- * Testing
|
||||
-- #############################################################################
|
||||
-- #############################################################################
|
||||
|
||||
|
||||
|
||||
-- #############################################################################
|
||||
-- ** A small custom Prelude
|
||||
-- #############################################################################
|
||||
|
||||
|
||||
|
||||
prelude :: Env
|
||||
prelude = Env (M.fromList
|
||||
[ ("(*)", Forall [] (tInteger ~> tInteger ~> tInteger))
|
||||
, ("(+)", Forall [] (tInteger ~> tInteger ~> tInteger))
|
||||
, ("(,)", Forall ["a","b"] ("a" ~> "b" ~> TTuple "a" "b"))
|
||||
, ("(-)", Forall [] (tInteger ~> tInteger ~> tInteger))
|
||||
, ("(.)", Forall ["a", "b", "c"] (("b" ~> "c") ~> ("a" ~> "b") ~> "a" ~> "c"))
|
||||
, ("(<)", Forall [] (tInteger ~> tInteger ~> tBool))
|
||||
, ("(<=)", Forall [] (tInteger ~> tInteger ~> tBool))
|
||||
, ("(>)", Forall [] (tInteger ~> tInteger ~> tBool))
|
||||
, ("(>=)", Forall [] (tInteger ~> tInteger ~> tBool))
|
||||
, ("const", Forall ["a","b"] ("a" ~> "b" ~> "a"))
|
||||
, ("Cont/>>=", Forall ["a"] ((("a" ~> "r") ~> "r") ~> ("a" ~> (("b" ~> "r") ~> "r")) ~> (("b" ~> "r") ~> "r")))
|
||||
, ("find", Forall ["a","b"] (("a" ~> tBool) ~> TList "a" ~> tMaybe "a"))
|
||||
, ("fix", Forall ["a"] (("a" ~> "a") ~> "a"))
|
||||
, ("foldr", Forall ["a","b"] (("a" ~> "b" ~> "b") ~> "b" ~> TList "a" ~> "b"))
|
||||
, ("id", Forall ["a"] ("a" ~> "a"))
|
||||
, ("ifThenElse", Forall ["a"] (tBool ~> "a" ~> "a" ~> "a"))
|
||||
, ("Left", Forall ["a","b"] ("a" ~> TEither "a" "b"))
|
||||
, ("length", Forall ["a"] (TList "a" ~> tInteger))
|
||||
, ("map", Forall ["a","b"] (("a" ~> "b") ~> TList "a" ~> TList "b"))
|
||||
, ("reverse", Forall ["a"] (TList "a" ~> TList "a"))
|
||||
, ("Right", Forall ["a","b"] ("b" ~> TEither "a" "b"))
|
||||
, ("[]", Forall ["a"] (TList "a"))
|
||||
, ("(:)", Forall ["a"] ("a" ~> TList "a" ~> TList "a"))
|
||||
])
|
||||
where
|
||||
tBool = TConst "Bool"
|
||||
tInteger = TConst "Integer"
|
||||
tMaybe = TEither (TConst "()")
|
||||
|
||||
|
||||
|
||||
-- | Synonym for 'TFun' to make writing type signatures easier.
|
||||
--
|
||||
-- Instead of
|
||||
--
|
||||
-- @
|
||||
-- Forall ["a","b"] (TFun "a" (TFun "b" "a"))
|
||||
-- @
|
||||
--
|
||||
-- we can write
|
||||
--
|
||||
-- @
|
||||
-- Forall ["a","b"] ("a" ~> "b" ~> "a")
|
||||
-- @
|
||||
(~>) :: MType -> MType -> MType
|
||||
(~>) = TFun
|
||||
infixr 9 ~>
|
||||
|
||||
|
||||
|
||||
-- #############################################################################
|
||||
-- ** Run it!
|
||||
-- #############################################################################
|
||||
|
||||
|
||||
|
||||
-- | Run type inference on a cuple of values
|
||||
main :: IO ()
|
||||
main = do
|
||||
let inferAndPrint = T.putStrLn . (" " <>) . showType prelude
|
||||
T.putStrLn "Well-typed:"
|
||||
do
|
||||
inferAndPrint (lambda ["x"] "x")
|
||||
inferAndPrint (lambda ["f","g","x"] (apply "f" ["x", apply "g" ["x"]]))
|
||||
inferAndPrint (lambda ["f","g","x"] (apply "f" [apply "g" ["x"]]))
|
||||
inferAndPrint (lambda ["m", "k", "c"] (apply "m" [lambda ["x"] (apply "k" ["x", "c"])])) -- >>= for Cont
|
||||
inferAndPrint (lambda ["f"] (apply "(.)" ["reverse", apply "map" ["f"]]))
|
||||
inferAndPrint (apply "find" [lambda ["x"] (apply "(>)" ["x", int 0])])
|
||||
inferAndPrint (apply "map" [apply "map" ["map"]])
|
||||
inferAndPrint (apply "(*)" [int 1, int 2])
|
||||
inferAndPrint (apply "foldr" ["(+)", int 0])
|
||||
inferAndPrint (apply "map" ["length"])
|
||||
inferAndPrint (apply "map" ["map"])
|
||||
inferAndPrint (lambda ["x"] (apply "ifThenElse" [apply "(<)" ["x", int 0], int 0, "x"]))
|
||||
inferAndPrint (lambda ["x"] (apply "fix" [lambda ["xs"] (apply "(:)" ["x", "xs"])]))
|
||||
T.putStrLn "Ill-typed:"
|
||||
do
|
||||
inferAndPrint (apply "(*)" [int 1, bool True])
|
||||
inferAndPrint (apply "foldr" [int 1])
|
||||
inferAndPrint (lambda ["x"] (apply "x" ["x"]))
|
||||
inferAndPrint (lambda ["x"] (ELet "xs" (apply "(:)" ["x", "xs"]) "xs"))
|
||||
|
||||
|
||||
|
||||
-- | Build multiple lambda bindings.
|
||||
--
|
||||
-- Instead of
|
||||
--
|
||||
-- @
|
||||
-- EAbs "f" (EAbs "x" (EApp "f" "x"))
|
||||
-- @
|
||||
--
|
||||
-- we can write
|
||||
--
|
||||
-- @
|
||||
-- lambda ["f", "x"] (EApp "f" "x")
|
||||
-- @
|
||||
--
|
||||
-- for
|
||||
--
|
||||
-- @
|
||||
-- λf x. f x
|
||||
-- @
|
||||
lambda :: [Name] -> Exp -> Exp
|
||||
lambda names expr = foldr EAbs expr names
|
||||
|
||||
|
||||
|
||||
-- | Apply a function to multiple arguments.
|
||||
--
|
||||
-- Instead of
|
||||
--
|
||||
-- @
|
||||
-- EApp (EApp (EApp "f" "x") "y") "z")
|
||||
-- @
|
||||
--
|
||||
-- we can write
|
||||
--
|
||||
-- @
|
||||
-- apply "f" ["x", "y", "z"]
|
||||
-- @
|
||||
--
|
||||
-- for
|
||||
--
|
||||
-- @
|
||||
-- f x y z
|
||||
-- @
|
||||
apply :: Exp -> [Exp] -> Exp
|
||||
apply = foldl EApp
|
||||
|
||||
|
||||
|
||||
-- | Construct an integer literal.
|
||||
int :: Integer -> Exp
|
||||
int = ELit . LInteger
|
||||
|
||||
|
||||
|
||||
-- | Construct a boolean literal.
|
||||
bool :: Bool -> Exp
|
||||
bool = ELit . LBool
|
||||
|
||||
|
||||
|
||||
-- | Convenience function to run type inference algorithm
|
||||
showType :: Env -- ^ Starting environment, e.g. 'prelude'.
|
||||
-> Exp -- ^ Expression to typecheck
|
||||
-> Text -- ^ Text representation of the result. Contains an error
|
||||
-- message on failure.
|
||||
showType env expr =
|
||||
case (runInfer . fmap (generalize (Env mempty) . uncurry applySubst) . infer env) expr of
|
||||
Left err -> "Error inferring type of " <> ppr expr <>": " <> ppr err
|
||||
Right ty -> ppr expr <> " :: " <> ppr ty
|
||||
58
README.md
58
README.md
@@ -1,24 +1,21 @@
|
||||
|
||||
# Schala - a programming language meta-interpreter
|
||||
|
||||
Schala is a Rust framework written to make it easy to create and experiment
|
||||
with multipl toy programming languages. It provides a cross-language REPL and
|
||||
provisions for tokenizing text, parsing tokens, evaluating an abstract syntax
|
||||
tree, and other tasks that are common to all programming languages, as well as sharing state
|
||||
between multiple programming languages.
|
||||
Schala is a Rust framework written to make it easy to
|
||||
create and experiment with toy programming languages. It provides
|
||||
a common REPL, and a trait `ProgrammingLanguage` with provisions
|
||||
for tokenizing text, parsing tokens, evaluating an abstract syntax tree,
|
||||
and other tasks that are common to all programming languages.
|
||||
|
||||
Schala is implemented as a Rust library `schala-repl`, which provides a
|
||||
function `start_repl`, meant to be used as entry point into a common REPL or
|
||||
non-interactive environment. Clients are expected to invoke `start_repl` with a
|
||||
vector of programming languages. Individual programming language
|
||||
implementations are Rust types that implement the
|
||||
`ProgrammingLanguageInterface` trait and store whatever persistent state is
|
||||
relevant to that language.
|
||||
Schala is implemented as a Rust library `schala_lib`, which provides a
|
||||
`schala_main` function. This function serves as the main loop of the REPL, if run
|
||||
interactively, or otherwise reads and interprets programming language source
|
||||
files. It expects as input a vector of `PLIGenerator`, which is a type representing
|
||||
a closure that returns a boxed trait object that implements the `ProgrammingLanguage` trait,
|
||||
and stores any persistent state relevant to that programming language. The ability
|
||||
to share state between different programming languages is in the works.
|
||||
|
||||
Run schala with: `cargo run`. This will drop you into a REPL environment. Type
|
||||
`:help` for more information, or type in text in any supported programming
|
||||
language (currently only schala-lang) to evaluate it in the REPL.
|
||||
|
||||
## History
|
||||
## About
|
||||
|
||||
Schala started out life as an experiment in writing a Javascript-like
|
||||
programming language that would never encounter any kind of runtime value
|
||||
@@ -36,18 +33,18 @@ creating a language name confusingly close to Scala. The naming scheme for
|
||||
languages implemented with the Schala meta-interpreter is Chrono Trigger
|
||||
characters.
|
||||
|
||||
Schala and languages implemented with it are incomplete alpha software and are
|
||||
not ready for public release.
|
||||
Schala is incomplete alpha software and is not ready for public release.
|
||||
|
||||
## Languages implemented using the meta-interpreter
|
||||
|
||||
* The eponymous *Schala* language is a work-in-progress general purpose
|
||||
programming language with static typing and algebraic data types. Its design
|
||||
goals include having a very straightforward implemenation and being syntactically
|
||||
minimal.
|
||||
* The eponymous *Schala* language is an interpreted/compiled scripting langauge,
|
||||
designed to be relatively simple, but with a reasonably sophisticated type
|
||||
system.
|
||||
|
||||
* *Maaru* is a very simple dynamically-typed scripting language, with the semantics
|
||||
that all runtime errors return a `null` value rather than fail.
|
||||
* *Maaru* was the original Schala (since renamed to free up the name *Schala*
|
||||
for the above language), a very simple dynamically-typed scripting language
|
||||
such that all possible runtime errors result in null rather than program
|
||||
failure.
|
||||
|
||||
* *Robo* is an experiment in creating a lazy, functional, strongly-typed language
|
||||
much like Haskell
|
||||
@@ -61,15 +58,6 @@ of learning how to write a programming language.
|
||||
|
||||
### Type-checking
|
||||
https://skillsmatter.com/skillscasts/10868-inside-the-rust-compiler
|
||||
https://www.youtube.com/watch?v=il3gD7XMdmA
|
||||
http://dev.stephendiehl.com/fun/006_hindley_milner.html
|
||||
https://rust-lang-nursery.github.io/rustc-guide/type-inference.html
|
||||
|
||||
https://eli.thegreenplace.net/2018/unification/
|
||||
https://eli.thegreenplace.net/2018/type-inference/
|
||||
http://smallcultfollowing.com/babysteps/blog/2017/03/25/unification-in-chalk-part-1/
|
||||
http://reasonableapproximation.net/2019/05/05/hindley-milner.html
|
||||
https://rickyhan.com/jekyll/update/2018/05/26/hindley-milner-tutorial-rust.html
|
||||
|
||||
### Evaluation
|
||||
*Understanding Computation*, Tom Stuart, O'Reilly 2013
|
||||
@@ -78,7 +66,6 @@ https://rickyhan.com/jekyll/update/2018/05/26/hindley-milner-tutorial-rust.html
|
||||
|
||||
### Parsing
|
||||
http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/
|
||||
https://soc.github.io/languages/unified-condition-syntax
|
||||
|
||||
[Crafting Interpreters](http://www.craftinginterpreters.com/)
|
||||
|
||||
@@ -87,5 +74,4 @@ http://blog.ulysse.io/2016/07/03/llvm-getting-started.html
|
||||
|
||||
###Rust resources
|
||||
https://thefullsnack.com/en/rust-for-the-web.html
|
||||
|
||||
https://rocket.rs/guide/getting-started/
|
||||
|
||||
127
TODO.md
127
TODO.md
@@ -1,123 +1,46 @@
|
||||
# TODO items
|
||||
|
||||
## Typechecking
|
||||
# TODO Items
|
||||
|
||||
- look at https://rickyhan.com/jekyll/update/2018/05/26/hindley-milner-tutorial-rust.html
|
||||
* Share state between programming languages
|
||||
|
||||
- cf. the notation mentioned in the cardelli paper, the debug information for the `typechecking` pass should
|
||||
print the generated type variable for every subexpression in an expression
|
||||
* idea for Schala - scoped types - be able to define a quick enum type scoped to a function ro something, that only is meant to be used as a quick bespoke interface between two other things
|
||||
|
||||
- change 'trait' to 'interface'
|
||||
|
||||
- think about idris-related ideas of multiple implementations of a type for an interface (+ vs * impl for monoids, for preorder/inorder/postorder for Foldable)
|
||||
|
||||
-should have an Idris-like `cast To From` function
|
||||
|
||||
## Schala-lang syntax
|
||||
|
||||
-idea: the `type` declaration should have some kind of GADT-like syntax
|
||||
|
||||
-idea: I should make the BNF grammar part of the documentation...
|
||||
|
||||
- Idea: if you have a pattern-match where one variant has a variable and the other lacks it
|
||||
instead of treating this as a type error, promote the bound variable to an option type
|
||||
|
||||
- Include extensible scala-style html"string ${var}" string interpolations
|
||||
|
||||
- A neat idea for pattern matching optimization would be if you could match on one of several things in a list
|
||||
ex:
|
||||
```if x {
|
||||
is (comp, LHSPat, RHSPat) if comp in ["==, "<"] -> ...
|
||||
}```
|
||||
|
||||
- Schala should have both currying *and* default arguments!
|
||||
```fn a(b: Int, c:Int, d:Int = 1) -> Int
|
||||
a(1,2) : Int
|
||||
a(1,2,d=2): Int
|
||||
a(_,1,3) : Int -> Int
|
||||
a(1,2, c=_): Int -> Int
|
||||
a(_,_,_) : Int -> Int -> Int -> Int
|
||||
```
|
||||
|
||||
- scoped types - be able to define a quick enum type scoped to a function or other type for
|
||||
something, that only is meant to be used as a quick bespoke interface between
|
||||
two other things
|
||||
|
||||
ex.
|
||||
```type enum {
|
||||
* another idea, allow:
|
||||
type enum {
|
||||
type enum MySubVariant {
|
||||
SubVariant1, SubVariant2, etc.
|
||||
}
|
||||
Variant1(MySubVariant),
|
||||
Variant2(...),
|
||||
}```
|
||||
|
||||
- inclusive/exclusive range syntax like .. vs ..=
|
||||
|
||||
## Compilation
|
||||
-look into Inkwell for rust LLVM bindings
|
||||
|
||||
-https://cranelift.readthedocs.io/en/latest/?badge=latest<Paste>
|
||||
|
||||
|
||||
## Other links of note
|
||||
|
||||
- https://nshipster.com/never/
|
||||
-consult http://gluon-lang.org/book/embedding-api.html
|
||||
|
||||
|
||||
|
||||
## Playing around with conditional syntax ideas
|
||||
|
||||
- if/match playground
|
||||
|
||||
simple if
|
||||
`if x == 1.0 { "a" } else { "b" }`
|
||||
|
||||
one comparison multiple targets:
|
||||
`if x == { 1.0 -> "a", 2.0 -> "b", else -> "c" }`
|
||||
|
||||
different comparison operators/ method calls:
|
||||
`if x { == 1.0 -> "a", eq NaN -> "n", .hella() -> "h", else -> "z" }`
|
||||
|
||||
pattern matching/introducing bindings:
|
||||
`if alice { .age < 18 -> "18", is Person("Alice", age) -> "${age}", else -> "none" }`
|
||||
|
||||
pattern matching w/ if-let:
|
||||
`if person is Person("Alice", age) { "${age}" } else { "nope" }`
|
||||
|
||||
-https://soc.github.io/languages/unified-condition-syntax syntax:
|
||||
|
||||
`if <cond-expr>" then <then-expr> else <else-expr>`
|
||||
`if <half-expr> \n <rest-expr1> then <result1-expr> \n <rest-expr2> then <result-expr2> else <result3-expr>`
|
||||
-and rest-exprs (or "targets") can have 'is' for pattern-matching, actually so can a full cond-expr
|
||||
|
||||
UNIFIED IF EXPRESSIONS FINAL WORK:
|
||||
|
||||
basic syntax:
|
||||
|
||||
`if_expr := if discriminator '{' (guard_expr)* '}'`
|
||||
`guard_expr := pattern 'then' block_or_expr'`
|
||||
`pattern := rhs | is_pattern`
|
||||
`is_pattern := 'is' ???`
|
||||
`rhs := expression | ???`
|
||||
|
||||
|
||||
if the only two guard patterns are true and false, then the abbreviated syntax:
|
||||
`'if' discriminator 'then' block_or_expr 'else' block_or_expr`
|
||||
can replace `'if' discriminator '{' 'true' 'then' block_or_expr; 'false' 'then' block_or_expr '}'`
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
* idea for Schala: both currying *and* default arguments!
|
||||
ex. fn a(b: Int, c:Int, d:Int = 1) -> Int
|
||||
a(1,2) : Int
|
||||
a(1,2,d=2): Int
|
||||
a(_,1,3) : Int -> Int
|
||||
a(1,2, c=_): Int -> Int
|
||||
a(_,_,_) : Int -> Int -> Int -> Int
|
||||
|
||||
|
||||
|
||||
- AST : maybe replace the Expression type with "Ascription(TypeName, Box<Expression>) nodes??
|
||||
- parser: add a "debug" field to the Parser struct for all debug-related things
|
||||
|
||||
-scala-style html"dfasfsadf${}" string interpolations!
|
||||
|
||||
*Compiler passes architecture
|
||||
|
||||
-ProgrammingLanguageInterface defines a evaluate_in_repl() and evaluate_no_repl() functions
|
||||
-these take in a vec of CompilerPasses
|
||||
|
||||
struct CompilerPass {
|
||||
name: String,
|
||||
run: fn(PrevPass) -> NextPass
|
||||
}
|
||||
|
||||
-change "Type...." names in parser.rs to "Anno..." for non-collision with names in typechecking.rs
|
||||
|
||||
-get rid of code pertaining to compilation specifically, have a more generation notion of "execution type"
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
[package]
|
||||
name = "maaru-lang"
|
||||
version = "0.1.0"
|
||||
authors = ["greg <greg.shuflin@protonmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.5.8"
|
||||
take_mut = "0.1.3"
|
||||
llvm-sys = "*"
|
||||
|
||||
schala-repl = { path = "../schala-repl" }
|
||||
@@ -1,78 +0,0 @@
|
||||
#![feature(box_patterns)]
|
||||
|
||||
extern crate schala_repl;
|
||||
|
||||
mod tokenizer;
|
||||
mod parser;
|
||||
mod eval;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TokenError {
|
||||
pub msg: String,
|
||||
}
|
||||
|
||||
impl TokenError {
|
||||
pub fn new(msg: &str) -> TokenError {
|
||||
TokenError { msg: msg.to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
pub use self::eval::Evaluator as MaaruEvaluator;
|
||||
|
||||
pub struct Maaru<'a> {
|
||||
evaluator: MaaruEvaluator<'a>
|
||||
}
|
||||
|
||||
impl<'a> Maaru<'a> {
|
||||
pub fn new() -> Maaru<'a> {
|
||||
Maaru {
|
||||
evaluator: MaaruEvaluator::new(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
fn execute_pipeline(&mut self, input: &str, options: &EvalOptions) -> Result<String, String> {
|
||||
let mut output = UnfinishedComputation::default();
|
||||
|
||||
let tokens = match tokenizer::tokenize(input) {
|
||||
Ok(tokens) => {
|
||||
if let Some(_) = options.debug_passes.get("tokens") {
|
||||
output.add_artifact(TraceArtifact::new("tokens", format!("{:?}", tokens)));
|
||||
}
|
||||
tokens
|
||||
},
|
||||
Err(err) => {
|
||||
return output.finish(Err(format!("Tokenization error: {:?}\n", err.msg)))
|
||||
}
|
||||
};
|
||||
|
||||
let ast = match parser::parse(&tokens, &[]) {
|
||||
Ok(ast) => {
|
||||
if let Some(_) = options.debug_passes.get("ast") {
|
||||
output.add_artifact(TraceArtifact::new("ast", format!("{:?}", ast)));
|
||||
}
|
||||
ast
|
||||
},
|
||||
Err(err) => {
|
||||
return output.finish(Err(format!("Parse error: {:?}\n", err.msg)))
|
||||
}
|
||||
};
|
||||
let mut evaluation_output = String::new();
|
||||
for s in self.evaluator.run(ast).iter() {
|
||||
evaluation_output.push_str(s);
|
||||
}
|
||||
Ok(evaluation_output)
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
impl<'a> ProgrammingLanguageInterface for Maaru<'a> {
|
||||
fn get_language_name(&self) -> String {
|
||||
"Maaru".to_string()
|
||||
}
|
||||
fn get_source_file_suffix(&self) -> String {
|
||||
format!("maaru")
|
||||
}
|
||||
}
|
||||
*/
|
||||
@@ -1,11 +0,0 @@
|
||||
[package]
|
||||
name = "robo-lang"
|
||||
version = "0.1.0"
|
||||
authors = ["greg <greg.shuflin@protonmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.5.8"
|
||||
take_mut = "0.1.3"
|
||||
llvm-sys = "*"
|
||||
|
||||
schala-repl = { path = "../schala-repl" }
|
||||
@@ -1,11 +0,0 @@
|
||||
[package]
|
||||
name = "rukka-lang"
|
||||
version = "0.1.0"
|
||||
authors = ["greg <greg.shuflin@protonmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.5.8"
|
||||
take_mut = "0.1.3"
|
||||
llvm-sys = "*"
|
||||
|
||||
schala-repl = { path = "../schala-repl" }
|
||||
@@ -1,12 +0,0 @@
|
||||
[package]
|
||||
name = "schala-lang-codegen"
|
||||
version = "0.1.0"
|
||||
authors = ["greg <greg.shuflin@protonmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
syn = { version = "0.15.12", features = ["full", "extra-traits", "fold"] }
|
||||
quote = "0.6.8"
|
||||
@@ -1,50 +0,0 @@
|
||||
#![feature(box_patterns)]
|
||||
#![recursion_limit="128"]
|
||||
extern crate proc_macro;
|
||||
#[macro_use]
|
||||
extern crate quote;
|
||||
#[macro_use]
|
||||
extern crate syn;
|
||||
|
||||
use self::proc_macro::TokenStream;
|
||||
use self::syn::fold::Fold;
|
||||
|
||||
struct RecursiveDescentFn {
|
||||
}
|
||||
|
||||
impl Fold for RecursiveDescentFn {
|
||||
fn fold_item_fn(&mut self, mut i: syn::ItemFn) -> syn::ItemFn {
|
||||
let box block = i.block;
|
||||
let ref ident = i.ident;
|
||||
|
||||
let new_block: syn::Block = parse_quote! {
|
||||
{
|
||||
let next_token_before_parse = self.token_handler.peek();
|
||||
let record = ParseRecord {
|
||||
production_name: stringify!(#ident).to_string(),
|
||||
next_token: format!("{}", next_token_before_parse.to_string_with_metadata()),
|
||||
level: self.parse_level,
|
||||
};
|
||||
self.parse_level += 1;
|
||||
self.parse_record.push(record);
|
||||
let result = { #block };
|
||||
|
||||
if self.parse_level != 0 {
|
||||
self.parse_level -= 1;
|
||||
}
|
||||
result
|
||||
}
|
||||
};
|
||||
i.block = Box::new(new_block);
|
||||
i
|
||||
}
|
||||
}
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn recursive_descent_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
|
||||
let input: syn::ItemFn = parse_macro_input!(item as syn::ItemFn);
|
||||
let mut folder = RecursiveDescentFn {};
|
||||
let output = folder.fold_item_fn(input);
|
||||
TokenStream::from(quote!(#output))
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
[package]
|
||||
name = "schala-lang"
|
||||
version = "0.1.0"
|
||||
authors = ["greg <greg.shuflin@protonmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.5.8"
|
||||
take_mut = "0.1.3"
|
||||
maplit = "*"
|
||||
lazy_static = "0.2.8"
|
||||
failure = "0.1.2"
|
||||
ena = "0.11.0"
|
||||
stopwatch = "0.0.7"
|
||||
|
||||
schala-lang-codegen = { path = "../codegen" }
|
||||
schala-repl = { path = "../../schala-repl" }
|
||||
@@ -1,215 +0,0 @@
|
||||
use std::rc::Rc;
|
||||
use std::convert::From;
|
||||
|
||||
use crate::builtin::{BinOp, PrefixOp};
|
||||
use crate::typechecking::TypeData;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Meta<T> {
|
||||
n: T,
|
||||
source_map: SourceMap,
|
||||
type_data: TypeData,
|
||||
}
|
||||
|
||||
impl<T> Meta<T> {
|
||||
pub fn new(n: T) -> Meta<T> {
|
||||
Meta { n, source_map: SourceMap::default(), type_data: TypeData::new() }
|
||||
}
|
||||
|
||||
pub fn node(&self) -> &T {
|
||||
&self.n
|
||||
}
|
||||
}
|
||||
|
||||
//TODO this PartialEq is here to make tests work - find a way to make it not necessary
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
struct SourceMap {
|
||||
}
|
||||
|
||||
impl From<Expression> for Meta<Expression> {
|
||||
fn from(expr: Expression) -> Meta<Expression> {
|
||||
Meta { n: expr, source_map: SourceMap::default(), type_data: TypeData::new() }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct AST(pub Vec<Meta<Statement>>);
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum Statement {
|
||||
ExpressionStatement(Meta<Expression>),
|
||||
Declaration(Declaration),
|
||||
}
|
||||
|
||||
pub type Block = Vec<Meta<Statement>>;
|
||||
pub type ParamName = Rc<String>;
|
||||
pub type FormalParam = (ParamName, Option<TypeIdentifier>);
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum Declaration {
|
||||
FuncSig(Signature),
|
||||
FuncDecl(Signature, Block),
|
||||
TypeDecl {
|
||||
name: TypeSingletonName,
|
||||
body: TypeBody,
|
||||
mutable: bool
|
||||
},
|
||||
TypeAlias(Rc<String>, Rc<String>), //should have TypeSingletonName in it, or maybe just String, not sure
|
||||
Binding {
|
||||
name: Rc<String>,
|
||||
constant: bool,
|
||||
type_anno: Option<TypeIdentifier>,
|
||||
expr: Meta<Expression>,
|
||||
},
|
||||
Impl {
|
||||
type_name: TypeIdentifier,
|
||||
interface_name: Option<TypeSingletonName>,
|
||||
block: Vec<Declaration>,
|
||||
},
|
||||
Interface {
|
||||
name: Rc<String>,
|
||||
signatures: Vec<Signature>
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Signature {
|
||||
pub name: Rc<String>,
|
||||
pub operator: bool,
|
||||
pub params: Vec<FormalParam>,
|
||||
pub type_anno: Option<TypeIdentifier>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct TypeBody(pub Vec<Variant>);
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum Variant {
|
||||
UnitStruct(Rc<String>),
|
||||
TupleStruct(Rc<String>, Vec<TypeIdentifier>),
|
||||
Record {
|
||||
name: Rc<String>,
|
||||
members: Vec<(Rc<String>, TypeIdentifier)>,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Expression(pub ExpressionKind, pub Option<TypeIdentifier>);
|
||||
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum TypeIdentifier {
|
||||
Tuple(Vec<TypeIdentifier>),
|
||||
Singleton(TypeSingletonName)
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct TypeSingletonName {
|
||||
pub name: Rc<String>,
|
||||
pub params: Vec<TypeIdentifier>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum ExpressionKind {
|
||||
NatLiteral(u64),
|
||||
FloatLiteral(f64),
|
||||
StringLiteral(Rc<String>),
|
||||
BoolLiteral(bool),
|
||||
BinExp(BinOp, Box<Meta<Expression>>, Box<Meta<Expression>>),
|
||||
PrefixExp(PrefixOp, Box<Meta<Expression>>),
|
||||
TupleLiteral(Vec<Meta<Expression>>),
|
||||
Value(Rc<String>),
|
||||
NamedStruct {
|
||||
name: Rc<String>,
|
||||
fields: Vec<(Rc<String>, Meta<Expression>)>,
|
||||
},
|
||||
Call {
|
||||
f: Box<Meta<Expression>>,
|
||||
arguments: Vec<Meta<Expression>>,
|
||||
},
|
||||
Index {
|
||||
indexee: Box<Meta<Expression>>,
|
||||
indexers: Vec<Meta<Expression>>,
|
||||
},
|
||||
IfExpression {
|
||||
discriminator: Box<Discriminator>,
|
||||
body: Box<IfExpressionBody>,
|
||||
},
|
||||
WhileExpression {
|
||||
condition: Option<Box<Meta<Expression>>>,
|
||||
body: Block,
|
||||
},
|
||||
ForExpression {
|
||||
enumerators: Vec<Enumerator>,
|
||||
body: Box<ForBody>,
|
||||
},
|
||||
Lambda {
|
||||
params: Vec<FormalParam>,
|
||||
type_anno: Option<TypeIdentifier>,
|
||||
body: Block,
|
||||
},
|
||||
ListLiteral(Vec<Meta<Expression>>),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum Discriminator {
|
||||
Simple(Expression),
|
||||
BinOp(Expression, BinOp)
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum IfExpressionBody {
|
||||
SimpleConditional(Block, Option<Block>),
|
||||
SimplePatternMatch(Pattern, Block, Option<Block>),
|
||||
GuardList(Vec<GuardArm>)
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct GuardArm {
|
||||
pub guard: Guard,
|
||||
pub body: Block,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum Guard {
|
||||
Pat(Pattern),
|
||||
HalfExpr(HalfExpr)
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct HalfExpr {
|
||||
pub op: Option<BinOp>,
|
||||
pub expr: ExpressionKind,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum Pattern {
|
||||
Ignored,
|
||||
TuplePattern(Vec<Pattern>),
|
||||
Literal(PatternLiteral),
|
||||
TupleStruct(Rc<String>, Vec<Pattern>),
|
||||
Record(Rc<String>, Vec<(Rc<String>, Pattern)>),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum PatternLiteral {
|
||||
NumPattern {
|
||||
neg: bool,
|
||||
num: ExpressionKind,
|
||||
},
|
||||
StringPattern(Rc<String>),
|
||||
BoolPattern(bool),
|
||||
VarPattern(Rc<String>)
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Enumerator {
|
||||
pub id: Rc<String>,
|
||||
pub generator: Meta<Expression>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum ForBody {
|
||||
MonadicReturn(Meta<Expression>),
|
||||
StatementBlock(Block),
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
use std::rc::Rc;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::tokenizing::TokenKind;
|
||||
use crate::typechecking::{TypeConst, Type};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct BinOp {
|
||||
sigil: Rc<String>
|
||||
}
|
||||
|
||||
impl BinOp {
|
||||
pub fn from_sigil(sigil: &str) -> BinOp {
|
||||
BinOp { sigil: Rc::new(sigil.to_string()) }
|
||||
}
|
||||
pub fn sigil(&self) -> &Rc<String> {
|
||||
&self.sigil
|
||||
}
|
||||
pub fn from_sigil_token(tok: &TokenKind) -> Option<BinOp> {
|
||||
use self::TokenKind::*;
|
||||
let s = match tok {
|
||||
Operator(op) => op,
|
||||
Period => ".",
|
||||
Pipe => "|",
|
||||
Slash => "/",
|
||||
LAngleBracket => "<",
|
||||
RAngleBracket => ">",
|
||||
_ => return None
|
||||
};
|
||||
Some(BinOp::from_sigil(s))
|
||||
}
|
||||
|
||||
pub fn get_type(&self) -> Result<Type, String> {
|
||||
let s = self.sigil.as_str();
|
||||
BINOPS.get(s).map(|x| x.0.clone()).ok_or(format!("Binop {} not found", s))
|
||||
}
|
||||
|
||||
pub fn min_precedence() -> i32 {
|
||||
i32::min_value()
|
||||
}
|
||||
pub fn get_precedence_from_token(op: &TokenKind) -> Option<i32> {
|
||||
use self::TokenKind::*;
|
||||
let s = match op {
|
||||
Operator(op) => op,
|
||||
Period => ".",
|
||||
Pipe => "|",
|
||||
Slash => "/",
|
||||
LAngleBracket => "<",
|
||||
RAngleBracket => ">",
|
||||
_ => return None
|
||||
};
|
||||
let default = 10_000_000;
|
||||
Some(BINOPS.get(s).map(|x| x.2.clone()).unwrap_or_else(|| {
|
||||
default
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn get_precedence(&self) -> i32 {
|
||||
let s: &str = &self.sigil;
|
||||
let default = 10_000_000;
|
||||
BINOPS.get(s).map(|x| x.2.clone()).unwrap_or_else(|| {
|
||||
default
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct PrefixOp {
|
||||
sigil: Rc<String>
|
||||
}
|
||||
|
||||
impl PrefixOp {
|
||||
pub fn from_sigil(sigil: &str) -> PrefixOp {
|
||||
PrefixOp { sigil: Rc::new(sigil.to_string()) }
|
||||
}
|
||||
pub fn sigil(&self) -> &Rc<String> {
|
||||
&self.sigil
|
||||
}
|
||||
pub fn is_prefix(op: &str) -> bool {
|
||||
PREFIX_OPS.get(op).is_some()
|
||||
}
|
||||
pub fn get_type(&self) -> Result<Type, String> {
|
||||
let s = self.sigil.as_str();
|
||||
PREFIX_OPS.get(s).map(|x| x.0.clone()).ok_or(format!("Prefix op {} not found", s))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
lazy_static! {
|
||||
static ref PREFIX_OPS: HashMap<&'static str, (Type, ())> =
|
||||
hashmap! {
|
||||
"+" => (ty!(Nat -> Int), ()),
|
||||
"-" => (ty!(Nat -> Int), ()),
|
||||
"!" => (ty!(Bool -> Bool), ()),
|
||||
};
|
||||
}
|
||||
|
||||
/* the second tuple member is a placeholder for when I want to make evaluation rules tied to the
|
||||
* binop definition */
|
||||
//TODO some of these types are going to have to be adjusted
|
||||
lazy_static! {
|
||||
static ref BINOPS: HashMap<&'static str, (Type, (), i32)> =
|
||||
hashmap! {
|
||||
"+" => (ty!(Nat -> Nat -> Nat), (), 10),
|
||||
"-" => (ty!(Nat -> Nat -> Nat), (), 10),
|
||||
"*" => (ty!(Nat -> Nat -> Nat), (), 20),
|
||||
"/" => (ty!(Nat -> Nat -> Float), (), 20),
|
||||
"quot" => (ty!(Nat -> Nat -> Nat), (), 20),
|
||||
"%" => (ty!(Nat -> Nat -> Nat), (), 20),
|
||||
"++" => (ty!(StringT -> StringT -> StringT), (), 30),
|
||||
"^" => (ty!(Nat -> Nat -> Nat), (), 20),
|
||||
"&" => (ty!(Nat -> Nat -> Nat), (), 20),
|
||||
"|" => (ty!(Nat -> Nat -> Nat), (), 20),
|
||||
">" => (ty!(Nat -> Nat -> Bool), (), 20),
|
||||
">=" => (ty!(Nat -> Nat -> Bool), (), 20),
|
||||
"<" => (ty!(Nat -> Nat -> Bool), (), 20),
|
||||
"<=" => (ty!(Nat -> Nat -> Bool), (), 20),
|
||||
"==" => (ty!(Nat -> Nat -> Bool), (), 20),
|
||||
"=" => (ty!(Unit), (), 20), //TODO not sure what the type of this should be b/c special fmr
|
||||
"<=>" => (ty!(Nat -> Nat -> Ordering), (), 20), //TODO figure out how to treat Order
|
||||
};
|
||||
}
|
||||
@@ -1,747 +0,0 @@
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use std::fmt::Write;
|
||||
use std::io;
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::util::ScopeStack;
|
||||
use crate::reduced_ast::{BoundVars, ReducedAST, Stmt, Expr, Lit, Func, Alternative, Subpattern};
|
||||
use crate::symbol_table::{SymbolSpec, Symbol, SymbolTable};
|
||||
|
||||
pub struct State<'a> {
|
||||
values: ScopeStack<'a, Rc<String>, ValueEntry>,
|
||||
symbol_table_handle: Rc<RefCell<SymbolTable>>,
|
||||
}
|
||||
|
||||
macro_rules! builtin_binding {
|
||||
($name:expr, $values:expr) => {
|
||||
$values.insert(Rc::new(format!($name)), ValueEntry::Binding { constant: true, val: Node::Expr(Expr::Func(Func::BuiltIn(Rc::new(format!($name))))) });
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> State<'a> {
|
||||
pub fn new(symbol_table_handle: Rc<RefCell<SymbolTable>>) -> State<'a> {
|
||||
let mut values = ScopeStack::new(Some(format!("global")));
|
||||
builtin_binding!("print", values);
|
||||
builtin_binding!("println", values);
|
||||
builtin_binding!("getline", values);
|
||||
State { values, symbol_table_handle }
|
||||
}
|
||||
|
||||
pub fn debug_print(&self) -> String {
|
||||
format!("Values: {:?}", self.values)
|
||||
}
|
||||
|
||||
fn new_frame(&'a self, items: &'a Vec<Node>, bound_vars: &BoundVars) -> State<'a> {
|
||||
let mut inner_state = State {
|
||||
values: self.values.new_scope(None),
|
||||
symbol_table_handle: self.symbol_table_handle.clone(),
|
||||
};
|
||||
for (bound_var, val) in bound_vars.iter().zip(items.iter()) {
|
||||
if let Some(bv) = bound_var.as_ref() {
|
||||
inner_state.values.insert(bv.clone(), ValueEntry::Binding { constant: true, val: val.clone() });
|
||||
}
|
||||
}
|
||||
inner_state
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum Node {
|
||||
Expr(Expr),
|
||||
PrimObject {
|
||||
name: Rc<String>,
|
||||
tag: usize,
|
||||
items: Vec<Node>,
|
||||
},
|
||||
PrimTuple {
|
||||
items: Vec<Node>
|
||||
}
|
||||
}
|
||||
|
||||
fn paren_wrapped_vec(terms: impl Iterator<Item=String>) -> String {
|
||||
let mut buf = String::new();
|
||||
write!(buf, "(").unwrap();
|
||||
for term in terms.map(|e| Some(e)).intersperse(None) {
|
||||
match term {
|
||||
Some(e) => write!(buf, "{}", e).unwrap(),
|
||||
None => write!(buf, ", ").unwrap(),
|
||||
};
|
||||
}
|
||||
write!(buf, ")").unwrap();
|
||||
buf
|
||||
}
|
||||
|
||||
|
||||
impl Node {
|
||||
fn to_repl(&self) -> String {
|
||||
match self {
|
||||
Node::Expr(e) => e.to_repl(),
|
||||
Node::PrimObject { name, items, .. } if items.len() == 0 => format!("{}", name),
|
||||
Node::PrimObject { name, items, .. } => format!("{}{}", name, paren_wrapped_vec(items.iter().map(|x| x.to_repl()))),
|
||||
Node::PrimTuple { items } => format!("{}", paren_wrapped_vec(items.iter().map(|x| x.to_repl()))),
|
||||
}
|
||||
}
|
||||
fn is_true(&self) -> bool {
|
||||
match self {
|
||||
Node::Expr(Expr::Lit(crate::reduced_ast::Lit::Bool(true))) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ValueEntry {
|
||||
Binding {
|
||||
constant: bool,
|
||||
val: /*FullyEvaluatedExpr*/ Node, //TODO make this use a subtype to represent fully evaluatedness
|
||||
}
|
||||
}
|
||||
|
||||
type EvalResult<T> = Result<T, String>;
|
||||
|
||||
impl Expr {
|
||||
fn to_node(self) -> Node {
|
||||
Node::Expr(self)
|
||||
}
|
||||
fn to_repl(&self) -> String {
|
||||
use self::Lit::*;
|
||||
use self::Func::*;
|
||||
|
||||
match self {
|
||||
Expr::Lit(ref l) => match l {
|
||||
Nat(n) => format!("{}", n),
|
||||
Int(i) => format!("{}", i),
|
||||
Float(f) => format!("{}", f),
|
||||
Bool(b) => format!("{}", b),
|
||||
StringLit(s) => format!("\"{}\"", s),
|
||||
},
|
||||
Expr::Func(f) => match f {
|
||||
BuiltIn(name) => format!("<built-in function '{}'>", name),
|
||||
UserDefined { name: None, .. } => format!("<function>"),
|
||||
UserDefined { name: Some(name), .. } => format!("<function '{}'>", name),
|
||||
},
|
||||
Expr::Constructor {
|
||||
type_name: _, name, arity, ..
|
||||
} => if *arity == 0 {
|
||||
format!("{}", name)
|
||||
} else {
|
||||
format!("<data constructor '{}'>", name)
|
||||
},
|
||||
Expr::Tuple(exprs) => paren_wrapped_vec(exprs.iter().map(|x| x.to_repl())),
|
||||
_ => format!("{:?}", self),
|
||||
}
|
||||
}
|
||||
|
||||
fn replace_conditional_target_sigil(self, replacement: &Expr) -> Expr {
|
||||
use self::Expr::*;
|
||||
|
||||
match self {
|
||||
ConditionalTargetSigilValue => replacement.clone(),
|
||||
Unit | Lit(_) | Func(_) | Val(_) | Constructor { .. } |
|
||||
CaseMatch { .. } | UnimplementedSigilValue => self,
|
||||
Tuple(exprs) => Tuple(exprs.into_iter().map(|e| e.replace_conditional_target_sigil(replacement)).collect()),
|
||||
Call { f, args } => {
|
||||
let new_args = args.into_iter().map(|e| e.replace_conditional_target_sigil(replacement)).collect();
|
||||
Call { f, args: new_args }
|
||||
},
|
||||
Conditional { .. } => panic!("Dunno if I need this, but if so implement"),
|
||||
Assign { .. } => panic!("I'm pretty sure I don't need this"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> State<'a> {
|
||||
pub fn evaluate(&mut self, ast: ReducedAST, repl: bool) -> Vec<Result<String, String>> {
|
||||
let mut acc = vec![];
|
||||
|
||||
// handle prebindings
|
||||
for statement in ast.0.iter() {
|
||||
self.prebinding(statement);
|
||||
}
|
||||
|
||||
for statement in ast.0 {
|
||||
match self.statement(statement) {
|
||||
Ok(Some(ref output)) if repl => acc.push(Ok(output.to_repl())),
|
||||
Ok(_) => (),
|
||||
Err(error) => {
|
||||
acc.push(Err(format!("Runtime error: {}", error)));
|
||||
return acc;
|
||||
},
|
||||
}
|
||||
}
|
||||
acc
|
||||
}
|
||||
|
||||
fn prebinding(&mut self, stmt: &Stmt) {
|
||||
match stmt {
|
||||
Stmt::PreBinding { name, func } => {
|
||||
let v_entry = ValueEntry::Binding { constant: true, val: Node::Expr(Expr::Func(func.clone())) };
|
||||
self.values.insert(name.clone(), v_entry);
|
||||
},
|
||||
Stmt::Expr(_expr) => {
|
||||
//TODO have this support things like nested function defs
|
||||
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
||||
fn statement(&mut self, stmt: Stmt) -> EvalResult<Option<Node>> {
|
||||
match stmt {
|
||||
Stmt::Binding { name, constant, expr } => {
|
||||
let val = self.expression(Node::Expr(expr))?;
|
||||
self.values.insert(name.clone(), ValueEntry::Binding { constant, val });
|
||||
Ok(None)
|
||||
},
|
||||
Stmt::Expr(expr) => Ok(Some(self.expression(expr.to_node())?)),
|
||||
Stmt::PreBinding {..} | Stmt::Noop => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn block(&mut self, stmts: Vec<Stmt>) -> EvalResult<Node> {
|
||||
let mut ret = None;
|
||||
for stmt in stmts {
|
||||
ret = self.statement(stmt)?;
|
||||
}
|
||||
Ok(ret.unwrap_or(Node::Expr(Expr::Unit)))
|
||||
}
|
||||
|
||||
fn expression(&mut self, node: Node) -> EvalResult<Node> {
|
||||
use self::Expr::*;
|
||||
match node {
|
||||
t @ Node::PrimTuple { .. } => Ok(t),
|
||||
obj @ Node::PrimObject { .. } => Ok(obj),
|
||||
Node::Expr(expr) => match expr {
|
||||
literal @ Lit(_) => Ok(Node::Expr(literal)),
|
||||
Call { box f, args } => self.call_expression(f, args),
|
||||
Val(v) => self.value(v),
|
||||
Constructor { arity, ref name, tag, .. } if arity == 0 => Ok(Node::PrimObject { name: name.clone(), tag, items: vec![] }),
|
||||
constructor @ Constructor { .. } => Ok(Node::Expr(constructor)),
|
||||
func @ Func(_) => Ok(Node::Expr(func)),
|
||||
Tuple(exprs) => {
|
||||
let nodes = exprs.into_iter().map(|expr| self.expression(Node::Expr(expr))).collect::<Result<Vec<Node>,_>>()?;
|
||||
Ok(Node::PrimTuple { items: nodes })
|
||||
},
|
||||
Conditional { box cond, then_clause, else_clause } => self.conditional(cond, then_clause, else_clause),
|
||||
Assign { box val, box expr } => self.assign_expression(val, expr),
|
||||
Unit => Ok(Node::Expr(Unit)),
|
||||
CaseMatch { box cond, alternatives } => self.case_match_expression(cond, alternatives),
|
||||
ConditionalTargetSigilValue => Ok(Node::Expr(ConditionalTargetSigilValue)),
|
||||
UnimplementedSigilValue => Err(format!("Sigil value eval not implemented")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn call_expression(&mut self, f: Expr, args: Vec<Expr>) -> EvalResult<Node> {
|
||||
use self::Expr::*;
|
||||
match self.expression(Node::Expr(f))? {
|
||||
Node::Expr(Constructor { type_name, name, tag, arity }) => self.apply_data_constructor(type_name, name, tag, arity, args),
|
||||
Node::Expr(Func(f)) => self.apply_function(f, args),
|
||||
other => return Err(format!("Tried to call {:?} which is not a function or data constructor", other)),
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_data_constructor(&mut self, _type_name: Rc<String>, name: Rc<String>, tag: usize, arity: usize, args: Vec<Expr>) -> EvalResult<Node> {
|
||||
if arity != args.len() {
|
||||
return Err(format!("Data constructor {} requires {} args", name, arity));
|
||||
}
|
||||
|
||||
let evaled_args = args.into_iter().map(|expr| self.expression(Node::Expr(expr))).collect::<Result<Vec<Node>,_>>()?;
|
||||
//let evaled_args = vec![];
|
||||
Ok(Node::PrimObject {
|
||||
name: name.clone(),
|
||||
items: evaled_args,
|
||||
tag
|
||||
})
|
||||
}
|
||||
|
||||
fn apply_function(&mut self, f: Func, args: Vec<Expr>) -> EvalResult<Node> {
|
||||
match f {
|
||||
Func::BuiltIn(sigil) => Ok(Node::Expr(self.apply_builtin(sigil, args)?)),
|
||||
Func::UserDefined { params, body, name } => {
|
||||
|
||||
if params.len() != args.len() {
|
||||
return Err(format!("calling a {}-argument function with {} args", params.len(), args.len()))
|
||||
}
|
||||
let mut func_state = State {
|
||||
values: self.values.new_scope(name.map(|n| format!("{}", n))),
|
||||
symbol_table_handle: self.symbol_table_handle.clone(),
|
||||
};
|
||||
for (param, val) in params.into_iter().zip(args.into_iter()) {
|
||||
let val = func_state.expression(Node::Expr(val))?;
|
||||
func_state.values.insert(param, ValueEntry::Binding { constant: true, val });
|
||||
}
|
||||
// TODO figure out function return semantics
|
||||
func_state.block(body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_builtin(&mut self, name: Rc<String>, args: Vec<Expr>) -> EvalResult<Expr> {
|
||||
use self::Expr::*;
|
||||
use self::Lit::*;
|
||||
let evaled_args: Result<Vec<Expr>, String> = args.into_iter().map(|arg| {
|
||||
match self.expression(Node::Expr(arg)) {
|
||||
Ok(Node::Expr(e)) => Ok(e),
|
||||
Ok(Node::PrimTuple { .. }) => Err(format!("Trying to apply a builtin to a tuple")),
|
||||
Ok(Node::PrimObject { .. }) => Err(format!("Trying to apply a builtin to a primitive object")),
|
||||
Err(e) => Err(e)
|
||||
}
|
||||
}).collect();
|
||||
let evaled_args = evaled_args?;
|
||||
|
||||
Ok(match (name.as_str(), evaled_args.as_slice()) {
|
||||
/* binops */
|
||||
("+", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l + r)),
|
||||
("++", &[Lit(StringLit(ref s1)), Lit(StringLit(ref s2))]) => Lit(StringLit(Rc::new(format!("{}{}", s1, s2)))),
|
||||
("-", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l - r)),
|
||||
("*", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l * r)),
|
||||
("/", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Float((l as f64)/ (r as f64))),
|
||||
("quot", &[Lit(Nat(l)), Lit(Nat(r))]) => if r == 0 {
|
||||
return Err(format!("divide by zero"));
|
||||
} else {
|
||||
Lit(Nat(l / r))
|
||||
},
|
||||
("%", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l % r)),
|
||||
("^", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l ^ r)),
|
||||
("&", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l & r)),
|
||||
("|", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l | r)),
|
||||
|
||||
/* comparisons */
|
||||
("==", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Bool(l == r)),
|
||||
("==", &[Lit(Int(l)), Lit(Int(r))]) => Lit(Bool(l == r)),
|
||||
("==", &[Lit(Float(l)), Lit(Float(r))]) => Lit(Bool(l == r)),
|
||||
("==", &[Lit(Bool(l)), Lit(Bool(r))]) => Lit(Bool(l == r)),
|
||||
("==", &[Lit(StringLit(ref l)), Lit(StringLit(ref r))]) => Lit(Bool(l == r)),
|
||||
|
||||
("<", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Bool(l < r)),
|
||||
("<", &[Lit(Int(l)), Lit(Int(r))]) => Lit(Bool(l < r)),
|
||||
("<", &[Lit(Float(l)), Lit(Float(r))]) => Lit(Bool(l < r)),
|
||||
|
||||
("<=", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Bool(l <= r)),
|
||||
("<=", &[Lit(Int(l)), Lit(Int(r))]) => Lit(Bool(l <= r)),
|
||||
("<=", &[Lit(Float(l)), Lit(Float(r))]) => Lit(Bool(l <= r)),
|
||||
|
||||
(">", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Bool(l > r)),
|
||||
(">", &[Lit(Int(l)), Lit(Int(r))]) => Lit(Bool(l > r)),
|
||||
(">", &[Lit(Float(l)), Lit(Float(r))]) => Lit(Bool(l > r)),
|
||||
|
||||
(">=", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Bool(l >= r)),
|
||||
(">=", &[Lit(Int(l)), Lit(Int(r))]) => Lit(Bool(l >= r)),
|
||||
(">=", &[Lit(Float(l)), Lit(Float(r))]) => Lit(Bool(l >= r)),
|
||||
|
||||
/* prefix ops */
|
||||
("!", &[Lit(Bool(true))]) => Lit(Bool(false)),
|
||||
("!", &[Lit(Bool(false))]) => Lit(Bool(true)),
|
||||
("-", &[Lit(Nat(n))]) => Lit(Int(-1*(n as i64))),
|
||||
("-", &[Lit(Int(n))]) => Lit(Int(-1*(n as i64))),
|
||||
("+", &[Lit(Int(n))]) => Lit(Int(n)),
|
||||
("+", &[Lit(Nat(n))]) => Lit(Nat(n)),
|
||||
|
||||
|
||||
/* builtin functions */
|
||||
("print", &[ref anything]) => {
|
||||
print!("{}", anything.to_repl());
|
||||
Expr::Unit
|
||||
},
|
||||
("println", &[ref anything]) => {
|
||||
println!("{}", anything.to_repl());
|
||||
Expr::Unit
|
||||
},
|
||||
("getline", &[]) => {
|
||||
let mut buf = String::new();
|
||||
io::stdin().read_line(&mut buf).expect("Error readling line in 'getline'");
|
||||
Lit(StringLit(Rc::new(buf.trim().to_string())))
|
||||
},
|
||||
(x, args) => return Err(format!("bad or unimplemented builtin {:?} | {:?}", x, args)),
|
||||
})
|
||||
}
|
||||
|
||||
fn conditional(&mut self, cond: Expr, then_clause: Vec<Stmt>, else_clause: Vec<Stmt>) -> EvalResult<Node> {
|
||||
let cond = self.expression(Node::Expr(cond))?;
|
||||
Ok(match cond {
|
||||
Node::Expr(Expr::Lit(Lit::Bool(true))) => self.block(then_clause)?,
|
||||
Node::Expr(Expr::Lit(Lit::Bool(false))) => self.block(else_clause)?,
|
||||
_ => return Err(format!("Conditional with non-boolean condition"))
|
||||
})
|
||||
}
|
||||
|
||||
fn assign_expression(&mut self, val: Expr, expr: Expr) -> EvalResult<Node> {
|
||||
let name = match val {
|
||||
Expr::Val(name) => name,
|
||||
_ => return Err(format!("Trying to assign to a non-value")),
|
||||
};
|
||||
|
||||
let constant = match self.values.lookup(&name) {
|
||||
None => return Err(format!("Constant {} is undefined", name)),
|
||||
Some(ValueEntry::Binding { constant, .. }) => constant.clone(),
|
||||
};
|
||||
if constant {
|
||||
return Err(format!("trying to update {}, a non-mutable binding", name));
|
||||
}
|
||||
let val = self.expression(Node::Expr(expr))?;
|
||||
self.values.insert(name.clone(), ValueEntry::Binding { constant: false, val });
|
||||
Ok(Node::Expr(Expr::Unit))
|
||||
}
|
||||
|
||||
fn guard_passes(&mut self, guard: &Option<Expr>, cond: &Node) -> EvalResult<bool> {
|
||||
if let Some(ref guard_expr) = guard {
|
||||
let guard_expr = match cond {
|
||||
Node::Expr(ref e) => guard_expr.clone().replace_conditional_target_sigil(e),
|
||||
_ => guard_expr.clone()
|
||||
};
|
||||
Ok(self.expression(guard_expr.to_node())?.is_true())
|
||||
} else {
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
fn case_match_expression(&mut self, cond: Expr, alternatives: Vec<Alternative>) -> EvalResult<Node> {
|
||||
|
||||
//TODO need to handle recursive subpatterns
|
||||
let all_subpatterns_pass = |state: &mut State, subpatterns: &Vec<Option<Subpattern>>, items: &Vec<Node>| -> EvalResult<bool> {
|
||||
|
||||
if subpatterns.len() == 0 {
|
||||
return Ok(true)
|
||||
}
|
||||
|
||||
if items.len() != subpatterns.len() {
|
||||
return Err(format!("Subpattern length isn't correct items {} subpatterns {}", items.len(), subpatterns.len()));
|
||||
}
|
||||
|
||||
for (maybe_subp, cond) in subpatterns.iter().zip(items.iter()) {
|
||||
if let Some(subp) = maybe_subp {
|
||||
if !state.guard_passes(&subp.guard, &cond)? {
|
||||
return Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(true)
|
||||
};
|
||||
|
||||
let cond = self.expression(Node::Expr(cond))?;
|
||||
for alt in alternatives {
|
||||
// no matter what type of condition we have, ignore alternative if the guard evaluates false
|
||||
if !self.guard_passes(&alt.guard, &cond)? {
|
||||
continue;
|
||||
}
|
||||
|
||||
match cond {
|
||||
Node::PrimObject { ref tag, ref items, .. } => {
|
||||
if alt.tag.map(|t| t == *tag).unwrap_or(true) {
|
||||
let mut inner_state = self.new_frame(items, &alt.bound_vars);
|
||||
if all_subpatterns_pass(&mut inner_state, &alt.subpatterns, items)? {
|
||||
return inner_state.block(alt.item);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
},
|
||||
Node::PrimTuple { ref items } => {
|
||||
let mut inner_state = self.new_frame(items, &alt.bound_vars);
|
||||
if all_subpatterns_pass(&mut inner_state, &alt.subpatterns, items)? {
|
||||
return inner_state.block(alt.item);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
},
|
||||
Node::Expr(ref _e) => {
|
||||
if let None = alt.tag {
|
||||
return self.block(alt.item)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(format!("{:?} failed pattern match", cond))
|
||||
}
|
||||
|
||||
//TODO if I don't need to lookup by name here...
|
||||
fn value(&mut self, name: Rc<String>) -> EvalResult<Node> {
|
||||
use self::ValueEntry::*;
|
||||
use self::Func::*;
|
||||
//TODO add a layer of indirection here to talk to the symbol table first, and only then look up
|
||||
//in the values table
|
||||
|
||||
let symbol_table = self.symbol_table_handle.borrow();
|
||||
let value = symbol_table.lookup_by_name(&name);
|
||||
Ok(match value {
|
||||
Some(Symbol { name, spec, .. }) => match spec {
|
||||
//TODO I'll need this type_name later to do a table lookup
|
||||
SymbolSpec::DataConstructor { type_name: _type_name, type_args, .. } => {
|
||||
if type_args.len() == 0 {
|
||||
Node::PrimObject { name: name.clone(), tag: 0, items: vec![] }
|
||||
} else {
|
||||
return Err(format!("This data constructor thing not done"))
|
||||
}
|
||||
},
|
||||
SymbolSpec::Func(_) => match self.values.lookup(&name) {
|
||||
Some(Binding { val: Node::Expr(Expr::Func(UserDefined { name, params, body })), .. }) => {
|
||||
Node::Expr(Expr::Func(UserDefined { name: name.clone(), params: params.clone(), body: body.clone() }))
|
||||
},
|
||||
_ => unreachable!(),
|
||||
},
|
||||
SymbolSpec::RecordConstructor { .. } => return Err(format!("This shouldn't be a record!")),
|
||||
SymbolSpec::Binding => match self.values.lookup(&name) {
|
||||
Some(Binding { val, .. }) => val.clone(),
|
||||
None => return Err(format!("Symbol {} exists in symbol table but not in evaluator table", name))
|
||||
}
|
||||
},
|
||||
//TODO ideally this should be returning a runtime error if this is ever None, but it's not
|
||||
//handling all bindings correctly yet
|
||||
//None => return Err(format!("Couldn't find value {}", name)),
|
||||
None => match self.values.lookup(&name) {
|
||||
Some(Binding { val, .. }) => val.clone(),
|
||||
None => return Err(format!("Couldn't find value {}", name)),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod eval_tests {
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::symbol_table::SymbolTable;
|
||||
use crate::eval::State;
|
||||
|
||||
fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> {
|
||||
let symbol_table = Rc::new(RefCell::new(SymbolTable::new()));
|
||||
let mut state = State::new(symbol_table);
|
||||
let ast = crate::util::quick_ast(input);
|
||||
state.symbol_table_handle.borrow_mut().add_top_level_symbols(&ast).unwrap();
|
||||
let reduced = ast.reduce(&state.symbol_table_handle.borrow());
|
||||
let all_output = state.evaluate(reduced, true);
|
||||
all_output
|
||||
}
|
||||
|
||||
macro_rules! test_in_fresh_env {
|
||||
($string:expr, $correct:expr) => {
|
||||
{
|
||||
let all_output = evaluate_all_outputs($string);
|
||||
let ref output = all_output.last().unwrap();
|
||||
assert_eq!(**output, Ok($correct.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_basic_eval() {
|
||||
test_in_fresh_env!("1 + 2", "3");
|
||||
test_in_fresh_env!("let mut a = 1; a = 2", "Unit");
|
||||
test_in_fresh_env!("let mut a = 1; a = 2; a", "2");
|
||||
test_in_fresh_env!(r#"("a", 1 + 2)"#, r#"("a", 3)"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_eval() {
|
||||
test_in_fresh_env!("fn oi(x) { x + 1 }; oi(4)", "5");
|
||||
test_in_fresh_env!("fn oi(x) { x + 1 }; oi(1+2)", "4");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scopes() {
|
||||
let scope_ok = r#"
|
||||
let a = 20
|
||||
fn haha() {
|
||||
let a = 10
|
||||
a
|
||||
}
|
||||
haha()
|
||||
"#;
|
||||
test_in_fresh_env!(scope_ok, "10");
|
||||
let scope_ok = r#"
|
||||
let a = 20
|
||||
fn haha() {
|
||||
let a = 10
|
||||
a
|
||||
}
|
||||
a
|
||||
"#;
|
||||
test_in_fresh_env!(scope_ok, "20");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn if_is_patterns() {
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
let x = Some(9); if x is Some(q) then { q } else { 0 }"#;
|
||||
test_in_fresh_env!(source, "9");
|
||||
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
let x = None; if x is Some(q) then { q } else { 0 }"#;
|
||||
test_in_fresh_env!(source, "0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_if_matching() {
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
let a = None
|
||||
if a { is None -> 4, is Some(x) -> x }
|
||||
"#;
|
||||
test_in_fresh_env!(source, "4");
|
||||
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
let a = Some(99)
|
||||
if a { is None -> 4, is Some(x) -> x }
|
||||
"#;
|
||||
test_in_fresh_env!(source, "99");
|
||||
|
||||
let source = r#"
|
||||
let a = 10
|
||||
if a { is 10 -> "x", is 4 -> "y" }
|
||||
"#;
|
||||
test_in_fresh_env!(source, "\"x\"");
|
||||
|
||||
let source = r#"
|
||||
let a = 10
|
||||
if a { is 15 -> "x", is 10 -> "y" }
|
||||
"#;
|
||||
test_in_fresh_env!(source, "\"y\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn string_pattern() {
|
||||
let source = r#"
|
||||
let a = "foo"
|
||||
if a { is "foo" -> "x", is _ -> "y" }
|
||||
"#;
|
||||
test_in_fresh_env!(source, "\"x\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boolean_pattern() {
|
||||
let source = r#"
|
||||
let a = true
|
||||
if a {
|
||||
is true -> "x",
|
||||
is false -> "y"
|
||||
}
|
||||
"#;
|
||||
test_in_fresh_env!(source, "\"x\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boolean_pattern_2() {
|
||||
let source = r#"
|
||||
let a = false
|
||||
if a { is true -> "x", is false -> "y" }
|
||||
"#;
|
||||
test_in_fresh_env!(source, "\"y\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignore_pattern() {
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
if Some(10) {
|
||||
is _ -> "hella"
|
||||
}
|
||||
"#;
|
||||
test_in_fresh_env!(source, "\"hella\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_pattern() {
|
||||
let source = r#"
|
||||
if (1, 2) {
|
||||
is (1, x) -> x,
|
||||
is _ -> 99
|
||||
}
|
||||
"#;
|
||||
test_in_fresh_env!(source, 2);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn tuple_pattern_2() {
|
||||
let source = r#"
|
||||
if (1, 2) {
|
||||
is (10, x) -> x,
|
||||
is (y, x) -> x + y
|
||||
}
|
||||
"#;
|
||||
test_in_fresh_env!(source, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_pattern_3() {
|
||||
let source = r#"
|
||||
if (1, 5) {
|
||||
is (10, x) -> x,
|
||||
is (1, x) -> x
|
||||
}
|
||||
"#;
|
||||
test_in_fresh_env!(source, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_pattern_4() {
|
||||
let source = r#"
|
||||
if (1, 5) {
|
||||
is (10, x) -> x,
|
||||
is (1, x) -> x,
|
||||
}
|
||||
"#;
|
||||
test_in_fresh_env!(source, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prim_obj_pattern() {
|
||||
let source = r#"
|
||||
type Stuff = Mulch(Nat) | Jugs(Nat, String) | Mardok
|
||||
let a = Mulch(20)
|
||||
let b = Jugs(1, "haha")
|
||||
let c = Mardok
|
||||
|
||||
let x = if a {
|
||||
is Mulch(20) -> "x",
|
||||
is _ -> "ERR"
|
||||
}
|
||||
|
||||
let y = if b {
|
||||
is Mulch(n) -> "ERR",
|
||||
is Jugs(2, _) -> "ERR",
|
||||
is Jugs(1, s) -> s,
|
||||
is _ -> "ERR",
|
||||
}
|
||||
|
||||
let z = if c {
|
||||
is Jugs(_, _) -> "ERR",
|
||||
is Mardok -> "NIGH",
|
||||
is _ -> "ERR",
|
||||
}
|
||||
|
||||
(x, y, z)
|
||||
"#;
|
||||
test_in_fresh_env!(source, r#"("x", "haha", "NIGH")"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_lambda_syntax() {
|
||||
let source = r#"
|
||||
let q = \(x, y) { x * y }
|
||||
let x = q(5,2)
|
||||
let y = \(m, n, o) { m + n + o }(1,2,3)
|
||||
(x, y)
|
||||
"#;
|
||||
test_in_fresh_env!(source, r"(10, 6)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lambda_syntax_2() {
|
||||
let source = r#"
|
||||
fn milta() {
|
||||
\(x) { x + 33 }
|
||||
}
|
||||
milta()(10)
|
||||
"#;
|
||||
test_in_fresh_env!(source, "43");
|
||||
}
|
||||
}
|
||||
@@ -1,332 +0,0 @@
|
||||
#![feature(trace_macros)]
|
||||
#![feature(custom_attribute)]
|
||||
//#![feature(unrestricted_attribute_tokens)]
|
||||
#![feature(slice_patterns, box_patterns, box_syntax)]
|
||||
|
||||
//! `schala-lang` is where the Schala programming language is actually implemented.
|
||||
//! It defines the `Schala` type, which contains the state for a Schala REPL, and implements
|
||||
//! `ProgrammingLanguageInterface` and the chain of compiler passes for it.
|
||||
|
||||
extern crate itertools;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
#[macro_use]
|
||||
extern crate maplit;
|
||||
extern crate schala_repl;
|
||||
#[macro_use]
|
||||
extern crate schala_lang_codegen;
|
||||
extern crate ena;
|
||||
|
||||
use stopwatch::Stopwatch;
|
||||
|
||||
use std::time::Duration;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use itertools::Itertools;
|
||||
use schala_repl::{ProgrammingLanguageInterface,
|
||||
ComputationRequest, ComputationResponse,
|
||||
LangMetaRequest, LangMetaResponse, GlobalOutputStats,
|
||||
DebugResponse, DebugAsk};
|
||||
|
||||
macro_rules! bx {
|
||||
($e:expr) => { Box::new($e) }
|
||||
}
|
||||
|
||||
#[macro_use]
|
||||
mod util;
|
||||
#[macro_use]
|
||||
mod typechecking;
|
||||
|
||||
mod tokenizing;
|
||||
mod ast;
|
||||
mod parsing;
|
||||
mod symbol_table;
|
||||
mod builtin;
|
||||
mod reduced_ast;
|
||||
mod eval;
|
||||
|
||||
/// All bits of state necessary to parse and execute a Schala program are stored in this struct.
|
||||
/// `state` represents the execution state for the AST-walking interpreter, the other fields
|
||||
/// should be self-explanatory.
|
||||
pub struct Schala {
|
||||
source_reference: SourceReference,
|
||||
state: eval::State<'static>,
|
||||
symbol_table: Rc<RefCell<symbol_table::SymbolTable>>,
|
||||
type_context: typechecking::TypeContext<'static>,
|
||||
active_parser: Option<parsing::Parser>,
|
||||
}
|
||||
|
||||
impl Schala {
|
||||
fn handle_docs(&self, source: String) -> LangMetaResponse {
|
||||
LangMetaResponse::Docs {
|
||||
doc_string: format!("Schala item `{}` : <<Schala-lang documentation not yet implemented>>", source)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Schala {
|
||||
/// Creates a new Schala environment *without* any prelude.
|
||||
fn new_blank_env() -> Schala {
|
||||
let symbols = Rc::new(RefCell::new(symbol_table::SymbolTable::new()));
|
||||
Schala {
|
||||
source_reference: SourceReference::new(),
|
||||
symbol_table: symbols.clone(),
|
||||
state: eval::State::new(symbols),
|
||||
type_context: typechecking::TypeContext::new(),
|
||||
active_parser: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new Schala environment with the standard prelude, which is defined as ordinary
|
||||
/// Schala code in the file `prelude.schala`
|
||||
pub fn new() -> Schala {
|
||||
let prelude = include_str!("prelude.schala");
|
||||
let mut s = Schala::new_blank_env();
|
||||
|
||||
let request = ComputationRequest { source: prelude, debug_requests: HashSet::default() };
|
||||
s.run_computation(request);
|
||||
s
|
||||
}
|
||||
|
||||
fn handle_debug_immediate(&self, request: DebugAsk) -> DebugResponse {
|
||||
use DebugAsk::*;
|
||||
match request {
|
||||
Timing => DebugResponse { ask: Timing, value: format!("Invalid") },
|
||||
ByStage { stage_name } => match &stage_name[..] {
|
||||
"symbol-table" => {
|
||||
let value = self.symbol_table.borrow().debug_symbol_table();
|
||||
DebugResponse {
|
||||
ask: ByStage { stage_name: format!("symbol-table") },
|
||||
value
|
||||
}
|
||||
},
|
||||
s => {
|
||||
DebugResponse {
|
||||
ask: ByStage { stage_name: s.to_string() },
|
||||
value: format!("Not-implemented")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn tokenizing(input: &str, _handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result<Vec<tokenizing::Token>, String> {
|
||||
let tokens = tokenizing::tokenize(input);
|
||||
comp.map(|comp| {
|
||||
let token_string = tokens.iter().map(|t| t.to_string_with_metadata()).join(", ");
|
||||
comp.add_artifact(token_string);
|
||||
});
|
||||
|
||||
let errors: Vec<String> = tokens.iter().filter_map(|t| t.get_error()).collect();
|
||||
if errors.len() == 0 {
|
||||
Ok(tokens)
|
||||
} else {
|
||||
Err(format!("{:?}", errors))
|
||||
}
|
||||
}
|
||||
|
||||
fn parsing(input: Vec<tokenizing::Token>, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result<ast::AST, String> {
|
||||
use crate::parsing::Parser;
|
||||
|
||||
let mut parser = match handle.active_parser.take() {
|
||||
None => Parser::new(input),
|
||||
Some(parser) => parser
|
||||
};
|
||||
|
||||
let ast = parser.parse();
|
||||
let _trace = parser.format_parse_trace();
|
||||
|
||||
comp.map(|_comp| {
|
||||
/*
|
||||
//TODO need to control which of these debug stages get added
|
||||
let opt = comp.cur_debug_options.get(0).map(|s| s.clone());
|
||||
match opt {
|
||||
None => comp.add_artifact(TraceArtifact::new("ast", format!("{:?}", ast))),
|
||||
Some(ref s) if s == "compact" => comp.add_artifact(TraceArtifact::new("ast", format!("{:?}", ast))),
|
||||
Some(ref s) if s == "expanded" => comp.add_artifact(TraceArtifact::new("ast", format!("{:#?}", ast))),
|
||||
Some(ref s) if s == "trace" => comp.add_artifact(TraceArtifact::new_parse_trace(trace)),
|
||||
Some(ref x) => println!("Bad parsing debug option: {}", x),
|
||||
};
|
||||
*/
|
||||
});
|
||||
ast.map_err(|err| format_parse_error(err, handle))
|
||||
}
|
||||
|
||||
fn format_parse_error(error: parsing::ParseError, handle: &mut Schala) -> String {
|
||||
let line_num = error.token.line_num;
|
||||
let ch = error.token.char_num;
|
||||
let line_from_program = handle.source_reference.get_line(line_num);
|
||||
let location_pointer = format!("{}^", " ".repeat(ch));
|
||||
|
||||
let line_num_digits = format!("{}", line_num).chars().count();
|
||||
let space_padding = " ".repeat(line_num_digits);
|
||||
|
||||
format!(r#"
|
||||
{error_msg}
|
||||
{space_padding} |
|
||||
{line_num} | {}
|
||||
{space_padding} | {}
|
||||
"#, line_from_program, location_pointer, error_msg=error.msg, space_padding=space_padding, line_num=line_num)
|
||||
}
|
||||
|
||||
fn symbol_table(input: ast::AST, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result<ast::AST, String> {
|
||||
let add = handle.symbol_table.borrow_mut().add_top_level_symbols(&input);
|
||||
match add {
|
||||
Ok(()) => {
|
||||
let debug = handle.symbol_table.borrow().debug_symbol_table();
|
||||
comp.map(|comp| comp.add_artifact(debug));
|
||||
Ok(input)
|
||||
},
|
||||
Err(msg) => Err(msg)
|
||||
}
|
||||
}
|
||||
|
||||
fn typechecking(input: ast::AST, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result<ast::AST, String> {
|
||||
let result = handle.type_context.typecheck(&input);
|
||||
|
||||
comp.map(|comp| {
|
||||
comp.add_artifact(match result {
|
||||
Ok(ty) => ty.to_string(),
|
||||
Err(err) => format!("Type error: {}", err.msg)
|
||||
});
|
||||
});
|
||||
|
||||
Ok(input)
|
||||
}
|
||||
|
||||
fn ast_reducing(input: ast::AST, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result<reduced_ast::ReducedAST, String> {
|
||||
let ref symbol_table = handle.symbol_table.borrow();
|
||||
let output = input.reduce(symbol_table);
|
||||
comp.map(|comp| comp.add_artifact(format!("{:?}", output)));
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn eval(input: reduced_ast::ReducedAST, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result<String, String> {
|
||||
comp.map(|comp| comp.add_artifact(handle.state.debug_print()));
|
||||
let evaluation_outputs = handle.state.evaluate(input, true);
|
||||
let text_output: Result<Vec<String>, String> = evaluation_outputs
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
let eval_output: Result<String, String> = text_output
|
||||
.map(|v| { v.into_iter().intersperse(format!("\n")).collect() });
|
||||
eval_output
|
||||
}
|
||||
|
||||
/// Represents lines of source code
|
||||
struct SourceReference {
|
||||
lines: Option<Vec<String>>
|
||||
}
|
||||
|
||||
impl SourceReference {
|
||||
fn new() -> SourceReference {
|
||||
SourceReference { lines: None }
|
||||
}
|
||||
|
||||
fn load_new_source(&mut self, source: &str) {
|
||||
//TODO this is a lot of heap allocations - maybe there's a way to make it more efficient?
|
||||
self.lines = Some(source.lines().map(|s| s.to_string()).collect()); }
|
||||
|
||||
fn get_line(&self, line: usize) -> String {
|
||||
self.lines.as_ref().and_then(|x| x.get(line).map(|s| s.to_string())).unwrap_or(format!("NO LINE FOUND"))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct PassDebugArtifact {
|
||||
artifacts: Vec<String>
|
||||
}
|
||||
impl PassDebugArtifact {
|
||||
fn add_artifact(&mut self, artifact: String) {
|
||||
self.artifacts.push(artifact)
|
||||
}
|
||||
}
|
||||
|
||||
fn stage_names() -> Vec<&'static str> {
|
||||
vec![
|
||||
"tokenizing",
|
||||
"parsing",
|
||||
"symbol-table",
|
||||
"typechecking",
|
||||
"ast-reduction",
|
||||
"ast-walking-evaluation"
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
impl ProgrammingLanguageInterface for Schala {
|
||||
fn get_language_name(&self) -> String { format!("Schala") }
|
||||
fn get_source_file_suffix(&self) -> String { format!("schala") }
|
||||
|
||||
fn run_computation(&mut self, request: ComputationRequest) -> ComputationResponse {
|
||||
struct PassToken<'a> {
|
||||
schala: &'a mut Schala,
|
||||
stage_durations: &'a mut Vec<(String, Duration)>,
|
||||
sw: &'a Stopwatch,
|
||||
debug_requests: &'a HashSet<DebugAsk>,
|
||||
debug_responses: &'a mut Vec<DebugResponse>,
|
||||
}
|
||||
|
||||
fn output_wrapper<Input, Output, F>(n: usize, func: F, input: Input, tok: &mut PassToken) -> Result<Output, String>
|
||||
where F: Fn(Input, &mut Schala, Option<&mut PassDebugArtifact>) -> Result<Output, String>
|
||||
{
|
||||
let stage_names = stage_names();
|
||||
let mut debug_artifact = if tok.debug_requests.contains(&DebugAsk::ByStage { stage_name: stage_names[n].to_string() }) {
|
||||
Some(PassDebugArtifact::default())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let output = func(input, tok.schala, debug_artifact.as_mut());
|
||||
tok.stage_durations.push((stage_names[n].to_string(), tok.sw.elapsed()));
|
||||
if let Some(artifact) = debug_artifact {
|
||||
for value in artifact.artifacts.into_iter() {
|
||||
let resp = DebugResponse {
|
||||
ask: DebugAsk::ByStage { stage_name: stage_names[n].to_string() },
|
||||
value,
|
||||
};
|
||||
tok.debug_responses.push(resp);
|
||||
}
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
let ComputationRequest { source, debug_requests } = request;
|
||||
self.source_reference.load_new_source(source);
|
||||
let sw = Stopwatch::start_new();
|
||||
let mut stage_durations = Vec::new();
|
||||
let mut debug_responses = Vec::new();
|
||||
let mut tok = PassToken { schala: self, stage_durations: &mut stage_durations, sw: &sw, debug_requests: &debug_requests, debug_responses: &mut debug_responses };
|
||||
|
||||
let main_output: Result<String, String> = Ok(source)
|
||||
.and_then(|source| output_wrapper(0, tokenizing, source, &mut tok))
|
||||
.and_then(|tokens| output_wrapper(1, parsing, tokens, &mut tok))
|
||||
.and_then(|ast| output_wrapper(2, symbol_table, ast, &mut tok))
|
||||
.and_then(|ast| output_wrapper(3, typechecking, ast, &mut tok))
|
||||
.and_then(|ast| output_wrapper(4, ast_reducing, ast, &mut tok))
|
||||
.and_then(|reduced_ast| output_wrapper(5, eval, reduced_ast, &mut tok));
|
||||
|
||||
let total_duration = sw.elapsed();
|
||||
let global_output_stats = GlobalOutputStats {
|
||||
total_duration, stage_durations
|
||||
};
|
||||
|
||||
ComputationResponse {
|
||||
main_output,
|
||||
global_output_stats,
|
||||
debug_responses,
|
||||
}
|
||||
}
|
||||
|
||||
fn request_meta(&mut self, request: LangMetaRequest) -> LangMetaResponse {
|
||||
match request {
|
||||
LangMetaRequest::StageNames => LangMetaResponse::StageNames(stage_names().iter().map(|s| s.to_string()).collect()),
|
||||
LangMetaRequest::Docs { source } => self.handle_docs(source),
|
||||
LangMetaRequest::ImmediateDebug(debug_request) =>
|
||||
LangMetaResponse::ImmediateDebug(self.handle_debug_immediate(debug_request)),
|
||||
LangMetaRequest::Custom { .. } => LangMetaResponse::Custom { kind: format!("not-implemented"), value: format!("") }
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,13 +0,0 @@
|
||||
|
||||
type Option<T> = Some(T) | None
|
||||
type Color = Red | Green | Blue
|
||||
type Ord = LT | EQ | GT
|
||||
|
||||
|
||||
fn map(input: Option<T>, func: Func): Option<T> {
|
||||
if input {
|
||||
is Some(x) -> Some(func(x)),
|
||||
is None -> None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,397 +0,0 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::ast::*;
|
||||
use crate::symbol_table::{Symbol, SymbolSpec, SymbolTable};
|
||||
use crate::builtin::{BinOp, PrefixOp};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ReducedAST(pub Vec<Stmt>);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Stmt {
|
||||
PreBinding {
|
||||
name: Rc<String>,
|
||||
func: Func,
|
||||
},
|
||||
Binding {
|
||||
name: Rc<String>,
|
||||
constant: bool,
|
||||
expr: Expr,
|
||||
},
|
||||
Expr(Expr),
|
||||
Noop,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Expr {
|
||||
Unit,
|
||||
Lit(Lit),
|
||||
Tuple(Vec<Expr>),
|
||||
Func(Func),
|
||||
Val(Rc<String>),
|
||||
Constructor {
|
||||
type_name: Rc<String>,
|
||||
name: Rc<String>,
|
||||
tag: usize,
|
||||
arity: usize,
|
||||
},
|
||||
Call {
|
||||
f: Box<Expr>,
|
||||
args: Vec<Expr>,
|
||||
},
|
||||
Assign {
|
||||
val: Box<Expr>,
|
||||
expr: Box<Expr>,
|
||||
},
|
||||
Conditional {
|
||||
cond: Box<Expr>,
|
||||
then_clause: Vec<Stmt>,
|
||||
else_clause: Vec<Stmt>,
|
||||
},
|
||||
ConditionalTargetSigilValue,
|
||||
CaseMatch {
|
||||
cond: Box<Expr>,
|
||||
alternatives: Vec<Alternative>
|
||||
},
|
||||
UnimplementedSigilValue
|
||||
}
|
||||
|
||||
pub type BoundVars = Vec<Option<Rc<String>>>; //remember that order matters here
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Alternative {
|
||||
pub tag: Option<usize>,
|
||||
pub subpatterns: Vec<Option<Subpattern>>,
|
||||
pub guard: Option<Expr>,
|
||||
pub bound_vars: BoundVars,
|
||||
pub item: Vec<Stmt>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Subpattern {
|
||||
pub tag: Option<usize>,
|
||||
pub subpatterns: Vec<Option<Subpattern>>,
|
||||
pub bound_vars: BoundVars,
|
||||
pub guard: Option<Expr>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Lit {
|
||||
Nat(u64),
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
Bool(bool),
|
||||
StringLit(Rc<String>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Func {
|
||||
BuiltIn(Rc<String>),
|
||||
UserDefined {
|
||||
name: Option<Rc<String>>,
|
||||
params: Vec<Rc<String>>,
|
||||
body: Vec<Stmt>,
|
||||
}
|
||||
}
|
||||
|
||||
impl AST {
|
||||
pub fn reduce(&self, symbol_table: &SymbolTable) -> ReducedAST {
|
||||
let mut output = vec![];
|
||||
for statement in self.0.iter() {
|
||||
output.push(statement.node().reduce(symbol_table));
|
||||
}
|
||||
ReducedAST(output)
|
||||
}
|
||||
}
|
||||
|
||||
impl Statement {
|
||||
fn reduce(&self, symbol_table: &SymbolTable) -> Stmt {
|
||||
use crate::ast::Statement::*;
|
||||
match self {
|
||||
ExpressionStatement(expr) => Stmt::Expr(expr.node().reduce(symbol_table)),
|
||||
Declaration(decl) => decl.reduce(symbol_table),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn reduce_block(block: &Block, symbol_table: &SymbolTable) -> Vec<Stmt> {
|
||||
block.iter().map(|stmt| stmt.node().reduce(symbol_table)).collect()
|
||||
}
|
||||
|
||||
impl Expression {
|
||||
fn reduce(&self, symbol_table: &SymbolTable) -> Expr {
|
||||
use crate::ast::ExpressionKind::*;
|
||||
let ref input = self.0;
|
||||
match input {
|
||||
NatLiteral(n) => Expr::Lit(Lit::Nat(*n)),
|
||||
FloatLiteral(f) => Expr::Lit(Lit::Float(*f)),
|
||||
StringLiteral(s) => Expr::Lit(Lit::StringLit(s.clone())),
|
||||
BoolLiteral(b) => Expr::Lit(Lit::Bool(*b)),
|
||||
BinExp(binop, lhs, rhs) => binop.reduce(symbol_table, lhs, rhs),
|
||||
PrefixExp(op, arg) => op.reduce(symbol_table, arg),
|
||||
Value(name) => match symbol_table.lookup_by_name(name) {
|
||||
Some(Symbol { spec: SymbolSpec::DataConstructor { index, type_args, type_name}, .. }) => Expr::Constructor {
|
||||
type_name: type_name.clone(),
|
||||
name: name.clone(),
|
||||
tag: index.clone(),
|
||||
arity: type_args.len(),
|
||||
},
|
||||
_ => Expr::Val(name.clone()),
|
||||
},
|
||||
Call { f, arguments } => Expr::Call {
|
||||
f: Box::new(f.node().reduce(symbol_table)),
|
||||
args: arguments.iter().map(|arg| arg.node().reduce(symbol_table)).collect(),
|
||||
},
|
||||
TupleLiteral(exprs) => Expr::Tuple(exprs.iter().map(|e| e.node().reduce(symbol_table)).collect()),
|
||||
IfExpression { discriminator, body } => reduce_if_expression(discriminator, body, symbol_table),
|
||||
Lambda { params, body, .. } => reduce_lambda(params, body, symbol_table),
|
||||
NamedStruct { .. } => Expr::UnimplementedSigilValue,
|
||||
Index { .. } => Expr::UnimplementedSigilValue,
|
||||
WhileExpression { .. } => Expr::UnimplementedSigilValue,
|
||||
ForExpression { .. } => Expr::UnimplementedSigilValue,
|
||||
ListLiteral { .. } => Expr::UnimplementedSigilValue,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn reduce_lambda(params: &Vec<FormalParam>, body: &Block, symbol_table: &SymbolTable) -> Expr {
|
||||
Expr::Func(Func::UserDefined {
|
||||
name: None,
|
||||
params: params.iter().map(|param| param.0.clone()).collect(),
|
||||
body: reduce_block(body, symbol_table),
|
||||
})
|
||||
}
|
||||
|
||||
fn reduce_if_expression(discriminator: &Discriminator, body: &IfExpressionBody, symbol_table: &SymbolTable) -> Expr {
|
||||
let cond = Box::new(match *discriminator {
|
||||
Discriminator::Simple(ref expr) => expr.reduce(symbol_table),
|
||||
Discriminator::BinOp(ref _expr, ref _binop) => panic!("Can't yet handle binop discriminators")
|
||||
});
|
||||
match *body {
|
||||
IfExpressionBody::SimpleConditional(ref then_clause, ref else_clause) => {
|
||||
let then_clause = reduce_block(then_clause, symbol_table);
|
||||
let else_clause = match else_clause {
|
||||
None => vec![],
|
||||
Some(stmts) => reduce_block(stmts, symbol_table),
|
||||
};
|
||||
Expr::Conditional { cond, then_clause, else_clause }
|
||||
},
|
||||
IfExpressionBody::SimplePatternMatch(ref pat, ref then_clause, ref else_clause) => {
|
||||
let then_clause = reduce_block(then_clause, symbol_table);
|
||||
let else_clause = match else_clause {
|
||||
None => vec![],
|
||||
Some(stmts) => reduce_block(stmts, symbol_table),
|
||||
};
|
||||
|
||||
let alternatives = vec![
|
||||
pat.to_alternative(then_clause, symbol_table),
|
||||
Alternative {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
bound_vars: vec![],
|
||||
guard: None,
|
||||
item: else_clause
|
||||
},
|
||||
];
|
||||
|
||||
Expr::CaseMatch {
|
||||
cond,
|
||||
alternatives,
|
||||
}
|
||||
},
|
||||
IfExpressionBody::GuardList(ref guard_arms) => {
|
||||
let mut alternatives = vec![];
|
||||
for arm in guard_arms {
|
||||
match arm.guard {
|
||||
Guard::Pat(ref p) => {
|
||||
let item = reduce_block(&arm.body, symbol_table);
|
||||
let alt = p.to_alternative(item, symbol_table);
|
||||
alternatives.push(alt);
|
||||
},
|
||||
Guard::HalfExpr(HalfExpr { op: _, expr: _ }) => {
|
||||
return Expr::UnimplementedSigilValue
|
||||
}
|
||||
}
|
||||
}
|
||||
Expr::CaseMatch { cond, alternatives }
|
||||
}
|
||||
}
|
||||
}
|
||||
/* ig var pat
|
||||
* x is SomeBigOldEnum(_, x, Some(t))
|
||||
*/
|
||||
|
||||
fn handle_symbol(symbol: Option<&Symbol>, inner_patterns: &Vec<Pattern>, symbol_table: &SymbolTable) -> Subpattern {
|
||||
use self::Pattern::*;
|
||||
let tag = symbol.map(|symbol| match symbol.spec {
|
||||
SymbolSpec::DataConstructor { index, .. } => index.clone(),
|
||||
_ => panic!("Symbol is not a data constructor - this should've been caught in type-checking"),
|
||||
});
|
||||
let bound_vars = inner_patterns.iter().map(|p| match p {
|
||||
Literal(PatternLiteral::VarPattern(var)) => Some(var.clone()),
|
||||
_ => None,
|
||||
}).collect();
|
||||
|
||||
let subpatterns = inner_patterns.iter().map(|p| match p {
|
||||
Ignored => None,
|
||||
Literal(PatternLiteral::VarPattern(_)) => None,
|
||||
Literal(other) => Some(other.to_subpattern(symbol_table)),
|
||||
tp @ TuplePattern(_) => Some(tp.to_subpattern(symbol_table)),
|
||||
ts @ TupleStruct(_, _) => Some(ts.to_subpattern(symbol_table)),
|
||||
Record(..) => unimplemented!(),
|
||||
}).collect();
|
||||
|
||||
let guard = None;
|
||||
/*
|
||||
let guard_equality_exprs: Vec<Expr> = subpatterns.iter().map(|p| match p {
|
||||
Literal(lit) => match lit {
|
||||
_ => unimplemented!()
|
||||
},
|
||||
_ => unimplemented!()
|
||||
}).collect();
|
||||
*/
|
||||
|
||||
Subpattern {
|
||||
tag,
|
||||
subpatterns,
|
||||
guard,
|
||||
bound_vars,
|
||||
}
|
||||
}
|
||||
|
||||
impl Pattern {
|
||||
fn to_alternative(&self, item: Vec<Stmt>, symbol_table: &SymbolTable) -> Alternative {
|
||||
let s = self.to_subpattern(symbol_table);
|
||||
Alternative {
|
||||
tag: s.tag,
|
||||
subpatterns: s.subpatterns,
|
||||
bound_vars: s.bound_vars,
|
||||
guard: s.guard,
|
||||
item
|
||||
}
|
||||
}
|
||||
|
||||
fn to_subpattern(&self, symbol_table: &SymbolTable) -> Subpattern {
|
||||
use self::Pattern::*;
|
||||
match self {
|
||||
TupleStruct(name, inner_patterns) => {
|
||||
let symbol = symbol_table.lookup_by_name(name).expect(&format!("Symbol {} not found", name));
|
||||
handle_symbol(Some(symbol), inner_patterns, symbol_table)
|
||||
},
|
||||
TuplePattern(inner_patterns) => handle_symbol(None, inner_patterns, symbol_table),
|
||||
Record(_name, _pairs) => {
|
||||
unimplemented!()
|
||||
},
|
||||
Ignored => Subpattern { tag: None, subpatterns: vec![], guard: None, bound_vars: vec![] },
|
||||
Literal(lit) => lit.to_subpattern(symbol_table),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PatternLiteral {
|
||||
fn to_subpattern(&self, symbol_table: &SymbolTable) -> Subpattern {
|
||||
use self::PatternLiteral::*;
|
||||
match self {
|
||||
NumPattern { neg, num } => {
|
||||
let comparison = Expr::Lit(match (neg, num) {
|
||||
(false, ExpressionKind::NatLiteral(n)) => Lit::Nat(*n),
|
||||
(false, ExpressionKind::FloatLiteral(f)) => Lit::Float(*f),
|
||||
(true, ExpressionKind::NatLiteral(n)) => Lit::Int(-1*(*n as i64)),
|
||||
(true, ExpressionKind::FloatLiteral(f)) => Lit::Float(-1.0*f),
|
||||
_ => panic!("This should never happen")
|
||||
});
|
||||
let guard = Some(Expr::Call {
|
||||
f: Box::new(Expr::Func(Func::BuiltIn(Rc::new("==".to_string())))),
|
||||
args: vec![comparison, Expr::ConditionalTargetSigilValue],
|
||||
});
|
||||
Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
guard,
|
||||
bound_vars: vec![],
|
||||
}
|
||||
},
|
||||
StringPattern(s) => {
|
||||
let guard = Some(Expr::Call {
|
||||
f: Box::new(Expr::Func(Func::BuiltIn(Rc::new("==".to_string())))),
|
||||
args: vec![Expr::Lit(Lit::StringLit(s.clone())), Expr::ConditionalTargetSigilValue]
|
||||
});
|
||||
|
||||
Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
guard,
|
||||
bound_vars: vec![],
|
||||
}
|
||||
},
|
||||
BoolPattern(b) => {
|
||||
let guard = Some(if *b {
|
||||
Expr::ConditionalTargetSigilValue
|
||||
} else {
|
||||
Expr::Call {
|
||||
f: Box::new(Expr::Func(Func::BuiltIn(Rc::new("!".to_string())))),
|
||||
args: vec![Expr::ConditionalTargetSigilValue]
|
||||
}
|
||||
});
|
||||
Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
guard,
|
||||
bound_vars: vec![],
|
||||
}
|
||||
},
|
||||
VarPattern(var) => match symbol_table.lookup_by_name(var) {
|
||||
Some(symbol) => handle_symbol(Some(symbol), &vec![], symbol_table),
|
||||
None => Subpattern {
|
||||
tag: None,
|
||||
subpatterns: vec![],
|
||||
guard: None,
|
||||
bound_vars: vec![Some(var.clone())],
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Declaration {
|
||||
fn reduce(&self, symbol_table: &SymbolTable) -> Stmt {
|
||||
use self::Declaration::*;
|
||||
match self {
|
||||
Binding {name, constant, expr, .. } => Stmt::Binding { name: name.clone(), constant: *constant, expr: expr.node().reduce(symbol_table) },
|
||||
FuncDecl(Signature { name, params, .. }, statements) => Stmt::PreBinding {
|
||||
name: name.clone(),
|
||||
func: Func::UserDefined {
|
||||
name: Some(name.clone()),
|
||||
params: params.iter().map(|param| param.0.clone()).collect(),
|
||||
body: reduce_block(&statements, symbol_table),
|
||||
}
|
||||
},
|
||||
TypeDecl { .. } => Stmt::Noop,
|
||||
TypeAlias(_, _) => Stmt::Noop,
|
||||
Interface { .. } => Stmt::Noop,
|
||||
Impl { .. } => Stmt::Expr(Expr::UnimplementedSigilValue),
|
||||
_ => Stmt::Expr(Expr::UnimplementedSigilValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BinOp {
|
||||
fn reduce(&self, symbol_table: &SymbolTable, lhs: &Box<Meta<Expression>>, rhs: &Box<Meta<Expression>>) -> Expr {
|
||||
if **self.sigil() == "=" {
|
||||
Expr::Assign {
|
||||
val: Box::new(lhs.node().reduce(symbol_table)),
|
||||
expr: Box::new(rhs.node().reduce(symbol_table)),
|
||||
}
|
||||
} else {
|
||||
let f = Box::new(Expr::Func(Func::BuiltIn(self.sigil().clone())));
|
||||
Expr::Call { f, args: vec![lhs.node().reduce(symbol_table), rhs.node().reduce(symbol_table)]}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PrefixOp {
|
||||
fn reduce(&self, symbol_table: &SymbolTable, arg: &Box<Meta<Expression>>) -> Expr {
|
||||
let f = Box::new(Expr::Func(Func::BuiltIn(self.sigil().clone())));
|
||||
Expr::Call { f, args: vec![arg.node().reduce(symbol_table)]}
|
||||
}
|
||||
}
|
||||
@@ -1,391 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
use std::collections::hash_map::Entry;
|
||||
use std::rc::Rc;
|
||||
use std::fmt;
|
||||
use std::fmt::Write;
|
||||
|
||||
use crate::ast;
|
||||
use crate::ast::{Meta, TypeBody, TypeSingletonName, Signature, Statement};
|
||||
use crate::typechecking::TypeName;
|
||||
|
||||
type LineNumber = u32;
|
||||
type SymbolTrackTable = HashMap<Rc<String>, LineNumber>;
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Debug)]
|
||||
struct PathToSymbol(Vec<Rc<String>>);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct ScopeSegment {
|
||||
scope_name: Rc<String>,
|
||||
scope_type: ScopeSegmentKind,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum ScopeSegmentKind {
|
||||
Function,
|
||||
//Type,
|
||||
}
|
||||
|
||||
//cf. p. 150 or so of Language Implementation Patterns
|
||||
pub struct SymbolTable {
|
||||
values: HashMap<PathToSymbol, Symbol>,
|
||||
}
|
||||
|
||||
//TODO add various types of lookups here, maybe multiple hash tables internally?
|
||||
impl SymbolTable {
|
||||
pub fn new() -> SymbolTable {
|
||||
SymbolTable {
|
||||
values: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_new_symbol(&mut self, name: &Rc<String>, scope_path: &Vec<ScopeSegment>, spec: SymbolSpec) {
|
||||
let mut vec: Vec<Rc<String>> = scope_path.iter().map(|segment| segment.scope_name.clone()).collect();
|
||||
vec.push(name.clone());
|
||||
let symbol_path = PathToSymbol(vec);
|
||||
let symbol = Symbol { name: name.clone(), scopes: scope_path.to_vec(), spec };
|
||||
self.values.insert(symbol_path, symbol);
|
||||
}
|
||||
|
||||
pub fn lookup_by_name(&self, name: &Rc<String>) -> Option<&Symbol> {
|
||||
self.lookup_by_path(name, &vec![])
|
||||
}
|
||||
|
||||
pub fn lookup_by_path(&self, name: &Rc<String>, path: &Vec<Rc<String>>) -> Option<&Symbol> {
|
||||
let mut vec = path.clone();
|
||||
vec.push(name.clone());
|
||||
let symbol_path = PathToSymbol(vec);
|
||||
self.values.get(&symbol_path)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Symbol {
|
||||
pub name: Rc<String>, //TODO does this need to be pub?
|
||||
scopes: Vec<ScopeSegment>,
|
||||
pub spec: SymbolSpec,
|
||||
}
|
||||
|
||||
impl fmt::Display for Symbol {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "<Name: {}, Spec: {}>", self.name, self.spec)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum SymbolSpec {
|
||||
Func(Vec<TypeName>),
|
||||
DataConstructor {
|
||||
index: usize,
|
||||
type_name: Rc<String>,
|
||||
type_args: Vec<Rc<String>>,
|
||||
},
|
||||
RecordConstructor {
|
||||
fields: HashMap<Rc<String>, Rc<String>>
|
||||
},
|
||||
Binding
|
||||
}
|
||||
|
||||
impl fmt::Display for SymbolSpec {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use self::SymbolSpec::*;
|
||||
match self {
|
||||
Func(type_names) => write!(f, "Func({:?})", type_names),
|
||||
DataConstructor { index, type_name, type_args } => write!(f, "DataConstructor(idx: {})({:?} -> {})", index, type_args, type_name),
|
||||
RecordConstructor { fields: _fields } => write!(f, "RecordConstructor( <fields> )"),
|
||||
Binding => write!(f, "Binding"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SymbolTable {
|
||||
/* note: this adds names for *forward reference* but doesn't actually create any types. solve that problem
|
||||
* later */
|
||||
|
||||
pub fn add_top_level_symbols(&mut self, ast: &ast::AST) -> Result<(), String> {
|
||||
let mut scope_name_stack = Vec::new();
|
||||
self.add_symbols_from_scope(&ast.0, &mut scope_name_stack)
|
||||
}
|
||||
|
||||
fn add_symbols_from_scope<'a>(&'a mut self, statements: &Vec<Meta<Statement>>, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
|
||||
use self::ast::Declaration::*;
|
||||
|
||||
fn insert_and_check_duplicate_symbol(table: &mut SymbolTrackTable, name: &Rc<String>) -> Result<(), String> {
|
||||
match table.entry(name.clone()) {
|
||||
Entry::Occupied(o) => {
|
||||
let line_number = o.get(); //TODO make this actually work
|
||||
Err(format!("Duplicate definition: {}. It's already defined at {}", name, line_number))
|
||||
},
|
||||
Entry::Vacant(v) => {
|
||||
let line_number = 0; //TODO should work
|
||||
v.insert(line_number);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut seen_identifiers: SymbolTrackTable = HashMap::new();
|
||||
|
||||
for meta in statements.iter() {
|
||||
let statement = meta.node();
|
||||
if let Statement::Declaration(decl) = statement {
|
||||
match decl {
|
||||
FuncSig(ref signature) => {
|
||||
insert_and_check_duplicate_symbol(&mut seen_identifiers, &signature.name)?;
|
||||
self.add_function_signature(signature, scope_name_stack)?
|
||||
}
|
||||
FuncDecl(ref signature, ref body) => {
|
||||
insert_and_check_duplicate_symbol(&mut seen_identifiers, &signature.name)?;
|
||||
self.add_function_signature(signature, scope_name_stack)?;
|
||||
scope_name_stack.push(ScopeSegment{
|
||||
scope_name: signature.name.clone(),
|
||||
scope_type: ScopeSegmentKind::Function,
|
||||
});
|
||||
let output = self.add_symbols_from_scope(body, scope_name_stack);
|
||||
let _ = scope_name_stack.pop();
|
||||
output?
|
||||
},
|
||||
TypeDecl { name, body, mutable } => {
|
||||
insert_and_check_duplicate_symbol(&mut seen_identifiers, &name.name)?;
|
||||
self.add_type_decl(name, body, mutable, scope_name_stack)?
|
||||
},
|
||||
Binding { name, .. } => {
|
||||
insert_and_check_duplicate_symbol(&mut seen_identifiers, name)?;
|
||||
self.add_new_symbol(name, scope_name_stack, SymbolSpec::Binding);
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn debug_symbol_table(&self) -> String {
|
||||
let mut output = format!("Symbol table\n");
|
||||
for (name, sym) in &self.values {
|
||||
write!(output, "{:?} -> {}\n", name, sym).unwrap();
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
fn add_function_signature(&mut self, signature: &Signature, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
|
||||
let mut local_type_context = LocalTypeContext::new();
|
||||
let types = signature.params.iter().map(|param| match param {
|
||||
(_, Some(type_identifier)) => Rc::new(format!("{:?}", type_identifier)),
|
||||
(_, None) => local_type_context.new_universal_type()
|
||||
}).collect();
|
||||
self.add_new_symbol(&signature.name, scope_name_stack, SymbolSpec::Func(types));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_type_decl(&mut self, type_name: &TypeSingletonName, body: &TypeBody, _mutable: &bool, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
|
||||
use crate::ast::{TypeIdentifier, Variant};
|
||||
let TypeBody(variants) = body;
|
||||
let TypeSingletonName { name, .. } = type_name;
|
||||
//scope_name_stack.push(name.clone()); //TODO adding this makes variants scoped under their
|
||||
//type name and breaks a lot of things - don't add it until importing names works
|
||||
//TODO figure out why _params isn't being used here
|
||||
for (index, var) in variants.iter().enumerate() {
|
||||
match var {
|
||||
Variant::UnitStruct(variant_name) => {
|
||||
let spec = SymbolSpec::DataConstructor {
|
||||
index,
|
||||
type_name: name.clone(),
|
||||
type_args: vec![],
|
||||
};
|
||||
self.add_new_symbol(variant_name, scope_name_stack, spec);
|
||||
},
|
||||
Variant::TupleStruct(variant_name, tuple_members) => {
|
||||
let type_args = tuple_members.iter().map(|type_name| match type_name {
|
||||
TypeIdentifier::Singleton(TypeSingletonName { name, ..}) => name.clone(),
|
||||
TypeIdentifier::Tuple(_) => unimplemented!(),
|
||||
}).collect();
|
||||
let spec = SymbolSpec::DataConstructor {
|
||||
index,
|
||||
type_name: name.clone(),
|
||||
type_args
|
||||
};
|
||||
self.add_new_symbol(variant_name, scope_name_stack, spec);
|
||||
},
|
||||
//TODO if there is only one variant, and it is a record, it doesn't need to have an
|
||||
//explicit name
|
||||
Variant::Record { name, members: _members } => {
|
||||
let fields = HashMap::new();
|
||||
let spec = SymbolSpec::RecordConstructor { fields };
|
||||
self.add_new_symbol(name, scope_name_stack, spec);
|
||||
},
|
||||
}
|
||||
}
|
||||
//scope_name_stack.pop();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct LocalTypeContext {
|
||||
state: u8
|
||||
}
|
||||
impl LocalTypeContext {
|
||||
fn new() -> LocalTypeContext {
|
||||
LocalTypeContext { state: 0 }
|
||||
}
|
||||
|
||||
fn new_universal_type(&mut self) -> TypeName {
|
||||
let n = self.state;
|
||||
self.state += 1;
|
||||
Rc::new(format!("{}", (('a' as u8) + n) as char))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod symbol_table_tests {
|
||||
use super::*;
|
||||
use crate::util::quick_ast;
|
||||
|
||||
macro_rules! values_in_table {
|
||||
//TODO multiple values
|
||||
($source:expr, $single_value:expr) => {
|
||||
{
|
||||
let mut symbol_table = SymbolTable::new();
|
||||
let ast = quick_ast($source);
|
||||
symbol_table.add_top_level_symbols(&ast).unwrap();
|
||||
match symbol_table.lookup_by_name($single_value) {
|
||||
Some(_spec) => (),
|
||||
None => panic!(),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_symbol_table() {
|
||||
values_in_table! { "let a = 10; fn b() { 20 }", &rc!(b) };
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_duplicates() {
|
||||
let source = r#"
|
||||
fn a() { 1 }
|
||||
fn b() { 2 }
|
||||
fn a() { 3 }
|
||||
"#;
|
||||
let mut symbol_table = SymbolTable::new();
|
||||
let ast = quick_ast(source);
|
||||
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
|
||||
assert!(output.contains("Duplicate"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_duplicates_2() {
|
||||
let source = r#"
|
||||
let a = 20;
|
||||
let q = 39;
|
||||
let a = 30;
|
||||
"#;
|
||||
let mut symbol_table = SymbolTable::new();
|
||||
let ast = quick_ast(source);
|
||||
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
|
||||
assert!(output.contains("Duplicate"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_duplicates_3() {
|
||||
let source = r#"
|
||||
fn a() {
|
||||
let a = 20
|
||||
let b = 40
|
||||
a + b
|
||||
}
|
||||
|
||||
fn q() {
|
||||
let x = 30
|
||||
let x = 33
|
||||
}
|
||||
"#;
|
||||
let mut symbol_table = SymbolTable::new();
|
||||
let ast = quick_ast(source);
|
||||
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
|
||||
assert!(output.contains("Duplicate"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dont_falsely_detect_duplicates() {
|
||||
let source = r#"
|
||||
let a = 20;
|
||||
fn some_func() {
|
||||
let a = 40;
|
||||
77
|
||||
}
|
||||
let q = 39;
|
||||
"#;
|
||||
let mut symbol_table = SymbolTable::new();
|
||||
let ast = quick_ast(source);
|
||||
symbol_table.add_top_level_symbols(&ast).unwrap();
|
||||
assert!(symbol_table.lookup_by_path(&rc!(a), &vec![]).is_some());
|
||||
assert!(symbol_table.lookup_by_path(&rc!(a), &vec![rc!(some_func)]).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enclosing_scopes() {
|
||||
let source = r#"
|
||||
fn outer_func(x) {
|
||||
fn inner_func(arg) {
|
||||
arg
|
||||
}
|
||||
x + inner_func(x)
|
||||
}"#;
|
||||
let mut symbol_table = SymbolTable::new();
|
||||
let ast = quick_ast(source);
|
||||
symbol_table.add_top_level_symbols(&ast).unwrap();
|
||||
assert!(symbol_table.lookup_by_path(&rc!(outer_func), &vec![]).is_some());
|
||||
assert!(symbol_table.lookup_by_path(&rc!(inner_func), &vec![rc!(outer_func)]).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enclosing_scopes_2() {
|
||||
let source = r#"
|
||||
fn outer_func(x) {
|
||||
fn inner_func(arg) {
|
||||
arg
|
||||
}
|
||||
|
||||
fn second_inner_func() {
|
||||
fn another_inner_func() {
|
||||
}
|
||||
}
|
||||
|
||||
inner_func(x)
|
||||
}"#;
|
||||
let mut symbol_table = SymbolTable::new();
|
||||
let ast = quick_ast(source);
|
||||
symbol_table.add_top_level_symbols(&ast).unwrap();
|
||||
println!("{}", symbol_table.debug_symbol_table());
|
||||
assert!(symbol_table.lookup_by_path(&rc!(outer_func), &vec![]).is_some());
|
||||
assert!(symbol_table.lookup_by_path(&rc!(inner_func), &vec![rc!(outer_func)]).is_some());
|
||||
assert!(symbol_table.lookup_by_path(&rc!(second_inner_func), &vec![rc!(outer_func)]).is_some());
|
||||
assert!(symbol_table.lookup_by_path(&rc!(another_inner_func), &vec![rc!(outer_func), rc!(second_inner_func)]).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enclosing_scopes_3() {
|
||||
let source = r#"
|
||||
fn outer_func(x) {
|
||||
fn inner_func(arg) {
|
||||
arg
|
||||
}
|
||||
|
||||
fn second_inner_func() {
|
||||
fn another_inner_func() {
|
||||
}
|
||||
fn another_inner_func() {
|
||||
}
|
||||
}
|
||||
|
||||
inner_func(x)
|
||||
}"#;
|
||||
let mut symbol_table = SymbolTable::new();
|
||||
let ast = quick_ast(source);
|
||||
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
|
||||
assert!(output.contains("Duplicate"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,472 +0,0 @@
|
||||
use std::rc::Rc;
|
||||
use std::fmt::Write;
|
||||
|
||||
use ena::unify::{UnifyKey, InPlaceUnificationTable, UnificationTable, EqUnifyValue};
|
||||
|
||||
use crate::ast::*;
|
||||
use crate::util::ScopeStack;
|
||||
use crate::builtin::{PrefixOp, BinOp};
|
||||
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct TypeData {
|
||||
ty: Option<Type>
|
||||
}
|
||||
|
||||
impl TypeData {
|
||||
pub fn new() -> TypeData {
|
||||
TypeData { ty: None }
|
||||
}
|
||||
}
|
||||
|
||||
pub type TypeName = Rc<String>;
|
||||
|
||||
pub struct TypeContext<'a> {
|
||||
variable_map: ScopeStack<'a, Rc<String>, Type>,
|
||||
unification_table: InPlaceUnificationTable<TypeVar>,
|
||||
}
|
||||
|
||||
/// `InferResult` is the monad in which type inference takes place.
|
||||
type InferResult<T> = Result<T, TypeError>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TypeError { pub msg: String }
|
||||
|
||||
impl TypeError {
|
||||
fn new<A, T>(msg: T) -> InferResult<A> where T: Into<String> {
|
||||
Err(TypeError { msg: msg.into() })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Type {
|
||||
Const(TypeConst),
|
||||
Var(TypeVar),
|
||||
Arrow {
|
||||
params: Vec<Type>,
|
||||
ret: Box<Type>
|
||||
},
|
||||
Compound {
|
||||
ty_name: String,
|
||||
args:Vec<Type>
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct TypeVar(usize);
|
||||
|
||||
impl UnifyKey for TypeVar {
|
||||
type Value = Option<TypeConst>;
|
||||
fn index(&self) -> u32 { self.0 as u32 }
|
||||
fn from_index(u: u32) -> TypeVar { TypeVar(u as usize) }
|
||||
fn tag() -> &'static str { "TypeVar" }
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum TypeConst {
|
||||
Unit,
|
||||
Nat,
|
||||
Int,
|
||||
Float,
|
||||
StringT,
|
||||
Bool,
|
||||
Ordering,
|
||||
//UserDefined
|
||||
}
|
||||
|
||||
impl TypeConst {
|
||||
pub fn to_string(&self) -> String {
|
||||
use self::TypeConst::*;
|
||||
match self {
|
||||
Unit => format!("()"),
|
||||
Nat => format!("Nat"),
|
||||
Int => format!("Int"),
|
||||
Float => format!("Float"),
|
||||
StringT => format!("String"),
|
||||
Bool => format!("Bool"),
|
||||
Ordering => format!("Ordering"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EqUnifyValue for TypeConst { }
|
||||
|
||||
macro_rules! ty {
|
||||
($type_name:ident) => { Type::Const(TypeConst::$type_name) };
|
||||
($t1:ident -> $t2:ident) => { Type::Arrow { params: vec![ty!($t1)], ret: box ty!($t2) } };
|
||||
($t1:ident -> $t2:ident -> $t3:ident) => { Type::Arrow { params: vec![ty!($t1), ty!($t2)], ret: box ty!($t3) } };
|
||||
($type_list:ident, $ret_type:ident) => {
|
||||
Type::Arrow {
|
||||
params: $type_list,
|
||||
ret: box $ret_type,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//TODO find a better way to capture the to/from string logic
|
||||
impl Type {
|
||||
pub fn to_string(&self) -> String {
|
||||
use self::Type::*;
|
||||
match self {
|
||||
Const(c) => c.to_string(),
|
||||
Var(v) => format!("t_{}", v.0),
|
||||
Arrow { params, box ref ret } => {
|
||||
if params.len() == 0 {
|
||||
format!("-> {}", ret.to_string())
|
||||
} else {
|
||||
let mut buf = String::new();
|
||||
for p in params.iter() {
|
||||
write!(buf, "{} -> ", p.to_string()).unwrap();
|
||||
}
|
||||
write!(buf, "{}", ret.to_string()).unwrap();
|
||||
buf
|
||||
}
|
||||
},
|
||||
Compound { .. } => format!("<some compound type>")
|
||||
}
|
||||
}
|
||||
|
||||
fn from_string(string: &str) -> Option<Type> {
|
||||
Some(match string {
|
||||
"()" | "Unit" => ty!(Unit),
|
||||
"Nat" => ty!(Nat),
|
||||
"Int" => ty!(Int),
|
||||
"Float" => ty!(Float),
|
||||
"String" => ty!(StringT),
|
||||
"Bool" => ty!(Bool),
|
||||
"Ordering" => ty!(Ordering),
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
/// `Type` is parameterized by whether the type variables can be just universal, or universal or
|
||||
/// existential.
|
||||
#[derive(Debug, Clone)]
|
||||
enum Type<A> {
|
||||
Var(A),
|
||||
Const(TConst),
|
||||
Arrow(Box<Type<A>>, Box<Type<A>>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum TVar {
|
||||
Univ(UVar),
|
||||
Exist(ExistentialVar)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct UVar(Rc<String>);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct ExistentialVar(u32);
|
||||
|
||||
impl Type<UVar> {
|
||||
fn to_tvar(&self) -> Type<TVar> {
|
||||
match self {
|
||||
Type::Var(UVar(name)) => Type::Var(TVar::Univ(UVar(name.clone()))),
|
||||
Type::Const(ref c) => Type::Const(c.clone()),
|
||||
Type::Arrow(a, b) => Type::Arrow(
|
||||
Box::new(a.to_tvar()),
|
||||
Box::new(b.to_tvar())
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Type<TVar> {
|
||||
fn skolemize(&self) -> Type<UVar> {
|
||||
match self {
|
||||
Type::Var(TVar::Univ(uvar)) => Type::Var(uvar.clone()),
|
||||
Type::Var(TVar::Exist(_)) => Type::Var(UVar(Rc::new(format!("sk")))),
|
||||
Type::Const(ref c) => Type::Const(c.clone()),
|
||||
Type::Arrow(a, b) => Type::Arrow(
|
||||
Box::new(a.skolemize()),
|
||||
Box::new(b.skolemize())
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeIdentifier {
|
||||
fn to_monotype(&self) -> Type<UVar> {
|
||||
match self {
|
||||
TypeIdentifier::Tuple(_) => Type::Const(TConst::Nat),
|
||||
TypeIdentifier::Singleton(TypeSingletonName { name, .. }) => {
|
||||
match &name[..] {
|
||||
"Nat" => Type::Const(TConst::Nat),
|
||||
"Int" => Type::Const(TConst::Int),
|
||||
"Float" => Type::Const(TConst::Float),
|
||||
"Bool" => Type::Const(TConst::Bool),
|
||||
"String" => Type::Const(TConst::StringT),
|
||||
_ => Type::Const(TConst::Nat),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum TConst {
|
||||
User(Rc<String>),
|
||||
Unit,
|
||||
Nat,
|
||||
Int,
|
||||
Float,
|
||||
StringT,
|
||||
Bool,
|
||||
}
|
||||
|
||||
impl TConst {
|
||||
fn user(name: &str) -> TConst {
|
||||
TConst::User(Rc::new(name.to_string()))
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
impl<'a> TypeContext<'a> {
|
||||
pub fn new() -> TypeContext<'a> {
|
||||
TypeContext {
|
||||
variable_map: ScopeStack::new(None),
|
||||
unification_table: UnificationTable::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
fn new_env(&'a self, new_var: Rc<String>, ty: Type) -> TypeContext<'a> {
|
||||
let mut new_context = TypeContext {
|
||||
variable_map: self.variable_map.new_scope(None),
|
||||
unification_table: UnificationTable::new(), //???? not sure if i want this
|
||||
};
|
||||
|
||||
new_context.variable_map.insert(new_var, ty);
|
||||
new_context
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
fn get_type_from_name(&self, name: &TypeIdentifier) -> InferResult<Type> {
|
||||
use self::TypeIdentifier::*;
|
||||
Ok(match name {
|
||||
Singleton(TypeSingletonName { name,.. }) => {
|
||||
match Type::from_string(&name) {
|
||||
Some(ty) => ty,
|
||||
None => return TypeError::new(format!("Unknown type name: {}", name))
|
||||
}
|
||||
},
|
||||
Tuple(_) => return TypeError::new("tuples aren't ready yet"),
|
||||
})
|
||||
}
|
||||
|
||||
/// `typecheck` is the entry into the type-inference system, accepting an AST as an argument
|
||||
/// Following the example of GHC, the compiler deliberately does typechecking before de-sugaring
|
||||
/// the AST to ReducedAST
|
||||
pub fn typecheck(&mut self, ast: &AST) -> Result<Type, TypeError> {
|
||||
let mut returned_type = Type::Const(TypeConst::Unit);
|
||||
for statement in ast.0.iter() {
|
||||
returned_type = self.statement(statement.node())?;
|
||||
}
|
||||
Ok(returned_type)
|
||||
}
|
||||
|
||||
fn statement(&mut self, statement: &Statement) -> InferResult<Type> {
|
||||
match statement {
|
||||
Statement::ExpressionStatement(e) => self.expr(e.node()),
|
||||
Statement::Declaration(decl) => self.decl(decl),
|
||||
}
|
||||
}
|
||||
|
||||
fn decl(&mut self, decl: &Declaration) -> InferResult<Type> {
|
||||
use self::Declaration::*;
|
||||
match decl {
|
||||
Binding { name, expr, .. } => {
|
||||
let ty = self.expr(expr.node())?;
|
||||
self.variable_map.insert(name.clone(), ty);
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
Ok(ty!(Unit))
|
||||
}
|
||||
|
||||
fn expr(&mut self, expr: &Expression) -> InferResult<Type> {
|
||||
match expr {
|
||||
Expression(expr_type, Some(anno)) => {
|
||||
let t1 = self.expr_type(expr_type)?;
|
||||
let t2 = self.get_type_from_name(anno)?;
|
||||
self.unify(t2, t1)
|
||||
},
|
||||
Expression(expr_type, None) => self.expr_type(expr_type)
|
||||
}
|
||||
}
|
||||
|
||||
fn expr_type(&mut self, expr: &ExpressionKind) -> InferResult<Type> {
|
||||
use self::ExpressionKind::*;
|
||||
Ok(match expr {
|
||||
NatLiteral(_) => ty!(Nat),
|
||||
BoolLiteral(_) => ty!(Bool),
|
||||
FloatLiteral(_) => ty!(Float),
|
||||
StringLiteral(_) => ty!(StringT),
|
||||
PrefixExp(op, expr) => self.prefix(op, expr.node())?,
|
||||
BinExp(op, lhs, rhs) => self.binexp(op, lhs.node(), rhs.node())?,
|
||||
IfExpression { discriminator, body } => self.if_expr(discriminator, body)?,
|
||||
Value(val) => self.handle_value(val)?,
|
||||
Call { box ref f, arguments } => self.call(f.node(), arguments)?,
|
||||
Lambda { params, type_anno, body } => self.lambda(params, type_anno, body)?,
|
||||
_ => ty!(Unit),
|
||||
})
|
||||
}
|
||||
|
||||
fn prefix(&mut self, op: &PrefixOp, expr: &Expression) -> InferResult<Type> {
|
||||
let tf = match op.get_type() {
|
||||
Ok(ty) => ty,
|
||||
Err(e) => return TypeError::new(e)
|
||||
};
|
||||
|
||||
let tx = self.expr(expr)?;
|
||||
self.handle_apply(tf, vec![tx])
|
||||
}
|
||||
|
||||
fn binexp(&mut self, op: &BinOp, lhs: &Expression, rhs: &Expression) -> InferResult<Type> {
|
||||
let tf = match op.get_type() {
|
||||
Ok(ty) => ty,
|
||||
Err(e) => return TypeError::new(e),
|
||||
};
|
||||
|
||||
let t_lhs = self.expr(lhs)?;
|
||||
let t_rhs = self.expr(rhs)?; //TODO is this order a problem? not sure
|
||||
|
||||
self.handle_apply(tf, vec![t_lhs, t_rhs])
|
||||
}
|
||||
|
||||
fn if_expr(&mut self, discriminator: &Discriminator, body: &IfExpressionBody) -> InferResult<Type> {
|
||||
use self::Discriminator::*; use self::IfExpressionBody::*;
|
||||
match (discriminator, body) {
|
||||
(Simple(expr), SimpleConditional(then_clause, else_clause)) => self.handle_simple_if(expr, then_clause, else_clause),
|
||||
_ => TypeError::new(format!("Complex conditionals not supported"))
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_simple_if(&mut self, expr: &Expression, then_clause: &Block, else_clause: &Option<Block>) -> InferResult<Type> {
|
||||
let t1 = self.expr(expr)?;
|
||||
let t2 = self.block(then_clause)?;
|
||||
let t3 = match else_clause {
|
||||
Some(block) => self.block(block)?,
|
||||
None => ty!(Unit)
|
||||
};
|
||||
|
||||
let _ = self.unify(ty!(Bool), t1)?;
|
||||
self.unify(t2, t3)
|
||||
}
|
||||
|
||||
fn lambda(&mut self, params: &Vec<FormalParam>, type_anno: &Option<TypeIdentifier>, _body: &Block) -> InferResult<Type> {
|
||||
let argument_types: InferResult<Vec<Type>> = params.iter().map(|param: &FormalParam| {
|
||||
if let (_, Some(type_identifier)) = param {
|
||||
self.get_type_from_name(type_identifier)
|
||||
} else {
|
||||
Ok(Type::Var(self.fresh_type_variable()))
|
||||
}
|
||||
}).collect();
|
||||
let argument_types = argument_types?;
|
||||
let ret_type = match type_anno.as_ref() {
|
||||
Some(anno) => self.get_type_from_name(anno)?,
|
||||
None => Type::Var(self.fresh_type_variable())
|
||||
};
|
||||
|
||||
Ok(ty!(argument_types, ret_type))
|
||||
}
|
||||
|
||||
fn call(&mut self, f: &Expression, args: &Vec<Meta<Expression>>) -> InferResult<Type> {
|
||||
let tf = self.expr(f)?;
|
||||
let arg_types: InferResult<Vec<Type>> = args.iter().map(|ex| self.expr(ex.node())).collect();
|
||||
let arg_types = arg_types?;
|
||||
self.handle_apply(tf, arg_types)
|
||||
}
|
||||
|
||||
fn handle_apply(&mut self, tf: Type, args: Vec<Type>) -> InferResult<Type> {
|
||||
Ok(match tf {
|
||||
Type::Arrow { ref params, ret: box ref t_ret } if params.len() == args.len() => {
|
||||
for (t_param, t_arg) in params.iter().zip(args.iter()) {
|
||||
let _ = self.unify(t_param.clone(), t_arg.clone())?; //TODO I think this needs to reference a sub-scope
|
||||
}
|
||||
t_ret.clone()
|
||||
},
|
||||
Type::Arrow { .. } => return TypeError::new("Wrong length"),
|
||||
_ => return TypeError::new(format!("Not a function"))
|
||||
})
|
||||
}
|
||||
|
||||
fn block(&mut self, block: &Block) -> InferResult<Type> {
|
||||
let mut output = ty!(Unit);
|
||||
for s in block.iter() {
|
||||
let statement = s.node();
|
||||
output = self.statement(statement)?;
|
||||
}
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn handle_value(&mut self, val: &Rc<String>) -> InferResult<Type> {
|
||||
match self.variable_map.lookup(val) {
|
||||
Some(ty) => Ok(ty.clone()),
|
||||
None => TypeError::new(format!("Couldn't find variable: {}", val))
|
||||
}
|
||||
}
|
||||
|
||||
fn unify(&mut self, t1: Type, t2: Type) -> InferResult<Type> {
|
||||
use self::Type::*;
|
||||
|
||||
match (t1, t2) {
|
||||
(Const(ref c1), Const(ref c2)) if c1 == c2 => Ok(Const(c1.clone())), //choice of c1 is arbitrary I *think*
|
||||
(a @ Var(_), b @ Const(_)) => self.unify(b, a),
|
||||
(Const(ref c1), Var(ref v2)) => {
|
||||
self.unification_table.unify_var_value(v2.clone(), Some(c1.clone()))
|
||||
.or_else(|_| TypeError::new(format!("Couldn't unify {:?} and {:?}", Const(c1.clone()), Var(*v2))))?;
|
||||
Ok(Const(c1.clone()))
|
||||
},
|
||||
(Var(v1), Var(v2)) => {
|
||||
//TODO add occurs check
|
||||
self.unification_table.unify_var_var(v1.clone(), v2.clone())
|
||||
.or_else(|e| {
|
||||
println!("Unify error: {:?}", e);
|
||||
TypeError::new(format!("Two type variables {:?} and {:?} couldn't unify", v1, v2))
|
||||
})?;
|
||||
Ok(Var(v1.clone())) //arbitrary decision I think
|
||||
},
|
||||
(a, b) => TypeError::new(format!("{:?} and {:?} do not unify", a, b)),
|
||||
}
|
||||
}
|
||||
|
||||
fn fresh_type_variable(&mut self) -> TypeVar {
|
||||
let new_type_var = self.unification_table.new_key(None);
|
||||
new_type_var
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod typechecking_tests {
|
||||
use super::*;
|
||||
|
||||
macro_rules! assert_type_in_fresh_context {
|
||||
($string:expr, $type:expr) => {
|
||||
let mut tc = TypeContext::new();
|
||||
let ref ast = crate::util::quick_ast($string);
|
||||
let ty = tc.typecheck(ast).unwrap();
|
||||
assert_eq!(ty, $type)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_test() {
|
||||
assert_type_in_fresh_context!("1", ty!(Nat));
|
||||
assert_type_in_fresh_context!(r#""drugs""#, ty!(StringT));
|
||||
assert_type_in_fresh_context!("true", ty!(Bool));
|
||||
assert_type_in_fresh_context!("-1", ty!(Int));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn operators() {
|
||||
assert_type_in_fresh_context!("1 + 2", ty!(Nat));
|
||||
assert_type_in_fresh_context!("-2", ty!(Int));
|
||||
assert_type_in_fresh_context!("!true", ty!(Bool));
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
use std::cmp::Eq;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct ScopeStack<'a, T: 'a, V: 'a> where T: Hash + Eq {
|
||||
parent: Option<&'a ScopeStack<'a, T, V>>,
|
||||
values: HashMap<T, V>,
|
||||
scope_name: Option<String>
|
||||
}
|
||||
|
||||
impl<'a, T, V> ScopeStack<'a, T, V> where T: Hash + Eq {
|
||||
pub fn new(name: Option<String>) -> ScopeStack<'a, T, V> where T: Hash + Eq {
|
||||
ScopeStack {
|
||||
parent: None,
|
||||
values: HashMap::new(),
|
||||
scope_name: name
|
||||
}
|
||||
}
|
||||
pub fn insert(&mut self, key: T, value: V) where T: Hash + Eq {
|
||||
self.values.insert(key, value);
|
||||
}
|
||||
pub fn lookup(&self, key: &T) -> Option<&V> where T: Hash + Eq {
|
||||
match (self.values.get(key), self.parent) {
|
||||
(None, None) => None,
|
||||
(None, Some(parent)) => parent.lookup(key),
|
||||
(Some(value), _) => Some(value),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_scope(&'a self, name: Option<String>) -> ScopeStack<'a, T, V> where T: Hash + Eq {
|
||||
ScopeStack {
|
||||
parent: Some(self),
|
||||
values: HashMap::default(),
|
||||
scope_name: name,
|
||||
}
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
pub fn get_name(&self) -> Option<&String> {
|
||||
self.scope_name.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
/// this is intended for use in tests, and does no error-handling whatsoever
|
||||
#[allow(dead_code)]
|
||||
pub fn quick_ast(input: &str) -> crate::ast::AST {
|
||||
let tokens = crate::tokenizing::tokenize(input);
|
||||
let mut parser = crate::parsing::Parser::new(tokens);
|
||||
parser.parse().unwrap()
|
||||
}
|
||||
|
||||
#[allow(unused_macros)]
|
||||
macro_rules! rc {
|
||||
($string:tt) => { Rc::new(stringify!($string).to_string()) }
|
||||
}
|
||||
@@ -1,24 +1,25 @@
|
||||
[package]
|
||||
name = "schala-repl"
|
||||
name = "schala-lib"
|
||||
version = "0.1.0"
|
||||
authors = ["greg <greg.shuflin@protonmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
llvm-sys = "70.0.2"
|
||||
take_mut = "0.2.2"
|
||||
llvm-sys = "*"
|
||||
take_mut = "0.1.3"
|
||||
itertools = "0.5.8"
|
||||
getopts = "0.2.18"
|
||||
getopts = "*"
|
||||
lazy_static = "0.2.8"
|
||||
maplit = "*"
|
||||
colored = "1.7"
|
||||
serde = "1.0.91"
|
||||
serde_derive = "1.0.91"
|
||||
serde_json = "1.0.15"
|
||||
colored = "1.5"
|
||||
serde = "1.0.15"
|
||||
serde_derive = "1.0.15"
|
||||
serde_json = "1.0.3"
|
||||
rocket = "0.3.5"
|
||||
rocket_codegen = "0.3.5"
|
||||
rocket_contrib = "0.3.5"
|
||||
phf = "0.7.12"
|
||||
includedir = "0.2.0"
|
||||
linefeed = "0.5.0"
|
||||
regex = "0.2"
|
||||
rustyline = "1.0.0"
|
||||
|
||||
[build-dependencies]
|
||||
includedir_codegen = "0.2.0"
|
||||
106
schala-lib/src/language.rs
Normal file
106
schala-lib/src/language.rs
Normal file
@@ -0,0 +1,106 @@
|
||||
extern crate colored;
|
||||
|
||||
use self::colored::*;
|
||||
|
||||
pub struct LLVMCodeString(pub String);
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||
pub struct EvalOptions {
|
||||
pub debug_tokens: bool,
|
||||
pub debug_parse: bool,
|
||||
pub debug_type: bool,
|
||||
pub debug_symbol_table: bool,
|
||||
pub show_llvm_ir: bool,
|
||||
pub trace_evaluation: bool,
|
||||
pub compile: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct LanguageOutput {
|
||||
output: String,
|
||||
artifacts: Vec<TraceArtifact>,
|
||||
failed: bool,
|
||||
}
|
||||
|
||||
impl LanguageOutput {
|
||||
pub fn add_artifact(&mut self, artifact: TraceArtifact) {
|
||||
self.artifacts.push(artifact);
|
||||
}
|
||||
pub fn add_output(&mut self, output: String) {
|
||||
self.output = output;
|
||||
}
|
||||
|
||||
pub fn to_string(&self) -> String {
|
||||
let mut acc = String::new();
|
||||
for line in self.artifacts.iter() {
|
||||
acc.push_str(&line.debug_output.color(line.text_color).to_string());
|
||||
acc.push_str(&"\n");
|
||||
}
|
||||
acc.push_str(&self.output);
|
||||
acc
|
||||
}
|
||||
|
||||
pub fn print_to_screen(&self) {
|
||||
for line in self.artifacts.iter() {
|
||||
let color = line.text_color;
|
||||
let stage = line.stage_name.color(color).to_string();
|
||||
let output = line.debug_output.color(color).to_string();
|
||||
println!("{}: {}", stage, output);
|
||||
}
|
||||
println!("{}", self.output);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
//TODO I'll probably wanna implement this later
|
||||
#[derive(Debug)]
|
||||
pub struct CompilationOutput {
|
||||
output: LLVMCodeString,
|
||||
artifacts: Vec<TraceArtifact>,
|
||||
}
|
||||
*/
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TraceArtifact {
|
||||
stage_name: String,
|
||||
debug_output: String,
|
||||
text_color: &'static str,
|
||||
}
|
||||
|
||||
impl TraceArtifact {
|
||||
pub fn new(stage: &str, debug: String) -> TraceArtifact {
|
||||
let color = match stage {
|
||||
"parse_trace" | "ast" => "red",
|
||||
"tokens" => "green",
|
||||
"type_check" => "magenta",
|
||||
_ => "blue",
|
||||
};
|
||||
TraceArtifact { stage_name: stage.to_string(), debug_output: debug, text_color: color}
|
||||
}
|
||||
|
||||
pub fn new_parse_trace(trace: Vec<String>) -> TraceArtifact {
|
||||
let mut output = String::new();
|
||||
|
||||
for t in trace {
|
||||
output.push_str(&t);
|
||||
output.push_str("\n");
|
||||
}
|
||||
|
||||
TraceArtifact { stage_name: "parse_trace".to_string(), debug_output: output, text_color: "red"}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ProgrammingLanguageInterface {
|
||||
fn evaluate_in_repl(&mut self, input: &str, eval_options: &EvalOptions) -> LanguageOutput;
|
||||
fn evaluate_noninteractive(&mut self, input: &str, eval_options: &EvalOptions) -> LanguageOutput {
|
||||
self.evaluate_in_repl(input, eval_options)
|
||||
}
|
||||
fn get_language_name(&self) -> String;
|
||||
fn get_source_file_suffix(&self) -> String;
|
||||
fn compile(&mut self, _input: &str) -> LLVMCodeString {
|
||||
LLVMCodeString("".to_string())
|
||||
}
|
||||
fn can_compile(&self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
378
schala-lib/src/lib.rs
Normal file
378
schala-lib/src/lib.rs
Normal file
@@ -0,0 +1,378 @@
|
||||
#![feature(link_args)]
|
||||
#![feature(advanced_slice_patterns, slice_patterns, box_patterns, box_syntax)]
|
||||
#![feature(plugin)]
|
||||
#![plugin(rocket_codegen)]
|
||||
extern crate getopts;
|
||||
extern crate rustyline;
|
||||
extern crate itertools;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
#[macro_use]
|
||||
extern crate maplit;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
extern crate serde_json;
|
||||
extern crate rocket;
|
||||
extern crate rocket_contrib;
|
||||
extern crate includedir;
|
||||
extern crate phf;
|
||||
|
||||
use std::path::Path;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use std::process::exit;
|
||||
use std::default::Default;
|
||||
|
||||
use rustyline::error::ReadlineError;
|
||||
use rustyline::Editor;
|
||||
|
||||
mod language;
|
||||
mod webapp;
|
||||
pub mod llvm_wrap;
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/static.rs"));
|
||||
|
||||
pub use language::{ProgrammingLanguageInterface, EvalOptions, TraceArtifact, LanguageOutput, LLVMCodeString};
|
||||
pub type PLIGenerator = Box<Fn() -> Box<ProgrammingLanguageInterface> + Send + Sync>;
|
||||
|
||||
pub fn schala_main(generators: Vec<PLIGenerator>) {
|
||||
let languages: Vec<Box<ProgrammingLanguageInterface>> = generators.iter().map(|x| x()).collect();
|
||||
|
||||
let option_matches = program_options().parse(std::env::args()).unwrap_or_else(|e| {
|
||||
println!("{:?}", e);
|
||||
exit(1);
|
||||
});
|
||||
|
||||
if option_matches.opt_present("list-languages") {
|
||||
for lang in languages {
|
||||
println!("{}", lang.get_language_name());
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if option_matches.opt_present("help") {
|
||||
println!("{}", program_options().usage("Schala metainterpreter"));
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if option_matches.opt_present("webapp") {
|
||||
webapp::web_main(generators);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
let language_names: Vec<String> = languages.iter().map(|lang| {lang.get_language_name()}).collect();
|
||||
let initial_index: usize =
|
||||
option_matches.opt_str("lang")
|
||||
.and_then(|lang| { language_names.iter().position(|x| { x.to_lowercase() == lang.to_lowercase() }) })
|
||||
.unwrap_or(0);
|
||||
|
||||
let mut options = EvalOptions::default();
|
||||
options.compile = match option_matches.opt_str("eval-style") {
|
||||
Some(ref s) if s == "compile" => true,
|
||||
_ => false
|
||||
};
|
||||
|
||||
match option_matches.free[..] {
|
||||
[] | [_] => {
|
||||
let mut repl = Repl::new(languages, initial_index);
|
||||
repl.options.show_llvm_ir = true; //TODO make this be configurable
|
||||
repl.run();
|
||||
}
|
||||
[_, ref filename, _..] => {
|
||||
|
||||
run_noninteractive(filename, languages, options);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn run_noninteractive(filename: &str, languages: Vec<Box<ProgrammingLanguageInterface>>, options: EvalOptions) {
|
||||
let path = Path::new(filename);
|
||||
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or_else(|| {
|
||||
println!("Source file lacks extension");
|
||||
exit(1);
|
||||
});
|
||||
let mut language = Box::new(languages.into_iter().find(|lang| lang.get_source_file_suffix() == ext)
|
||||
.unwrap_or_else(|| {
|
||||
println!("Extension .{} not recognized", ext);
|
||||
exit(1);
|
||||
}));
|
||||
|
||||
let mut source_file = File::open(path).unwrap();
|
||||
let mut buffer = String::new();
|
||||
|
||||
source_file.read_to_string(&mut buffer).unwrap();
|
||||
|
||||
if options.compile {
|
||||
if !language.can_compile() {
|
||||
panic!("Trying to compile a non-compileable language");
|
||||
} else {
|
||||
let llvm_bytecode = language.compile(&buffer);
|
||||
compilation_sequence(llvm_bytecode, filename);
|
||||
}
|
||||
} else {
|
||||
let output = language.evaluate_in_repl(&buffer, &options);
|
||||
// if output.has_error....
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
struct Repl {
|
||||
options: EvalOptions,
|
||||
languages: Vec<Box<ProgrammingLanguageInterface>>,
|
||||
current_language_index: usize,
|
||||
interpreter_directive_sigil: char,
|
||||
console: rustyline::Editor<()>,
|
||||
}
|
||||
|
||||
impl Repl {
|
||||
fn new(languages: Vec<Box<ProgrammingLanguageInterface>>, initial_index: usize) -> Repl {
|
||||
let i = if initial_index < languages.len() { initial_index } else { 0 };
|
||||
|
||||
let console = Editor::<()>::new();
|
||||
|
||||
Repl {
|
||||
options: Repl::get_options(),
|
||||
languages: languages,
|
||||
current_language_index: i,
|
||||
interpreter_directive_sigil: '.',
|
||||
console
|
||||
}
|
||||
}
|
||||
|
||||
fn get_options() -> EvalOptions {
|
||||
File::open(".schala_repl")
|
||||
.and_then(|mut file| {
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents)?;
|
||||
Ok(contents)
|
||||
})
|
||||
.and_then(|contents| {
|
||||
let options: EvalOptions = serde_json::from_str(&contents)?;
|
||||
Ok(options)
|
||||
}).unwrap_or(EvalOptions::default())
|
||||
}
|
||||
|
||||
fn save_options(&self) {
|
||||
let ref options = self.options;
|
||||
let read = File::create(".schala_repl")
|
||||
.and_then(|mut file| {
|
||||
let buf = serde_json::to_string(options).unwrap();
|
||||
file.write_all(buf.as_bytes())
|
||||
});
|
||||
|
||||
if let Err(err) = read {
|
||||
println!("Error saving .schala_repl file {}", err);
|
||||
}
|
||||
}
|
||||
|
||||
fn run(&mut self) {
|
||||
println!("MetaInterpreter v 0.05");
|
||||
|
||||
self.console.get_history().load(".schala_history").unwrap_or(());
|
||||
|
||||
loop {
|
||||
let language_name = self.languages[self.current_language_index].get_language_name();
|
||||
let prompt_str = format!("{} >> ", language_name);
|
||||
|
||||
match self.console.readline(&prompt_str) {
|
||||
Err(ReadlineError::Eof) | Err(ReadlineError::Interrupted) => break,
|
||||
Err(e) => {
|
||||
println!("Terminal read error: {}", e);
|
||||
},
|
||||
Ok(ref input) => {
|
||||
self.console.add_history_entry(input);
|
||||
if self.handle_interpreter_directive(input) {
|
||||
continue;
|
||||
}
|
||||
let output = self.input_handler(input);
|
||||
println!("=> {}", output);
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
self.console.get_history().save(".schala_history").unwrap_or(());
|
||||
self.save_options();
|
||||
println!("Exiting...");
|
||||
}
|
||||
|
||||
fn input_handler(&mut self, input: &str) -> String {
|
||||
let ref mut language = self.languages[self.current_language_index];
|
||||
let interpreter_output = language.evaluate_in_repl(input, &self.options);
|
||||
interpreter_output.to_string()
|
||||
}
|
||||
|
||||
fn handle_interpreter_directive(&mut self, input: &str) -> bool {
|
||||
match input.chars().nth(0) {
|
||||
Some(ch) if ch == self.interpreter_directive_sigil => (),
|
||||
_ => return false
|
||||
}
|
||||
|
||||
let mut iter = input.chars();
|
||||
iter.next();
|
||||
let trimmed_sigil: &str = iter.as_str();
|
||||
|
||||
let commands: Vec<&str> = trimmed_sigil
|
||||
.split_whitespace()
|
||||
.collect();
|
||||
|
||||
let cmd: &str = match commands.get(0).clone() {
|
||||
None => return true,
|
||||
Some(s) => s
|
||||
};
|
||||
|
||||
match cmd {
|
||||
"exit" | "quit" => {
|
||||
self.save_options();
|
||||
exit(0)
|
||||
},
|
||||
"help" => {
|
||||
println!("Commands:");
|
||||
println!("exit | quit");
|
||||
println!("lang(uage) [go|show|next|previous]");
|
||||
println!("set [show|hide] [tokens|parse|symbols|eval|llvm]");
|
||||
}
|
||||
"lang" | "language" => {
|
||||
match commands.get(1) {
|
||||
Some(&"show") => {
|
||||
for (i, lang) in self.languages.iter().enumerate() {
|
||||
if i == self.current_language_index {
|
||||
println!("* {}", lang.get_language_name());
|
||||
} else {
|
||||
println!("{}", lang.get_language_name());
|
||||
}
|
||||
}
|
||||
},
|
||||
Some(&"go") => {
|
||||
match commands.get(2) {
|
||||
None => println!("Must specify a language name"),
|
||||
Some(&desired_name) => {
|
||||
for (i, _) in self.languages.iter().enumerate() {
|
||||
let lang_name = self.languages[i].get_language_name();
|
||||
if lang_name.to_lowercase() == desired_name.to_lowercase() {
|
||||
self.current_language_index = i;
|
||||
println!("Switching to {}", self.languages[self.current_language_index].get_language_name());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
println!("Language {} not found", desired_name);
|
||||
}
|
||||
}
|
||||
},
|
||||
Some(&"next") => {
|
||||
self.current_language_index = (self.current_language_index + 1) % self.languages.len();
|
||||
println!("Switching to {}", self.languages[self.current_language_index].get_language_name());
|
||||
}
|
||||
Some(&"prev") | Some(&"previous") => {
|
||||
self.current_language_index = if self.current_language_index == 0 { self.languages.len() - 1 } else { self.current_language_index - 1 };
|
||||
println!("Switching to {}", self.languages[self.current_language_index].get_language_name());
|
||||
},
|
||||
Some(e) => println!("Bad `lang` argument: {}", e),
|
||||
None => println!("`lang` - valid arguments `show`, `next`, `prev`|`previous`"),
|
||||
}
|
||||
},
|
||||
"set" => {
|
||||
let show = match commands.get(1) {
|
||||
Some(&"show") => true,
|
||||
Some(&"hide") => false,
|
||||
Some(e) => {
|
||||
println!("Bad `set` argument: {}", e);
|
||||
return true;
|
||||
}
|
||||
None => {
|
||||
println!("`set` - valid arguments `show {{option}}`, `hide {{option}}`");
|
||||
return true;
|
||||
}
|
||||
};
|
||||
match commands.get(2) {
|
||||
Some(&"tokens") => self.options.debug_tokens = show,
|
||||
Some(&"parse") => self.options.debug_parse = show,
|
||||
Some(&"symbols") => self.options.debug_symbol_table = show,
|
||||
Some(&"eval") => {
|
||||
//let ref mut language = self.languages[self.current_language_index];
|
||||
//language.set_option("trace_evaluation", show);
|
||||
},
|
||||
Some(&"llvm") => self.options.show_llvm_ir = show,
|
||||
Some(e) => {
|
||||
println!("Bad `show`/`hide` argument: {}", e);
|
||||
return true;
|
||||
}
|
||||
None => {
|
||||
println!("`show`/`hide` requires an argument");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
},
|
||||
e => println!("Unknown command: {}", e)
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compilation_sequence(llvm_code: LLVMCodeString, sourcefile: &str) {
|
||||
use std::process::Command;
|
||||
|
||||
let ll_filename = "out.ll";
|
||||
let obj_filename = "out.o";
|
||||
let q: Vec<&str> = sourcefile.split('.').collect();
|
||||
let bin_filename = match &q[..] {
|
||||
&[name, "maaru"] => name,
|
||||
_ => panic!("Bad filename {}", sourcefile),
|
||||
};
|
||||
|
||||
let LLVMCodeString(llvm_str) = llvm_code;
|
||||
|
||||
println!("Compilation process finished for {}", ll_filename);
|
||||
File::create(ll_filename)
|
||||
.and_then(|mut f| f.write_all(llvm_str.as_bytes()))
|
||||
.expect("Error writing file");
|
||||
|
||||
let llc_output = Command::new("llc")
|
||||
.args(&["-filetype=obj", ll_filename, "-o", obj_filename])
|
||||
.output()
|
||||
.expect("Failed to run llc");
|
||||
|
||||
|
||||
if !llc_output.status.success() {
|
||||
println!("{}", String::from_utf8_lossy(&llc_output.stderr));
|
||||
}
|
||||
|
||||
let gcc_output = Command::new("gcc")
|
||||
.args(&["-o", bin_filename, &obj_filename])
|
||||
.output()
|
||||
.expect("failed to run gcc");
|
||||
|
||||
if !gcc_output.status.success() {
|
||||
println!("{}", String::from_utf8_lossy(&gcc_output.stdout));
|
||||
println!("{}", String::from_utf8_lossy(&gcc_output.stderr));
|
||||
}
|
||||
|
||||
for filename in [obj_filename].iter() {
|
||||
Command::new("rm")
|
||||
.arg(filename)
|
||||
.output()
|
||||
.expect(&format!("failed to run rm {}", filename));
|
||||
}
|
||||
}
|
||||
|
||||
fn program_options() -> getopts::Options {
|
||||
let mut options = getopts::Options::new();
|
||||
options.optopt("s",
|
||||
"eval-style",
|
||||
"Specify whether to compile (if supported) or interpret the language. If not specified, the default is language-specific",
|
||||
"[compile|interpret]"
|
||||
);
|
||||
options.optflag("",
|
||||
"list-languages",
|
||||
"Show a list of all supported languages");
|
||||
options.optopt("l",
|
||||
"lang",
|
||||
"Start up REPL in a language",
|
||||
"LANGUAGE");
|
||||
options.optflag("h",
|
||||
"help",
|
||||
"Show help text");
|
||||
options.optflag("w",
|
||||
"webapp",
|
||||
"Start up web interpreter");
|
||||
options
|
||||
}
|
||||
279
schala-lib/src/llvm_wrap.rs
Normal file
279
schala-lib/src/llvm_wrap.rs
Normal file
@@ -0,0 +1,279 @@
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(dead_code)]
|
||||
extern crate llvm_sys;
|
||||
|
||||
use self::llvm_sys::{LLVMIntPredicate, LLVMRealPredicate};
|
||||
use self::llvm_sys::prelude::*;
|
||||
use self::llvm_sys::core;
|
||||
use std::ptr;
|
||||
use std::ffi::{CString, CStr};
|
||||
use std::os::raw::c_char;
|
||||
|
||||
pub fn create_context() -> LLVMContextRef {
|
||||
unsafe { core::LLVMContextCreate() }
|
||||
}
|
||||
pub fn module_create_with_name(name: &str) -> LLVMModuleRef {
|
||||
unsafe {
|
||||
let n = name.as_ptr() as *const _;
|
||||
core::LLVMModuleCreateWithName(n)
|
||||
}
|
||||
}
|
||||
pub fn CreateBuilderInContext(context: LLVMContextRef) -> LLVMBuilderRef {
|
||||
unsafe { core::LLVMCreateBuilderInContext(context) }
|
||||
}
|
||||
|
||||
pub fn AppendBasicBlockInContext(context: LLVMContextRef,
|
||||
function: LLVMValueRef,
|
||||
name: &str)
|
||||
-> LLVMBasicBlockRef {
|
||||
let c_name = CString::new(name).unwrap();
|
||||
unsafe { core::LLVMAppendBasicBlockInContext(context, function, c_name.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn AddFunction(module: LLVMModuleRef, name: &str, function_type: LLVMTypeRef) -> LLVMValueRef {
|
||||
let c_name = CString::new(name).unwrap();
|
||||
unsafe { core::LLVMAddFunction(module, c_name.as_ptr(), function_type) }
|
||||
}
|
||||
|
||||
pub fn FunctionType(return_type: LLVMTypeRef,
|
||||
mut param_types: Vec<LLVMTypeRef>,
|
||||
is_var_rag: bool)
|
||||
-> LLVMTypeRef {
|
||||
let len = param_types.len();
|
||||
unsafe {
|
||||
let pointer = param_types.as_mut_ptr();
|
||||
core::LLVMFunctionType(return_type,
|
||||
pointer,
|
||||
len as u32,
|
||||
if is_var_rag { 1 } else { 0 })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn GetNamedFunction(module: LLVMModuleRef,
|
||||
name: &str) -> Option<LLVMValueRef> {
|
||||
|
||||
let c_name = CString::new(name).unwrap();
|
||||
let ret = unsafe { core::LLVMGetNamedFunction(module, c_name.as_ptr()) };
|
||||
|
||||
if ret.is_null() {
|
||||
None
|
||||
} else {
|
||||
Some(ret)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn VoidTypeInContext(context: LLVMContextRef) -> LLVMTypeRef {
|
||||
unsafe { core::LLVMVoidTypeInContext(context) }
|
||||
}
|
||||
|
||||
pub fn DisposeBuilder(builder: LLVMBuilderRef) {
|
||||
unsafe { core::LLVMDisposeBuilder(builder) }
|
||||
}
|
||||
|
||||
pub fn DisposeModule(module: LLVMModuleRef) {
|
||||
unsafe { core::LLVMDisposeModule(module) }
|
||||
}
|
||||
|
||||
pub fn ContextDispose(context: LLVMContextRef) {
|
||||
unsafe { core::LLVMContextDispose(context) }
|
||||
}
|
||||
|
||||
pub fn PositionBuilderAtEnd(builder: LLVMBuilderRef, basic_block: LLVMBasicBlockRef) {
|
||||
unsafe { core::LLVMPositionBuilderAtEnd(builder, basic_block) }
|
||||
}
|
||||
|
||||
pub fn BuildRet(builder: LLVMBuilderRef, val: LLVMValueRef) -> LLVMValueRef {
|
||||
unsafe { core::LLVMBuildRet(builder, val) }
|
||||
}
|
||||
|
||||
pub fn BuildRetVoid(builder: LLVMBuilderRef) -> LLVMValueRef {
|
||||
unsafe { core::LLVMBuildRetVoid(builder) }
|
||||
}
|
||||
|
||||
pub fn DumpModule(module: LLVMModuleRef) {
|
||||
unsafe { core::LLVMDumpModule(module) }
|
||||
}
|
||||
|
||||
pub fn Int64TypeInContext(context: LLVMContextRef) -> LLVMTypeRef {
|
||||
unsafe { core::LLVMInt64TypeInContext(context) }
|
||||
}
|
||||
|
||||
pub fn ConstInt(int_type: LLVMTypeRef, n: u64, sign_extend: bool) -> LLVMValueRef {
|
||||
unsafe { core::LLVMConstInt(int_type, n, if sign_extend { 1 } else { 0 }) }
|
||||
}
|
||||
|
||||
pub fn BuildAdd(builder: LLVMBuilderRef,
|
||||
lhs: LLVMValueRef,
|
||||
rhs: LLVMValueRef,
|
||||
reg_name: &str)
|
||||
-> LLVMValueRef {
|
||||
let name = CString::new(reg_name).unwrap();
|
||||
unsafe { core::LLVMBuildAdd(builder, lhs, rhs, name.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn BuildSub(builder: LLVMBuilderRef,
|
||||
lhs: LLVMValueRef,
|
||||
rhs: LLVMValueRef,
|
||||
reg_name: &str)
|
||||
-> LLVMValueRef {
|
||||
let name = CString::new(reg_name).unwrap();
|
||||
unsafe { core::LLVMBuildSub(builder, lhs, rhs, name.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn BuildMul(builder: LLVMBuilderRef,
|
||||
lhs: LLVMValueRef,
|
||||
rhs: LLVMValueRef,
|
||||
reg_name: &str)
|
||||
-> LLVMValueRef {
|
||||
let name = CString::new(reg_name).unwrap();
|
||||
unsafe { core::LLVMBuildMul(builder, lhs, rhs, name.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn BuildUDiv(builder: LLVMBuilderRef,
|
||||
lhs: LLVMValueRef,
|
||||
rhs: LLVMValueRef,
|
||||
reg_name: &str)
|
||||
-> LLVMValueRef {
|
||||
let name = CString::new(reg_name).unwrap();
|
||||
unsafe { core::LLVMBuildUDiv(builder, lhs, rhs, name.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn BuildSRem(builder: LLVMBuilderRef,
|
||||
lhs: LLVMValueRef,
|
||||
rhs: LLVMValueRef,
|
||||
reg_name: &str)
|
||||
-> LLVMValueRef {
|
||||
let name = CString::new(reg_name).unwrap();
|
||||
unsafe { core::LLVMBuildSRem(builder, lhs, rhs, name.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn BuildCondBr(builder: LLVMBuilderRef,
|
||||
if_expr: LLVMValueRef,
|
||||
then_expr: LLVMBasicBlockRef,
|
||||
else_expr: LLVMBasicBlockRef) -> LLVMValueRef {
|
||||
|
||||
|
||||
unsafe { core::LLVMBuildCondBr(builder, if_expr, then_expr, else_expr) }
|
||||
}
|
||||
|
||||
pub fn BuildBr(builder: LLVMBuilderRef,
|
||||
dest: LLVMBasicBlockRef) -> LLVMValueRef {
|
||||
unsafe { core::LLVMBuildBr(builder, dest) }
|
||||
}
|
||||
|
||||
pub fn GetInsertBlock(builder: LLVMBuilderRef) -> LLVMBasicBlockRef {
|
||||
unsafe { core::LLVMGetInsertBlock(builder) }
|
||||
}
|
||||
|
||||
pub fn BuildPhi(builder: LLVMBuilderRef, ty: LLVMTypeRef, name: &str) -> LLVMValueRef {
|
||||
let name = CString::new(name).unwrap();
|
||||
unsafe { core::LLVMBuildPhi(builder, ty, name.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn SetValueName(value: LLVMValueRef, name: &str) {
|
||||
let name = CString::new(name).unwrap();
|
||||
unsafe {
|
||||
core::LLVMSetValueName(value, name.as_ptr())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn GetValueName(value: LLVMValueRef) -> String {
|
||||
unsafe {
|
||||
let name_ptr: *const c_char = core::LLVMGetValueName(value);
|
||||
CStr::from_ptr(name_ptr).to_string_lossy().into_owned()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn GetParams(function: LLVMValueRef) -> Vec<LLVMValueRef> {
|
||||
let size = CountParams(function);
|
||||
unsafe {
|
||||
let mut container = Vec::with_capacity(size);
|
||||
container.set_len(size);
|
||||
core::LLVMGetParams(function, container.as_mut_ptr());
|
||||
container
|
||||
}
|
||||
}
|
||||
|
||||
pub fn CountParams(function: LLVMValueRef) -> usize {
|
||||
unsafe { core::LLVMCountParams(function) as usize }
|
||||
}
|
||||
|
||||
pub fn BuildFCmp(builder: LLVMBuilderRef,
|
||||
op: LLVMRealPredicate,
|
||||
lhs: LLVMValueRef,
|
||||
rhs: LLVMValueRef,
|
||||
name: &str) -> LLVMValueRef {
|
||||
let name = CString::new(name).unwrap();
|
||||
unsafe { core::LLVMBuildFCmp(builder, op, lhs, rhs, name.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn BuildZExt(builder: LLVMBuilderRef,
|
||||
val: LLVMValueRef,
|
||||
dest_type: LLVMTypeRef,
|
||||
name: &str) -> LLVMValueRef {
|
||||
let name = CString::new(name).unwrap();
|
||||
unsafe { core::LLVMBuildZExt(builder, val, dest_type, name.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn BuildUIToFP(builder: LLVMBuilderRef,
|
||||
val: LLVMValueRef,
|
||||
dest_type: LLVMTypeRef,
|
||||
name: &str) -> LLVMValueRef {
|
||||
|
||||
let name = CString::new(name).unwrap();
|
||||
unsafe { core::LLVMBuildUIToFP(builder, val, dest_type, name.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn BuildICmp(builder: LLVMBuilderRef,
|
||||
op: LLVMIntPredicate,
|
||||
lhs: LLVMValueRef,
|
||||
rhs: LLVMValueRef,
|
||||
name: &str) -> LLVMValueRef {
|
||||
let name = CString::new(name).unwrap();
|
||||
unsafe { core::LLVMBuildICmp(builder, op, lhs, rhs, name.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn GetBasicBlockParent(block: LLVMBasicBlockRef) -> LLVMValueRef {
|
||||
unsafe { core::LLVMGetBasicBlockParent(block) }
|
||||
}
|
||||
|
||||
pub fn GetBasicBlocks(function: LLVMValueRef) -> Vec<LLVMBasicBlockRef> {
|
||||
let size = CountBasicBlocks(function);
|
||||
unsafe {
|
||||
let mut container = Vec::with_capacity(size);
|
||||
container.set_len(size);
|
||||
core::LLVMGetBasicBlocks(function, container.as_mut_ptr());
|
||||
container
|
||||
}
|
||||
}
|
||||
|
||||
pub fn CountBasicBlocks(function: LLVMValueRef) -> usize {
|
||||
unsafe { core::LLVMCountBasicBlocks(function) as usize }
|
||||
}
|
||||
|
||||
pub fn PrintModuleToString(module: LLVMModuleRef) -> String {
|
||||
unsafe {
|
||||
let str_ptr: *const c_char = core::LLVMPrintModuleToString(module);
|
||||
CStr::from_ptr(str_ptr).to_string_lossy().into_owned()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn AddIncoming(phi_node: LLVMValueRef, mut incoming_values: Vec<LLVMValueRef>,
|
||||
mut incoming_blocks: Vec<LLVMBasicBlockRef>) {
|
||||
|
||||
let count = incoming_blocks.len() as u32;
|
||||
if incoming_values.len() as u32 != count {
|
||||
panic!("Bad invocation of AddIncoming");
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let vals = incoming_values.as_mut_ptr();
|
||||
let blocks = incoming_blocks.as_mut_ptr();
|
||||
core::LLVMAddIncoming(phi_node, vals, blocks, count)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn PrintModuleToFile(module: LLVMModuleRef, filename: &str) -> LLVMBool {
|
||||
let out_file = CString::new(filename).unwrap();
|
||||
unsafe { core::LLVMPrintModuleToFile(module, out_file.as_ptr(), ptr::null_mut()) }
|
||||
}
|
||||
45
schala-lib/src/webapp.rs
Normal file
45
schala-lib/src/webapp.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use rocket;
|
||||
use rocket::State;
|
||||
use rocket::response::Content;
|
||||
use rocket::response::NamedFile;
|
||||
use rocket::http::ContentType;
|
||||
use rocket_contrib::Json;
|
||||
use language::{ProgrammingLanguageInterface, EvalOptions};
|
||||
use WEBFILES;
|
||||
use ::PLIGenerator;
|
||||
|
||||
#[get("/")]
|
||||
fn index() -> Content<String> {
|
||||
let path = "static/index.html";
|
||||
let html_contents = String::from_utf8(WEBFILES.get(path).unwrap().into_owned()).unwrap();
|
||||
Content(ContentType::HTML, html_contents)
|
||||
}
|
||||
|
||||
#[get("/bundle.js")]
|
||||
fn js_bundle() -> Content<String> {
|
||||
let path = "static/bundle.js";
|
||||
let js_contents = String::from_utf8(WEBFILES.get(path).unwrap().into_owned()).unwrap();
|
||||
Content(ContentType::JavaScript, js_contents)
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Input {
|
||||
source: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Output {
|
||||
text: String,
|
||||
}
|
||||
|
||||
#[post("/input", format = "application/json", data = "<input>")]
|
||||
fn interpreter_input(input: Json<Input>, generators: State<Vec<PLIGenerator>>) -> Json<Output> {
|
||||
let schala_gen = generators.get(0).unwrap();
|
||||
let mut schala: Box<ProgrammingLanguageInterface> = schala_gen();
|
||||
let code_output = schala.evaluate_in_repl(&input.source, &EvalOptions::default());
|
||||
Json(Output { text: code_output.to_string() })
|
||||
}
|
||||
|
||||
pub fn web_main(language_generators: Vec<PLIGenerator>) {
|
||||
rocket::ignite().manage(language_generators).mount("/", routes![index, js_bundle, interpreter_input]).launch();
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
use std::time;
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub trait ProgrammingLanguageInterface {
|
||||
fn get_language_name(&self) -> String;
|
||||
fn get_source_file_suffix(&self) -> String;
|
||||
|
||||
fn run_computation(&mut self, _request: ComputationRequest) -> ComputationResponse {
|
||||
ComputationResponse {
|
||||
main_output: Err(format!("Computation pipeline not implemented")),
|
||||
global_output_stats: GlobalOutputStats::default(),
|
||||
debug_responses: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn request_meta(&mut self, _request: LangMetaRequest) -> LangMetaResponse {
|
||||
LangMetaResponse::Custom { kind: format!("not-implemented"), value: format!("") }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ComputationRequest<'a> {
|
||||
pub source: &'a str,
|
||||
pub debug_requests: HashSet<DebugAsk>,
|
||||
}
|
||||
|
||||
pub struct ComputationResponse {
|
||||
pub main_output: Result<String, String>,
|
||||
pub global_output_stats: GlobalOutputStats,
|
||||
pub debug_responses: Vec<DebugResponse>,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct GlobalOutputStats {
|
||||
pub total_duration: time::Duration,
|
||||
pub stage_durations: Vec<(String, time::Duration)>
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Hash, Eq, PartialEq, Deserialize, Serialize)]
|
||||
pub enum DebugAsk {
|
||||
Timing,
|
||||
ByStage { stage_name: String },
|
||||
}
|
||||
|
||||
pub struct DebugResponse {
|
||||
pub ask: DebugAsk,
|
||||
pub value: String
|
||||
}
|
||||
|
||||
pub enum LangMetaRequest {
|
||||
StageNames,
|
||||
Docs {
|
||||
source: String,
|
||||
},
|
||||
Custom {
|
||||
kind: String,
|
||||
value: String
|
||||
},
|
||||
ImmediateDebug(DebugAsk),
|
||||
}
|
||||
|
||||
pub enum LangMetaResponse {
|
||||
StageNames(Vec<String>),
|
||||
Docs {
|
||||
doc_string: String,
|
||||
},
|
||||
Custom {
|
||||
kind: String,
|
||||
value: String
|
||||
},
|
||||
ImmediateDebug(DebugResponse),
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
#![feature(link_args)]
|
||||
#![feature(slice_patterns, box_patterns, box_syntax, proc_macro_hygiene, decl_macro)]
|
||||
#![feature(plugin)]
|
||||
extern crate getopts;
|
||||
extern crate linefeed;
|
||||
extern crate itertools;
|
||||
extern crate colored;
|
||||
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
extern crate serde_json;
|
||||
extern crate includedir;
|
||||
extern crate phf;
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::path::Path;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::process::exit;
|
||||
|
||||
mod repl;
|
||||
mod language;
|
||||
|
||||
pub use language::{ProgrammingLanguageInterface,
|
||||
ComputationRequest, ComputationResponse,
|
||||
LangMetaRequest, LangMetaResponse,
|
||||
DebugResponse, DebugAsk, GlobalOutputStats};
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/static.rs"));
|
||||
const VERSION_STRING: &'static str = "0.1.0";
|
||||
|
||||
pub fn start_repl(langs: Vec<Box<dyn ProgrammingLanguageInterface>>) {
|
||||
let options = command_line_options().parse(std::env::args()).unwrap_or_else(|e| {
|
||||
println!("{:?}", e);
|
||||
exit(1);
|
||||
});
|
||||
|
||||
if options.opt_present("help") {
|
||||
println!("{}", command_line_options().usage("Schala metainterpreter"));
|
||||
exit(0);
|
||||
}
|
||||
|
||||
match options.free[..] {
|
||||
[] | [_] => {
|
||||
let mut repl = repl::Repl::new(langs);
|
||||
repl.run_repl();
|
||||
}
|
||||
[_, ref filename, _..] => {
|
||||
run_noninteractive(filename, langs);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn run_noninteractive(filename: &str, languages: Vec<Box<ProgrammingLanguageInterface>>) {
|
||||
let path = Path::new(filename);
|
||||
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or_else(|| {
|
||||
println!("Source file lacks extension");
|
||||
exit(1);
|
||||
});
|
||||
let mut language = Box::new(languages.into_iter().find(|lang| lang.get_source_file_suffix() == ext)
|
||||
.unwrap_or_else(|| {
|
||||
println!("Extension .{} not recognized", ext);
|
||||
exit(1);
|
||||
}));
|
||||
|
||||
let mut source_file = File::open(path).unwrap();
|
||||
let mut buffer = String::new();
|
||||
source_file.read_to_string(&mut buffer).unwrap();
|
||||
|
||||
let request = ComputationRequest {
|
||||
source: &buffer,
|
||||
debug_requests: HashSet::new(),
|
||||
};
|
||||
|
||||
let response = language.run_computation(request);
|
||||
match response.main_output {
|
||||
Ok(s) => println!("{}", s),
|
||||
Err(s) => println!("{}", s)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
fn command_line_options() -> getopts::Options {
|
||||
let mut options = getopts::Options::new();
|
||||
options.optflag("h",
|
||||
"help",
|
||||
"Show help text");
|
||||
options.optflag("w",
|
||||
"webapp",
|
||||
"Start up web interpreter");
|
||||
options
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
use std::fmt::Write as FmtWrite;
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::repl::Repl;
|
||||
use crate::repl::old_command_tree::OldCommandTree;
|
||||
use crate::language::{LangMetaRequest, LangMetaResponse, DebugAsk, DebugResponse};
|
||||
|
||||
pub fn directives_from_pass_names(pass_names: &Vec<String>) -> OldCommandTree {
|
||||
let passes_directives: Vec<OldCommandTree> = pass_names.iter()
|
||||
.map(|pass_name| { OldCommandTree::nonterm_no_further_tab_completions(pass_name, None) })
|
||||
.collect();
|
||||
|
||||
OldCommandTree::Top(vec![
|
||||
OldCommandTree::terminal("exit", Some("exit the REPL"), vec![], Box::new(|repl: &mut Repl, _cmds: &[&str]| {
|
||||
repl.save_before_exit();
|
||||
::std::process::exit(0)
|
||||
})),
|
||||
OldCommandTree::terminal("quit", Some("exit the REPL"), vec![], Box::new(|repl: &mut Repl, _cmds: &[&str]| {
|
||||
repl.save_before_exit();
|
||||
::std::process::exit(0)
|
||||
})),
|
||||
OldCommandTree::terminal("help", Some("Print this help message"), vec![], Box::new(|repl: &mut Repl, cmds: &[&str]| {
|
||||
Some(repl.print_help_message(cmds))
|
||||
})),
|
||||
OldCommandTree::nonterm("debug",
|
||||
Some("Configure debug information"),
|
||||
vec![
|
||||
OldCommandTree::terminal("list-passes", Some("List all registered compiler passes"), vec![], Box::new(|repl: &mut Repl, _cmds: &[&str]| {
|
||||
let language_state = repl.get_cur_language_state();
|
||||
let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) {
|
||||
LangMetaResponse::StageNames(names) => names,
|
||||
_ => vec![],
|
||||
};
|
||||
|
||||
let mut buf = String::new();
|
||||
for pass in pass_names.iter().map(|name| Some(name)).intersperse(None) {
|
||||
match pass {
|
||||
Some(pass) => write!(buf, "{}", pass).unwrap(),
|
||||
None => write!(buf, " -> ").unwrap(),
|
||||
}
|
||||
}
|
||||
Some(buf)
|
||||
})),
|
||||
OldCommandTree::terminal("show-immediate", None, passes_directives.clone(),
|
||||
Box::new(|repl: &mut Repl, cmds: &[&str]| {
|
||||
let cur_state = repl.get_cur_language_state();
|
||||
let stage_name = match cmds.get(1) {
|
||||
Some(s) => s.to_string(),
|
||||
None => return Some(format!("Must specify a thing to debug")),
|
||||
};
|
||||
let meta = LangMetaRequest::ImmediateDebug(DebugAsk::ByStage { stage_name: stage_name.clone() });
|
||||
|
||||
let response = match cur_state.request_meta(meta) {
|
||||
LangMetaResponse::ImmediateDebug(DebugResponse { ask, value }) => {
|
||||
if (ask != DebugAsk::ByStage { stage_name: stage_name }) {
|
||||
return Some(format!("Didn't get debug stage requested"));
|
||||
}
|
||||
value
|
||||
},
|
||||
_ => return Some(format!("Invalid language meta response")),
|
||||
};
|
||||
Some(response)
|
||||
})),
|
||||
OldCommandTree::terminal("show", None, passes_directives.clone(), Box::new(|repl: &mut Repl, cmds: &[&str]| {
|
||||
let stage_name = match cmds.get(0) {
|
||||
Some(s) => s.to_string(),
|
||||
None => return Some(format!("Must specify a stage to show")),
|
||||
};
|
||||
let ask = DebugAsk::ByStage { stage_name };
|
||||
repl.options.debug_asks.insert(ask);
|
||||
None
|
||||
})),
|
||||
OldCommandTree::terminal("hide", None, passes_directives.clone(), Box::new(|repl: &mut Repl, cmds: &[&str]| {
|
||||
let stage_name = match cmds.get(0) {
|
||||
Some(s) => s.to_string(),
|
||||
None => return Some(format!("Must specify a stage to hide")),
|
||||
};
|
||||
let ask = DebugAsk::ByStage { stage_name };
|
||||
repl.options.debug_asks.remove(&ask);
|
||||
None
|
||||
})),
|
||||
OldCommandTree::nonterm("total-time", None, vec![
|
||||
OldCommandTree::terminal("on", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| {
|
||||
repl.options.show_total_time = true;
|
||||
None
|
||||
})),
|
||||
OldCommandTree::terminal("off", None, vec![], Box::new(turn_off)),
|
||||
]),
|
||||
OldCommandTree::nonterm("stage-times", Some("Computation time per-stage"), vec![
|
||||
OldCommandTree::terminal("on", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| {
|
||||
repl.options.show_stage_times = true;
|
||||
None
|
||||
})),
|
||||
OldCommandTree::terminal("off", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| {
|
||||
repl.options.show_stage_times = false;
|
||||
None
|
||||
})),
|
||||
])
|
||||
]
|
||||
),
|
||||
OldCommandTree::nonterm("lang",
|
||||
Some("switch between languages, or go directly to a langauge by name"),
|
||||
vec![
|
||||
OldCommandTree::nonterm_no_further_tab_completions("next", None),
|
||||
OldCommandTree::nonterm_no_further_tab_completions("prev", None),
|
||||
OldCommandTree::nonterm("go", None, vec![]),
|
||||
]
|
||||
),
|
||||
OldCommandTree::terminal("doc", Some("Get language-specific help for an item"), vec![], Box::new(|repl: &mut Repl, cmds: &[&str]| {
|
||||
cmds.get(0).map(|cmd| {
|
||||
let source = cmd.to_string();
|
||||
let meta = LangMetaRequest::Docs { source };
|
||||
let cur_state = repl.get_cur_language_state();
|
||||
match cur_state.request_meta(meta) {
|
||||
LangMetaResponse::Docs { doc_string } => Some(doc_string),
|
||||
_ => Some(format!("Invalid doc response"))
|
||||
}
|
||||
}).unwrap_or(Some(format!(":docs needs an argument")))
|
||||
}))
|
||||
])
|
||||
}
|
||||
|
||||
fn turn_off(repl: &mut Repl, _cmds: &[&str]) -> Option<String> {
|
||||
repl.options.show_total_time = false;
|
||||
None
|
||||
}
|
||||
@@ -1,302 +0,0 @@
|
||||
use std::fmt::Write as FmtWrite;
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use colored::*;
|
||||
use crate::language::{ProgrammingLanguageInterface,
|
||||
ComputationRequest, ComputationResponse,
|
||||
DebugAsk, LangMetaResponse, LangMetaRequest};
|
||||
|
||||
mod old_command_tree;
|
||||
use self::old_command_tree::{OldCommandTree, BoxedCommandFunction};
|
||||
mod repl_options;
|
||||
use repl_options::ReplOptions;
|
||||
mod directives;
|
||||
use directives::directives_from_pass_names;
|
||||
|
||||
const HISTORY_SAVE_FILE: &'static str = ".schala_history";
|
||||
const OPTIONS_SAVE_FILE: &'static str = ".schala_repl";
|
||||
|
||||
pub struct Repl {
|
||||
interpreter_directive_sigil: char,
|
||||
line_reader: ::linefeed::interface::Interface<::linefeed::terminal::DefaultTerminal>,
|
||||
language_states: Vec<Box<ProgrammingLanguageInterface>>,
|
||||
options: ReplOptions,
|
||||
directives: OldCommandTree,
|
||||
}
|
||||
|
||||
impl Repl {
|
||||
pub fn new(mut initial_states: Vec<Box<ProgrammingLanguageInterface>>) -> Repl {
|
||||
use linefeed::Interface;
|
||||
let line_reader = Interface::new("schala-repl").unwrap();
|
||||
let interpreter_directive_sigil = ':';
|
||||
|
||||
let pass_names = match initial_states[0].request_meta(LangMetaRequest::StageNames) {
|
||||
LangMetaResponse::StageNames(names) => names,
|
||||
_ => vec![],
|
||||
};
|
||||
|
||||
Repl {
|
||||
interpreter_directive_sigil,
|
||||
line_reader,
|
||||
language_states: initial_states,
|
||||
options: ReplOptions::new(),
|
||||
directives: directives_from_pass_names(&pass_names)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_repl(&mut self) {
|
||||
println!("Schala MetaInterpreter version {}", crate::VERSION_STRING);
|
||||
println!("Type {}help for help with the REPL", self.interpreter_directive_sigil);
|
||||
self.load_options();
|
||||
self.handle_repl_loop();
|
||||
self.save_before_exit();
|
||||
println!("Exiting...");
|
||||
}
|
||||
|
||||
fn load_options(&mut self) {
|
||||
self.line_reader.load_history(HISTORY_SAVE_FILE).unwrap_or(());
|
||||
match ReplOptions::load_from_file(OPTIONS_SAVE_FILE) {
|
||||
Ok(options) => {
|
||||
self.options = options;
|
||||
},
|
||||
Err(()) => ()
|
||||
};
|
||||
}
|
||||
|
||||
fn handle_repl_loop(&mut self) {
|
||||
use linefeed::ReadResult::*;
|
||||
|
||||
loop {
|
||||
self.update_line_reader();
|
||||
match self.line_reader.read_line() {
|
||||
Err(e) => {
|
||||
println!("readline IO Error: {}", e);
|
||||
break;
|
||||
},
|
||||
Ok(Eof) | Ok(Signal(_)) => break,
|
||||
Ok(Input(ref input)) => {
|
||||
self.line_reader.add_history_unique(input.to_string());
|
||||
let output = match input.chars().nth(0) {
|
||||
Some(ch) if ch == self.interpreter_directive_sigil => self.handle_interpreter_directive(input),
|
||||
_ => Some(self.handle_input(input)),
|
||||
};
|
||||
if let Some(o) = output {
|
||||
println!("=> {}", o);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_line_reader(&mut self) {
|
||||
let tab_complete_handler = TabCompleteHandler::new(self.interpreter_directive_sigil, self.get_directives());
|
||||
self.line_reader.set_completer(Arc::new(tab_complete_handler)); //TODO fix this here
|
||||
let prompt_str = format!(">> ");
|
||||
self.line_reader.set_prompt(&prompt_str).unwrap();
|
||||
}
|
||||
|
||||
fn save_before_exit(&self) {
|
||||
self.line_reader.save_history(HISTORY_SAVE_FILE).unwrap_or(());
|
||||
self.options.save_to_file(OPTIONS_SAVE_FILE);
|
||||
}
|
||||
|
||||
fn get_function_from_directives<'a>(directives: &'a OldCommandTree, commands: &Vec<&str>) -> Result<(&'a BoxedCommandFunction, usize), String> {
|
||||
let mut dir_pointer: &OldCommandTree = &directives;
|
||||
let mut idx = 0;
|
||||
|
||||
loop {
|
||||
match dir_pointer {
|
||||
OldCommandTree::Top(subcommands) | OldCommandTree::NonTerminal { children: subcommands, .. } => {
|
||||
let next_command = match commands.get(idx) {
|
||||
Some(cmd) => cmd,
|
||||
None => break Err(format!("Command requires arguments"))
|
||||
};
|
||||
idx += 1;
|
||||
match subcommands.iter().find(|sc| sc.get_cmd() == *next_command) {
|
||||
Some(command_tree) => {
|
||||
dir_pointer = command_tree;
|
||||
},
|
||||
None => break Err(format!("Command {} not found", next_command))
|
||||
};
|
||||
},
|
||||
OldCommandTree::Terminal { function, .. } => {
|
||||
break Ok((function, idx));
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_interpreter_directive(&mut self, input: &str) -> Option<String> {
|
||||
let mut iter = input.chars();
|
||||
iter.next();
|
||||
let commands: Vec<&str> = iter
|
||||
.as_str()
|
||||
.split_whitespace()
|
||||
.collect();
|
||||
|
||||
if commands.len() < 1 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let directives = self.get_directives();
|
||||
let result: Result<(&BoxedCommandFunction, _), String> = Repl::get_function_from_directives(&directives, &commands);
|
||||
match result {
|
||||
Ok((f, idx)) => f(self, &commands[idx..]),
|
||||
Err(err) => Some(err.red().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn print_help_message(&mut self, commands_passed_to_help: &[&str] ) -> String {
|
||||
let mut buf = String::new();
|
||||
let directives = match self.get_directives() {
|
||||
OldCommandTree::Top(children) => children,
|
||||
_ => panic!("Top-level OldCommandTree not Top")
|
||||
};
|
||||
|
||||
match commands_passed_to_help {
|
||||
[] => {
|
||||
writeln!(buf, "MetaInterpreter options").unwrap();
|
||||
writeln!(buf, "-----------------------").unwrap();
|
||||
|
||||
for directive in directives {
|
||||
let trailer = " ";
|
||||
writeln!(buf, "{}{}- {}", directive.get_cmd(), trailer, directive.get_help()).unwrap();
|
||||
}
|
||||
|
||||
let ref lang = self.get_cur_language_state();
|
||||
writeln!(buf, "").unwrap();
|
||||
writeln!(buf, "Language-specific help for {}", lang.get_language_name()).unwrap();
|
||||
writeln!(buf, "-----------------------").unwrap();
|
||||
},
|
||||
_ => {
|
||||
writeln!(buf, "Command-specific help not available yet").unwrap();
|
||||
}
|
||||
};
|
||||
buf
|
||||
}
|
||||
|
||||
fn get_cur_language_state(&mut self) -> &mut Box<ProgrammingLanguageInterface> {
|
||||
//TODO this is obviously not complete
|
||||
&mut self.language_states[0]
|
||||
}
|
||||
|
||||
fn handle_input(&mut self, input: &str) -> String {
|
||||
let mut debug_requests = HashSet::new();
|
||||
for ask in self.options.debug_asks.iter() {
|
||||
debug_requests.insert(ask.clone());
|
||||
}
|
||||
|
||||
let request = ComputationRequest {
|
||||
source: input,
|
||||
debug_requests,
|
||||
};
|
||||
|
||||
let ref mut language_state = self.get_cur_language_state();
|
||||
let response = language_state.run_computation(request);
|
||||
|
||||
self.handle_computation_response(response)
|
||||
}
|
||||
|
||||
fn handle_computation_response(&mut self, response: ComputationResponse) -> String {
|
||||
let mut buf = String::new();
|
||||
|
||||
if self.options.show_total_time {
|
||||
buf.push_str(&format!("Total duration: {:?}\n", response.global_output_stats.total_duration));
|
||||
}
|
||||
|
||||
if self.options.show_stage_times {
|
||||
buf.push_str(&format!("{:?}\n", response.global_output_stats.stage_durations));
|
||||
}
|
||||
|
||||
|
||||
for debug_resp in response.debug_responses {
|
||||
let stage_name = match debug_resp.ask {
|
||||
DebugAsk::ByStage { stage_name } => stage_name,
|
||||
_ => continue,
|
||||
};
|
||||
let s = format!("{} - {}\n", stage_name, debug_resp.value);
|
||||
buf.push_str(&s);
|
||||
}
|
||||
|
||||
buf.push_str(&match response.main_output {
|
||||
Ok(s) => s,
|
||||
Err(e) => format!("{} {}", "Error".red(), e)
|
||||
});
|
||||
|
||||
buf
|
||||
}
|
||||
|
||||
fn get_directives(&mut self) -> OldCommandTree {
|
||||
let language_state = self.get_cur_language_state();
|
||||
let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) {
|
||||
LangMetaResponse::StageNames(names) => names,
|
||||
_ => vec![],
|
||||
};
|
||||
|
||||
directives_from_pass_names(&pass_names)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct TabCompleteHandler {
|
||||
sigil: char,
|
||||
top_level_commands: OldCommandTree,
|
||||
}
|
||||
|
||||
use linefeed::complete::{Completion, Completer};
|
||||
use linefeed::terminal::Terminal;
|
||||
|
||||
impl TabCompleteHandler {
|
||||
fn new(sigil: char, top_level_commands: OldCommandTree) -> TabCompleteHandler {
|
||||
TabCompleteHandler {
|
||||
top_level_commands,
|
||||
sigil,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Terminal> Completer<T> for TabCompleteHandler {
|
||||
fn complete(&self, word: &str, prompter: &::linefeed::prompter::Prompter<T>, start: usize, _end: usize) -> Option<Vec<Completion>> {
|
||||
let line = prompter.buffer();
|
||||
|
||||
if !line.starts_with(self.sigil) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut words = line[1..(if start == 0 { 1 } else { start })].split_whitespace();
|
||||
let mut completions = Vec::new();
|
||||
let mut command_tree: Option<&OldCommandTree> = Some(&self.top_level_commands);
|
||||
|
||||
loop {
|
||||
match words.next() {
|
||||
None => {
|
||||
let top = match command_tree {
|
||||
Some(OldCommandTree::Top(_)) => true,
|
||||
_ => false
|
||||
};
|
||||
let word = if top { word.get(1..).unwrap() } else { word };
|
||||
for cmd in command_tree.map(|x| x.get_children()).unwrap_or(vec![]).into_iter() {
|
||||
if cmd.starts_with(word) {
|
||||
completions.push(Completion {
|
||||
completion: format!("{}{}", if top { ":" } else { "" }, cmd),
|
||||
display: Some(cmd.to_string()),
|
||||
suffix: ::linefeed::complete::Suffix::Some(' ')
|
||||
})
|
||||
}
|
||||
}
|
||||
break;
|
||||
},
|
||||
Some(s) => {
|
||||
let new_ptr: Option<&OldCommandTree> = command_tree.and_then(|cm| match cm {
|
||||
OldCommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s),
|
||||
OldCommandTree::NonTerminal { children, .. } => children.iter().find(|c| c.get_cmd() == s),
|
||||
OldCommandTree::Terminal { children, .. } => children.iter().find(|c| c.get_cmd() == s),
|
||||
});
|
||||
command_tree = new_ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(completions)
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
use super::Repl;
|
||||
|
||||
pub type BoxedCommandFunction = Box<(fn(&mut Repl, &[&str]) -> Option<String>)>;
|
||||
|
||||
/// A OldCommandTree is either a `Terminal` or a `NonTerminal`. When command parsing reaches the first
|
||||
/// Terminal, it will execute the `BoxedCommandFunction` found there with any remaining arguments
|
||||
#[derive(Clone)]
|
||||
pub enum OldCommandTree {
|
||||
Terminal {
|
||||
name: String,
|
||||
children: Vec<OldCommandTree>,
|
||||
help_msg: Option<String>,
|
||||
function: BoxedCommandFunction,
|
||||
},
|
||||
NonTerminal {
|
||||
name: String,
|
||||
children: Vec<OldCommandTree>,
|
||||
help_msg: Option<String>,
|
||||
},
|
||||
Top(Vec<OldCommandTree>),
|
||||
}
|
||||
|
||||
impl OldCommandTree {
|
||||
pub fn nonterm_no_further_tab_completions(s: &str, help: Option<&str>) -> OldCommandTree {
|
||||
OldCommandTree::NonTerminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), children: vec![] }
|
||||
}
|
||||
|
||||
pub fn terminal(s: &str, help: Option<&str>, children: Vec<OldCommandTree>, function: BoxedCommandFunction) -> OldCommandTree {
|
||||
OldCommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function, children }
|
||||
}
|
||||
|
||||
pub fn nonterm(s: &str, help: Option<&str>, children: Vec<OldCommandTree>) -> OldCommandTree {
|
||||
OldCommandTree::NonTerminal {
|
||||
name: s.to_string(),
|
||||
help_msg: help.map(|x| x.to_string()),
|
||||
children,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn nonterm_with_function(s: &str, help: Option<&str>, children: Vec<OldCommandTree>, func: BoxedCommandFunction) -> OldCommandTree {
|
||||
OldCommandTree::NonTerminal {
|
||||
name: s.to_string(),
|
||||
help_msg: help.map(|x| x.to_string()),
|
||||
children,
|
||||
function: Some(func),
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn get_cmd(&self) -> &str {
|
||||
match self {
|
||||
OldCommandTree::Terminal { name, .. } => name.as_str(),
|
||||
OldCommandTree::NonTerminal {name, ..} => name.as_str(),
|
||||
OldCommandTree::Top(_) => "",
|
||||
}
|
||||
}
|
||||
pub fn get_help(&self) -> &str {
|
||||
match self {
|
||||
OldCommandTree::Terminal { help_msg, ..} => help_msg.as_ref().map(|s| s.as_str()).unwrap_or(""),
|
||||
OldCommandTree::NonTerminal { help_msg, .. } => help_msg.as_ref().map(|s| s.as_str()).unwrap_or(""),
|
||||
OldCommandTree::Top(_) => ""
|
||||
}
|
||||
}
|
||||
pub fn get_children(&self) -> Vec<&str> {
|
||||
use OldCommandTree::*;
|
||||
match self {
|
||||
Terminal { children, .. } |
|
||||
NonTerminal { children, .. } |
|
||||
Top(children) => children.iter().map(|x| x.get_cmd()).collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
use crate::language::DebugAsk;
|
||||
|
||||
use std::io::{Read, Write};
|
||||
use std::collections::HashSet;
|
||||
use std::fs::File;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ReplOptions {
|
||||
pub debug_asks: HashSet<DebugAsk>,
|
||||
pub show_total_time: bool,
|
||||
pub show_stage_times: bool,
|
||||
}
|
||||
|
||||
impl ReplOptions {
|
||||
pub fn new() -> ReplOptions {
|
||||
ReplOptions {
|
||||
debug_asks: HashSet::new(),
|
||||
show_total_time: true,
|
||||
show_stage_times: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save_to_file(&self, filename: &str) {
|
||||
let res = File::create(filename)
|
||||
.and_then(|mut file| {
|
||||
let buf = crate::serde_json::to_string(self).unwrap();
|
||||
file.write_all(buf.as_bytes())
|
||||
});
|
||||
if let Err(err) = res {
|
||||
println!("Error saving {} file {}", filename, err);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_from_file(filename: &str) -> Result<ReplOptions, ()> {
|
||||
File::open(filename)
|
||||
.and_then(|mut file| {
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents)?;
|
||||
Ok(contents)
|
||||
})
|
||||
.and_then(|contents| {
|
||||
let output: ReplOptions = crate::serde_json::from_str(&contents)?;
|
||||
Ok(output)
|
||||
})
|
||||
.map_err(|_| ())
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
let c = 10
|
||||
|
||||
fn add(a, b) {
|
||||
let c = a + b
|
||||
c
|
||||
}
|
||||
|
||||
let mut b = 20
|
||||
|
||||
println(add(1,2))
|
||||
println(c + b)
|
||||
10
source_files/schala/first.schala
Normal file
10
source_files/schala/first.schala
Normal file
@@ -0,0 +1,10 @@
|
||||
fn main() {
|
||||
const a = 10
|
||||
const b = 20
|
||||
a + b
|
||||
}
|
||||
|
||||
print(main())
|
||||
|
||||
const xxx
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
fn main() {
|
||||
let a = 10
|
||||
let b = 20
|
||||
a + b
|
||||
}
|
||||
|
||||
//this is a one-line comment
|
||||
|
||||
/* this is
|
||||
a multiline
|
||||
comment
|
||||
*/
|
||||
|
||||
print(main())
|
||||
|
||||
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
|
||||
for n <- 1..=100 {
|
||||
if n % 15 == 0 {
|
||||
print("FizzBuzz")
|
||||
} else if n % 5 == 0 {
|
||||
print("Buzz")
|
||||
} else if n % 3 == 0 {
|
||||
print("Fizz")
|
||||
} else {
|
||||
print(n.to_string())
|
||||
}
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
|
||||
fn main() {
|
||||
|
||||
//comments are C-style
|
||||
/* nested comments /* are cool */ */
|
||||
|
||||
}
|
||||
|
||||
@annotations are with @-
|
||||
|
||||
// variable expressions
|
||||
var a: I32 = 20
|
||||
const b: String = 20
|
||||
|
||||
there(); can(); be(); multiple(); statements(); per_line();
|
||||
|
||||
//string interpolation
|
||||
const yolo = "I have ${a + b} people in my house"
|
||||
|
||||
// let expressions ??? not sure if I want this
|
||||
let a = 10, b = 20, c = 30 in a + b + c
|
||||
|
||||
//list literal
|
||||
const q = [1,2,3,4]
|
||||
|
||||
//lambda literal
|
||||
q.map({|item| item * 100 })
|
||||
|
||||
fn yolo(a: MyType, b: YourType): ReturnType<Param1, Param2> {
|
||||
if a == 20 {
|
||||
return "early"
|
||||
}
|
||||
var sex = 20
|
||||
sex
|
||||
}
|
||||
|
||||
|
||||
/* for/while loop topics */
|
||||
|
||||
//infinite loop
|
||||
while {
|
||||
if x() { break }
|
||||
...
|
||||
}
|
||||
|
||||
|
||||
//conditional loop
|
||||
while conditionHolds() {
|
||||
...
|
||||
}
|
||||
|
||||
|
||||
//iteration over a variable
|
||||
for i <- [1..1000] {
|
||||
|
||||
} //return type is return type of block
|
||||
|
||||
|
||||
//monadic decomposition
|
||||
for {
|
||||
a <- maybeInt();
|
||||
s <- foo()
|
||||
} return {
|
||||
a + s
|
||||
} //return type is Monad<return type of block>
|
||||
|
||||
/* end of for loops */
|
||||
|
||||
|
||||
|
||||
/* conditionals/pattern matching */
|
||||
|
||||
// "is" operator for "does this pattern match"
|
||||
|
||||
x is Some(t) // type bool
|
||||
|
||||
if x {
|
||||
is Some(t) => {
|
||||
},
|
||||
is None => {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//syntax is, I guess, for <expr> <brace-block>, where <expr> is a bool, or a <arrow-expr>
|
||||
|
||||
// type level alises
|
||||
typealias <name> = <other type> #maybe thsi should be 'alias'?
|
||||
|
||||
/*
|
||||
what if type A = B meant that you could had to create A's with A(B), but when you used A's the interface was exactly like B's?
|
||||
maybe introduce a 'newtype' keyword for this
|
||||
*/
|
||||
|
||||
//declaring types of all stripes
|
||||
type MyData = { a: i32, b: String }
|
||||
type MyType = MyType
|
||||
type Option<a> = None | Some(a)
|
||||
type Signal = Absence | SimplePresence(i32) | ComplexPresence {a: i32, b: MyCustomData}
|
||||
|
||||
//traits
|
||||
|
||||
trait Bashable { }
|
||||
trait Luggable {
|
||||
fn lug(self, a: Option<Self>)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// lambdas
|
||||
// ruby-style not rust-style
|
||||
const a: X -> Y -> Z = {|x,y| }
|
||||
@@ -1,17 +1,105 @@
|
||||
|
||||
println(sua(4))
|
||||
fn main() {
|
||||
|
||||
# comments are scripting-style
|
||||
#{ but can also be
|
||||
|
||||
}# blocks
|
||||
|
||||
@annotations are with @-
|
||||
|
||||
# variable expressions
|
||||
var a: I32 = 20
|
||||
const b: String = 20
|
||||
|
||||
there(); can(); be(); multiple(); statements(); per_line();
|
||||
|
||||
#string interpolation
|
||||
const yolo = "I have ${a + b} people in my house"
|
||||
|
||||
# let expressions ??? not sure if I want this
|
||||
let a = 10, b = 20, c = 30 in a + b + c
|
||||
|
||||
#list literal
|
||||
const q = [1,2,3,4]
|
||||
|
||||
#lambda literal ?? maybe? not sure how this should work
|
||||
q.map(|item| { item * 100 })
|
||||
|
||||
fn yolo(a: MyType, b: YourType): ReturnType<Param1, Param2> {
|
||||
if a == 20 {
|
||||
return "early"
|
||||
}
|
||||
var sex = 20
|
||||
sex
|
||||
}
|
||||
|
||||
|
||||
for {
|
||||
# infinite loop
|
||||
}
|
||||
|
||||
#iteration over a variable
|
||||
for i <- [1..1000] {
|
||||
|
||||
} #return type is return type of block
|
||||
|
||||
#while loop
|
||||
for a != 3 || fuckTard() {
|
||||
break
|
||||
} #return type is return type of block
|
||||
|
||||
#monadic decomposition
|
||||
for {
|
||||
a <- maybeInt();
|
||||
s <- foo()
|
||||
} return {
|
||||
a + s
|
||||
} #return type is Monad<return type of block>
|
||||
|
||||
# let statements too!!
|
||||
for (a = 20
|
||||
b = fuck) {
|
||||
a + b
|
||||
}
|
||||
|
||||
|
||||
# pattern-matching
|
||||
match <expr> {
|
||||
Some(a) => {
|
||||
|
||||
},
|
||||
None => {
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
#syntax is, I guess, for <expr> <brace-block>, where <expr> is a bool, or a <arrow-expr>
|
||||
|
||||
# type level alises
|
||||
typealias <name> = <other type> #maybe thsi should be 'alias'?
|
||||
|
||||
#what if type A = B meant that you could had to create A's with A(B), but when you used A's the interface was exactly like B's?
|
||||
# maybe introduce a 'newtype' keyword for this
|
||||
|
||||
#declaring types of all stripes
|
||||
type MyData = { a: i32, b: String }
|
||||
type MyType = MyType
|
||||
type Option<a> = None | Some(a)
|
||||
type Signal = Absence | SimplePresence(i32) | ComplexPresence {a: i32, b: MyCustomData}
|
||||
|
||||
#traits
|
||||
|
||||
trait Bashable { }
|
||||
trait Luggable {
|
||||
fn lug(self, a: Option<Self>)
|
||||
}
|
||||
|
||||
fn sua(x): Int {
|
||||
x + 10
|
||||
}
|
||||
|
||||
|
||||
//let a = getline()
|
||||
|
||||
/*
|
||||
if a == "true" {
|
||||
println("You typed true")
|
||||
} else {
|
||||
println("You typed something else")
|
||||
}
|
||||
*/
|
||||
# lambdas
|
||||
#
|
||||
|x,y| { }() #is probably fine
|
||||
const a = |x: Type, y|: RetType { <statementblock> }
|
||||
const a: X -> Y -> Z = |x,y| { }
|
||||
|
||||
279
src/maaru_lang/compilation.rs
Normal file
279
src/maaru_lang/compilation.rs
Normal file
@@ -0,0 +1,279 @@
|
||||
extern crate llvm_sys;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use self::llvm_sys::prelude::*;
|
||||
use self::llvm_sys::{LLVMIntPredicate};
|
||||
|
||||
use maaru_lang::parser::{AST, Statement, Function, Prototype, Expression, BinOp};
|
||||
use schala_lib::LLVMCodeString;
|
||||
|
||||
use schala_lib::llvm_wrap as LLVMWrap;
|
||||
|
||||
type VariableMap = HashMap<String, LLVMValueRef>;
|
||||
|
||||
struct CompilationData {
|
||||
context: LLVMContextRef,
|
||||
module: LLVMModuleRef,
|
||||
builder: LLVMBuilderRef,
|
||||
variables: VariableMap,
|
||||
main_function: LLVMValueRef,
|
||||
current_function: Option<LLVMValueRef>,
|
||||
}
|
||||
|
||||
pub fn compile_ast(ast: AST) -> LLVMCodeString {
|
||||
println!("Compiling!");
|
||||
let names: VariableMap = HashMap::new();
|
||||
|
||||
let context = LLVMWrap::create_context();
|
||||
let module = LLVMWrap::module_create_with_name("example module");
|
||||
let builder = LLVMWrap::CreateBuilderInContext(context);
|
||||
|
||||
let program_return_type = LLVMWrap::Int64TypeInContext(context);
|
||||
let main_function_type = LLVMWrap::FunctionType(program_return_type, Vec::new(), false);
|
||||
let main_function: LLVMValueRef = LLVMWrap::AddFunction(module, "main", main_function_type);
|
||||
|
||||
let mut data = CompilationData {
|
||||
context: context,
|
||||
builder: builder,
|
||||
module: module,
|
||||
variables: names,
|
||||
main_function: main_function,
|
||||
current_function: None,
|
||||
};
|
||||
|
||||
let bb = LLVMWrap::AppendBasicBlockInContext(data.context, data.main_function, "entry");
|
||||
LLVMWrap::PositionBuilderAtEnd(builder, bb);
|
||||
|
||||
let value = ast.codegen(&mut data);
|
||||
|
||||
LLVMWrap::BuildRet(builder, value);
|
||||
|
||||
let ret = LLVMWrap::PrintModuleToString(module);
|
||||
|
||||
// Clean up. Values created in the context mostly get cleaned up there.
|
||||
LLVMWrap::DisposeBuilder(builder);
|
||||
LLVMWrap::DisposeModule(module);
|
||||
LLVMWrap::ContextDispose(context);
|
||||
LLVMCodeString(ret)
|
||||
}
|
||||
|
||||
trait CodeGen {
|
||||
fn codegen(&self, &mut CompilationData) -> LLVMValueRef;
|
||||
}
|
||||
|
||||
impl CodeGen for AST {
|
||||
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
||||
|
||||
let int_type = LLVMWrap::Int64TypeInContext(data.context);
|
||||
let mut ret = LLVMWrap::ConstInt(int_type, 0, false);
|
||||
|
||||
for statement in self {
|
||||
ret = statement.codegen(data);
|
||||
}
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
impl CodeGen for Statement {
|
||||
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
||||
use self::Statement::*;
|
||||
match self {
|
||||
&ExprNode(ref expr) => expr.codegen(data),
|
||||
&FuncDefNode(ref func) => func.codegen(data),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CodeGen for Function {
|
||||
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
||||
|
||||
/* should have a check here for function already being defined */
|
||||
let function = self.prototype.codegen(data);
|
||||
let ref body = self.body;
|
||||
|
||||
data.current_function = Some(function);
|
||||
|
||||
let return_type = LLVMWrap::Int64TypeInContext(data.context);
|
||||
let mut ret = LLVMWrap::ConstInt(return_type, 0, false);
|
||||
|
||||
let block = LLVMWrap::AppendBasicBlockInContext(data.context, function, "entry");
|
||||
LLVMWrap::PositionBuilderAtEnd(data.builder, block);
|
||||
|
||||
//insert function params into variables
|
||||
for value in LLVMWrap::GetParams(function) {
|
||||
let name = LLVMWrap::GetValueName(value);
|
||||
data.variables.insert(name, value);
|
||||
}
|
||||
|
||||
for expr in body {
|
||||
ret = expr.codegen(data);
|
||||
}
|
||||
|
||||
LLVMWrap::BuildRet(data.builder, ret);
|
||||
|
||||
// get basic block of main
|
||||
let main_bb = LLVMWrap::GetBasicBlocks(data.main_function).get(0).expect("Couldn't get first block of main").clone();
|
||||
LLVMWrap::PositionBuilderAtEnd(data.builder, main_bb);
|
||||
|
||||
data.current_function = None;
|
||||
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
impl CodeGen for Prototype {
|
||||
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
||||
let num_args = self.parameters.len();
|
||||
let return_type = LLVMWrap::Int64TypeInContext(data.context);
|
||||
let mut arguments: Vec<LLVMTypeRef> = vec![];
|
||||
|
||||
for _ in 0..num_args {
|
||||
arguments.push(LLVMWrap::Int64TypeInContext(data.context));
|
||||
}
|
||||
|
||||
let function_type =
|
||||
LLVMWrap::FunctionType(return_type,
|
||||
arguments,
|
||||
false);
|
||||
|
||||
let function = LLVMWrap::AddFunction(data.module,
|
||||
&*self.name,
|
||||
function_type);
|
||||
|
||||
let function_params = LLVMWrap::GetParams(function);
|
||||
for (index, param) in function_params.iter().enumerate() {
|
||||
let name = self.parameters.get(index).expect(&format!("Failed this check at index {}", index));
|
||||
let new = *param;
|
||||
|
||||
LLVMWrap::SetValueName(new, name);
|
||||
}
|
||||
|
||||
function
|
||||
}
|
||||
}
|
||||
|
||||
impl CodeGen for Expression {
|
||||
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
||||
use self::BinOp::*;
|
||||
use self::Expression::*;
|
||||
|
||||
let int_type = LLVMWrap::Int64TypeInContext(data.context);
|
||||
let zero = LLVMWrap::ConstInt(int_type, 0, false);
|
||||
|
||||
match *self {
|
||||
Variable(ref name) => *data.variables.get(&**name).expect(&format!("Can't find variable {}", name)),
|
||||
BinExp(Assign, ref left, ref right) => {
|
||||
if let Variable(ref name) = **left {
|
||||
let new_value = right.codegen(data);
|
||||
data.variables.insert((**name).clone(), new_value);
|
||||
new_value
|
||||
} else {
|
||||
panic!("Bad variable assignment")
|
||||
}
|
||||
}
|
||||
BinExp(ref op, ref left, ref right) => {
|
||||
let lhs = left.codegen(data);
|
||||
let rhs = right.codegen(data);
|
||||
op.codegen_with_ops(data, lhs, rhs)
|
||||
}
|
||||
Number(ref n) => {
|
||||
let native_val = *n as u64;
|
||||
let int_value: LLVMValueRef = LLVMWrap::ConstInt(int_type, native_val, false);
|
||||
int_value
|
||||
}
|
||||
Conditional(ref test, ref then_expr, ref else_expr) => {
|
||||
let condition_value = test.codegen(data);
|
||||
let is_nonzero =
|
||||
LLVMWrap::BuildICmp(data.builder,
|
||||
LLVMIntPredicate::LLVMIntNE,
|
||||
condition_value,
|
||||
zero,
|
||||
"ifcond");
|
||||
|
||||
let func = LLVMWrap::GetBasicBlockParent(LLVMWrap::GetInsertBlock(data.builder));
|
||||
|
||||
let mut then_block =
|
||||
LLVMWrap::AppendBasicBlockInContext(data.context, func, "then_block");
|
||||
let mut else_block =
|
||||
LLVMWrap::AppendBasicBlockInContext(data.context, func, "else_block");
|
||||
let merge_block =
|
||||
LLVMWrap::AppendBasicBlockInContext(data.context, func, "ifcont");
|
||||
|
||||
// add conditional branch to ifcond block
|
||||
LLVMWrap::BuildCondBr(data.builder, is_nonzero, then_block, else_block);
|
||||
|
||||
// start inserting into then block
|
||||
LLVMWrap::PositionBuilderAtEnd(data.builder, then_block);
|
||||
|
||||
// then-block codegen
|
||||
let then_return = then_expr.codegen(data);
|
||||
LLVMWrap::BuildBr(data.builder, merge_block);
|
||||
|
||||
// update then block b/c recursive codegen() call may have changed the notion of
|
||||
// the current block
|
||||
then_block = LLVMWrap::GetInsertBlock(data.builder);
|
||||
|
||||
// then do the same stuff again for the else branch
|
||||
//
|
||||
LLVMWrap::PositionBuilderAtEnd(data.builder, else_block);
|
||||
let else_return = match *else_expr {
|
||||
Some(ref e) => e.codegen(data),
|
||||
None => zero,
|
||||
};
|
||||
LLVMWrap::BuildBr(data.builder, merge_block);
|
||||
else_block = LLVMWrap::GetInsertBlock(data.builder);
|
||||
|
||||
LLVMWrap::PositionBuilderAtEnd(data.builder, merge_block);
|
||||
|
||||
let phi = LLVMWrap::BuildPhi(data.builder, int_type, "phinode");
|
||||
let values = vec![then_return, else_return];
|
||||
let blocks = vec![then_block, else_block];
|
||||
LLVMWrap::AddIncoming(phi, values, blocks);
|
||||
phi
|
||||
}
|
||||
Block(ref exprs) => {
|
||||
let mut ret = zero;
|
||||
for e in exprs.iter() {
|
||||
ret = e.codegen(data);
|
||||
}
|
||||
ret
|
||||
}
|
||||
ref e => {
|
||||
println!("Unimplemented {:?}", e);
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BinOp {
|
||||
fn codegen_with_ops(&self, data: &CompilationData, lhs: LLVMValueRef, rhs: LLVMValueRef) -> LLVMValueRef {
|
||||
use self::BinOp::*;
|
||||
macro_rules! simple_binop {
|
||||
($fnname: expr, $name: expr) => {
|
||||
$fnname(data.builder, lhs, rhs, $name)
|
||||
}
|
||||
}
|
||||
let int_type = LLVMWrap::Int64TypeInContext(data.context);
|
||||
match *self {
|
||||
Add => simple_binop!(LLVMWrap::BuildAdd, "addtemp"),
|
||||
Sub => simple_binop!(LLVMWrap::BuildSub, "subtemp"),
|
||||
Mul => simple_binop!(LLVMWrap::BuildMul, "multemp"),
|
||||
Div => simple_binop!(LLVMWrap::BuildUDiv, "divtemp"),
|
||||
Mod => simple_binop!(LLVMWrap::BuildSRem, "remtemp"),
|
||||
Less => {
|
||||
let pred: LLVMValueRef =
|
||||
LLVMWrap::BuildICmp(data.builder, LLVMIntPredicate::LLVMIntULT, lhs, rhs, "tmp");
|
||||
LLVMWrap::BuildZExt(data.builder, pred, int_type, "temp")
|
||||
}
|
||||
Greater => {
|
||||
let pred: LLVMValueRef =
|
||||
LLVMWrap::BuildICmp(data.builder, LLVMIntPredicate::LLVMIntUGT, lhs, rhs, "tmp");
|
||||
LLVMWrap::BuildZExt(data.builder, pred, int_type, "temp")
|
||||
}
|
||||
ref unknown => panic!("Bad operator {:?}", unknown),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@ extern crate take_mut;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::VecDeque;
|
||||
use parser::{AST, Statement, Expression, Function, Callable, BinOp};
|
||||
use maaru_lang::parser::{AST, Statement, Expression, Function, Callable, BinOp};
|
||||
use std::rc::Rc;
|
||||
use std::io::{Write, Stdout, BufWriter};
|
||||
use std::convert::From;
|
||||
|
||||
use parser::Expression::*;
|
||||
use parser::Statement::*;
|
||||
use maaru_lang::parser::Expression::*;
|
||||
use maaru_lang::parser::Statement::*;
|
||||
|
||||
type Reduction<T> = (T, Option<SideEffect>);
|
||||
|
||||
99
src/maaru_lang/mod.rs
Normal file
99
src/maaru_lang/mod.rs
Normal file
@@ -0,0 +1,99 @@
|
||||
pub mod tokenizer;
|
||||
pub mod parser;
|
||||
pub mod eval;
|
||||
pub mod compilation;
|
||||
|
||||
use schala_lib::{ProgrammingLanguageInterface, EvalOptions, LanguageOutput, TraceArtifact, LLVMCodeString};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TokenError {
|
||||
pub msg: String,
|
||||
}
|
||||
|
||||
impl TokenError {
|
||||
pub fn new(msg: &str) -> TokenError {
|
||||
TokenError { msg: msg.to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
pub use self::eval::Evaluator as MaaruEvaluator;
|
||||
|
||||
pub struct Maaru<'a> {
|
||||
evaluator: MaaruEvaluator<'a>
|
||||
}
|
||||
|
||||
impl<'a> Maaru<'a> {
|
||||
pub fn new() -> Maaru<'a> {
|
||||
Maaru {
|
||||
evaluator: MaaruEvaluator::new(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ProgrammingLanguageInterface for Maaru<'a> {
|
||||
fn get_language_name(&self) -> String {
|
||||
"Maaru".to_string()
|
||||
}
|
||||
fn get_source_file_suffix(&self) -> String {
|
||||
format!("maaru")
|
||||
}
|
||||
|
||||
fn evaluate_in_repl(&mut self, input: &str, options: &EvalOptions) -> LanguageOutput {
|
||||
let mut output = LanguageOutput::default();
|
||||
|
||||
let tokens = match tokenizer::tokenize(input) {
|
||||
Ok(tokens) => {
|
||||
if options.debug_tokens {
|
||||
output.add_artifact(TraceArtifact::new("tokens", format!("{:?}", tokens)));
|
||||
}
|
||||
tokens
|
||||
},
|
||||
Err(err) => {
|
||||
output.add_output(format!("Tokenization error: {:?}\n", err.msg));
|
||||
return output;
|
||||
}
|
||||
};
|
||||
|
||||
let ast = match parser::parse(&tokens, &[]) {
|
||||
Ok(ast) => {
|
||||
if options.debug_parse {
|
||||
output.add_artifact(TraceArtifact::new("ast", format!("{:?}", ast)));
|
||||
}
|
||||
ast
|
||||
},
|
||||
Err(err) => {
|
||||
output.add_output(format!("Parse error: {:?}\n", err.msg));
|
||||
return output;
|
||||
}
|
||||
};
|
||||
let mut evaluation_output = String::new();
|
||||
for s in self.evaluator.run(ast).iter() {
|
||||
evaluation_output.push_str(s);
|
||||
}
|
||||
output.add_output(evaluation_output);
|
||||
return output;
|
||||
}
|
||||
|
||||
fn can_compile(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn compile(&mut self, input: &str) -> LLVMCodeString {
|
||||
let tokens = match tokenizer::tokenize(input) {
|
||||
Ok(tokens) => tokens,
|
||||
Err(err) => {
|
||||
let msg = format!("Tokenization error: {:?}\n", err.msg);
|
||||
panic!("{}", msg);
|
||||
}
|
||||
};
|
||||
|
||||
let ast = match parser::parse(&tokens, &[]) {
|
||||
Ok(ast) => ast,
|
||||
Err(err) => {
|
||||
let msg = format!("Parse error: {:?}\n", err.msg);
|
||||
panic!("{}", msg);
|
||||
}
|
||||
};
|
||||
compilation::compile_ast(ast)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
use tokenizer::{Token, Kw, OpTok};
|
||||
use tokenizer::Token::*;
|
||||
use maaru_lang::tokenizer::{Token, Kw, OpTok};
|
||||
use maaru_lang::tokenizer::Token::*;
|
||||
|
||||
use std::fmt;
|
||||
use std::collections::VecDeque;
|
||||
@@ -5,7 +5,7 @@ use std::str::Chars;
|
||||
use self::itertools::Itertools;
|
||||
use std::rc::Rc;
|
||||
|
||||
use TokenError;
|
||||
use maaru_lang::TokenError;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Token {
|
||||
30
src/main.rs
30
src/main.rs
@@ -1,15 +1,29 @@
|
||||
extern crate schala_repl;
|
||||
#![feature(advanced_slice_patterns, slice_patterns, box_patterns, box_syntax)]
|
||||
#![feature(plugin)]
|
||||
extern crate itertools;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
#[macro_use]
|
||||
extern crate maplit;
|
||||
|
||||
//extern crate maaru_lang;
|
||||
//extern crate rukka_lang;
|
||||
//extern crate robo_lang;
|
||||
extern crate schala_lang;
|
||||
use schala_repl::{ProgrammingLanguageInterface, start_repl};
|
||||
mod schala_lang;
|
||||
mod maaru_lang;
|
||||
mod robo_lang;
|
||||
mod rukka_lang;
|
||||
|
||||
extern crate schala_lib;
|
||||
use schala_lib::{PLIGenerator, schala_main};
|
||||
|
||||
extern { }
|
||||
|
||||
fn main() {
|
||||
let langs: Vec<Box<ProgrammingLanguageInterface>> = vec![Box::new(schala_lang::Schala::new())];
|
||||
start_repl(langs);
|
||||
let generators: Vec<PLIGenerator> = vec![
|
||||
Box::new(|| { Box::new(schala_lang::Schala::new())}),
|
||||
Box::new(|| { Box::new(schala_lang::autoparser::Schala::new())}),
|
||||
Box::new(|| { Box::new(maaru_lang::Maaru::new())}),
|
||||
Box::new(|| { Box::new(robo_lang::Robo::new())}),
|
||||
Box::new(|| { Box::new(rukka_lang::Rukka::new())}),
|
||||
];
|
||||
schala_main(generators);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
#![feature(box_patterns)]
|
||||
|
||||
extern crate itertools;
|
||||
extern crate schala_repl;
|
||||
|
||||
use itertools::Itertools;
|
||||
use schala_repl::{ProgrammingLanguageInterface, EvalOptions};
|
||||
use schala_lib::{ProgrammingLanguageInterface, EvalOptions, LanguageOutput};
|
||||
|
||||
pub struct Robo {
|
||||
}
|
||||
@@ -154,5 +149,19 @@ impl ProgrammingLanguageInterface for Robo {
|
||||
fn get_source_file_suffix(&self) -> String {
|
||||
format!("robo")
|
||||
}
|
||||
|
||||
fn evaluate_in_repl(&mut self, input: &str, _eval_options: &EvalOptions) -> LanguageOutput {
|
||||
let mut output = LanguageOutput::default();
|
||||
let tokens = match tokenize(input) {
|
||||
Ok(tokens) => tokens,
|
||||
Err(e) => {
|
||||
output.add_output(format!("Tokenize error: {:?}", e));
|
||||
return output;
|
||||
}
|
||||
};
|
||||
|
||||
output.add_output(format!("{:?}", tokens));
|
||||
output
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
#![feature(box_patterns)]
|
||||
|
||||
extern crate itertools;
|
||||
extern crate schala_repl;
|
||||
|
||||
use itertools::Itertools;
|
||||
use schala_repl::{ProgrammingLanguageInterface, EvalOptions};
|
||||
use schala_lib::{ProgrammingLanguageInterface, EvalOptions, LanguageOutput};
|
||||
use std::iter::Peekable;
|
||||
use std::vec::IntoIter;
|
||||
use std::str::Chars;
|
||||
@@ -72,6 +67,26 @@ impl ProgrammingLanguageInterface for Rukka {
|
||||
fn get_source_file_suffix(&self) -> String {
|
||||
format!("rukka")
|
||||
}
|
||||
|
||||
fn evaluate_in_repl(&mut self, input: &str, _eval_options: &EvalOptions) -> LanguageOutput {
|
||||
let mut output = LanguageOutput::default();
|
||||
let sexps = match read(input) {
|
||||
Err(err) => {
|
||||
output.add_output(format!("Error: {}", err));
|
||||
return output;
|
||||
},
|
||||
Ok(sexps) => sexps
|
||||
};
|
||||
|
||||
let output_str: String = sexps.into_iter().enumerate().map(|(i, sexp)| {
|
||||
match self.state.eval(sexp) {
|
||||
Ok(result) => format!("{}: {}", i, result.print()),
|
||||
Err(err) => format!("{} Error: {}", i, err),
|
||||
}
|
||||
}).intersperse(format!("\n")).collect();
|
||||
output.add_output(output_str);
|
||||
output
|
||||
}
|
||||
}
|
||||
|
||||
impl EvaluatorState {
|
||||
133
src/schala_lang/autoparser.rs
Normal file
133
src/schala_lang/autoparser.rs
Normal file
@@ -0,0 +1,133 @@
|
||||
use schala_lib::{ProgrammingLanguageInterface, EvalOptions, TraceArtifact, LanguageOutput};
|
||||
use itertools::Itertools;
|
||||
|
||||
use schala_lang::{tokenizing, parsing};
|
||||
use self::tokenizing::*;
|
||||
use self::parsing::*;
|
||||
|
||||
use schala_lang::tokenizing::TokenType::*;
|
||||
|
||||
struct AutoParser {
|
||||
tokens: Vec<Token>,
|
||||
}
|
||||
|
||||
/* BNF
|
||||
* all terminals in this BNF refer to TokenType values
|
||||
|
||||
literal := Kw::True | Kw::False | StrLiteral | number_literal
|
||||
number_literal := int_literal | float_literal
|
||||
float_literal := digits float_continued
|
||||
float_continued := ε | Period digits
|
||||
int_literal := HexLiteral | nonhex_int
|
||||
nonhex_int := BinNumberSigil+ digits
|
||||
digits := (DigitGroup Underscore)+
|
||||
*/
|
||||
|
||||
impl AutoParser {
|
||||
fn new(tokens: Vec<Token>) -> AutoParser {
|
||||
AutoParser { tokens: tokens.into_iter().rev().collect() }
|
||||
}
|
||||
fn peek(&mut self) -> TokenType {
|
||||
self.tokens.last().map(|ref t| { t.token_type.clone() }).unwrap_or(TokenType::EOF)
|
||||
}
|
||||
fn next(&mut self) -> TokenType {
|
||||
self.tokens.pop().map(|t| { t.token_type }).unwrap_or(TokenType::EOF)
|
||||
}
|
||||
fn parse(&mut self) -> (Result<AST, ParseError>, Vec<String>) {
|
||||
let ast = self.program();
|
||||
(ast, vec![])
|
||||
}
|
||||
fn program(&mut self) -> ParseResult<AST> {
|
||||
let etype = self.literal()?;
|
||||
Ok(AST(vec![Statement::ExpressionStatement(Expression(etype, None))]))
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! expand_match_var {
|
||||
(($pat:pat => $e:expr)) => { $pat };
|
||||
(nonterm ($pat:pat => $e:expr)) => { $pat };
|
||||
}
|
||||
|
||||
macro_rules! expand_match_expr {
|
||||
($self:ident, ($pat:pat => $e:expr)) => {
|
||||
{ $self.next(); $e }
|
||||
};
|
||||
($self:ident, nonterm ($pat:pat => $e:expr)) => {
|
||||
{ $self.next(); $e }
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! bnf_rule {
|
||||
($self:ident, $type:ty, $rule:ident := $( $rule_clauses:tt )|*) => {
|
||||
fn $rule(&mut $self) -> ParseResult<$type> {
|
||||
Ok(match $self.peek() {
|
||||
$(
|
||||
expand_match_var!($rule_clauses) => expand_match_expr!($self, $rule_clauses),
|
||||
)*
|
||||
_ => return ParseError::new("Not found"),
|
||||
})
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl AutoParser {
|
||||
bnf_rule!(self, ExpressionType, literal :=
|
||||
(Keyword(Kw::True) => ExpressionType::BoolLiteral(true)) |
|
||||
(Keyword(Kw::False) => ExpressionType::BoolLiteral(false))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
pub struct Schala { }
|
||||
|
||||
impl Schala {
|
||||
pub fn new() -> Schala {
|
||||
Schala { }
|
||||
}
|
||||
}
|
||||
|
||||
impl ProgrammingLanguageInterface for Schala {
|
||||
fn get_language_name(&self) -> String {
|
||||
"Schala-autoparser".to_string()
|
||||
}
|
||||
fn get_source_file_suffix(&self) -> String {
|
||||
format!("schala")
|
||||
}
|
||||
|
||||
fn evaluate_in_repl(&mut self, input: &str, options: &EvalOptions) -> LanguageOutput {
|
||||
let mut output = LanguageOutput::default();
|
||||
|
||||
let tokens = tokenizing::tokenize(input);
|
||||
if options.debug_tokens {
|
||||
let token_string = tokens.iter().map(|t| format!("{:?}<L:{},C:{}>", t.token_type, t.offset.0, t.offset.1)).join(", ");
|
||||
output.add_artifact(TraceArtifact::new("tokens", format!("{:?}", token_string)));
|
||||
}
|
||||
{
|
||||
let token_errors: Vec<&String> = tokens.iter().filter_map(|t| t.get_error()).collect();
|
||||
if token_errors.len() != 0 {
|
||||
output.add_output(format!("Tokenization error: {:?}\n", token_errors));
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
let mut parser = AutoParser::new(tokens);
|
||||
|
||||
let ast = match parser.parse() {
|
||||
(Ok(ast), trace) => {
|
||||
if options.debug_parse {
|
||||
output.add_artifact(TraceArtifact::new_parse_trace(trace));
|
||||
output.add_artifact(TraceArtifact::new("ast", format!("{:?}", ast)));
|
||||
}
|
||||
ast
|
||||
},
|
||||
(Err(err), trace) => {
|
||||
output.add_artifact(TraceArtifact::new_parse_trace(trace));
|
||||
output.add_output(format!("Parse error: {:?}\n", err.msg));
|
||||
return output;
|
||||
}
|
||||
};
|
||||
|
||||
output.add_output(format!("{:?}", ast));
|
||||
output
|
||||
}
|
||||
}
|
||||
77
src/schala_lang/builtin.rs
Normal file
77
src/schala_lang/builtin.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
use std::rc::Rc;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use schala_lang::typechecking::{Type, TypeResult, TConst};
|
||||
use self::Type::*; use self::TConst::*;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct BinOp {
|
||||
sigil: Rc<String>
|
||||
}
|
||||
|
||||
impl BinOp {
|
||||
pub fn from_sigil(sigil: &str) -> BinOp {
|
||||
BinOp { sigil: Rc::new(sigil.to_string()) }
|
||||
}
|
||||
pub fn sigil(&self) -> &Rc<String> {
|
||||
&self.sigil
|
||||
}
|
||||
pub fn get_type(&self) -> TypeResult<Type> {
|
||||
let s = self.sigil.as_str();
|
||||
BINOPS.get(s).map(|x| x.0.clone()).ok_or(format!("Binop {} not found", s))
|
||||
}
|
||||
pub fn min_precedence() -> i32 {
|
||||
i32::min_value()
|
||||
}
|
||||
pub fn get_precedence(op: &str) -> i32 {
|
||||
let default = 10_000_000;
|
||||
BINOPS.get(op).map(|x| x.2.clone()).unwrap_or(default)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct PrefixOp {
|
||||
sigil: Rc<String>
|
||||
}
|
||||
|
||||
impl PrefixOp {
|
||||
pub fn from_sigil(sigil: &str) -> PrefixOp {
|
||||
PrefixOp { sigil: Rc::new(sigil.to_string()) }
|
||||
}
|
||||
pub fn sigil(&self) -> &Rc<String> {
|
||||
&self.sigil
|
||||
}
|
||||
pub fn is_prefix(op: &str) -> bool {
|
||||
PREFIX_OPS.get(op).is_some()
|
||||
}
|
||||
pub fn get_type(&self) -> TypeResult<Type> {
|
||||
let s = self.sigil.as_str();
|
||||
PREFIX_OPS.get(s).map(|x| x.0.clone()).ok_or(format!("Prefix op {} not found", s))
|
||||
}
|
||||
}
|
||||
lazy_static! {
|
||||
static ref PREFIX_OPS: HashMap<&'static str, (Type, ())> =
|
||||
hashmap! {
|
||||
"+" => (Func(bx!(Const(Int)), bx!(Const(Int))), ()),
|
||||
"-" => (Func(bx!(Const(Int)), bx!(Const(Int))), ()),
|
||||
"!" => (Func(bx!(Const(Bool)), bx!(Const(Bool))), ()),
|
||||
};
|
||||
}
|
||||
|
||||
/* the second tuple member is a placeholder for when I want to make evaluation rules tied to the
|
||||
* binop definition */
|
||||
lazy_static! {
|
||||
static ref BINOPS: HashMap<&'static str, (Type, (), i32)> =
|
||||
hashmap! {
|
||||
"+" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 10),
|
||||
"-" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 10),
|
||||
"*" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
|
||||
"/" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Float))))), (), 20),
|
||||
"//" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
|
||||
"%" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
|
||||
"++" => (Func(bx!(Const(StringT)), bx!(Func(bx!(Const(StringT)), bx!(Const(StringT))))), (), 30),
|
||||
"^" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
|
||||
"&" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
|
||||
"|" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
|
||||
};
|
||||
}
|
||||
317
src/schala_lang/eval.rs
Normal file
317
src/schala_lang/eval.rs
Normal file
@@ -0,0 +1,317 @@
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use std::fmt::Write;
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
use schala_lang::parsing::{AST, Statement, Declaration, Expression, Variant, ExpressionType};
|
||||
use schala_lang::builtin::{BinOp, PrefixOp};
|
||||
|
||||
pub struct State<'a> {
|
||||
parent_frame: Option<&'a State<'a>>,
|
||||
values: HashMap<Rc<String>, ValueEntry>,
|
||||
}
|
||||
|
||||
impl<'a> State<'a> {
|
||||
|
||||
fn insert(&mut self, name: Rc<String>, value: ValueEntry) {
|
||||
self.values.insert(name, value);
|
||||
}
|
||||
fn lookup(&self, name: &Rc<String>) -> Option<&ValueEntry> {
|
||||
match (self.values.get(name), self.parent_frame) {
|
||||
(None, None) => None,
|
||||
(None, Some(parent)) => parent.lookup(name),
|
||||
(Some(value), _) => Some(value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ValueEntry {
|
||||
Binding {
|
||||
val: FullyEvaluatedExpr,
|
||||
},
|
||||
Function {
|
||||
param_names: Vec<Rc<String>>,
|
||||
body: Vec<Statement>,
|
||||
}
|
||||
}
|
||||
|
||||
type EvalResult<T> = Result<T, String>;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
enum FullyEvaluatedExpr {
|
||||
UnsignedInt(u64),
|
||||
SignedInt(i64),
|
||||
Float(f64),
|
||||
Str(String),
|
||||
Bool(bool),
|
||||
FuncLit(Rc<String>),
|
||||
Custom {
|
||||
string_rep: Rc<String>,
|
||||
},
|
||||
Tuple(Vec<FullyEvaluatedExpr>),
|
||||
List(Vec<FullyEvaluatedExpr>)
|
||||
}
|
||||
|
||||
impl FullyEvaluatedExpr {
|
||||
fn to_string(&self) -> String {
|
||||
use self::FullyEvaluatedExpr::*;
|
||||
match self {
|
||||
&UnsignedInt(ref n) => format!("{}", n),
|
||||
&SignedInt(ref n) => format!("{}", n),
|
||||
&Float(ref f) => format!("{}", f),
|
||||
&Str(ref s) => format!("\"{}\"", s),
|
||||
&Bool(ref b) => format!("{}", b),
|
||||
&Custom { ref string_rep } => format!("{}", string_rep),
|
||||
&Tuple(ref items) => {
|
||||
let mut buf = String::new();
|
||||
write!(buf, "(").unwrap();
|
||||
for term in items.iter().map(|e| Some(e)).intersperse(None) {
|
||||
match term {
|
||||
Some(e) => write!(buf, "{}", e.to_string()).unwrap(),
|
||||
None => write!(buf, ", ").unwrap(),
|
||||
};
|
||||
}
|
||||
write!(buf, ")").unwrap();
|
||||
buf
|
||||
},
|
||||
&FuncLit(ref name) => format!("<function {}>", name),
|
||||
&List(ref items) => {
|
||||
let mut buf = String::new();
|
||||
write!(buf, "[").unwrap();
|
||||
for term in items.iter().map(|e| Some(e)).intersperse(None) {
|
||||
match term {
|
||||
Some(e) => write!(buf, "{}", e.to_string()).unwrap(),
|
||||
None => write!(buf, ", ").unwrap()
|
||||
}
|
||||
}
|
||||
write!(buf, "]").unwrap();
|
||||
buf
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> State<'a> {
|
||||
pub fn new() -> State<'a> {
|
||||
State { parent_frame: None, values: HashMap::new() }
|
||||
}
|
||||
|
||||
pub fn new_with_parent(parent: &'a State<'a>) -> State<'a> {
|
||||
State { parent_frame: Some(parent), values: HashMap::new() }
|
||||
}
|
||||
|
||||
pub fn evaluate(&mut self, ast: AST) -> Vec<String> {
|
||||
let mut acc = vec![];
|
||||
for statement in ast.0 {
|
||||
match self.eval_statement(statement) {
|
||||
Ok(output) => {
|
||||
if let Some(fully_evaluated) = output {
|
||||
acc.push(fully_evaluated.to_string());
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
acc.push(format!("Eval error: {}", error));
|
||||
return acc;
|
||||
},
|
||||
}
|
||||
}
|
||||
acc
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> State<'a> {
|
||||
fn eval_statement(&mut self, statement: Statement) -> EvalResult<Option<FullyEvaluatedExpr>> {
|
||||
Ok(match statement {
|
||||
Statement::ExpressionStatement(expr) => Some(self.eval_expr(expr)?),
|
||||
Statement::Declaration(decl) => { self.eval_decl(decl)?; None }
|
||||
})
|
||||
}
|
||||
|
||||
fn eval_decl(&mut self, decl: Declaration) -> EvalResult<()> {
|
||||
use self::Declaration::*;
|
||||
use self::Variant::*;
|
||||
|
||||
match decl {
|
||||
FuncDecl(signature, statements) => {
|
||||
let name = signature.name;
|
||||
let param_names: Vec<Rc<String>> = signature.params.iter().map(|fp| fp.0.clone()).collect();
|
||||
self.insert(name, ValueEntry::Function { body: statements.clone(), param_names });
|
||||
},
|
||||
TypeDecl(_name, body) => {
|
||||
for variant in body.0.iter() {
|
||||
match variant {
|
||||
&UnitStruct(ref name) => self.insert(name.clone(),
|
||||
ValueEntry::Binding { val: FullyEvaluatedExpr::Custom { string_rep: name.clone() } }),
|
||||
&TupleStruct(ref _name, ref _args) => unimplemented!(),
|
||||
&Record(ref _name, ref _fields) => unimplemented!(),
|
||||
};
|
||||
}
|
||||
},
|
||||
Binding { name, expr, ..} => {
|
||||
let val = self.eval_expr(expr)?;
|
||||
self.insert(name.clone(), ValueEntry::Binding { val });
|
||||
},
|
||||
_ => return Err(format!("Declaration evaluation not yet implemented"))
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn eval_expr(&mut self, expr: Expression) -> EvalResult<FullyEvaluatedExpr> {
|
||||
use self::ExpressionType::*;
|
||||
use self::FullyEvaluatedExpr::*;
|
||||
|
||||
let expr_type = expr.0;
|
||||
match expr_type {
|
||||
IntLiteral(n) => Ok(UnsignedInt(n)),
|
||||
FloatLiteral(f) => Ok(Float(f)),
|
||||
StringLiteral(s) => Ok(Str(s.to_string())),
|
||||
BoolLiteral(b) => Ok(Bool(b)),
|
||||
PrefixExp(op, expr) => self.eval_prefix_exp(op, expr),
|
||||
BinExp(op, lhs, rhs) => self.eval_binexp(op, lhs, rhs),
|
||||
Value(name) => self.eval_value(name),
|
||||
TupleLiteral(expressions) => {
|
||||
let mut evals = Vec::new();
|
||||
for expr in expressions {
|
||||
match self.eval_expr(expr) {
|
||||
Ok(fully_evaluated) => evals.push(fully_evaluated),
|
||||
error => return error,
|
||||
}
|
||||
}
|
||||
Ok(Tuple(evals))
|
||||
}
|
||||
Call { f, arguments } => {
|
||||
let mut evaled_arguments = Vec::new();
|
||||
for arg in arguments.into_iter() {
|
||||
evaled_arguments.push(self.eval_expr(arg)?);
|
||||
}
|
||||
self.eval_application(*f, evaled_arguments)
|
||||
},
|
||||
Index { box indexee, indexers } => {
|
||||
let evaled = self.eval_expr(indexee)?;
|
||||
match evaled {
|
||||
Tuple(mut exprs) => {
|
||||
let len = indexers.len();
|
||||
if len == 1 {
|
||||
let idx = indexers.into_iter().nth(0).unwrap();
|
||||
match self.eval_expr(idx)? {
|
||||
UnsignedInt(n) if (n as usize) < exprs.len() => Ok(exprs.drain(n as usize..).next().unwrap()),
|
||||
UnsignedInt(n) => Err(format!("Index {} out of range", n)),
|
||||
other => Err(format!("{:?} is not an unsigned integer", other)),
|
||||
}
|
||||
} else {
|
||||
Err(format!("Tuple index must be one integer"))
|
||||
}
|
||||
},
|
||||
_ => Err(format!("Bad index expression"))
|
||||
}
|
||||
},
|
||||
ListLiteral(items) => Ok(List(items.into_iter().map(|item| self.eval_expr(item)).collect::<Result<Vec<_>,_>>()?)),
|
||||
x => Err(format!("Unimplemented thing {:?}", x)),
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_application(&mut self, f: Expression, arguments: Vec<FullyEvaluatedExpr>) -> EvalResult<FullyEvaluatedExpr> {
|
||||
use self::ExpressionType::*;
|
||||
match f {
|
||||
Expression(Value(ref identifier), _) if self.is_builtin(identifier) => self.eval_builtin(identifier, arguments),
|
||||
Expression(Value(identifier), _) => {
|
||||
match self.lookup(&identifier) {
|
||||
Some(&ValueEntry::Function { ref body, ref param_names }) => {
|
||||
if arguments.len() != param_names.len() {
|
||||
return Err(format!("Wrong number of arguments for the function"));
|
||||
}
|
||||
let mut new_state = State::new_with_parent(self);
|
||||
let sub_ast = body.clone();
|
||||
for (param, val) in param_names.iter().zip(arguments.into_iter()) {
|
||||
new_state.insert(param.clone(), ValueEntry::Binding { val });
|
||||
}
|
||||
let mut ret: Option<FullyEvaluatedExpr> = None;
|
||||
for statement in sub_ast.into_iter() {
|
||||
ret = new_state.eval_statement(statement)?;
|
||||
}
|
||||
Ok(ret.unwrap_or(FullyEvaluatedExpr::Custom { string_rep: Rc::new("()".to_string()) }))
|
||||
},
|
||||
_ => Err(format!("Function {} not found", identifier)),
|
||||
}
|
||||
},
|
||||
x => Err(format!("Trying to apply {:?} which is not a function", x)),
|
||||
}
|
||||
}
|
||||
fn is_builtin(&self, name: &Rc<String>) -> bool {
|
||||
match &name.as_ref()[..] {
|
||||
"print" | "println" => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
fn eval_builtin(&mut self, name: &Rc<String>, args: Vec<FullyEvaluatedExpr>) -> EvalResult<FullyEvaluatedExpr> {
|
||||
use self::FullyEvaluatedExpr::*;
|
||||
match &name.as_ref()[..] {
|
||||
"print" => {
|
||||
for arg in args {
|
||||
print!("{}", arg.to_string());
|
||||
}
|
||||
Ok(Tuple(vec![]))
|
||||
},
|
||||
"println" => {
|
||||
for arg in args {
|
||||
println!("{}", arg.to_string());
|
||||
}
|
||||
Ok(Tuple(vec![]))
|
||||
},
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
fn eval_value(&mut self, name: Rc<String>) -> EvalResult<FullyEvaluatedExpr> {
|
||||
use self::ValueEntry::*;
|
||||
match self.lookup(&name) {
|
||||
None => return Err(format!("Value {} not found", *name)),
|
||||
Some(lookup) => match lookup {
|
||||
&Binding { ref val } => Ok(val.clone()),
|
||||
&Function { .. } => Ok(FullyEvaluatedExpr::FuncLit(name.clone()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_binexp(&mut self, op: BinOp, lhs: Box<Expression>, rhs: Box<Expression>) -> EvalResult<FullyEvaluatedExpr> {
|
||||
use self::FullyEvaluatedExpr::*;
|
||||
let evaled_lhs = self.eval_expr(*lhs)?;
|
||||
let evaled_rhs = self.eval_expr(*rhs)?;
|
||||
let sigil = op.sigil();
|
||||
//let sigil: &str = op.sigil().as_ref().as_str();
|
||||
Ok(match (sigil.as_str(), evaled_lhs, evaled_rhs) {
|
||||
("+", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l + r),
|
||||
("++", Str(s1), Str(s2)) => Str(format!("{}{}", s1, s2)),
|
||||
("-", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l - r),
|
||||
("*", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l * r),
|
||||
("/", UnsignedInt(l), UnsignedInt(r)) => Float((l as f64)/ (r as f64)),
|
||||
("//", UnsignedInt(l), UnsignedInt(r)) => if r == 0 {
|
||||
return Err(format!("Runtime error: divide by zero"));
|
||||
} else {
|
||||
UnsignedInt(l / r)
|
||||
},
|
||||
("%", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l % r),
|
||||
("^", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l ^ r),
|
||||
("&", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l & r),
|
||||
("|", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l | r),
|
||||
_ => return Err(format!("Runtime error: not yet implemented")),
|
||||
})
|
||||
}
|
||||
|
||||
fn eval_prefix_exp(&mut self, op: PrefixOp, expr: Box<Expression>) -> EvalResult<FullyEvaluatedExpr> {
|
||||
use self::FullyEvaluatedExpr::*;
|
||||
let evaled_expr = self.eval_expr(*expr)?;
|
||||
let sigil = op.sigil();
|
||||
|
||||
Ok(match (sigil.as_str(), evaled_expr) {
|
||||
("!", Bool(true)) => Bool(false),
|
||||
("!", Bool(false)) => Bool(true),
|
||||
("-", UnsignedInt(n)) => SignedInt(-1*(n as i64)),
|
||||
("-", SignedInt(n)) => SignedInt(-1*(n as i64)),
|
||||
("+", SignedInt(n)) => SignedInt(n),
|
||||
("+", UnsignedInt(n)) => UnsignedInt(n),
|
||||
_ => return Err(format!("Runtime error: not yet implemented")),
|
||||
})
|
||||
}
|
||||
}
|
||||
104
src/schala_lang/mod.rs
Normal file
104
src/schala_lang/mod.rs
Normal file
@@ -0,0 +1,104 @@
|
||||
use itertools::Itertools;
|
||||
use schala_lib::{ProgrammingLanguageInterface, EvalOptions, TraceArtifact, LanguageOutput};
|
||||
|
||||
macro_rules! bx {
|
||||
($e:expr) => { Box::new($e) }
|
||||
}
|
||||
|
||||
pub mod autoparser;
|
||||
|
||||
mod builtin;
|
||||
|
||||
mod tokenizing;
|
||||
mod parsing;
|
||||
mod typechecking;
|
||||
mod eval;
|
||||
|
||||
use self::typechecking::{TypeContext};
|
||||
|
||||
pub struct Schala {
|
||||
state: eval::State<'static>,
|
||||
type_context: TypeContext
|
||||
}
|
||||
|
||||
impl Schala {
|
||||
pub fn new() -> Schala {
|
||||
Schala {
|
||||
state: eval::State::new(),
|
||||
type_context: TypeContext::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ProgrammingLanguageInterface for Schala {
|
||||
fn get_language_name(&self) -> String {
|
||||
"Schala".to_string()
|
||||
}
|
||||
|
||||
fn get_source_file_suffix(&self) -> String {
|
||||
format!("schala")
|
||||
}
|
||||
|
||||
fn evaluate_in_repl(&mut self, input: &str, options: &EvalOptions) -> LanguageOutput {
|
||||
let mut output = LanguageOutput::default();
|
||||
let tokens = tokenizing::tokenize(input);
|
||||
if options.debug_tokens {
|
||||
let token_string = tokens.iter().map(|t| format!("{:?}<L:{},C:{}>", t.token_type, t.offset.0, t.offset.1)).join(", ");
|
||||
output.add_artifact(TraceArtifact::new("tokens", format!("{:?}", token_string)));
|
||||
}
|
||||
|
||||
{
|
||||
let token_errors: Vec<&String> = tokens.iter().filter_map(|t| t.get_error()).collect();
|
||||
if token_errors.len() != 0 {
|
||||
output.add_output(format!("Tokenization error: {:?}\n", token_errors));
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
let ast = match parsing::parse(tokens) {
|
||||
(Ok(ast), trace) => {
|
||||
if options.debug_parse {
|
||||
output.add_artifact(TraceArtifact::new_parse_trace(trace));
|
||||
output.add_artifact(TraceArtifact::new("ast", format!("{:?}", ast)));
|
||||
}
|
||||
ast
|
||||
},
|
||||
(Err(err), trace) => {
|
||||
output.add_artifact(TraceArtifact::new_parse_trace(trace));
|
||||
output.add_output(format!("Parse error: {:?}\n", err.msg));
|
||||
return output;
|
||||
}
|
||||
};
|
||||
|
||||
match self.type_context.add_top_level_types(&ast) {
|
||||
Ok(()) => (),
|
||||
Err(msg) => {
|
||||
output.add_artifact(TraceArtifact::new("type_check", msg));
|
||||
//return output
|
||||
}
|
||||
};
|
||||
|
||||
if options.debug_symbol_table {
|
||||
let text = self.type_context.debug_symbol_table();
|
||||
output.add_artifact(TraceArtifact::new("symbol_table", text));
|
||||
}
|
||||
|
||||
match self.type_context.type_check_ast(&ast) {
|
||||
Ok(ty) => {
|
||||
output.add_artifact(TraceArtifact::new("type_check", format!("{:?}", ty)));
|
||||
},
|
||||
Err(msg) => {
|
||||
output.add_artifact(TraceArtifact::new("type_check", msg));
|
||||
/*
|
||||
output.add_output(format!("Type error"));
|
||||
return output;
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
let evaluation_outputs = self.state.evaluate(ast);
|
||||
let text_output: String = evaluation_outputs.into_iter().intersperse(format!("\n")).collect();
|
||||
output.add_output(text_output);
|
||||
return output;
|
||||
}
|
||||
}
|
||||
1234
src/schala_lang/parsing.rs
Normal file
1234
src/schala_lang/parsing.rs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -5,17 +5,16 @@ use std::iter::{Iterator, Peekable};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum TokenKind {
|
||||
pub enum TokenType {
|
||||
Newline, Semicolon,
|
||||
|
||||
LParen, RParen,
|
||||
LSquareBracket, RSquareBracket,
|
||||
LAngleBracket, RAngleBracket,
|
||||
LCurlyBrace, RCurlyBrace,
|
||||
Pipe, Backslash,
|
||||
Pipe,
|
||||
|
||||
Comma, Period, Colon, Underscore,
|
||||
Slash,
|
||||
|
||||
Operator(Rc<String>),
|
||||
DigitGroup(Rc<String>), HexLiteral(Rc<String>), BinNumberSigil,
|
||||
@@ -27,9 +26,9 @@ pub enum TokenKind {
|
||||
|
||||
Error(String),
|
||||
}
|
||||
use self::TokenKind::*;
|
||||
use self::TokenType::*;
|
||||
|
||||
impl fmt::Display for TokenKind {
|
||||
impl fmt::Display for TokenType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
&Operator(ref s) => write!(f, "Operator({})", **s),
|
||||
@@ -45,15 +44,14 @@ impl fmt::Display for TokenKind {
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum Kw {
|
||||
If, Then, Else,
|
||||
Is,
|
||||
If, Else,
|
||||
Func,
|
||||
For, While,
|
||||
Const, Let, In,
|
||||
Mut,
|
||||
For,
|
||||
Match,
|
||||
Var, Const, Let, In,
|
||||
Return,
|
||||
Alias, Type, SelfType, SelfIdent,
|
||||
Interface, Impl,
|
||||
Trait, Impl,
|
||||
True, False,
|
||||
Module
|
||||
}
|
||||
@@ -62,22 +60,20 @@ lazy_static! {
|
||||
static ref KEYWORDS: HashMap<&'static str, Kw> =
|
||||
hashmap! {
|
||||
"if" => Kw::If,
|
||||
"then" => Kw::Then,
|
||||
"else" => Kw::Else,
|
||||
"is" => Kw::Is,
|
||||
"fn" => Kw::Func,
|
||||
"for" => Kw::For,
|
||||
"while" => Kw::While,
|
||||
"match" => Kw::Match,
|
||||
"var" => Kw::Var,
|
||||
"const" => Kw::Const,
|
||||
"let" => Kw::Let,
|
||||
"in" => Kw::In,
|
||||
"mut" => Kw::Mut,
|
||||
"return" => Kw::Return,
|
||||
"alias" => Kw::Alias,
|
||||
"type" => Kw::Type,
|
||||
"Self" => Kw::SelfType,
|
||||
"self" => Kw::SelfIdent,
|
||||
"interface" => Kw::Interface,
|
||||
"trait" => Kw::Trait,
|
||||
"impl" => Kw::Impl,
|
||||
"true" => Kw::True,
|
||||
"false" => Kw::False,
|
||||
@@ -87,73 +83,49 @@ lazy_static! {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Token {
|
||||
pub kind: TokenKind,
|
||||
pub line_num: usize,
|
||||
pub char_num: usize
|
||||
pub token_type: TokenType,
|
||||
pub offset: (usize, usize),
|
||||
}
|
||||
|
||||
impl Token {
|
||||
pub fn get_error(&self) -> Option<String> {
|
||||
match self.kind {
|
||||
TokenKind::Error(ref s) => Some(s.clone()),
|
||||
pub fn get_error(&self) -> Option<&String> {
|
||||
match self.token_type {
|
||||
TokenType::Error(ref s) => Some(s),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
pub fn to_string_with_metadata(&self) -> String {
|
||||
format!("{}(L:{},c:{})", self.kind, self.line_num, self.char_num)
|
||||
}
|
||||
|
||||
pub fn get_kind(&self) -> TokenKind {
|
||||
self.kind.clone()
|
||||
format!("{}(L:{},c:{})", self.token_type, self.offset.0, self.offset.1)
|
||||
}
|
||||
}
|
||||
|
||||
const OPERATOR_CHARS: [char; 18] = ['!', '$', '%', '&', '*', '+', '-', '.', ':', '<', '>', '=', '?', '@', '^', '|', '~', '`'];
|
||||
const OPERATOR_CHARS: [char; 19] = ['!', '$', '%', '&', '*', '+', '-', '.', '/', ':', '<', '>', '=', '?', '@', '^', '|', '~', '`'];
|
||||
fn is_operator(c: &char) -> bool {
|
||||
OPERATOR_CHARS.iter().any(|x| x == c)
|
||||
}
|
||||
|
||||
type CharData = (usize, usize, char);
|
||||
type CharIter<I: Iterator<Item=(usize,usize,char)>> = Peekable<I>;
|
||||
|
||||
pub fn tokenize(input: &str) -> Vec<Token> {
|
||||
let mut tokens: Vec<Token> = Vec::new();
|
||||
|
||||
let mut input = input.lines().enumerate()
|
||||
.intersperse((0, "\n"))
|
||||
.flat_map(|(line_idx, ref line)| {
|
||||
line.chars().enumerate().map(move |(ch_idx, ch)| (line_idx, ch_idx, ch))
|
||||
})
|
||||
.peekable();
|
||||
}).peekable();
|
||||
|
||||
while let Some((line_num, char_num, c)) = input.next() {
|
||||
let cur_tok_kind = match c {
|
||||
'/' => match input.peek().map(|t| t.2) {
|
||||
Some('/') => {
|
||||
while let Some((line_idx, ch_idx, c)) = input.next() {
|
||||
let cur_tok_type = match c {
|
||||
'#' => {
|
||||
if let Some(&(_, _, '{')) = input.peek() {
|
||||
} else {
|
||||
while let Some((_, _, c)) = input.next() {
|
||||
if c == '\n' {
|
||||
break;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
},
|
||||
Some('*') => {
|
||||
input.next();
|
||||
let mut comment_level = 1;
|
||||
while let Some((_, _, c)) = input.next() {
|
||||
if c == '*' && input.peek().map(|t| t.2) == Some('/') {
|
||||
input.next();
|
||||
comment_level -= 1;
|
||||
} else if c == '/' && input.peek().map(|t| t.2) == Some('*') {
|
||||
input.next();
|
||||
comment_level += 1;
|
||||
}
|
||||
if comment_level == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
},
|
||||
_ => Slash
|
||||
}
|
||||
continue;
|
||||
},
|
||||
c if c.is_whitespace() && c != '\n' => continue,
|
||||
'\n' => Newline, ';' => Semicolon,
|
||||
@@ -162,18 +134,17 @@ pub fn tokenize(input: &str) -> Vec<Token> {
|
||||
'{' => LCurlyBrace, '}' => RCurlyBrace,
|
||||
'[' => LSquareBracket, ']' => RSquareBracket,
|
||||
'"' => handle_quote(&mut input),
|
||||
'\\' => Backslash,
|
||||
c if c.is_digit(10) => handle_digit(c, &mut input),
|
||||
c if c.is_alphabetic() || c == '_' => handle_alphabetic(c, &mut input),
|
||||
c if c.is_alphabetic() || c == '_' => handle_alphabetic(c, &mut input), //TODO I'll probably have to rewrite this if I care about types being uppercase, also type parameterization
|
||||
c if is_operator(&c) => handle_operator(c, &mut input),
|
||||
unknown => Error(format!("Unexpected character: {}", unknown)),
|
||||
};
|
||||
tokens.push(Token { kind: cur_tok_kind, line_num, char_num });
|
||||
tokens.push(Token { token_type: cur_tok_type, offset: (line_idx, ch_idx) });
|
||||
}
|
||||
tokens
|
||||
}
|
||||
|
||||
fn handle_digit(c: char, input: &mut Peekable<impl Iterator<Item=CharData>>) -> TokenKind {
|
||||
fn handle_digit<I: Iterator<Item=(usize,usize,char)>>(c: char, input: &mut CharIter<I>) -> TokenType {
|
||||
if c == '0' && input.peek().map_or(false, |&(_, _, c)| { c == 'x' }) {
|
||||
input.next();
|
||||
let rest: String = input.peeking_take_while(|&(_, _, ref c)| c.is_digit(16) || *c == '_').map(|(_, _, c)| { c }).collect();
|
||||
@@ -188,7 +159,7 @@ fn handle_digit(c: char, input: &mut Peekable<impl Iterator<Item=CharData>>) ->
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_quote(input: &mut Peekable<impl Iterator<Item=CharData>>) -> TokenKind {
|
||||
fn handle_quote<I: Iterator<Item=(usize,usize,char)>>(input: &mut CharIter<I>) -> TokenType {
|
||||
let mut buf = String::new();
|
||||
loop {
|
||||
match input.next().map(|(_, _, c)| { c }) {
|
||||
@@ -207,22 +178,22 @@ fn handle_quote(input: &mut Peekable<impl Iterator<Item=CharData>>) -> TokenKind
|
||||
}
|
||||
},
|
||||
Some(c) => buf.push(c),
|
||||
None => return TokenKind::Error(format!("Unclosed string")),
|
||||
None => return TokenType::Error(format!("Unclosed string")),
|
||||
}
|
||||
}
|
||||
TokenKind::StrLiteral(Rc::new(buf))
|
||||
TokenType::StrLiteral(Rc::new(buf))
|
||||
}
|
||||
|
||||
fn handle_alphabetic(c: char, input: &mut Peekable<impl Iterator<Item=CharData>>) -> TokenKind {
|
||||
fn handle_alphabetic<I: Iterator<Item=(usize,usize,char)>>(c: char, input: &mut CharIter<I>) -> TokenType {
|
||||
let mut buf = String::new();
|
||||
buf.push(c);
|
||||
if c == '_' && input.peek().map(|&(_, _, c)| { !c.is_alphabetic() }).unwrap_or(true) {
|
||||
return TokenKind::Underscore
|
||||
return TokenType::Underscore
|
||||
}
|
||||
|
||||
loop {
|
||||
match input.peek().map(|&(_, _, c)| { c }) {
|
||||
Some(c) if c.is_alphanumeric() || c == '_' => {
|
||||
Some(c) if c.is_alphanumeric() => {
|
||||
input.next();
|
||||
buf.push(c);
|
||||
},
|
||||
@@ -231,12 +202,12 @@ fn handle_alphabetic(c: char, input: &mut Peekable<impl Iterator<Item=CharData>>
|
||||
}
|
||||
|
||||
match KEYWORDS.get(buf.as_str()) {
|
||||
Some(kw) => TokenKind::Keyword(*kw),
|
||||
None => TokenKind::Identifier(Rc::new(buf)),
|
||||
Some(kw) => TokenType::Keyword(*kw),
|
||||
None => TokenType::Identifier(Rc::new(buf)),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_operator(c: char, input: &mut Peekable<impl Iterator<Item=CharData>>) -> TokenKind {
|
||||
fn handle_operator<I: Iterator<Item=(usize,usize,char)>>(c: char, input: &mut CharIter<I>) -> TokenType {
|
||||
match c {
|
||||
'<' | '>' | '|' | '.' => {
|
||||
let ref next = input.peek().map(|&(_, _, c)| { c });
|
||||
@@ -254,34 +225,17 @@ fn handle_operator(c: char, input: &mut Peekable<impl Iterator<Item=CharData>>)
|
||||
};
|
||||
|
||||
let mut buf = String::new();
|
||||
|
||||
if c == '`' {
|
||||
loop {
|
||||
match input.peek().map(|&(_, _, c)| { c }) {
|
||||
Some(c) if c.is_alphabetic() || c == '_' => {
|
||||
input.next();
|
||||
buf.push(c);
|
||||
},
|
||||
Some('`') => {
|
||||
input.next();
|
||||
break;
|
||||
},
|
||||
_ => break
|
||||
}
|
||||
}
|
||||
} else {
|
||||
buf.push(c);
|
||||
loop {
|
||||
match input.peek().map(|&(_, _, c)| { c }) {
|
||||
Some(c) if is_operator(&c) => {
|
||||
input.next();
|
||||
buf.push(c);
|
||||
},
|
||||
_ => break
|
||||
}
|
||||
buf.push(c);
|
||||
loop {
|
||||
match input.peek().map(|&(_, _, c)| { c }) {
|
||||
Some(c) if is_operator(&c) => {
|
||||
input.next();
|
||||
buf.push(c);
|
||||
},
|
||||
_ => break
|
||||
}
|
||||
}
|
||||
TokenKind::Operator(Rc::new(buf))
|
||||
TokenType::Operator(Rc::new(buf))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -293,32 +247,18 @@ mod schala_tokenizer_tests {
|
||||
macro_rules! ident { ($ident:expr) => { Identifier(Rc::new($ident.to_string())) } }
|
||||
macro_rules! op { ($ident:expr) => { Operator(Rc::new($ident.to_string())) } }
|
||||
|
||||
|
||||
#[test]
|
||||
fn tokens() {
|
||||
let a = tokenize("let a: A<B> = c ++ d");
|
||||
let token_kinds: Vec<TokenKind> = a.into_iter().map(move |t| t.kind).collect();
|
||||
assert_eq!(token_kinds, vec![Keyword(Let), ident!("a"), Colon, ident!("A"),
|
||||
let token_types: Vec<TokenType> = a.into_iter().map(move |t| t.token_type).collect();
|
||||
assert_eq!(token_types, vec![Keyword(Let), ident!("a"), Colon, ident!("A"),
|
||||
LAngleBracket, ident!("B"), RAngleBracket, op!("="), ident!("c"), op!("++"), ident!("d")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn underscores() {
|
||||
let token_kinds: Vec<TokenKind> = tokenize("4_8").into_iter().map(move |t| t.kind).collect();
|
||||
assert_eq!(token_kinds, vec![digit!("4"), Underscore, digit!("8")]);
|
||||
|
||||
let token_kinds2: Vec<TokenKind> = tokenize("aba_yo").into_iter().map(move |t| t.kind).collect();
|
||||
assert_eq!(token_kinds2, vec![ident!("aba_yo")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn comments() {
|
||||
let token_kinds: Vec<TokenKind> = tokenize("1 + /* hella /* bro */ */ 2").into_iter().map(move |t| t.kind).collect();
|
||||
assert_eq!(token_kinds, vec![digit!("1"), op!("+"), digit!("2")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn backtick_operators() {
|
||||
let token_kinds: Vec<TokenKind> = tokenize("1 `plus` 2").into_iter().map(move |t| t.kind).collect();
|
||||
assert_eq!(token_kinds, vec![digit!("1"), op!("plus"), digit!("2")]);
|
||||
let token_types: Vec<TokenType> = tokenize("4_8").into_iter().map(move |t| t.token_type).collect();
|
||||
assert_eq!(token_types, vec![digit!("4"), Underscore, digit!("8")]);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
|
||||
use parsing::{AST, Statement, Declaration, Signature, Expression, ExpressionType, Operation, Variant, TypeName, TypeSingletonName};
|
||||
use schala_lang::parsing::{AST, Statement, Declaration, Signature, Expression, ExpressionType, Operation, Variant, TypeName, TypeSingletonName};
|
||||
|
||||
// from Niko's talk
|
||||
/* fn type_check(expression, expected_ty) -> Ty {
|
||||
254
src/schala_lang/typechecking.rs
Normal file
254
src/schala_lang/typechecking.rs
Normal file
@@ -0,0 +1,254 @@
|
||||
use std::rc::Rc;
|
||||
use std::collections::HashMap;
|
||||
use std::char;
|
||||
use std::fmt;
|
||||
use std::fmt::Write;
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
use schala_lang::parsing;
|
||||
|
||||
pub struct TypeContext {
|
||||
type_var_count: u64,
|
||||
bindings: HashMap<Rc<String>, Type>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum Type {
|
||||
Const(TConst),
|
||||
Sum(Vec<Type>),
|
||||
Func(Box<Type>, Box<Type>),
|
||||
UVar(String),
|
||||
EVar(u64),
|
||||
Void
|
||||
}
|
||||
|
||||
impl fmt::Display for Type {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use self::Type::*;
|
||||
match self {
|
||||
&Const(ref c) => write!(f, "{:?}", c),
|
||||
&Sum(ref types) => {
|
||||
write!(f, "(")?;
|
||||
for item in types.iter().map(|ty| Some(ty)).intersperse(None) {
|
||||
match item {
|
||||
Some(ty) => write!(f, "{}", ty)?,
|
||||
None => write!(f, ",")?,
|
||||
};
|
||||
}
|
||||
write!(f, ")")
|
||||
},
|
||||
&Func(ref a, ref b) => write!(f, "{} -> {}", a, b),
|
||||
&UVar(ref s) => write!(f, "{}_u", s),
|
||||
&EVar(ref n) => write!(f, "{}_e", n),
|
||||
&Void => write!(f, "Void")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct UVarGenerator {
|
||||
n: u32,
|
||||
}
|
||||
impl UVarGenerator {
|
||||
fn new() -> UVarGenerator {
|
||||
UVarGenerator::default()
|
||||
}
|
||||
fn next(&mut self) -> Type {
|
||||
//TODO handle this in the case where someone wants to make a function with more than 26 variables
|
||||
let s = format!("{}", unsafe { char::from_u32_unchecked(self.n + ('a' as u32)) });
|
||||
self.n += 1;
|
||||
Type::UVar(s)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum TConst {
|
||||
Unit,
|
||||
Int,
|
||||
Float,
|
||||
StringT,
|
||||
Bool,
|
||||
Custom(String),
|
||||
}
|
||||
|
||||
impl parsing::TypeName {
|
||||
fn to_type(&self) -> TypeResult<Type> {
|
||||
use self::parsing::TypeSingletonName;
|
||||
use self::parsing::TypeName::*;
|
||||
use self::Type::*; use self::TConst::*;
|
||||
Ok(match self {
|
||||
&Tuple(_) => return Err(format!("Tuples not yet implemented")),
|
||||
&Singleton(ref name) => match name {
|
||||
&TypeSingletonName { ref name, .. } => match &name[..] {
|
||||
"Int" => Const(Int),
|
||||
"Float" => Const(Float),
|
||||
"Bool" => Const(Bool),
|
||||
"String" => Const(StringT),
|
||||
n => Const(Custom(n.to_string()))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub type TypeResult<T> = Result<T, String>;
|
||||
|
||||
impl TypeContext {
|
||||
pub fn new() -> TypeContext {
|
||||
TypeContext { bindings: HashMap::new(), type_var_count: 0 }
|
||||
}
|
||||
pub fn fresh(&mut self) -> Type {
|
||||
let ret = self.type_var_count;
|
||||
self.type_var_count += 1;
|
||||
Type::EVar(ret)
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeContext {
|
||||
pub fn add_top_level_types(&mut self, ast: &parsing::AST) -> TypeResult<()> {
|
||||
use self::parsing::TypeName;
|
||||
use self::parsing::Declaration::*;
|
||||
use self::Type::*;
|
||||
for statement in ast.0.iter() {
|
||||
if let &self::parsing::Statement::Declaration(ref decl) = statement {
|
||||
match decl {
|
||||
&FuncSig(ref signature) | &FuncDecl(ref signature, _) => {
|
||||
let mut uvar_gen = UVarGenerator::new();
|
||||
let mut ty: Type = signature.type_anno.as_ref().map(|name: &TypeName| name.to_type()).unwrap_or_else(|| {Ok(uvar_gen.next())} )?;
|
||||
for &(_, ref type_name) in signature.params.iter().rev() {
|
||||
let arg_type = type_name.as_ref().map(|name| name.to_type()).unwrap_or_else(|| {Ok(uvar_gen.next())} )?;
|
||||
ty = Func(bx!(arg_type), bx!(ty));
|
||||
}
|
||||
self.bindings.insert(signature.name.clone(), ty);
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn debug_symbol_table(&self) -> String {
|
||||
let mut output = format!("Symbols\n");
|
||||
for (sym, ty) in &self.bindings {
|
||||
write!(output, "{} : {}\n", sym, ty).unwrap();
|
||||
}
|
||||
output
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeContext {
|
||||
pub fn type_check_ast(&mut self, ast: &parsing::AST) -> TypeResult<Type> {
|
||||
use self::Type::*; use self::TConst::*;
|
||||
let mut ret_type = Const(Unit);
|
||||
for statement in ast.0.iter() {
|
||||
ret_type = self.type_check_statement(statement)?;
|
||||
}
|
||||
Ok(ret_type)
|
||||
}
|
||||
fn type_check_statement(&mut self, statement: &parsing::Statement) -> TypeResult<Type> {
|
||||
use self::parsing::Statement::*;
|
||||
match statement {
|
||||
&ExpressionStatement(ref expr) => self.infer(expr),
|
||||
&Declaration(ref decl) => self.add_declaration(decl),
|
||||
}
|
||||
}
|
||||
fn add_declaration(&mut self, decl: &parsing::Declaration) -> TypeResult<Type> {
|
||||
use self::parsing::Declaration::*;
|
||||
use self::Type::*;
|
||||
match decl {
|
||||
&Binding { ref name, ref expr, .. } => {
|
||||
let ty = self.infer(expr)?;
|
||||
self.bindings.insert(name.clone(), ty);
|
||||
},
|
||||
_ => return Err(format!("other formats not done"))
|
||||
}
|
||||
Ok(Void)
|
||||
}
|
||||
fn infer(&mut self, expr: &parsing::Expression) -> TypeResult<Type> {
|
||||
use self::parsing::Expression;
|
||||
match expr {
|
||||
&Expression(ref e, Some(ref anno)) => {
|
||||
let anno_ty = anno.to_type()?;
|
||||
let ty = self.infer_exprtype(&e)?;
|
||||
self.unify(ty, anno_ty)
|
||||
},
|
||||
&Expression(ref e, None) => self.infer_exprtype(e)
|
||||
}
|
||||
}
|
||||
fn infer_exprtype(&mut self, expr: &parsing::ExpressionType) -> TypeResult<Type> {
|
||||
use self::parsing::ExpressionType::*;
|
||||
use self::Type::*; use self::TConst::*;
|
||||
match expr {
|
||||
&IntLiteral(_) => Ok(Const(Int)),
|
||||
&FloatLiteral(_) => Ok(Const(Float)),
|
||||
&StringLiteral(_) => Ok(Const(StringT)),
|
||||
&BoolLiteral(_) => Ok(Const(Bool)),
|
||||
&BinExp(ref op, ref lhs, ref rhs) => { /* remember there are both the haskell convention talk and the write you a haskell ways to do this! */
|
||||
match op.get_type()? {
|
||||
Func(box t1, box Func(box t2, box t3)) => {
|
||||
let lhs_ty = self.infer(lhs)?;
|
||||
let rhs_ty = self.infer(rhs)?;
|
||||
self.unify(t1, lhs_ty)?;
|
||||
self.unify(t2, rhs_ty)?;
|
||||
Ok(t3)
|
||||
},
|
||||
other => Err(format!("{:?} is not a binary function type", other))
|
||||
}
|
||||
},
|
||||
&PrefixExp(ref op, ref expr) => match op.get_type()? {
|
||||
Func(box t1, box t2) => {
|
||||
let expr_ty = self.infer(expr)?;
|
||||
self.unify(t1, expr_ty)?;
|
||||
Ok(t2)
|
||||
},
|
||||
other => Err(format!("{:?} is not a prefix op function type", other))
|
||||
},
|
||||
&Value(ref name) => {
|
||||
match self.bindings.get(name) {
|
||||
Some(ty) => Ok(ty.clone()),
|
||||
None => Err(format!("No binding found for variable: {}", name)),
|
||||
}
|
||||
},
|
||||
&Call { ref f, ref arguments } => {
|
||||
let mut tf = self.infer(f)?;
|
||||
for arg in arguments.iter() {
|
||||
match tf {
|
||||
Func(box t, box rest) => {
|
||||
let t_arg = self.infer(arg)?;
|
||||
self.unify(t, t_arg)?;
|
||||
tf = rest;
|
||||
},
|
||||
other => return Err(format!("Function call failed to unify; last type: {:?}", other)),
|
||||
}
|
||||
}
|
||||
Ok(tf)
|
||||
},
|
||||
&TupleLiteral(ref expressions) => {
|
||||
let mut types = vec![];
|
||||
for expr in expressions {
|
||||
types.push(self.infer(expr)?);
|
||||
}
|
||||
Ok(Sum(types))
|
||||
},
|
||||
/*
|
||||
Index {
|
||||
indexee: Box<Expression>,
|
||||
indexers: Vec<Expression>,
|
||||
},
|
||||
IfExpression(Box<Expression>, Vec<Statement>, Option<Vec<Statement>>),
|
||||
MatchExpression(Box<Expression>, Vec<MatchArm>),
|
||||
ForExpression
|
||||
*/
|
||||
_ => Err(format!("Type not yet implemented"))
|
||||
}
|
||||
}
|
||||
fn unify(&mut self, t1: Type, t2: Type) -> TypeResult<Type> {
|
||||
use self::Type::*;// use self::TConst::*;
|
||||
match (t1, t2) {
|
||||
(Const(ref a), Const(ref b)) if a == b => Ok(Const(a.clone())),
|
||||
(a, b) => Err(format!("Types {:?} and {:?} don't unify", a, b))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user