Checkpoint some existing changes.

This commit is contained in:
2023-04-10 21:24:07 -07:00
parent 6e7a7c7d04
commit 5c294850a8
6 changed files with 130 additions and 80 deletions

View File

@@ -107,7 +107,6 @@ letExpr = do
scope <- term scope <- term
pure $ Let alts scope pure $ Let alts scope
-- Let <$ keyword "let" <*> ident <* keyword "=" <*> term <* keyword "in" <*> term -- Let <$ keyword "let" <*> ident <* keyword "=" <*> term <* keyword "in" <*> term
where where
letAssign : Parser (Name,Term) letAssign : Parser (Name,Term)
letAssign = do letAssign = do
@@ -127,7 +126,7 @@ lamExpr = do
keyword "\\" keyword "\\"
commit commit
name <- pPattern name <- pPattern
keyword "." keyword "=>"
scope <- term scope <- term
pure $ Lam name scope pure $ Lam name scope
@@ -150,25 +149,33 @@ caseExpr = do
alts <- startBlock $ someSame $ caseAlt alts <- startBlock $ someSame $ caseAlt
pure $ Case sc alts pure $ Case sc alts
term = defer $ \_ => term = defer $ \_ =>
caseExpr caseExpr
<|> letExpr <|> letExpr
<|> lamExpr <|> lamExpr
<|> parseOp <|> parseOp
-- And top level stuff -- And top level stuff
optional : Parser a -> Parser (Maybe a)
optional pa = Just <$> pa <|> pure Nothing
export
parseSig : Parser Decl parseSig : Parser Decl
parseSig = TypeSig <$> ident <* keyword ":" <*> term parseSig = TypeSig <$> ident <* keyword ":" <*> term
parseImport : Parser Decl
parseImport = DImport <$ keyword "import" <*> ident
-- Do we do pattern stuff now? or just name = lambda?
export
parseDef : Parser Decl parseDef : Parser Decl
parseDef = Def <$> ident <* keyword "=" <*> term parseDef = Def <$> ident <* keyword "=" <*> term
export
parseDecl : Parser Decl parseDecl : Parser Decl
parseDecl = parseSig <|> parseDef parseDecl = parseImport <|> parseSig <|> parseDef
export export
parseMod : Parser Module parseMod : Parser Module
@@ -176,7 +183,7 @@ parseMod = do
keyword "module" keyword "module"
name <- ident name <- ident
-- probably should be manySame, and we want to start with col -1 -- probably should be manySame, and we want to start with col -1
-- if we enforce blocks indent -- if we enforce blocks indent more than parent
decls <- startBlock $ someSame $ parseDecl decls <- startBlock $ someSame $ parseDecl
pure $ MkModule name [] decls pure $ MkModule name [] decls

View File

@@ -44,6 +44,7 @@ parse pa toks = case runP pa toks False (0,0) of
OK a [] _ => Right a OK a [] _ => Right a
OK a ts _ => Left "Extra toks \{show ts}" OK a ts _ => Left "Extra toks \{show ts}"
-- I think I want to drop the typeclasses for v1
export export
fail : String -> Parser a fail : String -> Parser a
@@ -147,11 +148,9 @@ indented (P p) = P $ \toks,com,(l,c) => case toks of
in if tc > c || tl == l then p toks com (l,c) in if tc > c || tl == l then p toks com (l,c)
else Fail (E "unexpected outdent") toks com else Fail (E "unexpected outdent") toks com
export export
token' : Kind -> Parser String token' : Kind -> Parser String
token' k = pred (\t => t.val.kind == k) "Expected a \{show k}" token' k = pred (\t => t.val.kind == k) "Expected a \{show k} token"
export export

View File

@@ -10,10 +10,12 @@ data Kind
| Number | Number
| Symbol | Symbol
| Space | Space
| Comment
-- not doing Layout.idr -- not doing Layout.idr
| LBrace | LBrace
| Semi | Semi
| RBrace | RBrace
| EOI
export export
Show Kind where Show Kind where
@@ -26,6 +28,8 @@ Show Kind where
show LBrace = "LBrace" show LBrace = "LBrace"
show Semi = "Semi" show Semi = "Semi"
show RBrace = "RBrace" show RBrace = "RBrace"
show Comment = "Comment"
show EOI = "EOI"
export export
Eq Kind where Eq Kind where

View File

