say it with me girls, "good enough!"

this is so dogshit
This commit is contained in:
crumbtoo
2023-11-22 01:16:11 -07:00
parent 5fb348fbb6
commit 0d32ecd20e
4 changed files with 61 additions and 20 deletions

View File

@@ -5,18 +5,21 @@ import Core.Syntax
import Core.TH import Core.TH
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
{- -- TODO: my shitty lexer isn't inserting semicolons
letrecExample :: Program letrecExample :: Program
letrecExample = [core| letrecExample = [coreProg|
pair x y f = f x y; pair x y f = f x y
fst p = p k;
snd p = p k1; fst p = p k
f x y = letrec snd p = p k1
{ a = pair x b;
; b = pair y a f x y =
} in fst (snd (snd (snd a))); letrec a = pair x b
main = f 3 4; b = pair y a
in fst (snd (snd (snd a)));
main = f 3 4;
|] |]
-}

View File

@@ -23,9 +23,7 @@ import Lens.Micro.TH
$whitechar = [ \t\n\r\f\v] $whitechar = [ \t\n\r\f\v]
$special = [\(\)\,\;\[\]\{\}] $special = [\(\)\,\;\[\]\{\}]
$ascdigit = 0-9 $digit = 0-9
$unidigit = [] -- TODO
$digit = [$ascdigit $unidigit]
$ascsymbol = [\!\#\$\%\&\*\+\.\/\<\=\>\?\@\\\^\|\-\~] $ascsymbol = [\!\#\$\%\&\*\+\.\/\<\=\>\?\@\\\^\|\-\~]
$unisymbol = [] -- TODO $unisymbol = [] -- TODO
@@ -62,6 +60,8 @@ rlp :-
-- everywhere: skip whitespace -- everywhere: skip whitespace
$white_no_nl+ { skip } $white_no_nl+ { skip }
-- TODO: `--` could begin an operator
"--"[^$nl]* { skip }
"--"\-*[^$symbol].* { skip } "--"\-*[^$symbol].* { skip }
"{-" { nestedComment } "{-" { nestedComment }
@@ -82,7 +82,7 @@ $white_no_nl+ { skip }
<0> <0>
{ {
"let" { constTok TokenLet `andBegin` layout } "let" { constTok TokenLet `andBegin` layout }
"letrec" { constTok TokenLet `andBegin` layout } "letrec" { constTok TokenLetrec `andBegin` layout }
"of" { constTok TokenOf `andBegin` layout } "of" { constTok TokenOf `andBegin` layout }
"case" { constTok TokenCase } "case" { constTok TokenCase }
"module" { constTok TokenModule } "module" { constTok TokenModule }
@@ -106,8 +106,21 @@ $white_no_nl+ { skip }
@varsym { lexWith TokenVarSym } @varsym { lexWith TokenVarSym }
} }
-- literals
<0>
{
@decimal { lexWith (TokenLitInt . read @Int) }
}
<0> \n { begin bol } <0> \n { begin bol }
<initial>
{
$white { skip }
\n { skip }
() { topLevelOff `andBegin` 0 }
}
<bol> <bol>
{ {
\n { skip } \n { skip }
@@ -188,7 +201,7 @@ getContext = do
type Lexer = AlexInput -> Int -> Alex (Located CoreToken) type Lexer = AlexInput -> Int -> Alex (Located CoreToken)
alexInitUserState :: AlexUserState alexInitUserState :: AlexUserState
alexInitUserState = AlexUserState [Layout 1] alexInitUserState = AlexUserState []
nestedComment :: Lexer nestedComment :: Lexer
nestedComment _ _ = undefined nestedComment _ _ = undefined
@@ -205,13 +218,13 @@ lexCore :: String -> RLPC ParseError [Located CoreToken]
lexCore s = case m of lexCore s = case m of
Left e -> addFatal err Left e -> addFatal err
where err = SrcError where err = SrcError
{ _errLocation = undefined -- TODO: location { _errLocation = (0,0) -- TODO: location
, _errSeverity = Error , _errSeverity = Error
, _errDiagnostic = ParErrLexical e , _errDiagnostic = ParErrLexical e
} }
Right ts -> pure ts Right ts -> pure ts
where where
m = runAlex s (alexSetStartCode 0 *> lexStream) m = runAlex s (alexSetStartCode initial *> lexStream)
-- | @lexCore@, but the tokens are stripped of location info. Useful for -- | @lexCore@, but the tokens are stripped of location info. Useful for
-- debugging -- debugging
@@ -280,7 +293,7 @@ getOffside = do
_ -> pure GT _ -> pure GT
doBol :: Lexer doBol :: Lexer
doBol (p,c,_,s) l = do doBol (p,c,_,s) _ = do
off <- getOffside off <- getOffside
case off of case off of
LT -> insRBraceV p LT -> insRBraceV p
@@ -292,7 +305,11 @@ letin (p,_,_,_) l = do
popContext popContext
pure $ Located p TokenIn pure $ Located p TokenIn
topLevelOff :: Lexer
topLevelOff = noBrace
alexEOF :: Alex (Located CoreToken) alexEOF :: Alex (Located CoreToken)
alexEOF = Alex $ \ st@(AlexState { alex_pos = p }) -> Right (st, Located p TokenEOF) alexEOF = Alex $ \ st@(AlexState { alex_pos = p }) ->
Right (st, Located p TokenEOF)
} }

View File

@@ -2,6 +2,7 @@
module Core.Parse module Core.Parse
( parseCore ( parseCore
, parseCoreExpr , parseCoreExpr
, parseCoreProg
, module Core.Lex -- temp convenience , module Core.Lex -- temp convenience
, parseTmp , parseTmp
, SrcError , SrcError
@@ -19,6 +20,7 @@ import Compiler.RLPC
%name parseCore Module %name parseCore Module
%name parseCoreExpr StandaloneExpr %name parseCoreExpr StandaloneExpr
%name parseCoreProg StandaloneProgram
%tokentype { Located CoreToken } %tokentype { Located CoreToken }
%error { parseError } %error { parseError }
%monad { RLPC ParseError } %monad { RLPC ParseError }
@@ -57,6 +59,9 @@ Eof :: { () }
Eof : eof { () } Eof : eof { () }
| error { () } | error { () }
StandaloneProgram :: { Program }
StandaloneProgram : Program eof { $1 }
Program :: { Program } Program :: { Program }
Program : VOpen ScDefs VClose { Program $2 } Program : VOpen ScDefs VClose { Program $2 }
| '{' ScDefs '}' { Program $2 } | '{' ScDefs '}' { Program $2 }

View File

@@ -1,5 +1,6 @@
module Core.TH module Core.TH
( coreExpr ( coreExpr
, coreProg
, core , core
) )
where where
@@ -21,6 +22,14 @@ core = QuasiQuoter
, quoteDec = error "core quasiquotes may only be used in expressions" , quoteDec = error "core quasiquotes may only be used in expressions"
} }
coreProg :: QuasiQuoter
coreProg = QuasiQuoter
{ quoteExp = qCoreProg
, quotePat = error "core quasiquotes may only be used in expressions"
, quoteType = error "core quasiquotes may only be used in expressions"
, quoteDec = error "core quasiquotes may only be used in expressions"
}
coreExpr :: QuasiQuoter coreExpr :: QuasiQuoter
coreExpr = QuasiQuoter coreExpr = QuasiQuoter
{ quoteExp = qCoreExpr { quoteExp = qCoreExpr
@@ -43,3 +52,10 @@ qCoreExpr s = case parseExpr s of
where where
parseExpr = evalRLPC RLPCOptions . (lexCore >=> parseCoreExpr) parseExpr = evalRLPC RLPCOptions . (lexCore >=> parseCoreExpr)
qCoreProg :: String -> Q Exp
qCoreProg s = case parseProg s of
Left e -> error (show e)
Right (m,ts) -> lift m
where
parseProg = evalRLPC RLPCOptions . (lexCore >=> parseCoreProg)