Skip to content

Commit

Permalink
clean up parser code and a lot of other stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangjunphy committed Jun 6, 2024
1 parent 61afa2a commit 98de27a
Show file tree
Hide file tree
Showing 23 changed files with 501 additions and 396 deletions.
123 changes: 62 additions & 61 deletions src/AST.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-- AST -- AST after type checking and clean up
-- Copyright (C) 2018 Jun Zhang <zhangjunphy[at]gmail[dot]com>
-- Copyright (C) 2018-2024 Jun Zhang <zhangjunphy[at]gmail[dot]com>
--
-- This file is a part of decafc.
--
Expand All @@ -9,6 +8,8 @@
-- decafc is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the X11 license for more details.

-- AST -- AST after type checking and clean up
module AST where

import GHC.Generics (Generic)
Expand Down Expand Up @@ -162,11 +163,11 @@ parseAssignOp s = case s of

-- auxiliary data types
data Location = Location
{ name :: Name,
idx :: Maybe Expr,
variableDef :: Either Argument FieldDecl,
tpe :: Type,
loc :: SL.Range
{ name :: !Name,
idx :: !(Maybe Expr),
variableDef :: !(Either Argument FieldDecl),
tpe :: !Type,
loc :: !SL.Range
} deriving (Generic)

typeOfDef :: Either Argument FieldDecl -> Type
Expand All @@ -177,103 +178,103 @@ instance Show Location where
show Location{name=nm, idx=idx} = printf "Location {name=%s, idx=%s}" nm (show idx)

data Assignment = Assignment
{ location :: Location,
op :: AssignOp,
expr :: Maybe Expr,
loc :: SL.Range
{ location :: !Location,
op :: !AssignOp,
expr :: !(Maybe Expr),
loc :: !SL.Range
} deriving (Generic, Show)

data MethodCall = MethodCall
{ name :: Name,
args :: [Expr],
loc :: SL.Range
{ name :: !Name,
args :: ![Expr],
loc :: !SL.Range
} deriving (Generic, Show)

-- AST nodes
data ASTRoot = ASTRoot
{ imports :: [ImportDecl],
vars :: [FieldDecl],
methods :: [MethodDecl]
{ imports :: ![ImportDecl],
vars :: ![FieldDecl],
methods :: ![MethodDecl]
} deriving (Generic, Show)

data ImportDecl = ImportDecl
{ name :: Name
, loc :: SL.Range
{ name :: !Name
, loc :: !SL.Range
} deriving (Generic, Show)

data FieldDecl = FieldDecl
{ name :: Name,
tpe :: Type,
loc :: SL.Range
{ name :: !Name,
tpe :: !Type,
loc :: !SL.Range
}
deriving (Generic, Show)

data Argument = Argument
{ name :: Name,
tpe :: Type,
loc :: SL.Range
{ name :: !Name,
tpe :: !Type,
loc :: !SL.Range
}
deriving (Generic, Show)

data MethodSig = MethodSig
{ name :: Name,
tpe :: Maybe Type,
args :: [Argument]
{ name :: !Name,
tpe :: !(Maybe Type),
args :: ![Argument]
}
deriving (Generic, Show)

data MethodDecl = MethodDecl
{ sig :: MethodSig,
block :: Block,
loc :: SL.Range
{ sig :: !MethodSig,
block :: !Block,
loc :: !SL.Range
}
deriving (Generic, Show)

data Statement = Statement
{ statement_ :: Statement_
, loc :: SL.Range
{ statement_ :: !Statement_
, loc :: !SL.Range
} deriving (Generic, Show)
data Statement_
= AssignStmt {assign :: Assignment}
| IfStmt {pred :: Expr, ifBlock :: Block, elseBlock :: Maybe Block}
| ForStmt { counter :: Name, initCounter :: Expr, pred :: Expr, update :: Assignment, block :: Block}
| WhileStmt {pred :: Expr, block :: Block}
| ReturnStmt {expr :: Maybe Expr}
| MethodCallStmt {methodCall :: MethodCall}
= AssignStmt {assign :: !Assignment}
| IfStmt {pred :: !Expr, ifBlock :: !Block, elseBlock :: !(Maybe Block)}
| ForStmt { counter :: !Name, initCounter :: !Expr, pred :: !Expr, update :: !Assignment, block :: !Block}
| WhileStmt {pred :: !Expr, block :: !Block}
| ReturnStmt {expr :: !(Maybe Expr)}
| MethodCallStmt {methodCall :: !MethodCall}
| BreakStmt
| ContinueStmt
deriving (Generic, Show)

data Expr = Expr
{ expr_ :: Expr_
, tpe :: Type
, loc :: SL.Range
{ expr_ :: !Expr_
, tpe :: !Type
, loc :: !SL.Range
} deriving (Generic, Show)

data Expr_
= LocationExpr {location :: Location}
| MethodCallExpr {methodCall :: MethodCall}
| ExternCallExpr {name :: Name, args :: [Expr]}
| IntLiteralExpr {intVal :: Int64}
| BoolLiteralExpr {boolVal :: Bool}
| CharLiteralExpr {charVal :: Char}
| StringLiteralExpr {strVal :: Text}
| ArithOpExpr {arithOp :: ArithOp, lhs :: Expr, rhs :: Expr}
| RelOpExpr {relOp :: RelOp, lhs :: Expr, rhs :: Expr}
| CondOpExpr {condOp :: CondOp, lhs :: Expr, rhs :: Expr}
| EqOpExpr {eqOp :: EqOp, lhs :: Expr, rhs :: Expr}
| NegOpExpr {negOp :: NegOp, expr :: Expr}
| NotOpExpr {notOp :: NotOp, expr :: Expr}
| ChoiceOpExpr {choiceOp :: ChoiceOp, expr1 :: Expr, expr2 :: Expr, expr3 :: Expr}
| LengthExpr {name :: Name}
= LocationExpr {location :: !Location}
| MethodCallExpr {methodCall :: !MethodCall}
| ExternCallExpr {name :: !Name, args :: ![Expr]}
| IntLiteralExpr {intVal :: !Int64}
| BoolLiteralExpr {boolVal :: !Bool}
| CharLiteralExpr {charVal :: !Char}
| StringLiteralExpr {strVal :: !Text}
| ArithOpExpr {arithOp :: !ArithOp, lhs :: !Expr, rhs :: !Expr}
| RelOpExpr {relOp :: !RelOp, lhs :: !Expr, rhs :: !Expr}
| CondOpExpr {condOp :: !CondOp, lhs :: !Expr, rhs :: !Expr}
| EqOpExpr {eqOp :: !EqOp, lhs :: !Expr, rhs :: !Expr}
| NegOpExpr {negOp :: !NegOp, expr :: !Expr}
| NotOpExpr {notOp :: !NotOp, expr :: !Expr}
| ChoiceOpExpr {choiceOp :: !ChoiceOp, expr1 :: !Expr, expr2 :: !Expr, expr3 :: !Expr}
| LengthExpr {name :: !Name}
deriving (Generic, Show)

data Typed a = Typed {ele :: a, tpe :: Type}
data Typed a = Typed {ele :: !a, tpe :: !Type}
deriving (Generic, Show)

data Block = Block
{ vars :: [FieldDecl],
stmts :: [Statement],
blockID :: ScopeID
{ vars :: ![FieldDecl],
stmts :: ![Statement],
blockID :: !ScopeID
}
deriving (Generic, Show)
6 changes: 3 additions & 3 deletions src/CFG.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-- CFG -- Control Flow Graph with SSA nodes
-- Copyright (C) 2018 Jun Zhang <zhangjunphy[at]gmail[dot]com>
-- Copyright (C) 2018-2024 Jun Zhang <zhangjunphy[at]gmail[dot]com>
--
-- This file is a part of decafc.
--
Expand All @@ -9,6 +8,8 @@
-- decafc is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the X11 license for more details.

-- CFG -- Control Flow Graph with SSA nodes
module CFG where

import AST qualified
Expand All @@ -21,7 +22,6 @@ import Control.Monad.State
import Control.Monad.Writer
import Data.Functor ((<&>))
import Data.Generics.Labels
import Data.GraphViz.Types.Graph qualified as GV
import Data.List qualified as List
import Data.Map (Map)
import Data.Map.Strict qualified as Map
Expand Down
68 changes: 38 additions & 30 deletions src/Configuration.hs
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
{- Configuration -- describing the overall behavior of the compiler
Copyright (C) 2013 Benjamin Barenblat <bbaren@mit.edu>
This file is a part of decafc.
decafc is free software: you can redistribute it and/or modify it under the
terms of the MIT (X11) License as described in the LICENSE file.
decafc is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the X11 license for more details. -}
module Configuration ( Configuration
, input, target, debug, opt, outputFileName
, defaultConfiguration
, CompilerStage(..)
, OptimizationSpecification(..)
, OptimizationName(..)
) where

-- Copyright (C) 2018-2024 Jun Zhang <zhangjunphy[at]gmail[dot]com>
--
-- This file is a part of decafc.
--
-- decafc is free software: you can redistribute it and/or modify it under the
-- terms of the MIT (X11) License as described in the LICENSE file.
--
-- decafc is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the X11 license for more details.
module Configuration
( Configuration,
input,
target,
debug,
opt,
outputFileName,
defaultConfiguration,
CompilerStage (..),
OptimizationSpecification (..),
OptimizationName (..),
)
where

import Configuration.Types
( CompilerStage (..),
Configuration (..),
OptimizationName (..),
OptimizationSpecification (..),
debug,
defaultConfiguration,
explicitTarget,
input,
opt,
outputFileName,
)
import Data.Maybe (fromMaybe)

import Configuration.Types ( Configuration(..)
, input, debug, opt, outputFileName
, explicitTarget
, defaultConfiguration
, CompilerStage(..)
, OptimizationSpecification(..)
, OptimizationName(..)
)


--------------------------- The configuration type ----------------------------
{- 'input', 'debug', 'opt', and 'outputFileName' are fine accessor functions.
'target', on the other hand, is a bit special. -}

target :: Configuration -> CompilerStage
target conf = fromMaybe defaultTarget $ explicitTarget conf
where defaultTarget = Parse -- this will change as the course proceeds
where
defaultTarget = Parse -- this will change as the course proceeds
5 changes: 3 additions & 2 deletions src/Constants.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-- Constants -- Definitions for some constants
-- Copyright (C) 2018 Jun Zhang <zhangjunphy[at]gmail[dot]com>
-- Copyright (C) 2018-2024 Jun Zhang <zhangjunphy[at]gmail[dot]com>
--
-- This file is a part of decafc.
--
Expand All @@ -9,6 +8,8 @@
-- decafc is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the X11 license for more details.

-- Constants -- Definitions for some constants
module Constants
where

Expand Down
5 changes: 3 additions & 2 deletions src/DominatorTree.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-- DominatorTree -- A Module to help computing dominance frontiers
-- Copyright (C) 2018 Jun Zhang <zhangjunphy[at]gmail[dot]com>
-- Copyright (C) 2018-2024 Jun Zhang <zhangjunphy[at]gmail[dot]com>
--
-- This file is a part of decafc.
--
Expand All @@ -9,4 +8,6 @@
-- decafc is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the X11 license for more details.

-- DominatorTree -- A Module to help computing dominance frontiers
module DominatorTree where
5 changes: 3 additions & 2 deletions src/Lexer.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-- Scanner entry point
-- Copyright (C) 2018 Jun Zhang <zhangjunphy[at]gmail[dot]com>
-- Copyright (C) 2018-2024 Jun Zhang <zhangjunphy[at]gmail[dot]com>
--
-- This file is a part of decafc.
--
Expand All @@ -9,6 +8,8 @@
-- decafc is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the X11 license for more details.

-- Lexer entry point
module Lexer
( Token (..),
Alex (..),
Expand Down
9 changes: 5 additions & 4 deletions src/Lexer/Lex.x
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Scanner -- Decaf scanner -*- haskell -*-
-- Copyright (C) 2013 Benjamin Barenblat <bbaren@mit.edu>
-- -*- haskell -*-
-- Copyright (C) 2018-2024 Jun Zhang <zhangjunphy[at]gmail[dot]com>
--
-- This file is a part of decafc.
--
Expand All @@ -9,6 +9,8 @@
-- decafc is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the X11 license for more details.

-- Decaf scanner
{
{-# OPTIONS_GHC -w #-}
module Lexer.Lex ( Alex(..)
Expand All @@ -18,9 +20,8 @@ module Lexer.Lex ( Alex(..)
, AlexUserState(..)
) where

import qualified Util.SourceLoc as SL

import Lexer.Token
import qualified Util.SourceLoc as SL

import Control.Monad.State
import Data.ByteString.Lazy (ByteString)
Expand Down
5 changes: 3 additions & 2 deletions src/Lexer/Token.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-- Parser -- Re-export Happy parser functionalities
-- Copyright (C) 2018 Jun Zhang <zhangjunphy[at]gmail[dot]com>
-- Copyright (C) 2018-2024 Jun Zhang <zhangjunphy[at]gmail[dot]com>
--
-- This file is a part of decafc.
--
Expand All @@ -9,6 +8,8 @@
-- decafc is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the X11 license for more details.

-- Lexer tokens
module Lexer.Token (Token(..)) where

import Data.Text (Text)
Expand Down
Loading

0 comments on commit 98de27a

Please sign in to comment.