@@ -8,7 +8,7 @@ keywords : List String
keywords = ["let", "in", "where", "case", "of", "data"] keywords = ["let", "in", "where", "case", "of", "data"]
specialOps : List String specialOps : List String
specialOps = ["->", ":"] specialOps = ["->", ":", "=>"]
checkKW : String -> Token Kind checkKW : String -> Token Kind
checkKW s = if elem s keywords then Tok Keyword s else Tok Ident s checkKW s = if elem s keywords then Tok Keyword s else Tok Ident s
@@ -19,20 +19,12 @@ isOpChar c = c `elem` (unpack ":!#$%&*+./<=>?@\\^|-~")
opChar : Lexer opChar : Lexer
opChar = pred isOpChar opChar = pred isOpChar
-- so Text.Lexer.Core.lex is broken identMore : Lexer
-- tmap : TokenMap (Token Kind) identMore = alphaNum <|> exact "."
-- tmap = [
-- (alpha <+> many alphaNum, checkKW),
-- (some digit, Tok Number),
-- (some opChar, \s => Tok Oper s),
-- (lineComment (exact "--"), Tok Space),
-- (symbol, Tok Symbol),
-- (spaces, Tok Space)
-- ]
rawTokens : Tokenizer (Token Kind) rawTokens : Tokenizer (Token Kind)
rawTokens rawTokens
= match (alpha <+> many alphaNum) checkKW = match (alpha <+> many identMore) checkKW
<|> match (some digit) (Tok Number) <|> match (some digit) (Tok Number)
<|> match (some opChar) (\s => Tok Oper s) <|> match (some opChar) (\s => Tok Oper s)
<|> match (lineComment (exact "--")) (Tok Space) <|> match (lineComment (exact "--")) (Tok Space)
@@ -46,5 +38,3 @@ notSpace _ = True
export export
tokenise : String -> List BTok tokenise : String -> List BTok
tokenise = filter notSpace . fst . lex rawTokens tokenise = filter notSpace . fst . lex rawTokens

View File

@@ -1,18 +1,35 @@
module Main module Main
import Data.String
import Lib.Tokenizer import Lib.Tokenizer
import Lib.Layout import Lib.Layout
import Lib.Token import Lib.Token
import Lib.Parser.Impl import Lib.Parser.Impl
import Lib.Parser import Lib.Parser
import Syntax import Syntax
import System
import System.File
import System.Directory
import Control.App
-- Put stuff in attic
-- Error printing
-- Review surface syntax
-- Prettier printer
-- First pass at typecheck
-- Just do it in zoo order
-- showPError : String ->
test : Show a => Parser a -> String -> IO () test : Show a => Parser a -> String -> IO ()
test pa src = do test pa src = do
_ <- putStrLn "--" _ <- putStrLn "--"
_ <- putStrLn $ src _ <- putStrLn $ src
let toks = tokenise src let toks = tokenise src
putStrLn "- Toks"
printLn $ toks printLn $ toks
putStrLn "- Parse"
let res = parse pa toks let res = parse pa toks
printLn res printLn res
-- let toks2 = layout toks -- let toks2 = layout toks
@@ -20,11 +37,15 @@ test pa src = do
-- gotta fix up error messages. Show it with some source -- gotta fix up error messages. Show it with some source
main : IO () testFile : String -> IO ()
main = do testFile fn = do
-- this stuff is working with layout, should I bother with col. putStrLn ("***" ++ fn)
-- downside is that it will be somewhat picky about the indentation of `in` Right text <- readFile $ "eg/" ++ fn
-- The sixty stuff looks promising. I might want my own tokenizer though. | Left err => printLn err
test parseMod text
olderTests : IO ()
olderTests = do
test letExpr "let x = 1\n y = 2\n in x + y" test letExpr "let x = 1\n y = 2\n in x + y"
test term "let x = 1 in x + 2" test term "let x = 1 in x + 2"
printLn "BREAK" printLn "BREAK"
@@ -35,3 +56,19 @@ main = do
test term "x y z" test term "x y z"
test parseMod "module Foo\nfoo : Int -> Int\nfoo = \\ x . x" test parseMod "module Foo\nfoo : Int -> Int\nfoo = \\ x . x"
test lamExpr "\\ x . x" test lamExpr "\\ x . x"
test parseMod "module Foo\nimport Foo.Bar\nfoo = 1\n"
test parseDef "foo = 1"
test parseSig "foo : Bar -> Int"
test parseMod "module Test\nid : a -> a\nid = \\ x => x\n"
foo : Maybe Int -> Int
foo Nothing = ?foo_rhs_0
foo (Just x) = ?foo_rhs_1
main : IO ()
main = do
Right files <- listDir "eg"
| Left err => printLn err
traverse_ testFile (filter (".newt" `isSuffixOf`) files)

