Interim commit; lots of progress though.

This commit is contained in:
2011-01-18 08:43:30 -08:00
parent 022bdf240b
commit 617e841b05
5 changed files with 210 additions and 162 deletions

View File

@@ -24,7 +24,7 @@ export length(ls :: [a]) :: Int
{ {
case ls of case ls of
[] -> 0 [] -> 0
_:rest -> length rest _:rest -> length(rest)
}; };
export reverse(ls :: [a]) :: [a] export reverse(ls :: [a]) :: [a]
@@ -32,10 +32,10 @@ export reverse(ls :: [a]) :: [a]
helper(xs,acc) = { helper(xs,acc) = {
case xs of case xs of
[] -> acc [] -> acc
(a:rest) -> helper rest (a:acc) (a:rest) -> helper(rest, (a:acc))
} }
helper ls []; helper(ls, []);
}; };
export restrict(Eq a) find(f :: a -> Bool, ls :: [a]) :: Maybe a export restrict(Eq a) find(f :: a -> Bool, ls :: [a]) :: Maybe a

View File

@@ -1,5 +1,7 @@
module Syntax.AST where module Syntax.AST where
import Syntax.ParserCore
data Show a => Module a = Module { data Show a => Module a = Module {
modName :: QualifiedName modName :: QualifiedName
, modImports :: [Import] , modImports :: [Import]
@@ -13,6 +15,11 @@ data QualifiedName = QualifiedName {
} }
deriving (Show) deriving (Show)
gensym :: Parser QualifiedName
gensym = do
name <- genstr
return (QualifiedName [] name)
data Import = Import { data Import = Import {
imName :: QualifiedName imName :: QualifiedName
, imQualified :: Bool , imQualified :: Bool
@@ -31,7 +38,7 @@ data Show a => Decl a =
| DeclNewtype a [Type] | DeclNewtype a [Type]
| DeclClass a [Type] | DeclClass a [Type]
| DeclInstance a [Type] | DeclInstance a [Type]
| DeclValue a [Type] Type QualifiedName [Stmt a] | DeclValue a [Type] Type (Expr a)
| DeclExport a (Decl a) | DeclExport a (Decl a)
deriving (Show) deriving (Show)
@@ -41,7 +48,7 @@ addTypeRestrictions rs (DeclType s _) = DeclType s rs
addTypeRestrictions rs (DeclNewtype s _) = DeclNewtype s rs addTypeRestrictions rs (DeclNewtype s _) = DeclNewtype s rs
addTypeRestrictions rs (DeclClass s _) = DeclClass s rs addTypeRestrictions rs (DeclClass s _) = DeclClass s rs
addTypeRestrictions rs (DeclInstance s _) = DeclInstance s rs addTypeRestrictions rs (DeclInstance s _) = DeclInstance s rs
addTypeRestrictions rs (DeclValue s _ a b c) = DeclValue s rs a b c addTypeRestrictions rs (DeclValue s _ a b) = DeclValue s rs a b
addTypeRestrictions rs (DeclExport s d) = addTypeRestrictions rs (DeclExport s d) =
DeclExport s (addTypeRestrictions rs d) DeclExport s (addTypeRestrictions rs d)
@@ -51,16 +58,32 @@ data DataClause a = DataClause a QualifiedName [Maybe QualifiedName] [Type]
data Show a => Expr a = data Show a => Expr a =
Const a ConstVal Const a ConstVal
| VarRef a QualifiedName | VarRef a QualifiedName
| Cond a (Expr a) (Expr a) | Cond a (Expr a) (Expr a) (Expr a)
| App a (Expr a) (Expr a) | App a (Expr a) [Expr a]
| Block a [Expr a] | Block a [Stmt a]
| Lambda a [QualifiedName] (Expr a) | Lambda a [QualifiedName] (Expr a)
deriving (Show) deriving (Show)
getSpecial :: Show a => Expr a -> a
getSpecial (Const a _) = a
getSpecial (VarRef a _) = a
getSpecial (Cond a _ _ _) = a
getSpecial (App a _ _) = a
getSpecial (Block a _) = a
getSpecial (Lambda a _ _) = a
data Show a => Stmt a = data Show a => Stmt a =
SExpr a (Expr a) SExpr a (Expr a)
| SBind a QualifiedName (Stmt a) | SBind a QualifiedName (Stmt a)
| SCase a | SCase a (Expr a) [(Pattern,Maybe (Expr a),Expr a)]
deriving (Show)
data Pattern =
ListNull
| PConst ConstVal
| PVar QualifiedName
| PNamed QualifiedName Pattern
| PAp Pattern Pattern
deriving (Show) deriving (Show)
data Kind = Star | KFun Kind Kind data Kind = Star | KFun Kind Kind

View File

@@ -58,6 +58,7 @@ $escape_char = [abfnrtv'\"\\]
"|" { emitT Bar } "|" { emitT Bar }
";" { emitT Semi } ";" { emitT Semi }
"," { emitT Comma } "," { emitT Comma }
"`" { emitT BTick }
{ {

View File

@@ -27,36 +27,37 @@ import qualified Codec.Binary.UTF8.Generic as UTF8
'instance' { Lexeme $$ (TokValIdent "instance") } 'instance' { Lexeme $$ (TokValIdent "instance") }
'qualified' { Lexeme $$ (TokValIdent "qualified") } 'qualified' { Lexeme $$ (TokValIdent "qualified") }
'as' { Lexeme $$ (TokValIdent "as") } 'as' { Lexeme $$ (TokValIdent "as") }
-- 'case' { Lexeme $$ (TokValIdent "case") } 'case' { Lexeme $$ (TokValIdent "case") }
-- 'of' { Lexeme $$ (TokValIdent "of") } 'of' { Lexeme $$ (TokValIdent "of") }
'restrict' { Lexeme $$ (TokValIdent "restrict") } 'restrict' { Lexeme $$ (TokValIdent "restrict") }
-- symbols -- symbols
'=' { Lexeme $$ (TokOpIdent "=") } '=' { Lexeme $$ (TokOpIdent "=") }
'->' { Lexeme $$ (TokOpIdent "->") } '->' { Lexeme $$ (TokOpIdent "->") }
-- '=>' { Lexeme $$ (TokOpIdent "=>") } '@' { Lexeme $$ (TokOpIdent "@") }
'::' { Lexeme $$ (TokOpIdent "::") } '::' { Lexeme $$ (TokOpIdent "::") }
-- '\\' { Lexeme $$ (TokOpIdent "\\") } '\\' { Lexeme $$ (TokOpIdent "\\") }
'(' { Lexeme $$ LParen } '(' { Lexeme $$ LParen }
')' { Lexeme $$ RParen } ')' { Lexeme $$ RParen }
'[' { Lexeme $$ LSquare } '[' { Lexeme $$ LSquare }
']' { Lexeme $$ RSquare } ']' { Lexeme $$ RSquare }
-- '{' { Lexeme $$ LBrace } '{' { Lexeme $$ LBrace }
-- '}' { Lexeme $$ RBrace } '}' { Lexeme $$ RBrace }
'|' { Lexeme $$ Bar } '|' { Lexeme $$ Bar }
';' { Lexeme $$ Semi } ';' { Lexeme $$ Semi }
',' { Lexeme $$ Comma } ',' { Lexeme $$ Comma }
'`' { Lexeme $$ BTick }
-- identifiers -- identifiers
TYPE_IDENT { Lexeme _ (TokTypeIdent $$) } TYPE_IDENT { Lexeme _ (TokTypeIdent _) }
VAL_IDENT { Lexeme _ (TokValIdent $$) } VAL_IDENT { Lexeme _ (TokValIdent _) }
OP_IDENT { Lexeme _ (TokOpIdent $$) } OP_IDENT { Lexeme _ (TokOpIdent _) }
-- values -- values
-- INTVAL { Lexeme _ (TokInt $$) } INTVAL { Lexeme _ (TokInt _) }
-- FLOATVAL { Lexeme _ (TokFloat $$) } FLOATVAL { Lexeme _ (TokFloat _) }
-- CHARVAL { Lexeme _ (TokChar $$) } CHARVAL { Lexeme _ (TokChar _) }
-- STRVAL { Lexeme _ (TokString $$) } STRVAL { Lexeme _ (TokString _) }
%monad { Parser } { (>>=) } { return } %monad { Parser } { (>>=) } { return }
%name parseModule top_module %name parseModule top_module
@@ -204,7 +205,9 @@ instance_decl :: { Decl Position }
-- Value Declaration -------------------------------------------------------- -- Value Declaration --------------------------------------------------------
value_decl :: { Decl Position } value_decl :: { Decl Position }
: value_ident optional_args optional_type { undefined } : value_ident optional_args optional_type value_body
{% postProcessDeclVal $1 $2 $3 $4 }
optional_args :: { Maybe [(QualifiedName, Maybe Type)] } optional_args :: { Maybe [(QualifiedName, Maybe Type)] }
: '(' optional_args2 ')' { Just $2 } : '(' optional_args2 ')' { Just $2 }
@@ -225,6 +228,10 @@ value_ident :: { QualifiedName }
: VAL_IDENT { makeQualified $1 } : VAL_IDENT { makeQualified $1 }
| '(' OP_IDENT ')' { makeQualified $2 } | '(' OP_IDENT ')' { makeQualified $2 }
value_body :: { (Position, Expr Position) }
: '=' expression { ($1, $2) }
| '{' statements '}' { ($1, Block $1 $2) }
-- Types in Bang ------------------------------------------------------------ -- Types in Bang ------------------------------------------------------------
primary_type :: { Type } primary_type :: { Type }
@@ -253,142 +260,124 @@ list_type :: { Type }
bang_type :: { Type } bang_type :: { Type }
: list_type { $1 } : list_type { $1 }
-- -- Statements in bang
-- data_decl :: { Decl Position }
-- : 'datatype' mqualifiers TYPE_IDENT data_args dataclauses statements :: { [Stmt Position] }
-- { DeclData $2 (makeQualified $3) $4 $5 } : { [] }
-- | statements statement { $1 ++ [$2] }
-- mqualifiers :: { [Type] }
-- : { [] } statement :: { Stmt Position }
-- | '(' tqualifiers ')' '=>' { $2 } : assignment_statement ';' { $1 }
-- | case_statement ';' { $1 }
-- tqualifiers :: { [Type] } | expression ';' { SExpr $2 $1 }
-- : tqualifier { [$1] }
-- | tqualifiers ',' tqualifier { $1 ++ [$3] } assignment_statement :: { Stmt Position }
-- : value_ident '=' expression -- FIXME: Too restrictive!
-- tqualifier :: { Type } { SBind $2 $1 (SExpr $2 $3) }
-- : TYPE_IDENT VAL_IDENT
-- { TAp (TCon (makeQualified $1) Star) (TVar (makeQualified $2) Star) } case_statement :: { Stmt Position }
-- | tqualifier VAL_IDENT : 'case' expression 'of' case_items
-- { TAp $1 (TVar (makeQualified $2) Star) } { SCase $1 $2 $4 }
--
-- data_args :: { [QualifiedName] } case_items :: { [(Pattern,Maybe (Expr Position),(Expr Position))] }
-- : { [] } : case_item { [$1] }
-- | data_args VAL_IDENT { $1 ++ [makeQualified $2] } | case_items case_item { $1 ++ [$2] }
--
-- dataclauses :: { [DataClause] } case_item :: { (Pattern, Maybe (Expr Position), (Expr Position)) }
-- : '=' dataclause { [$2] } : pattern mguard '->' expression { ($1, $2, $4) }
-- | dataclauses '|' dataclause { $1 ++ [$3] }
-- mguard :: { Maybe (Expr Position) }
-- dataclause :: { DataClause } : { Nothing }
-- : constructor_id { DataClause $1 [] } | '|' expression { Just $2 }
-- | dataclause bangtype3 { let DataClause name items = $1
-- in DataClause name (items ++ [$2]) } -- Patterns for pattern matching
--
-- constructor_id :: { QualifiedName } infix_operator :: { QualifiedName }
-- : TYPE_IDENT { makeQualified $1 } : OP_IDENT { makeQualified $1 }
-- | '(' OP_IDENT ')' { makeQualified $2 } | '`' VAL_IDENT '`' { makeQualified $2 }
--
-- -- Type alias Declarations -------------------------------------------------- pattern_primary :: { Pattern }
-- : TYPE_IDENT { PVar (makeQualified $1) }
-- type_decl :: { Decl Position } | VAL_IDENT { PVar (makeQualified $1) }
-- : 'type' { DeclType } | '[' ']' { PVar (QualifiedName ["Data","List"] "NULL") }
-- | INTVAL { let (Lexeme _ (TokInt (base, val))) = $1
-- -- Newtype Declarations ----------------------------------------------------- in PConst (ConstInteger base val) }
-- | FLOATVAL { let (Lexeme _ (TokFloat val)) = $1
-- newtype_decl :: { Decl Position } in PConst (ConstFloat val) }
-- : 'newtype' { DeclNewtype } | CHARVAL { let (Lexeme _ (TokChar val)) = $1
-- in PConst (ConstChar val) }
-- -- Type class Declarations -------------------------------------------------- | STRVAL { let (Lexeme _ (TokString val)) = $1
-- in PConst (ConstString val) }
-- class_decl :: { Decl Position } | '(' pattern ')' { $2 }
-- : 'class' { DeclClass }
-- pattern_infix :: { Pattern }
-- -- Instance Declarations ---------------------------------------------------- : pattern_infix infix_operator pattern_primary { PAp (PAp $1 (PVar $2)) $3 }
-- | pattern_primary { $1 }
-- instance_decl :: { Decl Position }
-- : 'instance' { DeclInstance } pattern_ap :: { Pattern }
-- : pattern_ap pattern_infix { PAp $1 $2 }
-- -- Data value Declarations -------------------------------------------------- | pattern_infix { $1 }
--
-- value_decl :: { Decl Position } pattern_name :: { Pattern }
-- : value_name '=' expr { DeclValue Nothing $1 (Just $3) } : value_ident '@' pattern_name { PNamed $1 $3 }
-- | pattern_ap { $1 }
-- value_name :: { QualifiedName }
-- : VAL_IDENT { makeQualified $1 } pattern :: { Pattern }
-- | '(' OP_IDENT ')' { makeQualified $2 } : pattern_name { $1 }
--
-- -- Data value type Declarations --------------------------------------------- -- Expressions in bang
--
-- vtype_decl :: {Decl Position } primary_expression :: { Expr Position }
-- : value_name '::' bangtype { DeclValue (Just $3) $1 Nothing } : '(' expression ')' { $2 }
-- | '[' ']' { VarRef $1 (QualifiedName ["Data","List"] "NULL") }
-- -- Types -------------------------------------------------------------------- | INTVAL { let (Lexeme src (TokInt (base, val))) = $1
-- in Const src (ConstInteger base val) }
-- bangtype :: { Type } | FLOATVAL { let (Lexeme src (TokFloat val)) = $1
-- : bangtype1 { $1 } in Const src (ConstFloat val) }
-- | CHARVAL { let (Lexeme src (TokChar val)) = $1
-- bangtype1 :: { Type } in Const src (ConstChar val) }
-- : bangtype1 VAL_IDENT { TAp $1 (TVar (makeQualified $2) Star) } | STRVAL { let (Lexeme src (TokString val)) = $1
-- | bangtype2 { $1 } in Const src (ConstString val) }
-- | VAL_IDENT { let l@(Lexeme src (TokValIdent name)) = $1
-- bangtype2 :: { Type } in VarRef src (makeQualified l) }
-- : bangtype2 '->' bangtype3
-- { TAp (TAp (TCon (QualifiedName [] "->") Star) $1) $3 } conditional_expression :: { Expr Position }
-- | bangtype3 { $1 } : primary_expression { $1 }
--
-- bangtype3 :: { Type } infix_expression :: { Expr Position }
-- : '[' bangtype3 ']' : infix_expression infix_operator conditional_expression
-- { TAp (TVar (QualifiedName ["Data","List"] "List") Star) $2 } { App (getSpecial $1) (VarRef (getSpecial $1) $2) [$1, $3] }
-- | bangtype4 { $1 } | conditional_expression
-- { $1 }
-- bangtype4 :: { Type }
-- : TYPE_IDENT { TVar (makeQualified $1) Star } lambda_expression :: { Expr Position }
-- | '(' bangtype ')' { $2 } : '\\' arguments '->' infix_expression
-- { Lambda $1 $2 $4 }
-- -- Statements --------------------------------------------------------------- | infix_expression
-- { $1 }
-- statement :: { Statement }
-- : expr ';' { } arguments :: { [QualifiedName] }
-- | 'case' expr 'of' { } : value_ident { [$1] }
-- | arguments ',' value_ident { $1 ++ [$3] }
-- -- Expressions --------------------------------------------------------------
-- application_expression :: { Expr Position }
-- expr :: { Expr Position } : application_expression '(' app_args ')'
-- : '\\' arglist '->' expr1 { Lambda Position $2 $4 } { App $2 $1 $3 }
-- | expr1 { $1 } | application_expression '(' ')'
-- { App $2 $1 [] }
-- arglist :: { [QualifiedName] } | lambda_expression
-- : VAL_IDENT { [makeQualified $1] } { $1 }
-- | arglist VAL_IDENT { $1 ++ [makeQualified $2] }
-- app_args :: { [Expr Position] }
-- expr1 :: { Expr Position } : expression { [$1] }
-- : '{' exprs '}' { Block Position $2 } | app_args ',' expression { $1 ++ [$3] }
-- | expr2 { $1 }
-- block_expression :: { Expr Position }
-- exprs :: { [Expr Position] } : '{' statements '}' { Block $1 $2 }
-- : expr ';' { [$1] } | application_expression { $1 }
-- | exprs expr ';' { $1 ++ [$2] }
-- expression :: { Expr Position }
-- expr2 :: { Expr Position } : block_expression { $1 }
-- : '[' list_exprs ']' { unwindList $2 }
-- | expr3 { $1 }
--
-- list_exprs :: { [Expr Position] }
-- : { [] }
-- | list_exprs ',' expr3 { $1 ++ [$3] }
--
-- expr3 :: { Expr Position }
-- : bottom_expr { $1 }
--
-- bottom_expr :: { Expr Position }
-- : INTVAL { let (b,v) = $1 in Const () (ConstInteger b v) }
-- | FLOATVAL { Const Position (ConstFloat $1) }
-- | CHARVAL { Const Position (ConstChar $1) }
-- | STRVAL { Const Position (ConstString $1) }
-- | VAL_IDENT { VarRef Position (makeQualified $1) }
-- | '[' ']' { VarRef () (QualifiedName ["Data","List"] "Null") }
-- | '(' expr ')' { $2 }
{ {
lexer :: (Lexeme -> Parser a) -> Parser a lexer :: (Lexeme -> Parser a) -> Parser a
@@ -400,8 +389,14 @@ happyError = raiseP "Parse Error"
pappend :: ([a],[b]) -> ([a],[b]) -> ([a],[b]) pappend :: ([a],[b]) -> ([a],[b]) -> ([a],[b])
pappend (a,b) (c,d) = (a++c,b++d) pappend (a,b) (c,d) = (a++c,b++d)
makeQualified :: String -> QualifiedName makeQualified :: Lexeme -> QualifiedName
makeQualified str = QualifiedName prefixes name makeQualified (Lexeme _ (TokTypeIdent str)) = makeQualified' str
makeQualified (Lexeme _ (TokValIdent str)) = makeQualified' str
makeQualified (Lexeme _ (TokOpIdent str)) = makeQualified' str
makeQualified _ = error "makeQualified bad arg"
makeQualified' :: String -> QualifiedName
makeQualified' str = QualifiedName prefixes name
where where
(prefixes,name) = loop str (prefixes,name) = loop str
loop val = loop val =
@@ -410,5 +405,26 @@ makeQualified str = QualifiedName prefixes name
then ([], pre) then ([], pre)
else let (pres, name) = loop rest else let (pres, name) = loop rest
in (pre:pres, name) in (pre:pres, name)
postProcessDeclVal :: QualifiedName ->
Maybe [(QualifiedName, Maybe Type)] ->
Maybe Type ->
(Position, Expr Position) ->
Parser (Decl Position)
postProcessDeclVal name margs mrettype (src, body) = do
final_type <- case mrettype of
Nothing -> do
name <- gensym
return (TVar name Star)
Just x ->
return x
case margs of
Nothing ->
return (DeclValue src [] final_type body)
Just [] ->
fail "Need to figure out empty arg items."
Just args ->
fail "Need to figure out non-empty arg items."
} }

View File

@@ -37,7 +37,7 @@ pprtPosition p = posFile p ++ ":" ++ show (posLine p) ++ ":" ++ show (posCol p)
data Token = LParen | RParen data Token = LParen | RParen
| LSquare | RSquare | LSquare | RSquare
| LBrace | RBrace | LBrace | RBrace
| Bar | Semi | Comma | Bar | Semi | Comma | BTick
| TokTypeIdent String | TokTypeIdent String
| TokValIdent String | TokValIdent String
| TokOpIdent String | TokOpIdent String
@@ -90,6 +90,7 @@ data ParserState = ParserState {
, psChar :: !Char , psChar :: !Char
, psPos :: !Position , psPos :: !Position
, psLexCode :: !Int , psLexCode :: !Int
, psGenNum :: !Int
} }
deriving (Show) deriving (Show)
@@ -99,6 +100,7 @@ initParserState path bs = ParserState {
, psChar = '\n' , psChar = '\n'
, psPos = initPosition path , psPos = initPosition path
, psLexCode = 0 , psLexCode = 0
, psGenNum = 0
} }
-- -------------------------------------------------------------------------- -- --------------------------------------------------------------------------
@@ -138,3 +140,9 @@ runParser path bs (Parser m) =
Right (a,_) -> Right a Right (a,_) -> Right a
Left err -> Left err Left err -> Left err
genstr :: Parser String
genstr = do
st <- get
set st{ psGenNum = psGenNum st + 1 }
return $ "--gen" ++ show (psGenNum st)