-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started alex/happy parser for core language
- Loading branch information
1 parent
52b615f
commit faf5d62
Showing
10 changed files
with
339 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
{ | ||
{-# OPTIONS_GHC -XFlexibleContexts #-} | ||
|
||
module Lexer ( | ||
Token(..), | ||
scanTokens | ||
) where | ||
|
||
import Control.Monad.Except | ||
|
||
} | ||
|
||
%wrapper "basic" | ||
|
||
$digit = 0-9 | ||
$alpha = [a-zA-Z] | ||
$eol = [\n] | ||
|
||
tokens :- | ||
|
||
-- Whitespace insensitive | ||
$eol ; | ||
$white+ ; | ||
|
||
-- Comments | ||
"#".* ; | ||
|
||
-- Syntax | ||
let { \s -> TokenLet } | ||
True { \s -> TokenTrue } | ||
False { \s -> TokenFalse } | ||
Bool { \s -> TokenBool } | ||
in { \s -> TokenIn } | ||
Int { \s -> TokenInt } | ||
$digit+ { \s -> TokenNum (read s) } | ||
"->" { \s -> TokenArrow } | ||
\\ { \s -> TokenLambda } | ||
\= { \s -> TokenEq } | ||
\< { \s -> TokenLt } | ||
[\+] { \s -> TokenAdd } | ||
[\-] { \s -> TokenSub } | ||
[\*] { \s -> TokenMul } | ||
\: { \s -> TokenColon } | ||
\( { \s -> TokenLParen } | ||
\) { \s -> TokenRParen } | ||
$alpha [$alpha $digit \_ \']* { \s -> TokenSym s } | ||
|
||
{ | ||
|
||
data Token | ||
= TokenLet | ||
| TokenTrue | ||
| TokenFalse | ||
| TokenBool | ||
| TokenIn | ||
| TokenInt | ||
| TokenLambda | ||
| TokenNum Int | ||
| TokenSym String | ||
| TokenArrow | ||
| TokenEq | ||
| TokenLt | ||
| TokenAdd | ||
| TokenSub | ||
| TokenMul | ||
| TokenColon | ||
| TokenLParen | ||
| TokenRParen | ||
| TokenEOF | ||
deriving (Eq,Show) | ||
|
||
scanTokens :: String -> Except String [Token] | ||
scanTokens str = go ('\n',[],str) where | ||
go inp@(_,_bs,str) = | ||
case alexScan inp 0 of | ||
AlexEOF -> return [] | ||
AlexError _ -> throwError "Invalid lexeme." | ||
AlexSkip inp' len -> go inp' | ||
AlexToken inp' len act -> do | ||
res <- go inp' | ||
let rest = act (take len str) | ||
return (rest : res) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
module Main where | ||
|
||
import Parser (parseProgram) | ||
import System.Environment | ||
|
||
process :: String -> IO () | ||
process input = do | ||
let ast = parseProgram input | ||
case ast of | ||
Right ast -> putStrLn (show ast) | ||
Left err -> do | ||
putStrLn "Parser Error:" | ||
print err | ||
|
||
main :: IO () | ||
main = do | ||
putStrLn "hello world" | ||
args <- getArgs | ||
case args of | ||
[] -> putStrLn "Usage: core-language <input file>" | ||
[fname] -> do | ||
contents <- readFile fname | ||
process contents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
{ | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
|
||
module Parser ( | ||
parseExpr, | ||
parseTokens, | ||
) where | ||
|
||
import Lexer | ||
import Syntax | ||
|
||
import Control.Monad.Except | ||
|
||
} | ||
|
||
-- Entry point | ||
%name expr Expr | ||
|
||
-- Lexer structure | ||
%tokentype { Token } | ||
|
||
-- Parser monad | ||
%monad { Except String } { (>>=) } { return } | ||
%error { parseError } | ||
|
||
-- Token Names | ||
%token | ||
-- let { TokenLet } | ||
true { TokenTrue } | ||
false { TokenFalse } | ||
Bool {TokenBool} | ||
-- in { TokenIn } | ||
Int {TokenInt} | ||
NUM { TokenNum $$ } | ||
VAR { TokenSym $$ } | ||
'\\' { TokenLambda } | ||
'->' { TokenArrow } | ||
-- '=' { TokenEq } | ||
'<' { TokenLt } | ||
'+' { TokenAdd } | ||
'-' { TokenSub } | ||
'*' { TokenMul } | ||
':' { TokenColon } | ||
'(' { TokenLParen } | ||
')' { TokenRParen } | ||
|
||
-- Operators | ||
%left '<' | ||
%left '+' '-' | ||
%left '*' | ||
%right '->' | ||
%% | ||
|
||
Tp : Bool { BoolT } | ||
| Int { IntT } | ||
| Tp '->' Tp { FunT $1 $3 } | ||
| '(' Tp ')' { $2 } | ||
|
||
Expr : '\\' VAR ':' Tp '->' Expr { FunE () $2 $4 $6 } | ||
| Form { $1 } | ||
|
||
Form : Form '<' Form { BinOpE () (BCompar BClt) $1 $3 } | ||
| Form '+' Form { BinOpE () (BArith BAadd) $1 $3 } | ||
| Form '-' Form { BinOpE () (BArith BAsub) $1 $3 } | ||
| Form '*' Form { BinOpE () (BArith BAmul) $1 $3 } | ||
| Fact { $1 } | ||
|
||
Fact : Fact Atom { AppE () $1 $2 } | ||
| Atom { $1 } | ||
|
||
Atom : '(' Expr ')' { $2 } | ||
| NUM { ValE () (IntV $1) } | ||
| VAR { VarE () $1 0 } | ||
| true { ValE () (BoolV True) } | ||
| false { ValE () (BoolV False) } | ||
|
||
{ | ||
|
||
parseError :: [Token] -> Except String a | ||
parseError (l:ls) = throwError (show l) | ||
parseError [] = throwError "Unexpected end of Input" | ||
|
||
parseExpr :: String -> Either String (Exp ()) | ||
parseExpr input = runExcept $ do | ||
tokenStream <- scanTokens input | ||
expr tokenStream | ||
|
||
parseTokens :: String -> Either String [Token] | ||
parseTokens = runExcept . scanTokens | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.