View File

@@ -3,7 +3,6 @@ module Syntax
import Data.String import Data.String
import Derive import Derive
-- Good enough start, lets parse -- Good enough start, lets parse
-- This is informed by pi-forall and others and is somewhat low level -- This is informed by pi-forall and others and is somewhat low level
-- %language ElabReflection -- %language ElabReflection
@@ -64,6 +63,7 @@ public export
data Decl data Decl
= TypeSig Name TyTerm = TypeSig Name TyTerm
| Def Name Term | Def Name Term
| DImport Name
| Data Name Telescope (List ConstrDef) | Data Name Telescope (List ConstrDef)
public export public export
@@ -76,15 +76,32 @@ record Module where
foo : List String -> String foo : List String -> String
foo ts = "(" ++ unwords ts ++ ")" foo ts = "(" ++ unwords ts ++ ")"
mutual
Show Literal where
show (LString str) = foo [ "LString", show str]
show (LInt i) = foo [ "LInt", show i]
show (LBool x) = foo [ "LBool", show x]
export
covering
implementation Show Term
export
implementation Show Decl
covering
Show ConstrDef where Show ConstrDef where
show x = ?holex show (MkCDef str xs) = foo ["MkCDef", show str, show xs]
covering covering
Show Decl where Show Decl where
show (TypeSig str x) = foo ["TypeSig", show str, show x] show (TypeSig str x) = foo ["TypeSig", show str, show x]
show (Def str x) = foo ["Def", show str, show x] show (Def str x) = foo ["Def", show str, show x]
show (Data str xs ys) = foo ["Data", show str, show xs, show ys] show (Data str xs ys) = foo ["Data", show str, show xs, show ys]
show (DImport str) = foo ["DImport", show str]
export covering export covering
Show Module where Show Module where
@@ -100,7 +117,6 @@ mutual
show PatWild = "PatWild" show PatWild = "PatWild"
show (PatLit x) = foo ["PatLit" , show x] show (PatLit x) = foo ["PatLit" , show x]
Show CaseAlt where Show CaseAlt where
show (MkAlt x y)= foo ["MkAlt", show x, assert_total $ show y] show (MkAlt x y)= foo ["MkAlt", show x, assert_total $ show y]
@@ -109,22 +125,19 @@ mutual
show Explicit = "Explicit" show Explicit = "Explicit"
show Eq = "Eq" show Eq = "Eq"
Show Literal where covering
show (LString str) = foo [ "LString", show str]
show (LInt i) = foo [ "LInt", show i]
show (LBool x) = foo [ "LBool", show x]
export
Show Term where Show Term where
show (Ann t ty) = "Ann" ++ " " ++ show t ++ " " ++ show ty
show Wildcard = "Wildcard"
show (Var name) = foo ["Var", show name] show (Var name) = foo ["Var", show name]
show (Ann t ty) = foo [ "Ann", show t, show ty] show (Ann t ty) = foo [ "Ann", show t, show ty]
show (Lit x) = foo [ "Lit", show x] show (Lit x) = foo [ "Lit", show x]
show (Let alts y) = foo [ "Let", assert_total $ show alts, show y] show (Let alts y) = foo [ "Let", show alts, show y]
show (Pi str x y z) = foo [ "Pi", show str, show x, show y, show z] show (Pi str x y z) = foo [ "Pi", show str, show x, show y, show z]
show (App x y) = foo [ "App", show x, show y] show (App x y) = foo [ "App", show x, show y]
show (Lam x y) = foo [ "Lam", show x, show y] show (Lam x y) = foo [ "Lam", show x, show y]
show (Case x xs) = foo [ "Case", show x, show xs] show (Case x xs) = foo [ "Case", show x, show xs]
show Wildcard = "Wildcard"
show (ParseError str) = foo [ "ParseError", "str"]
show (ParseError str) = foo [ "ParseError", "str"]
show _ = "woo"